commit_controller.rb 4.4 KB
Newer Older
R
Robert Speicher 已提交
1 2 3
# Controller for a specific Commit
#
# Not to be confused with CommitsController, plural.
4
class Projects::CommitController < Projects::ApplicationController
5
  include CreatesCommit
S
Sean McGivern 已提交
6
  include DiffForPath
J
Jacob Vosmaer 已提交
7
  include DiffHelper
8

R
Robert Speicher 已提交
9
  # Authorize
10
  before_action :require_non_empty_project
11 12 13
  before_action :authorize_download_code!, except: [:cancel_builds, :retry_builds]
  before_action :authorize_update_build!, only: [:cancel_builds, :retry_builds]
  before_action :authorize_read_commit_status!, only: [:builds]
14
  before_action :commit
S
Sean McGivern 已提交
15 16 17
  before_action :define_commit_vars, only: [:show, :diff_for_path, :builds]
  before_action :define_status_vars, only: [:show, :builds]
  before_action :define_note_vars, only: [:show, :diff_for_path]
P
P.S.V.R 已提交
18
  before_action :authorize_edit_tree!, only: [:revert, :cherry_pick]
R
Robert Speicher 已提交
19 20

  def show
21 22
    apply_diff_view_cookie!

23
    respond_to do |format|
24
      format.html
R
Riyad Preukschas 已提交
25
      format.diff  { render text: @commit.to_diff }
R
Riyad Preukschas 已提交
26
      format.patch { render text: @commit.to_patch }
R
Robert Speicher 已提交
27 28
    end
  end
29

S
Sean McGivern 已提交
30
  def diff_for_path
31
    render_diff_for_path(@commit.diff_file_collection(diff_options))
S
Sean McGivern 已提交
32 33
  end

K
Kamil Trzcinski 已提交
34
  def builds
35 36
  end

37
  def cancel_builds
K
Kamil Trzcinski 已提交
38
    ci_builds.running_or_pending.each(&:cancel)
39

D
Douwe Maan 已提交
40
    redirect_back_or_default default: builds_namespace_project_commit_path(project.namespace, project, commit.sha)
41 42
  end

K
Kamil Trzcinski 已提交
43
  def retry_builds
K
Kamil Trzcinski 已提交
44
    ci_builds.latest.failed.each do |build|
K
Kamil Trzcinski 已提交
45
      if build.retryable?
46
        Ci::Build.retry(build, current_user)
K
Kamil Trzcinski 已提交
47 48 49
      end
    end

D
Douwe Maan 已提交
50
    redirect_back_or_default default: builds_namespace_project_commit_path(project.namespace, project, commit.sha)
K
Kamil Trzcinski 已提交
51
  end
52

53 54 55 56 57 58
  def branches
    @branches = @project.repository.branch_names_contains(commit.id)
    @tags = @project.repository.tag_names_contains(commit.id)
    render layout: false
  end

59
  def revert
P
P.S.V.R 已提交
60
    assign_change_commit_vars(@commit.revert_branch_name)
61

62
    return render_404 if @target_branch.blank?
63

P
P.S.V.R 已提交
64 65
    create_commit(Commits::RevertService, success_notice: "The #{@commit.change_type_title} has been successfully reverted.",
                                          success_path: successful_change_path, failure_path: failed_change_path)
66
  end
D
Douwe Maan 已提交
67

P
P.S.V.R 已提交
68 69
  def cherry_pick
    assign_change_commit_vars(@commit.cherry_pick_branch_name)
D
Douwe Maan 已提交
70

P
P.S.V.R 已提交
71
    return render_404 if @target_branch.blank?
72

P
P.S.V.R 已提交
73 74
    create_commit(Commits::CherryPickService, success_notice: "The #{@commit.change_type_title} has been successfully cherry-picked.",
                                              success_path: successful_change_path, failure_path: failed_change_path)
75 76
  end

P
P.S.V.R 已提交
77 78 79
  private

  def successful_change_path
80 81 82 83 84
    return referenced_merge_request_url if @commit.merged_merge_request

    namespace_project_commits_url(@project.namespace, @project, @target_branch)
  end

P
P.S.V.R 已提交
85
  def failed_change_path
86 87 88 89 90 91 92 93 94
    return referenced_merge_request_url if @commit.merged_merge_request

    namespace_project_commit_url(@project.namespace, @project, params[:id])
  end

  def referenced_merge_request_url
    namespace_project_merge_request_url(@project.namespace, @project, @commit.merged_merge_request)
  end

95
  def commit
96
    @commit ||= @project.commit(params[:id])
97
  end
K
Kamil Trzcinski 已提交
98

99 100
  def pipelines
    @pipelines ||= project.pipelines.where(sha: commit.sha)
K
Kamil Trzcinski 已提交
101 102
  end

K
Kamil Trzcinski 已提交
103
  def ci_builds
104
    @ci_builds ||= Ci::Build.where(pipeline: pipelines)
K
Kamil Trzcinski 已提交
105 106
  end

S
Sean McGivern 已提交
107
  def define_commit_vars
108 109
    return git_not_found! unless commit

J
Jacob Vosmaer 已提交
110 111
    opts = diff_options
    opts[:ignore_whitespace_change] = true if params[:format] == 'diff'
112

113
    @diffs = commit.diff_file_collection(opts)
K
Kamil Trzcinski 已提交
114
    @notes_count = commit.notes.count
S
Sean McGivern 已提交
115 116 117
  end

  def define_note_vars
118
    @grouped_diff_discussions = commit.notes.grouped_diff_discussions
S
Sean McGivern 已提交
119 120 121
    @notes = commit.notes.non_diff_notes.fresh

    Banzai::NoteRenderer.render(
122
      @grouped_diff_discussions.values.flat_map(&:notes) + @notes,
S
Sean McGivern 已提交
123 124 125 126 127 128 129 130 131 132 133 134
      @project,
      current_user,
    )

    @note = @project.build_commit_note(commit)

    @noteable = @commit
    @comments_target = {
      noteable_type: 'Commit',
      commit_id: @commit.id
    }
  end
D
Douwe Maan 已提交
135

S
Sean McGivern 已提交
136
  def define_status_vars
137 138
    @statuses = CommitStatus.where(pipeline: pipelines)
    @builds = Ci::Build.where(pipeline: pipelines)
K
Kamil Trzcinski 已提交
139
  end
140

P
P.S.V.R 已提交
141
  def assign_change_commit_vars(mr_source_branch)
142
    @commit = project.commit(params[:id])
143
    @target_branch = params[:target_branch]
P
P.S.V.R 已提交
144
    @mr_source_branch = mr_source_branch
145
    @mr_target_branch = @target_branch
146
    @commit_params = {
147 148
      commit: @commit,
      create_merge_request: params[:create_merge_request].present? || different_project?
149 150
    }
  end
R
Robert Speicher 已提交
151
end