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

R
Robert Speicher 已提交
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
K
Kamil Trzcinski 已提交
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]
R
Robert Speicher 已提交
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
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_commit.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 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

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
63
    # return render_404 unless @commit_params.values.all?
64

65 66 67
    create_commit(Commits::RevertService, success_notice: "The commit has been successfully reverted.",
                                          success_path: namespace_project_commits_path(@project.namespace, @project, @target_branch),
                                          failure_path: namespace_project_commit_path(@project.namespace, @project, params[:id]))
68 69
  end

K
Kamil Trzcinski 已提交
70 71
  private

72
  def commit
73
    @commit ||= @project.commit(params[:id])
74
  end
K
Kamil Trzcinski 已提交
75

K
Kamil Trzcinski 已提交
76 77 78 79 80
  def ci_commit
    @ci_commit ||= project.ci_commit(commit.sha)
  end

  def define_show_vars
81 82
    return git_not_found! unless commit

83 84 85 86 87 88
    if params[:w].to_i == 1
      @diffs = commit.diffs({ ignore_whitespace_change: true })
    else
      @diffs = commit.diffs
    end

89
    @diff_refs = [commit.parent || commit, commit]
K
Kamil Trzcinski 已提交
90
    @notes_count = commit.notes.count
D
Douwe Maan 已提交
91 92

    @statuses = ci_commit.statuses if ci_commit
K
Kamil Trzcinski 已提交
93
  end
94 95 96 97 98 99 100 101

  def assign_revert_commit_vars
    @target_branch = params[:target_branch]

    @commit_params = {
      revert_commit_id: params[:id],
    }
  end
R
Robert Speicher 已提交
102
end