BigW Consortium Gitlab

commit_controller.rb 5.16 KB
Newer Older
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 committed
5
  include RendersNotes
6
  include CreatesCommit
7
  include DiffForPath
8
  include DiffHelper
9

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

19 20
  BRANCH_SEARCH_LIMIT = 1000

Robert Speicher committed
21
  def show
22 23
    apply_diff_view_cookie!

24
    respond_to do |format|
25
      format.html  { render }
26
      format.diff  { render text: @commit.to_diff }
27
      format.patch { render text: @commit.to_patch }
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

35
  def pipelines
36 37 38 39 40
    @pipelines = @commit.pipelines.order(id: :desc)

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

43 44 45 46 47 48 49 50
        render json: {
          pipelines: PipelineSerializer
            .new(project: @project, current_user: @current_user)
            .represent(@pipelines),
          count: {
            all: @pipelines.count
          }
        }
51 52
      end
    end
53 54
  end

55
  def branches
56 57 58
    # branch_names_contains/tag_names_contains can take a long time when there are thousands of
    # branches/tags - each `git branch --contains xxx` request can consume a cpu core.
    # so only do the query when there are a manageable number of branches/tags
59 60 61 62 63
    @branches_limit_exceeded = @project.repository.branch_count > BRANCH_SEARCH_LIMIT
    @branches = @branches_limit_exceeded ? [] : @project.repository.branch_names_contains(commit.id)

    @tags_limit_exceeded = @project.repository.tag_count > BRANCH_SEARCH_LIMIT
    @tags = @tags_limit_exceeded ? [] : @project.repository.tag_names_contains(commit.id)
64 65 66
    render layout: false
  end

67
  def revert
68
    assign_change_commit_vars
69

70 71
    return render_404 if @start_branch.blank?

72
    @branch_name = create_new_branch? ? @commit.revert_branch_name : @start_branch
73

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

78
  def cherry_pick
79
    assign_change_commit_vars
80

81 82
    return render_404 if @start_branch.blank?

83
    @branch_name = create_new_branch? ? @commit.cherry_pick_branch_name : @start_branch
84

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

89 90
  private

91 92 93 94
  def create_new_branch?
    params[:create_merge_request].present? || !can?(current_user, :push_code, @project)
  end

95
  def successful_change_path
96
    referenced_merge_request_url || project_commits_url(@project, @branch_name)
97 98
  end

99
  def failed_change_path
100
    referenced_merge_request_url || project_commit_url(@project, params[:id])
101 102 103
  end

  def referenced_merge_request_url
104
    if merge_request = @commit.merged_merge_request(current_user)
105
      project_merge_request_url(merge_request.target_project, merge_request)
106
    end
107 108
  end

109
  def commit
110
    @noteable = @commit ||= @project.commit_by(oid: params[:id])
111
  end
112

113
  def define_commit_vars
114 115
    return git_not_found! unless commit

116 117
    opts = diff_options
    opts[:ignore_whitespace_change] = true if params[:format] == 'diff'
118

119
    @diffs = commit.diffs(opts)
Kamil Trzcinski committed
120
    @notes_count = commit.notes.count
121

Douwe Maan committed
122
    @environment = EnvironmentsFinder.new(@project, current_user, commit: @commit).execute.last
123 124 125
  end

  def define_note_vars
126
    @noteable = @commit
127 128
    @note = @project.build_commit_note(commit)

129
    @new_diff_note_attrs = {
130 131 132
      noteable_type: 'Commit',
      commit_id: @commit.id
    }
133 134 135 136

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

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    if merge_request_iid = params[:merge_request_iid]
      @merge_request = MergeRequestsFinder.new(current_user, project_id: @project.id).find_by(iid: merge_request_iid)

      if @merge_request
        @new_diff_note_attrs.merge!(
          noteable_type: 'MergeRequest',
          noteable_id: @merge_request.id
        )

        merge_request_commit_notes = @merge_request.notes.where(commit_id: @commit.id).inc_relations_for_view
        merge_request_commit_diff_discussions = merge_request_commit_notes.grouped_diff_discussions(@commit.diff_refs)
        @grouped_diff_discussions.merge!(merge_request_commit_diff_discussions) do |line_code, left, right|
          left + right
        end
      end
    end

154
    @notes = (@grouped_diff_discussions.values.flatten + @discussions).flat_map(&:notes)
155
    @notes = prepare_notes_for_rendering(@notes, @commit)
156
  end
157

158
  def assign_change_commit_vars
159 160
    @start_branch = params[:start_branch]
    @commit_params = { commit: @commit }
161
  end
Robert Speicher committed
162
end