commit_controller.rb 3.51 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 :authorize_edit_tree!, only: [:revert]
Robert Speicher's avatar
Robert Speicher committed
15 16

  def show
17 18
    apply_diff_view_cookie!

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

    respond_to do |format|
30
      format.html
31
      format.diff  { render text: @commit.to_diff }
32
      format.patch { render text: @commit.to_patch }
Robert Speicher's avatar
Robert Speicher committed
33 34
    end
  end
35

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

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

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

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

Douwe Maan's avatar
Douwe Maan committed
52
    redirect_back_or_default default: builds_namespace_project_commit_path(project.namespace, project, commit.sha)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
53
  end
54

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

61
  def revert
62 63
    assign_revert_commit_vars

64
    return render_404 if @target_branch.blank?
65

66 67
    create_commit(Commits::RevertService, success_notice: "The #{revert_type_title} has been successfully reverted.",
                                          success_path: successful_revert_path, failure_path: failed_revert_path)
68 69
  end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
70 71
  private

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
  def revert_type_title
    @commit.merged_merge_request ? 'merge request' : 'commit'
  end

  def successful_revert_path
    return referenced_merge_request_url if @commit.merged_merge_request

    namespace_project_commits_url(@project.namespace, @project, @target_branch)
  end

  def failed_revert_path
    return referenced_merge_request_url if @commit.merged_merge_request

    namespace_project_commit_url(@project.namespace, @project, params[:id])
  end

  def referenced_merge_request_url
    namespace_project_merge_request_url(@project.namespace, @project, @commit.merged_merge_request)
  end

92
  def commit
93
    @commit ||= @project.commit(params[:id])
94
  end
95

Kamil Trzcinski's avatar
Kamil Trzcinski committed
96 97 98 99 100
  def ci_commit
    @ci_commit ||= project.ci_commit(commit.sha)
  end

  def define_show_vars
101 102
    return git_not_found! unless commit

103 104 105 106 107 108
    if params[:w].to_i == 1
      @diffs = commit.diffs({ ignore_whitespace_change: true })
    else
      @diffs = commit.diffs
    end

109
    @diff_refs = [commit.parent || commit, commit]
Kamil Trzcinski's avatar
Kamil Trzcinski committed
110
    @notes_count = commit.notes.count
Douwe Maan's avatar
Douwe Maan committed
111 112

    @statuses = ci_commit.statuses if ci_commit
Kamil Trzcinski's avatar
Kamil Trzcinski committed
113
  end
114 115

  def assign_revert_commit_vars
116
    @commit = project.commit(params[:id])
117
    @target_branch = params[:target_branch]
118 119
    @mr_source_branch = @commit.revert_branch_name
    @mr_target_branch = @target_branch
120
    @commit_params = {
121
      commit: @commit,
122
      revert_type_title: revert_type_title,
123
      create_merge_request: params[:create_merge_request].present? || different_project?
124 125
    }
  end
Robert Speicher's avatar
Robert Speicher committed
126
end