commit_controller.rb 4.03 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
6
  include DiffHelper
7

Robert Speicher's avatar
Robert Speicher committed
8
  # Authorize
9
  before_action :require_non_empty_project
10 11 12
  before_action :authorize_download_code!, except: [:cancel_builds, :retry_builds]
  before_action :authorize_update_build!, only: [:cancel_builds, :retry_builds]
  before_action :authorize_read_commit_status!, only: [:builds]
13
  before_action :commit
Kamil Trzcinski's avatar
Kamil Trzcinski committed
14
  before_action :define_show_vars, only: [:show, :builds]
15
  before_action :authorize_edit_tree!, only: [:revert, :cherry_pick]
Robert Speicher's avatar
Robert Speicher committed
16 17

  def show
18 19
    apply_diff_view_cookie!

20
    @grouped_diff_notes = commit.notes.grouped_diff_notes
Douwe Maan's avatar
Douwe Maan committed
21 22 23 24 25 26 27
    @notes = commit.notes.non_diff_notes.fresh
    
    Banzai::NoteRenderer.render(
      @grouped_diff_notes.values.flatten + @notes,
      @project,
      current_user,
    )
28

29
    @note = @project.build_commit_note(commit)
Douwe Maan's avatar
Douwe Maan committed
30

31
    @noteable = @commit
Douwe Maan's avatar
Douwe Maan committed
32
    @comments_target = {
33 34 35
      noteable_type: 'Commit',
      commit_id: @commit.id
    }
36 37

    respond_to do |format|
38
      format.html
39
      format.diff  { render text: @commit.to_diff }
40
      format.patch { render text: @commit.to_patch }
Robert Speicher's avatar
Robert Speicher committed
41 42
    end
  end
43

Kamil Trzcinski's avatar
Kamil Trzcinski committed
44
  def builds
45 46
  end

47
  def cancel_builds
Kamil Trzcinski's avatar
Kamil Trzcinski committed
48
    ci_builds.running_or_pending.each(&:cancel)
49

Douwe Maan's avatar
Douwe Maan committed
50
    redirect_back_or_default default: builds_namespace_project_commit_path(project.namespace, project, commit.sha)
51 52
  end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
53
  def retry_builds
Kamil Trzcinski's avatar
Kamil Trzcinski committed
54
    ci_builds.latest.failed.each do |build|
Kamil Trzcinski's avatar
Kamil Trzcinski committed
55
      if build.retryable?
56
        Ci::Build.retry(build, current_user)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
57 58 59
      end
    end

Douwe Maan's avatar
Douwe Maan committed
60
    redirect_back_or_default default: builds_namespace_project_commit_path(project.namespace, project, commit.sha)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
61
  end
62

63 64 65 66 67 68
  def branches
    @branches = @project.repository.branch_names_contains(commit.id)
    @tags = @project.repository.tag_names_contains(commit.id)
    render layout: false
  end

69
  def revert
70
    assign_change_commit_vars(@commit.revert_branch_name)
71

72
    return render_404 if @target_branch.blank?
73

74 75
    create_commit(Commits::RevertService, success_notice: "The #{@commit.change_type_title} has been successfully reverted.",
                                          success_path: successful_change_path, failure_path: failed_change_path)
76
  end
77

78 79
  def cherry_pick
    assign_change_commit_vars(@commit.cherry_pick_branch_name)
80

81
    return render_404 if @target_branch.blank?
82

83 84
    create_commit(Commits::CherryPickService, success_notice: "The #{@commit.change_type_title} has been successfully cherry-picked.",
                                              success_path: successful_change_path, failure_path: failed_change_path)
85 86
  end

87 88 89
  private

  def successful_change_path
90 91 92 93 94
    return referenced_merge_request_url if @commit.merged_merge_request

    namespace_project_commits_url(@project.namespace, @project, @target_branch)
  end

95
  def failed_change_path
96 97 98 99 100 101 102 103 104
    return referenced_merge_request_url if @commit.merged_merge_request

    namespace_project_commit_url(@project.namespace, @project, params[:id])
  end

  def referenced_merge_request_url
    namespace_project_merge_request_url(@project.namespace, @project, @commit.merged_merge_request)
  end

105
  def commit
106
    @commit ||= @project.commit(params[:id])
107
  end
108

109 110
  def pipelines
    @pipelines ||= project.pipelines.where(sha: commit.sha)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
111 112
  end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
113
  def ci_builds
114
    @ci_builds ||= Ci::Build.where(pipeline: pipelines)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
115 116 117
  end

  def define_show_vars
118 119
    return git_not_found! unless commit

120 121
    opts = diff_options
    opts[:ignore_whitespace_change] = true if params[:format] == 'diff'
122

123
    @diffs = commit.diffs(opts)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
124
    @notes_count = commit.notes.count
Douwe Maan's avatar
Douwe Maan committed
125

126 127
    @statuses = CommitStatus.where(pipeline: pipelines)
    @builds = Ci::Build.where(pipeline: pipelines)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
128
  end
129

130
  def assign_change_commit_vars(mr_source_branch)
131
    @commit = project.commit(params[:id])
132
    @target_branch = params[:target_branch]
133
    @mr_source_branch = mr_source_branch
134
    @mr_target_branch = @target_branch
135
    @commit_params = {
136 137
      commit: @commit,
      create_merge_request: params[:create_merge_request].present? || different_project?
138 139
    }
  end
Robert Speicher's avatar
Robert Speicher committed
140
end