commit_controller.rb 4.0 KB
Newer Older
R
Robert Speicher 已提交
1 2 3
# Controller for a specific Commit
#
# Not to be confused with CommitsController, plural.
4
class Projects::CommitController < Projects::ApplicationController
5
  include CreatesCommit
J
Jacob Vosmaer 已提交
6
  include DiffHelper
7

R
Robert Speicher 已提交
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
K
Kamil Trzcinski 已提交
14
  before_action :define_show_vars, only: [:show, :builds]
P
P.S.V.R 已提交
15
  before_action :authorize_edit_tree!, only: [:revert, :cherry_pick]
R
Robert Speicher 已提交
16 17

  def show
18 19
    apply_diff_view_cookie!

D
Douwe Maan 已提交
20 21
    @grouped_diff_notes = commit.notes.grouped_diff_notes

22
    @note = @project.build_commit_note(commit)
D
Douwe Maan 已提交
23
    @notes = commit.notes.non_diff_notes.fresh
24
    @noteable = @commit
D
Douwe Maan 已提交
25
    @comments_target = {
26 27 28
      noteable_type: 'Commit',
      commit_id: @commit.id
    }
29 30

    respond_to do |format|
31
      format.html
R
Riyad Preukschas 已提交
32
      format.diff  { render text: @commit.to_diff }
R
Riyad Preukschas 已提交
33
      format.patch { render text: @commit.to_patch }
R
Robert Speicher 已提交
34 35
    end
  end
36

K
Kamil Trzcinski 已提交
37
  def builds
38 39
  end

40
  def cancel_builds
K
Kamil Trzcinski 已提交
41
    ci_builds.running_or_pending.each(&:cancel)
42

D
Douwe Maan 已提交
43
    redirect_back_or_default default: builds_namespace_project_commit_path(project.namespace, project, commit.sha)
44 45
  end

K
Kamil Trzcinski 已提交
46
  def retry_builds
K
Kamil Trzcinski 已提交
47
    ci_builds.latest.failed.each do |build|
K
Kamil Trzcinski 已提交
48
      if build.retryable?
49
        Ci::Build.retry(build, current_user)
K
Kamil Trzcinski 已提交
50 51 52
      end
    end

D
Douwe Maan 已提交
53
    redirect_back_or_default default: builds_namespace_project_commit_path(project.namespace, project, commit.sha)
K
Kamil Trzcinski 已提交
54
  end
55

56 57 58 59 60 61
  def branches
    @branches = @project.repository.branch_names_contains(commit.id)
    @tags = @project.repository.tag_names_contains(commit.id)
    render layout: false
  end

62
  def revert
P
P.S.V.R 已提交
63
    assign_change_commit_vars(@commit.revert_branch_name)
64

65
    return render_404 if @target_branch.blank?
66

P
P.S.V.R 已提交
67 68
    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)
69
  end
D
Douwe Maan 已提交
70

P
P.S.V.R 已提交
71 72
  def cherry_pick
    assign_change_commit_vars(@commit.cherry_pick_branch_name)
D
Douwe Maan 已提交
73

P
P.S.V.R 已提交
74
    return render_404 if @target_branch.blank?
75

P
P.S.V.R 已提交
76 77
    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)
78 79
  end

P
P.S.V.R 已提交
80 81 82
  private

  def successful_change_path
83 84 85 86 87
    return referenced_merge_request_url if @commit.merged_merge_request

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

P
P.S.V.R 已提交
88
  def failed_change_path
89 90 91 92 93 94 95 96 97
    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

98
  def commit
99
    @commit ||= @project.commit(params[:id])
100
  end
K
Kamil Trzcinski 已提交
101

102 103
  def pipelines
    @pipelines ||= project.pipelines.where(sha: commit.sha)
K
Kamil Trzcinski 已提交
104 105
  end

K
Kamil Trzcinski 已提交
106
  def ci_builds
107
    @ci_builds ||= Ci::Build.where(pipeline: pipelines)
K
Kamil Trzcinski 已提交
108 109 110
  end

  def define_show_vars
111 112
    return git_not_found! unless commit

J
Jacob Vosmaer 已提交
113 114
    opts = diff_options
    opts[:ignore_whitespace_change] = true if params[:format] == 'diff'
115

J
Jacob Vosmaer 已提交
116
    @diffs = commit.diffs(opts)
117
    @diff_refs = [commit.parent || commit, commit]
K
Kamil Trzcinski 已提交
118
    @notes_count = commit.notes.count
D
Douwe Maan 已提交
119

120 121
    @statuses = CommitStatus.where(pipeline: pipelines)
    @builds = Ci::Build.where(pipeline: pipelines)
K
Kamil Trzcinski 已提交
122
  end
123

P
P.S.V.R 已提交
124
  def assign_change_commit_vars(mr_source_branch)
125
    @commit = project.commit(params[:id])
126
    @target_branch = params[:target_branch]
P
P.S.V.R 已提交
127
    @mr_source_branch = mr_source_branch
128
    @mr_target_branch = @target_branch
129
    @commit_params = {
130 131
      commit: @commit,
      create_merge_request: params[:create_merge_request].present? || different_project?
132 133
    }
  end
R
Robert Speicher 已提交
134
end