commit_controller.rb 4.38 KB
Newer Older
Robert Speicher's avatar
Robert Speicher committed
1 2 3
# Controller for a specific Commit
#
# Not to be confused with CommitsController, plural.
4
class Projects::CommitController < Projects::ApplicationController
5
  include CreatesCommit
Sean McGivern's avatar
Sean McGivern committed
6
  include DiffForPath
7
  include DiffHelper
8

Robert Speicher's avatar
Robert Speicher committed
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
Sean McGivern's avatar
Sean McGivern committed
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]
18
  before_action :authorize_edit_tree!, only: [:revert, :cherry_pick]
Robert Speicher's avatar
Robert Speicher committed
19 20

  def show
21 22
    apply_diff_view_cookie!

23
    respond_to do |format|
24
      format.html
25
      format.diff  { render text: @commit.to_diff }
26
      format.patch { render text: @commit.to_patch }
Robert Speicher's avatar
Robert Speicher committed
27 28
    end
  end
29

30
  def diff_for_path
31
    render_diff_for_path(@commit.diffs(diff_options))
32 33
  end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
34
  def builds
35 36
  end

37
  def cancel_builds
Kamil Trzcinski's avatar
Kamil Trzcinski committed
38
    ci_builds.running_or_pending.each(&:cancel)
39

Douwe Maan's avatar
Douwe Maan committed
40
    redirect_back_or_default default: builds_namespace_project_commit_path(project.namespace, project, commit.sha)
41 42
  end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
43
  def retry_builds
Kamil Trzcinski's avatar
Kamil Trzcinski committed
44
    ci_builds.latest.failed.each do |build|
Kamil Trzcinski's avatar
Kamil Trzcinski committed
45
      if build.retryable?
46
        Ci::Build.retry(build, current_user)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
47 48 49
      end
    end

Douwe Maan's avatar
Douwe Maan committed
50
    redirect_back_or_default default: builds_namespace_project_commit_path(project.namespace, project, commit.sha)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
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
60
    assign_change_commit_vars(@commit.revert_branch_name)
61

62
    return render_404 if @target_branch.blank?
63

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
67

68 69
  def cherry_pick
    assign_change_commit_vars(@commit.cherry_pick_branch_name)
70

71
    return render_404 if @target_branch.blank?
72

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

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

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
    @noteable = @commit ||= @project.commit(params[:id])
97
  end
98

99 100
  def pipelines
    @pipelines ||= project.pipelines.where(sha: commit.sha)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
101 102
  end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
103
  def ci_builds
104
    @ci_builds ||= Ci::Build.where(pipeline: pipelines)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
105 106
  end

Sean McGivern's avatar
Sean McGivern committed
107
  def define_commit_vars
108 109
    return git_not_found! unless commit

110 111
    opts = diff_options
    opts[:ignore_whitespace_change] = true if params[:format] == 'diff'
112

113
    @diffs = commit.diffs(opts)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
114
    @notes_count = commit.notes.count
Sean McGivern's avatar
Sean McGivern committed
115 116 117
  end

  def define_note_vars
118
    @grouped_diff_discussions = commit.notes.grouped_diff_discussions
Sean McGivern's avatar
Sean McGivern committed
119 120 121
    @notes = commit.notes.non_diff_notes.fresh

    Banzai::NoteRenderer.render(
122
      @grouped_diff_discussions.values.flat_map(&:notes) + @notes,
Sean McGivern's avatar
Sean McGivern committed
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
Douwe Maan's avatar
Douwe Maan committed
135

Sean McGivern's avatar
Sean McGivern committed
136
  def define_status_vars
137 138
    @statuses = CommitStatus.where(pipeline: pipelines).relevant
    @builds = Ci::Build.where(pipeline: pipelines).relevant
Kamil Trzcinski's avatar
Kamil Trzcinski committed
139
  end
140

141
  def assign_change_commit_vars(mr_source_branch)
142
    @commit = project.commit(params[:id])
143
    @target_branch = params[:target_branch]
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
Robert Speicher's avatar
Robert Speicher committed
151
end