commit_controller.rb 3.82 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
        render json: PipelineSerializer
Fatih Acet's avatar
Fatih Acet committed
42
          .new(project: @project, current_user: @current_user)
43 44 45
          .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
    return render_404 if @start_branch.blank?

59
    @branch_name = create_new_branch? ? @commit.revert_branch_name : @start_branch
60

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

65
  def cherry_pick
66
    assign_change_commit_vars
67

68 69
    return render_404 if @start_branch.blank?

70
    @branch_name = create_new_branch? ? @commit.cherry_pick_branch_name : @start_branch
71

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

76 77
  private

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

82
  def successful_change_path
83
    referenced_merge_request_url || project_commits_url(@project, @branch_name)
84 85
  end

86
  def failed_change_path
87
    referenced_merge_request_url || project_commit_url(@project, params[:id])
88 89 90
  end

  def referenced_merge_request_url
91
    if merge_request = @commit.merged_merge_request(current_user)
92
      project_merge_request_url(merge_request.target_project, merge_request)
93
    end
94 95
  end

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

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

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

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

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

  def define_note_vars
113
    @noteable = @commit
Sean McGivern's avatar
Sean McGivern committed
114 115
    @note = @project.build_commit_note(commit)

116
    @new_diff_note_attrs = {
Sean McGivern's avatar
Sean McGivern committed
117 118 119
      noteable_type: 'Commit',
      commit_id: @commit.id
    }
120 121 122 123

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

124
    @notes = (@grouped_diff_discussions.values.flatten + @discussions).flat_map(&:notes)
125
    @notes = prepare_notes_for_rendering(@notes)
Sean McGivern's avatar
Sean McGivern committed
126
  end
Douwe Maan's avatar
Douwe Maan committed
127

128
  def assign_change_commit_vars
129 130
    @start_branch = params[:start_branch]
    @commit_params = { commit: @commit }
131
  end
Robert Speicher's avatar
Robert Speicher committed
132
end