Class: MergeRequests::CreateFromIssueService

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

Instance Attribute Summary

Attributes inherited from BaseService

#current_user, #params, #project

Instance Method Summary collapse

Methods inherited from CreateService

#after_create, #create

Methods inherited from BaseService

#cancel_review_app_jobs!, #cleanup_environments, #create_note, #execute_hooks, #hook_data, #inspect, #source_project

Methods included from Gitlab::Utils::Override

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

Methods included from AssignsMergeParams

#assign_allowed_merge_params, included

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, user, params) ⇒ CreateFromIssueService

Returns a new instance of CreateFromIssueService.


5
6
7
8
9
10
11
12
13
14
15
# File 'app/services/merge_requests/create_from_issue_service.rb', line 5

def initialize(project, user, params)
  # branch - the name of new branch
  # ref    - the source of new branch.

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

  super(project, user)
end

Instance Method Details

#executeObject


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/services/merge_requests/create_from_issue_service.rb', line 17

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?
    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