commit_controller.rb 3.93 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
5
  include NotesHelper
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 39 40 41 42 43
    @pipelines = @commit.pipelines.order(id: :desc)

    respond_to do |format|
      format.html
      format.json do
        render json: PipelineSerializer
          .new(project: @project, user: @current_user)
          .represent(@pipelines)
      end
    end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
44 45
  end

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

52
  def revert
53
    assign_change_commit_vars
54

55 56 57 58 59
    return render_404 if @start_branch.blank?

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

    @mr_target_branch = @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 70 71 72
    return render_404 if @start_branch.blank?

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

    @mr_target_branch = @start_branch
73

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

78 79
  private

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

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

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

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

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

Sean McGivern's avatar
Sean McGivern committed
102
  def define_commit_vars
103 104
    return git_not_found! unless commit

105 106
    opts = diff_options
    opts[:ignore_whitespace_change] = true if params[:format] == 'diff'
107

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

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

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

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

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

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

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