commit_controller.rb 4.48 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
  before_action :authorize_download_code!, except: [:cancel_builds, :retry_builds]
  before_action :authorize_update_build!, only: [:cancel_builds, :retry_builds]
Kamil Trzcinski's avatar
Kamil Trzcinski committed
13
  before_action :authorize_read_pipeline!, only: [:pipelines]
14
  before_action :authorize_read_commit_status!, only: [:builds]
15
  before_action :commit
Kamil Trzcinski's avatar
Kamil Trzcinski committed
16 17
  before_action :define_commit_vars, only: [:show, :diff_for_path, :builds, :pipelines]
  before_action :define_status_vars, only: [:show, :builds, :pipelines]
Sean McGivern's avatar
Sean McGivern committed
18
  before_action :define_note_vars, only: [:show, :diff_for_path]
19
  before_action :authorize_edit_tree!, only: [:revert, :cherry_pick]
Robert Speicher's avatar
Robert Speicher committed
20 21

  def show
22 23
    apply_diff_view_cookie!

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

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

Kamil Trzcinski's avatar
Kamil Trzcinski committed
35 36 37
  def pipelines
  end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
38
  def builds
39 40
  end

41
  def cancel_builds
Kamil Trzcinski's avatar
Kamil Trzcinski committed
42
    ci_builds.running_or_pending.each(&:cancel)
43

Douwe Maan's avatar
Douwe Maan committed
44
    redirect_back_or_default default: builds_namespace_project_commit_path(project.namespace, project, commit.sha)
45 46
  end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
47
  def retry_builds
Kamil Trzcinski's avatar
Kamil Trzcinski committed
48
    ci_builds.latest.failed.each do |build|
Kamil Trzcinski's avatar
Kamil Trzcinski committed
49
      if build.retryable?
50
        Ci::Build.retry(build, current_user)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
51 52 53
      end
    end

Douwe Maan's avatar
Douwe Maan committed
54
    redirect_back_or_default default: builds_namespace_project_commit_path(project.namespace, project, commit.sha)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
55
  end
56

57 58 59 60 61 62
  def branches
    @branches = @project.repository.branch_names_contains(commit.id)
    @tags = @project.repository.tag_names_contains(commit.id)
    render layout: false
  end

63
  def revert
64
    assign_change_commit_vars(@commit.revert_branch_name)
65

66
    return render_404 if @target_branch.blank?
67

68
    create_commit(Commits::RevertService, success_notice: "The #{@commit.change_type_title(current_user)} has been successfully reverted.",
69
                                          success_path: successful_change_path, failure_path: failed_change_path)
70
  end
71

72 73
  def cherry_pick
    assign_change_commit_vars(@commit.cherry_pick_branch_name)
74

75
    return render_404 if @target_branch.blank?
76

77
    create_commit(Commits::CherryPickService, success_notice: "The #{@commit.change_type_title(current_user)} has been successfully cherry-picked.",
78
                                              success_path: successful_change_path, failure_path: failed_change_path)
79 80
  end

81 82 83
  private

  def successful_change_path
84
    referenced_merge_request_url || namespace_project_commits_url(@project.namespace, @project, @target_branch)
85 86
  end

87
  def failed_change_path
88
    referenced_merge_request_url || namespace_project_commit_url(@project.namespace, @project, params[:id])
89 90 91
  end

  def referenced_merge_request_url
92 93 94
    if merge_request = @commit.merged_merge_request(current_user)
      namespace_project_merge_request_url(@project.namespace, @project, merge_request)
    end
95 96
  end

97
  def commit
98
    @noteable = @commit ||= @project.commit(params[:id])
99
  end
100

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

Sean McGivern's avatar
Sean McGivern committed
105
  def define_commit_vars
106 107
    return git_not_found! unless commit

108 109
    opts = diff_options
    opts[:ignore_whitespace_change] = true if params[:format] == 'diff'
110

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

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

    Banzai::NoteRenderer.render(
120
      @grouped_diff_discussions.values.flat_map(&:notes) + @notes,
Sean McGivern's avatar
Sean McGivern committed
121 122 123 124 125 126 127 128 129 130 131 132
      @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
133

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

140
  def assign_change_commit_vars(mr_source_branch)
141
    @commit = project.commit(params[:id])
142
    @target_branch = params[:target_branch]
143
    @mr_source_branch = mr_source_branch
144
    @mr_target_branch = @target_branch
145
    @commit_params = {
146 147
      commit: @commit,
      create_merge_request: params[:create_merge_request].present? || different_project?
148 149
    }
  end
Robert Speicher's avatar
Robert Speicher committed
150
end