commit_controller.rb 4.1 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
Douwe Maan's avatar
Douwe Maan committed
5
  include RendersNotes
6
  include CreatesCommit
Sean McGivern's avatar
Sean McGivern committed
7
  include DiffForPath
8
  include DiffHelper
9

Robert Speicher's avatar
Robert Speicher committed
10
  # Authorize
11
  before_action :require_non_empty_project
Filipa Lacerda's avatar
Filipa Lacerda committed
12
  before_action :authorize_download_code!
Kamil Trzcinski's avatar
Kamil Trzcinski committed
13
  before_action :authorize_read_pipeline!, only: [:pipelines]
14
  before_action :commit
15
  before_action :define_commit_vars, only: [:show, :diff_for_path, :pipelines]
Sean McGivern's avatar
Sean McGivern committed
16
  before_action :define_note_vars, only: [:show, :diff_for_path]
17
  before_action :authorize_edit_tree!, only: [:revert, :cherry_pick]
Robert Speicher's avatar
Robert Speicher committed
18 19

  def show
20 21
    apply_diff_view_cookie!

22
    respond_to do |format|
23 24 25 26 27 28
      format.html do
        # n+1: https://gitlab.com/gitlab-org/gitlab-ce/issues/37599
        Gitlab::GitalyClient.allow_n_plus_1_calls do
          render
        end
      end
29
      format.diff  { render text: @commit.to_diff }
30
      format.patch { render text: @commit.to_patch }
Robert Speicher's avatar
Robert Speicher committed
31 32
    end
  end
33

34
  def diff_for_path
35
    render_diff_for_path(@commit.diffs(diff_options))
36 37
  end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
38
  def pipelines
39 40 41 42 43
    @pipelines = @commit.pipelines.order(id: :desc)

    respond_to do |format|
      format.html
      format.json do
44 45
        Gitlab::PollingInterval.set_header(response, interval: 10_000)

46 47 48 49 50 51 52 53
        render json: {
          pipelines: PipelineSerializer
            .new(project: @project, current_user: @current_user)
            .represent(@pipelines),
          count: {
            all: @pipelines.count
          }
        }
54 55
      end
    end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
56 57
  end

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

64
  def revert
65
    assign_change_commit_vars
66

67 68
    return render_404 if @start_branch.blank?

69
    @branch_name = create_new_branch? ? @commit.revert_branch_name : @start_branch
70

71
    create_commit(Commits::RevertService, success_notice: "The #{@commit.change_type_title(current_user)} has been successfully reverted.",
72
                                          success_path: -> { successful_change_path }, failure_path: failed_change_path)
73
  end
74

75
  def cherry_pick
76
    assign_change_commit_vars
77

78 79
    return render_404 if @start_branch.blank?

80
    @branch_name = create_new_branch? ? @commit.cherry_pick_branch_name : @start_branch
81

82
    create_commit(Commits::CherryPickService, success_notice: "The #{@commit.change_type_title(current_user)} has been successfully cherry-picked.",
83
                                              success_path: -> { successful_change_path }, failure_path: failed_change_path)
84 85
  end

86 87
  private

88 89 90 91
  def create_new_branch?
    params[:create_merge_request].present? || !can?(current_user, :push_code, @project)
  end

92
  def successful_change_path
93
    referenced_merge_request_url || project_commits_url(@project, @branch_name)
94 95
  end

96
  def failed_change_path
97
    referenced_merge_request_url || project_commit_url(@project, params[:id])
98 99 100
  end

  def referenced_merge_request_url
101
    if merge_request = @commit.merged_merge_request(current_user)
102
      project_merge_request_url(merge_request.target_project, merge_request)
103
    end
104 105
  end

106
  def commit
107
    @noteable = @commit ||= @project.commit(params[:id])
108
  end
109

Sean McGivern's avatar
Sean McGivern committed
110
  def define_commit_vars
111 112
    return git_not_found! unless commit

113 114
    opts = diff_options
    opts[:ignore_whitespace_change] = true if params[:format] == 'diff'
115

116
    @diffs = commit.diffs(opts)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
117
    @notes_count = commit.notes.count
118

Douwe Maan's avatar
Douwe Maan committed
119
    @environment = EnvironmentsFinder.new(@project, current_user, commit: @commit).execute.last
Sean McGivern's avatar
Sean McGivern committed
120 121 122
  end

  def define_note_vars
123
    @noteable = @commit
Sean McGivern's avatar
Sean McGivern committed
124 125
    @note = @project.build_commit_note(commit)

126
    @new_diff_note_attrs = {
Sean McGivern's avatar
Sean McGivern committed
127 128 129
      noteable_type: 'Commit',
      commit_id: @commit.id
    }
130 131 132 133

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

134
    @notes = (@grouped_diff_discussions.values.flatten + @discussions).flat_map(&:notes)
135
    @notes = prepare_notes_for_rendering(@notes, @commit)
Sean McGivern's avatar
Sean McGivern committed
136
  end
Douwe Maan's avatar
Douwe Maan committed
137

138
  def assign_change_commit_vars
139 140
    @start_branch = params[:start_branch]
    @commit_params = { commit: @commit }
141
  end
Robert Speicher's avatar
Robert Speicher committed
142
end