commit_controller.rb 2.21 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
Robert Speicher's avatar
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's avatar
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's avatar
Robert Speicher committed
12 13

  def show
14
    return git_not_found! unless @commit
Robert Speicher's avatar
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's avatar
Robert Speicher committed
30 31
    end
  end
32

Kamil Trzcinski's avatar
Kamil Trzcinski committed
33
  def builds
34 35
  end

36
  def cancel_builds
Kamil Trzcinski's avatar
Kamil Trzcinski committed
37
    ci_commit.builds.running_or_pending.each(&:cancel)
38

Douwe Maan's avatar
Douwe Maan committed
39
    redirect_back_or_default default: builds_namespace_project_commit_path(project.namespace, project, commit.sha)
40 41
  end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
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

Douwe Maan's avatar
Douwe Maan committed
49
    redirect_back_or_default default: builds_namespace_project_commit_path(project.namespace, project, commit.sha)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
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

Kamil Trzcinski's avatar
Kamil Trzcinski committed
58 59
  private

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

Kamil Trzcinski's avatar
Kamil Trzcinski committed
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

75
    @diff_refs = [commit.parent || commit, commit]
Kamil Trzcinski's avatar
Kamil Trzcinski committed
76
    @notes_count = commit.notes.count
Douwe Maan's avatar
Douwe Maan committed
77 78

    @statuses = ci_commit.statuses if ci_commit
Kamil Trzcinski's avatar
Kamil Trzcinski committed
79
  end
80 81 82

  def authorize_manage_builds!
    unless can?(current_user, :manage_builds, project)
83
      return render_404
84 85
    end
  end
Robert Speicher's avatar
Robert Speicher committed
86
end