commit_controller.rb 4 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
        render json: PipelineSerializer
          .new(project: @project, user: @current_user)
          .represent(@pipelines)
      end
    end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
46 47
  end

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

54
  def revert
55
    assign_change_commit_vars
56

57 58 59 60 61
    return render_404 if @start_branch.blank?

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

    @mr_target_branch = @start_branch
62

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

67
  def cherry_pick
68
    assign_change_commit_vars
69

70 71 72 73 74
    return render_404 if @start_branch.blank?

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

    @mr_target_branch = @start_branch
75

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

80 81
  private

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

86
  def successful_change_path
87
    referenced_merge_request_url || namespace_project_commits_url(@project.namespace, @project, @target_branch)
88 89
  end

90
  def failed_change_path
91
    referenced_merge_request_url || namespace_project_commit_url(@project.namespace, @project, params[:id])
92 93 94
  end

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

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

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

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

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

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

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

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

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

128
    @notes = (@grouped_diff_discussions.values.flatten + @discussions).flat_map(&:notes)
129
    @notes = prepare_notes_for_rendering(@notes)
Sean McGivern's avatar
Sean McGivern committed
130
  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