commit_controller.rb 2.2 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
K
Kamil Trzcinski 已提交
7 8
  before_action :authorize_download_code!, except: [:cancel_builds]
  before_action :authorize_manage_builds!, only: [:cancel_builds]
9
  before_action :commit
K
Kamil Trzcinski 已提交
10 11
  before_action :authorize_manage_builds!, only: [:cancel_builds, :retry_builds]
  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
    @line_notes = commit.notes.inline
17
    @note = @project.build_commit_note(commit)
18
    @notes = commit.notes.not_inline.fresh
19
    @noteable = @commit
20
    @comments_allowed = @reply_allowed = true
21 22 23 24
    @comments_target  = {
      noteable_type: 'Commit',
      commit_id: @commit.id
    }
25 26

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

K
Kamil Trzcinski 已提交
33
  def builds
34 35
  end

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

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

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

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

52 53 54 55 56 57
  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 已提交
58 59
  private

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

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

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

K
Kamil Trzcinski 已提交
75
    @notes_count = commit.notes.count
D
Douwe Maan 已提交
76 77

    @statuses = ci_commit.statuses if ci_commit
K
Kamil Trzcinski 已提交
78
  end
K
Kamil Trzcinski 已提交
79 80 81

  def authorize_manage_builds!
    unless can?(current_user, :manage_builds, project)
82
      return render_404
K
Kamil Trzcinski 已提交
83 84
    end
  end
R
Robert Speicher 已提交
85
end