commit_controller.rb 3.75 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 CreatesCommit
Sean McGivern's avatar
Sean McGivern committed
6
  include DiffForPath
7
  include DiffHelper
8

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

  def show
19 20
    apply_diff_view_cookie!

21
    respond_to do |format|
22
      format.html
23
      format.diff  { render text: @commit.to_diff }
24
      format.patch { render text: @commit.to_patch }
Robert Speicher's avatar
Robert Speicher committed
25 26
    end
  end
27

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

Kamil Trzcinski's avatar
Kamil Trzcinski committed
32
  def pipelines
33 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)
          .with_pagination(request, response)
          .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
    return render_404 if @target_branch.blank?
56

57
    create_commit(Commits::RevertService, success_notice: "The #{@commit.change_type_title(current_user)} has been successfully reverted.",
58
                                          success_path: successful_change_path, failure_path: failed_change_path)
59
  end
60

61
  def cherry_pick
62
    assign_change_commit_vars
63

64
    return render_404 if @target_branch.blank?
65

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

70 71 72
  private

  def successful_change_path
73
    referenced_merge_request_url || namespace_project_commits_url(@project.namespace, @project, @target_branch)
74 75
  end

76
  def failed_change_path
77
    referenced_merge_request_url || namespace_project_commit_url(@project.namespace, @project, params[:id])
78 79 80
  end

  def referenced_merge_request_url
81 82 83
    if merge_request = @commit.merged_merge_request(current_user)
      namespace_project_merge_request_url(@project.namespace, @project, merge_request)
    end
84 85
  end

86
  def commit
87
    @noteable = @commit ||= @project.commit(params[:id])
88
  end
89

Sean McGivern's avatar
Sean McGivern committed
90
  def define_commit_vars
91 92
    return git_not_found! unless commit

93 94
    opts = diff_options
    opts[:ignore_whitespace_change] = true if params[:format] == 'diff'
95

96
    @diffs = commit.diffs(opts)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
97
    @notes_count = commit.notes.count
98

99
    @environment = @project.environments_for(commit: @commit).last
100
    @environment = nil unless can?(current_user, :read_environment, @environment)
Sean McGivern's avatar
Sean McGivern committed
101 102 103
  end

  def define_note_vars
104
    @grouped_diff_discussions = commit.notes.grouped_diff_discussions
Sean McGivern's avatar
Sean McGivern committed
105 106 107
    @notes = commit.notes.non_diff_notes.fresh

    Banzai::NoteRenderer.render(
108
      @grouped_diff_discussions.values.flat_map(&:notes) + @notes,
Sean McGivern's avatar
Sean McGivern committed
109 110 111 112 113 114 115 116 117 118 119 120
      @project,
      current_user,
    )

    @note = @project.build_commit_note(commit)

    @noteable = @commit
    @comments_target = {
      noteable_type: 'Commit',
      commit_id: @commit.id
    }
  end
Douwe Maan's avatar
Douwe Maan committed
121

122
  def assign_change_commit_vars
123
    @commit = project.commit(params[:id])
124 125
    @target_branch = params[:target_branch]
    @commit_params = {
126 127
      commit: @commit,
      create_merge_request: params[:create_merge_request].present? || different_project?
128 129
    }
  end
Robert Speicher's avatar
Robert Speicher committed
130
end