commit_controller.rb 3.54 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 6
  include CreatesCommit

Robert Speicher's avatar
Robert Speicher committed
7
  # Authorize
8
  before_action :require_non_empty_project
9 10 11
  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]
12
  before_action :commit
Kamil Trzcinski's avatar
Kamil Trzcinski committed
13
  before_action :define_show_vars, only: [:show, :builds]
14
  before_action :assign_revert_commit_vars, only: [:revert]
15
  before_action :authorize_edit_tree!, only: [:revert]
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_commit.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 47 48 49 50 51 52
  def retry_builds
    ci_commit.builds.latest.failed.each do |build|
      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
    return render_404 if @target_branch.blank?
64

65 66
    create_commit(Commits::RevertService, success_notice: "The #{revert_type_title} has been successfully reverted.",
                                          success_path: successful_revert_path, failure_path: failed_revert_path)
67 68
  end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
69 70
  private

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
  def revert_type_title
    @commit.merged_merge_request ? 'merge request' : 'commit'
  end

  def successful_revert_path
    return referenced_merge_request_url if @commit.merged_merge_request

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

  def failed_revert_path
    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

91
  def commit
92
    @commit ||= @project.commit(params[:id])
93
  end
94

Kamil Trzcinski's avatar
Kamil Trzcinski committed
95 96 97 98 99
  def ci_commit
    @ci_commit ||= project.ci_commit(commit.sha)
  end

  def define_show_vars
100 101
    return git_not_found! unless commit

102 103 104 105 106 107
    if params[:w].to_i == 1
      @diffs = commit.diffs({ ignore_whitespace_change: true })
    else
      @diffs = commit.diffs
    end

108
    @diff_refs = [commit.parent || commit, commit]
Kamil Trzcinski's avatar
Kamil Trzcinski committed
109
    @notes_count = commit.notes.count
Douwe Maan's avatar
Douwe Maan committed
110 111

    @statuses = ci_commit.statuses if ci_commit
Kamil Trzcinski's avatar
Kamil Trzcinski committed
112
  end
113 114

  def assign_revert_commit_vars
115
    @commit = project.commit(params[:id])
116
    @target_branch = params[:target_branch]
117 118
    @mr_source_branch = @commit.revert_branch_name
    @mr_target_branch = @target_branch
119
    @commit_params = {
120
      commit: @commit,
121
      revert_type_title: revert_type_title,
122
      create_merge_request: params[:create_merge_request].present? || different_project?
123 124
    }
  end
Robert Speicher's avatar
Robert Speicher committed
125
end