Class: MergeRequests::CreateRefService

Inherits:
Object
  • Object
show all
Includes:
Gitlab::Utils::StrongMemoize
Defined in:
app/services/merge_requests/create_ref_service.rb

Overview

CreateRefService creates or overwrites a ref under “refs/merge-requests/” with a commit for the merged result.

Constant Summary collapse

CreateRefError =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initialize(current_user:, merge_request:, target_ref:, first_parent_ref:, source_sha: nil) ⇒ CreateRefService

Returns a new instance of CreateRefService.



11
12
13
14
15
16
17
18
19
20
# File 'app/services/merge_requests/create_ref_service.rb', line 11

def initialize(
  current_user:, merge_request:, target_ref:, first_parent_ref:, source_sha: nil
)
  @current_user = current_user
  @merge_request = merge_request
  @source_sha = source_sha
  @target_ref = target_ref
  @first_parent_ref = first_parent_ref
  @first_parent_sha = target_project.commit(first_parent_ref)&.sha
end

Instance Method Details

#executeObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/services/merge_requests/create_ref_service.rb', line 22

def execute
  # TODO: Update this message with the removal of FF merge_trains_create_ref_service and update tests
  # This is for compatibility with MergeToRefService during the rollout.
  return ServiceResponse.error(message: '3:Invalid merge source') unless first_parent_sha.present?

  result = {
    commit_sha: source_sha,      # the SHA to be at HEAD of target_ref
    expected_old_oid: "",        # the SHA we expect target_ref to be at prior to an update (an optimistic lock)
    source_sha: source_sha,      # for pipeline.source_sha
    target_sha: first_parent_sha # for pipeline.target_sha
  }

  result = maybe_squash!(**result)
  result = maybe_rebase!(**result)
  result = maybe_merge!(**result)

  update_merge_request!(merge_request, result)

  ServiceResponse.success(payload: result)
rescue CreateRefError => error
  ServiceResponse.error(message: error.message)
end