Class: MergeRequests::CreateFromIssueService

Inherits:
CreateService show all
Defined in:
app/services/merge_requests/create_from_issue_service.rb

Instance Attribute Summary

Attributes inherited from BaseContainerService

#container, #current_user, #group, #params, #project

Instance Method Summary collapse

Methods inherited from CreateService

#after_create

Methods inherited from BaseService

#cancel_review_app_jobs!, #cleanup_environments, #create_note, #execute_external_hooks, #execute_group_mention_hooks, #execute_hooks, #handle_assignees_change, #handle_changes, #handle_reviewers_change, #hook_data, #inspect, #merge_request_activity_counter, #source_project

Methods included from Gitlab::Utils::Override

#extended, extensions, #included, #method_added, #override, #prepended, #queue_verification, verify!

Methods included from ErrorLogger

#log_error

Methods included from AssignsMergeParams

#assign_allowed_merge_params, included

Methods inherited from BaseContainerService

#group_container?, #namespace_container?, #project_container?, #project_group

Methods included from BaseServiceUtility

#deny_visibility_level, #event_service, #log_error, #log_info, #notification_service, #system_hook_service, #todo_service, #visibility_level

Methods included from Gitlab::Allowable

#can?

Constructor Details

#initialize(project:, current_user:, mr_params: {}) ⇒ CreateFromIssueService

TODO: This constructor does not use the “params:” argument from the superclass,

but instead has a custom "mr_params:" argument. This is because historically,
prior to named arguments being introduced to the constructor, it never passed
along the third positional argument when calling `super`.
This should be changed, in order to be consistent (all subclasses should pass
along all of the arguments to the superclass, otherwise it is probably not an
"is a" relationship). However, we need to be sure that passing the params
argument to `super` (especially target_project_id) will not cause any unexpected
behavior in the superclass. Since the addition of the named arguments is
intended to be a low-risk pure refactor, we will defer this fix
to this follow-on issue:
https://gitlab.com/gitlab-org/gitlab/-/issues/328726


17
18
19
20
21
22
23
24
25
26
27
# File 'app/services/merge_requests/create_from_issue_service.rb', line 17

def initialize(project:, current_user:, mr_params: {})
  # branch - the name of new branch
  # ref    - the source of new branch.

  @branch_name       = mr_params[:branch_name]
  @issue_iid         = mr_params[:issue_iid]
  @ref               = mr_params[:ref]
  @target_project_id = mr_params[:target_project_id]

  super(project: project, current_user: current_user)
end

Instance Method Details

#executeObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/services/merge_requests/create_from_issue_service.rb', line 29

def execute
  return error('Project not found') if target_project.blank?
  return error('Not allowed to create merge request') unless can_create_merge_request?
  return error('Invalid issue iid') unless @issue_iid.present? && issue.present?

  result = ::Branches::CreateService.new(target_project, current_user).execute(branch_name, ref)
  return result if result[:status] == :error

  new_merge_request = create(merge_request)

  if new_merge_request.valid?
    merge_request_activity_counter.track_mr_create_from_issue(user: current_user)
    SystemNoteService.new_merge_request(issue, project, current_user, new_merge_request)

    success(new_merge_request)
  else
    SystemNoteService.new_issue_branch(issue, project, current_user, branch_name, branch_project: target_project)

    error(new_merge_request.errors)
  end
end