commit_controller.rb 3.97 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
    @line_notes = commit.notes.inline
21
    @note = @project.build_commit_note(commit)
22
    @notes = commit.notes.not_inline.fresh
23
    @noteable = @commit
24
    @comments_allowed = @reply_allowed = true
25 26 27 28
    @comments_target  = {
      noteable_type: 'Commit',
      commit_id: @commit.id
    }
29 30

    respond_to do |format|
31
      format.html
32
      format.diff  { render text: @commit.to_diff }
33
      format.patch { render text: @commit.to_patch }
Robert Speicher's avatar
Robert Speicher committed
34 35
    end
  end
36

Kamil Trzcinski's avatar
Kamil Trzcinski committed
37
  def builds
38 39
  end

40
  def cancel_builds
Kamil Trzcinski's avatar
Kamil Trzcinski committed
41
    ci_builds.running_or_pending.each(&:cancel)
42

Douwe Maan's avatar
Douwe Maan committed
43
    redirect_back_or_default default: builds_namespace_project_commit_path(project.namespace, project, commit.sha)
44 45
  end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
46
  def retry_builds
Kamil Trzcinski's avatar
Kamil Trzcinski committed
47
    ci_builds.latest.failed.each do |build|
Kamil Trzcinski's avatar
Kamil Trzcinski committed
48 49 50 51 52
      if build.retryable?
        Ci::Build.retry(build)
      end
    end

Douwe Maan's avatar
Douwe Maan committed
53
    redirect_back_or_default default: builds_namespace_project_commit_path(project.namespace, project, commit.sha)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
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
63
    assign_change_commit_vars(@commit.revert_branch_name)
64

65
    return render_404 if @target_branch.blank?
66

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
70 71 72 73 74
  
  def cherry_pick
    assign_change_commit_vars(@commit.cherry_pick_branch_name)
    
    return render_404 if @target_branch.blank?
75

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

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

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
101

Kamil Trzcinski's avatar
Kamil Trzcinski committed
102 103
  def ci_commits
    @ci_commits ||= project.ci_commits.where(sha: commit.sha)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
104 105
  end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
106 107
  def ci_builds
    @ci_builds ||= Ci::Build.where(commit: ci_commits)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
108 109 110
  end

  def define_show_vars
111 112
    return git_not_found! unless commit

113 114
    opts = diff_options
    opts[:ignore_whitespace_change] = true if params[:format] == 'diff'
115

116
    @diffs = commit.diffs(opts)
117
    @diff_refs = [commit.parent || commit, commit]
Kamil Trzcinski's avatar
Kamil Trzcinski committed
118
    @notes_count = commit.notes.count
Douwe Maan's avatar
Douwe Maan committed
119

Kamil Trzcinski's avatar
Kamil Trzcinski committed
120 121
    @statuses = CommitStatus.where(commit: ci_commits)
    @builds = Ci::Build.where(commit: ci_commits)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
122
  end
123

124
  def assign_change_commit_vars(mr_source_branch)
125
    @commit = project.commit(params[:id])
126
    @target_branch = params[:target_branch]
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
Robert Speicher's avatar
Robert Speicher committed
134
end