Class: MergeRequests::BuildService

Inherits:
BaseService show all
Defined in:
app/services/merge_requests/build_service.rb

Instance Attribute Summary

Attributes inherited from BaseService

#current_user, #params, #project

Instance Method Summary collapse

Methods inherited from BaseService

#create_note, #create_title_change_note, #execute_hooks, #hook_data

Methods inherited from BaseService

#abilities, #can?, #deny_visibility_level, #event_service, #initialize, #log_info, #notification_service, #repository, #system_hook_service, #todo_service

Methods included from Gitlab::CurrentSettings

#current_application_settings, #fake_application_settings

Constructor Details

This class inherits a constructor from BaseService

Instance Method Details

#executeObject


3
4
5
6
7
8
9
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
# File 'app/services/merge_requests/build_service.rb', line 3

def execute
  merge_request = MergeRequest.new(params)

  # Set MR attributes
  merge_request.can_be_created = false
  merge_request.compare_commits = []
  merge_request.source_project = project unless merge_request.source_project

  merge_request.target_project = nil unless can?(current_user, :read_project, merge_request.target_project)

  merge_request.target_project ||= (project.forked_from_project || project)
  merge_request.target_branch ||= merge_request.target_project.default_branch

  if merge_request.target_branch.blank? || merge_request.source_branch.blank?
    message =
      if params[:source_branch] || params[:target_branch]
        "You must select source and target branch"
      end

    return build_failed(merge_request, message)
  end

  compare = CompareService.new.execute(
    merge_request.source_project,
    merge_request.source_branch,
    merge_request.target_project,
    merge_request.target_branch,
  )

  commits = compare.commits

  # At this point we decide if merge request can be created
  # If we have at least one commit to merge -> creation allowed
  if commits.present?
    merge_request.compare_commits = Commit.decorate(commits, merge_request.source_project)
    merge_request.can_be_created = true
    merge_request.compare = compare
  else
    merge_request.can_be_created = false
  end

  set_title_and_description(merge_request)
end