commit_controller.rb 2.1 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
R
Robert Speicher 已提交
5
  # Authorize
6
  before_action :require_non_empty_project
7 8 9
  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]
10
  before_action :commit
K
Kamil Trzcinski 已提交
11
  before_action :define_show_vars, only: [:show, :builds]
R
Robert Speicher 已提交
12 13

  def show
14
    return git_not_found! unless @commit
R
Robert Speicher 已提交
15

16 17
    apply_diff_view_cookie!

18
    @line_notes = commit.notes.inline
19
    @note = @project.build_commit_note(commit)
20
    @notes = commit.notes.not_inline.fresh
21
    @noteable = @commit
22
    @comments_allowed = @reply_allowed = true
23 24 25 26
    @comments_target  = {
      noteable_type: 'Commit',
      commit_id: @commit.id
    }
27 28

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

K
Kamil Trzcinski 已提交
35
  def builds
36 37
  end

38
  def cancel_builds
K
Kamil Trzcinski 已提交
39
    ci_commit.builds.running_or_pending.each(&:cancel)
40

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

K
Kamil Trzcinski 已提交
44 45 46 47 48 49 50
  def retry_builds
    ci_commit.builds.latest.failed.each do |build|
      if build.retryable?
        Ci::Build.retry(build)
      end
    end

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

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

K
Kamil Trzcinski 已提交
60 61
  private

62
  def commit
63
    @commit ||= @project.commit(params[:id])
64
  end
K
Kamil Trzcinski 已提交
65

K
Kamil Trzcinski 已提交
66 67 68 69 70
  def ci_commit
    @ci_commit ||= project.ci_commit(commit.sha)
  end

  def define_show_vars
71 72 73 74 75 76
    if params[:w].to_i == 1
      @diffs = commit.diffs({ ignore_whitespace_change: true })
    else
      @diffs = commit.diffs
    end

77
    @diff_refs = [commit.parent || commit, commit]
K
Kamil Trzcinski 已提交
78
    @notes_count = commit.notes.count
D
Douwe Maan 已提交
79 80

    @statuses = ci_commit.statuses if ci_commit
K
Kamil Trzcinski 已提交
81
  end
R
Robert Speicher 已提交
82
end