commit_controller.rb 3.93 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
Filipa Lacerda's avatar
Filipa Lacerda committed
11
  before_action :authorize_download_code!
Kamil Trzcinski's avatar
Kamil Trzcinski committed
12
  before_action :authorize_read_pipeline!, only: [:pipelines]
13
  before_action :commit
14
  before_action :define_commit_vars, only: [:show, :diff_for_path, :pipelines]
Sean McGivern's avatar
Sean McGivern committed
15
  before_action :define_note_vars, only: [:show, :diff_for_path]
16
  before_action :authorize_edit_tree!, only: [:revert, :cherry_pick]
Robert Speicher's avatar
Robert Speicher committed
17 18

  def show
19 20
    apply_diff_view_cookie!

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

28
  def diff_for_path
29
    render_diff_for_path(@commit.diffs(diff_options))
30 31
  end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
32
  def pipelines
33 34 35 36 37 38 39 40 41 42
    @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
Kamil Trzcinski's avatar
Kamil Trzcinski committed
43 44
  end

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

51
  def revert
52
    assign_change_commit_vars
53

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

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

    @mr_target_branch = @start_branch
59

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

64
  def cherry_pick
65
    assign_change_commit_vars
66

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

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

    @mr_target_branch = @start_branch
72

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

77 78
  private

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

83
  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
    if merge_request = @commit.merged_merge_request(current_user)
93
      namespace_project_merge_request_url(merge_request.target_project.namespace, merge_request.target_project, merge_request)
94
    end
95 96
  end

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

Sean McGivern's avatar
Sean McGivern committed
101
  def define_commit_vars
102 103
    return git_not_found! unless commit

104 105
    opts = diff_options
    opts[:ignore_whitespace_change] = true if params[:format] == 'diff'
106

107
    @diffs = commit.diffs(opts)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
108
    @notes_count = commit.notes.count
109

Douwe Maan's avatar
Douwe Maan committed
110
    @environment = EnvironmentsFinder.new(@project, current_user, commit: @commit).execute.last
Sean McGivern's avatar
Sean McGivern committed
111 112 113
  end

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

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

132
  def assign_change_commit_vars
133 134
    @start_branch = params[:start_branch]
    @commit_params = { commit: @commit }
135
  end
Robert Speicher's avatar
Robert Speicher committed
136
end