BigW Consortium Gitlab

commit_controller.rb 2.16 KB
Newer Older
Robert Speicher committed
1 2 3
# Controller for a specific Commit
#
# Not to be confused with CommitsController, plural.
4
class Projects::CommitController < Projects::ApplicationController
Robert Speicher committed
5
  # Authorize
6
  before_action :require_non_empty_project
7 8
  before_action :authorize_download_code!, except: [:cancel_builds]
  before_action :authorize_manage_builds!, only: [:cancel_builds]
9
  before_action :commit
Kamil Trzcinski committed
10 11
  before_action :authorize_manage_builds!, only: [:cancel_builds, :retry_builds]
  before_action :define_show_vars, only: [:show, :builds]
Robert Speicher committed
12 13

  def show
14
    return git_not_found! unless @commit
Robert Speicher committed
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
28
      format.diff  { render text: @commit.to_diff }
29
      format.patch { render text: @commit.to_patch }
Robert Speicher committed
30 31
    end
  end
32

Kamil Trzcinski committed
33
  def builds
34 35 36
    @ci_project = @project.gitlab_ci_project
  end

37
  def cancel_builds
Kamil Trzcinski committed
38
    ci_commit.builds.running_or_pending.each(&:cancel)
39

Kamil Trzcinski committed
40
    redirect_to builds_namespace_project_commit_path(project.namespace, project, commit.sha)
41 42
  end

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

    redirect_to builds_namespace_project_commit_path(project.namespace, project, commit.sha)
  end
52

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

Kamil Trzcinski committed
59 60
  private

61
  def commit
62
    @commit ||= @project.commit(params[:id])
63
  end
64

Kamil Trzcinski committed
65 66 67 68 69
  def ci_commit
    @ci_commit ||= project.ci_commit(commit.sha)
  end

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

Kamil Trzcinski committed
76 77 78 79
    @notes_count = commit.notes.count
    
    @builds = ci_commit.builds if ci_commit
  end
80 81 82 83 84 85

  def authorize_manage_builds!
    unless can?(current_user, :manage_builds, project)
      return page_404
    end
  end
Robert Speicher committed
86
end