commit_controller.rb 3.9 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 NotesHelper
6
  include CreatesCommit
S
Sean McGivern 已提交
7
  include DiffForPath
J
Jacob Vosmaer 已提交
8
  include DiffHelper
9

R
Robert Speicher 已提交
10
  # Authorize
11
  before_action :require_non_empty_project
F
Filipa Lacerda 已提交
12
  before_action :authorize_download_code!
K
Kamil Trzcinski 已提交
13
  before_action :authorize_read_pipeline!, only: [:pipelines]
14
  before_action :commit
F
Filipa Lacerda 已提交
15
  before_action :define_commit_vars, only: [:show, :diff_for_path, :pipelines]
S
Sean McGivern 已提交
16
  before_action :define_note_vars, only: [:show, :diff_for_path]
P
P.S.V.R 已提交
17
  before_action :authorize_edit_tree!, only: [:revert, :cherry_pick]
R
Robert Speicher 已提交
18 19

  def show
20 21
    apply_diff_view_cookie!

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

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

K
Kamil Trzcinski 已提交
33
  def pipelines
34 35 36 37 38 39 40 41 42 43
    @pipelines = @commit.pipelines.order(id: :desc)

    respond_to do |format|
      format.html
      format.json do
        render json: PipelineSerializer
          .new(project: @project, user: @current_user)
          .represent(@pipelines)
      end
    end
K
Kamil Trzcinski 已提交
44 45
  end

46 47 48 49 50 51
  def branches
    @branches = @project.repository.branch_names_contains(commit.id)
    @tags = @project.repository.tag_names_contains(commit.id)
    render layout: false
  end

52
  def revert
53
    assign_change_commit_vars
54

55 56 57 58 59
    return render_404 if @start_branch.blank?

    @target_branch = create_new_branch? ? @commit.revert_branch_name : @start_branch

    @mr_target_branch = @start_branch
60

61
    create_commit(Commits::RevertService, success_notice: "The #{@commit.change_type_title(current_user)} has been successfully reverted.",
62
                                          success_path: -> { successful_change_path }, failure_path: failed_change_path)
63
  end
D
Douwe Maan 已提交
64

P
P.S.V.R 已提交
65
  def cherry_pick
66
    assign_change_commit_vars
D
Douwe Maan 已提交
67

68 69 70 71 72
    return render_404 if @start_branch.blank?

    @target_branch = create_new_branch? ? @commit.cherry_pick_branch_name : @start_branch

    @mr_target_branch = @start_branch
73

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

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

80 81 82 83
  def create_new_branch?
    params[:create_merge_request].present? || !can?(current_user, :push_code, @project)
  end

P
P.S.V.R 已提交
84
  def successful_change_path
85
    referenced_merge_request_url || namespace_project_commits_url(@project.namespace, @project, @target_branch)
86 87
  end

P
P.S.V.R 已提交
88
  def failed_change_path
89
    referenced_merge_request_url || namespace_project_commit_url(@project.namespace, @project, params[:id])
90 91 92
  end

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

98
  def commit
99
    @noteable = @commit ||= @project.commit(params[:id])
100
  end
K
Kamil Trzcinski 已提交
101

S
Sean McGivern 已提交
102
  def define_commit_vars
103 104
    return git_not_found! unless commit

J
Jacob Vosmaer 已提交
105 106
    opts = diff_options
    opts[:ignore_whitespace_change] = true if params[:format] == 'diff'
107

108
    @diffs = commit.diffs(opts)
K
Kamil Trzcinski 已提交
109
    @notes_count = commit.notes.count
110

D
Douwe Maan 已提交
111
    @environment = EnvironmentsFinder.new(@project, current_user, commit: @commit).execute.last
S
Sean McGivern 已提交
112 113 114
  end

  def define_note_vars
115
    @noteable = @commit
S
Sean McGivern 已提交
116 117
    @note = @project.build_commit_note(commit)

118
    @new_diff_note_attrs = {
S
Sean McGivern 已提交
119 120 121
      noteable_type: 'Commit',
      commit_id: @commit.id
    }
122 123 124 125

    @grouped_diff_discussions = commit.grouped_diff_discussions
    @discussions = commit.discussions

126
    @notes = (@grouped_diff_discussions.values.flatten + @discussions).flat_map(&:notes)
127
    @notes = prepare_notes_for_rendering(@notes)
S
Sean McGivern 已提交
128
  end
D
Douwe Maan 已提交
129

130
  def assign_change_commit_vars
131 132
    @start_branch = params[:start_branch]
    @commit_params = { commit: @commit }
133
  end
R
Robert Speicher 已提交
134
end