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 69
    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)
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 78
    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)
79 80
  end

81 82 83
  private

  def successful_change_path
84 85 86 87 88
    return referenced_merge_request_url if @commit.merged_merge_request

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

89
  def failed_change_path
90 91 92 93 94 95 96 97 98
    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

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

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
Kamil Trzcinski's avatar
Kamil Trzcinski committed
137 138 139
    @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
140
  end
141

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