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

Robert Speicher's avatar
Robert Speicher committed
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
Kamil Trzcinski's avatar
Kamil Trzcinski committed
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]
Robert Speicher's avatar
Robert Speicher committed
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
32
      format.diff  { render text: @commit.to_diff }
33
      format.patch { render text: @commit.to_patch }
Robert Speicher's avatar
Robert Speicher committed
34 35
    end
  end
36

Kamil Trzcinski's avatar
Kamil Trzcinski committed
37
  def builds
38 39
  end

40
  def cancel_builds
Kamil Trzcinski's avatar
Kamil Trzcinski committed
41
    ci_commit.builds.running_or_pending.each(&:cancel)
42

Douwe Maan's avatar
Douwe Maan committed
43
    redirect_back_or_default default: builds_namespace_project_commit_path(project.namespace, project, commit.sha)
44 45
  end

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

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

Kamil Trzcinski's avatar
Kamil Trzcinski committed
70 71
  private

72
  def commit
73
    @commit ||= @project.commit(params[:id])
74
  end
75

Kamil Trzcinski's avatar
Kamil Trzcinski committed
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]
Kamil Trzcinski's avatar
Kamil Trzcinski committed
90
    @notes_count = commit.notes.count
Douwe Maan's avatar
Douwe Maan committed
91 92

    @statuses = ci_commit.statuses if ci_commit
Kamil Trzcinski's avatar
Kamil Trzcinski committed
93
  end
94 95

  def assign_revert_commit_vars
96
    @commit = project.commit(params[:id])
97
    @target_branch = params[:target_branch]
98 99
    @mr_source_branch = @commit.revert_branch_name
    @mr_target_branch = @target_branch
100
    @commit_params = {
101 102
      commit: @commit,
      create_merge_request: params[:create_merge_request].present? || different_project?
103 104
    }
  end
Robert Speicher's avatar
Robert Speicher committed
105
end