Module: CreatesCommit

Extended by:
ActiveSupport::Concern
Includes:
ActionView::Helpers::SanitizeHelper, Gitlab::Utils::StrongMemoize, SafeFormatHelper
Included in:
Projects::BlobController, Projects::CommitController, Projects::TreeController
Defined in:
app/controllers/concerns/creates_commit.rb

Instance Method Summary collapse

Methods included from SafeFormatHelper

#safe_format, #tag_pair

Instance Method Details

#authorize_edit_tree!Object

rubocop:enable Gitlab/ModuleWithInstanceVariables



68
69
70
71
72
# File 'app/controllers/concerns/creates_commit.rb', line 68

def authorize_edit_tree!
  return if can_collaborate_with_project?(project, ref: branch_name_or_ref)

  access_denied!
end

#create_commit(service, success_path:, failure_path:, failure_view: nil, success_notice: nil, target_project: nil) ⇒ Object

rubocop:disable Gitlab/ModuleWithInstanceVariables



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/concerns/creates_commit.rb', line 10

def create_commit(service, success_path:, failure_path:, failure_view: nil, success_notice: nil, target_project: nil)
  target_project ||= @project

  if user_access(target_project).can_push_to_branch?(branch_name_or_ref)
    @project_to_commit_into = target_project
    @different_project = false
    @branch_name ||= @ref
  else
    @project_to_commit_into = current_user.fork_of(target_project)
    @different_project = true
    @branch_name ||= generated_branch_name(@project_to_commit_into)
  end

  @start_branch ||= @ref || @branch_name

  commit_params = @commit_params.merge(
    start_project: @project_to_commit_into,
    start_branch: @start_branch,
    source_project: @project,
    target_project: target_project,
    branch_name: @branch_name
  )

  result = service.new(@project_to_commit_into, current_user, commit_params).execute

  if result[:status] == :success
    success_path = final_success_path(success_path, target_project)

    update_flash_notice(success_notice, success_path)

    respond_to do |format|
      format.html { redirect_to success_path }
      format.json { render json: { message: _("success"), filePath: success_path } }
    end
  else
    flash[:alert] = flash_message(result, @project, @branch_name, @commit_params)

    failure_path = failure_path.call if failure_path.respond_to?(:call)

    respond_to do |format|
      format.html do
        if failure_view
          render failure_view
        else
          redirect_to failure_path
        end
      end
      format.json do
        render json: {
          error: result[:message],
          filePath: failure_path
        }, status: :unprocessable_entity
      end
    end
  end
end

#format_flash_notice(message) ⇒ Object



74
75
76
77
# File 'app/controllers/concerns/creates_commit.rb', line 74

def format_flash_notice(message)
  formatted_message = message.gsub("\n", "<br>")
  sanitize(formatted_message, tags: %w[br])
end