commit_controller.rb 3.92 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
      format.html
24
      format.diff  { render text: @commit.to_diff }
25
      format.patch { render text: @commit.to_patch }
Robert Speicher's avatar
Robert Speicher committed
26 27
    end
  end
28

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

Kamil Trzcinski's avatar
Kamil Trzcinski committed
33
  def pipelines
34 35 36 37 38
    @pipelines = @commit.pipelines.order(id: :desc)

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

41 42 43 44 45 46 47 48
        render json: {
          pipelines: PipelineSerializer
            .new(project: @project, current_user: @current_user)
            .represent(@pipelines),
          count: {
            all: @pipelines.count
          }
        }
49 50
      end
    end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
51 52
  end

53 54 55 56 57 58
  def branches
    @branches = @project.repository.branch_names_contains(commit.id)
    @tags = @project.repository.tag_names_contains(commit.id)
    render layout: false
  end

59
  def revert
60
    assign_change_commit_vars
61

62 63
    return render_404 if @start_branch.blank?

64
    @branch_name = create_new_branch? ? @commit.revert_branch_name : @start_branch
65

66
    create_commit(Commits::RevertService, success_notice: "The #{@commit.change_type_title(current_user)} has been successfully reverted.",
67
                                          success_path: -> { successful_change_path }, failure_path: failed_change_path)
68
  end
69

70
  def cherry_pick
71
    assign_change_commit_vars
72

73 74
    return render_404 if @start_branch.blank?

75
    @branch_name = create_new_branch? ? @commit.cherry_pick_branch_name : @start_branch
76

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

81 82
  private

83 84 85 86
  def create_new_branch?
    params[:create_merge_request].present? || !can?(current_user, :push_code, @project)
  end

87
  def successful_change_path
88
    referenced_merge_request_url || project_commits_url(@project, @branch_name)
89 90
  end

91
  def failed_change_path
92
    referenced_merge_request_url || project_commit_url(@project, params[:id])
93 94 95
  end

  def referenced_merge_request_url
96
    if merge_request = @commit.merged_merge_request(current_user)
97
      project_merge_request_url(merge_request.target_project, merge_request)
98
    end
99 100
  end

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

Sean McGivern's avatar
Sean McGivern committed
105
  def define_commit_vars
106 107
    return git_not_found! unless commit

108 109
    opts = diff_options
    opts[:ignore_whitespace_change] = true if params[:format] == 'diff'
110

111
    @diffs = commit.diffs(opts)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
112
    @notes_count = commit.notes.count
113

Douwe Maan's avatar
Douwe Maan committed
114
    @environment = EnvironmentsFinder.new(@project, current_user, commit: @commit).execute.last
Sean McGivern's avatar
Sean McGivern committed
115 116 117
  end

  def define_note_vars
118
    @noteable = @commit
Sean McGivern's avatar
Sean McGivern committed
119 120
    @note = @project.build_commit_note(commit)

121
    @new_diff_note_attrs = {
Sean McGivern's avatar
Sean McGivern committed
122 123 124
      noteable_type: 'Commit',
      commit_id: @commit.id
    }
125 126 127 128

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

129
    @notes = (@grouped_diff_discussions.values.flatten + @discussions).flat_map(&:notes)
130
    @notes = prepare_notes_for_rendering(@notes)
Sean McGivern's avatar
Sean McGivern committed
131
  end
Douwe Maan's avatar
Douwe Maan committed
132

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