Class: MergeRequests::MergeWhenBuildSucceedsService

Inherits:
BaseService show all
Defined in:
app/services/merge_requests/merge_when_build_succeeds_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

#cancel(merge_request) ⇒ Object

Cancels the automatic merge


39
40
41
42
43
44
45
46
47
48
# File 'app/services/merge_requests/merge_when_build_succeeds_service.rb', line 39

def cancel(merge_request)
  if merge_request.merge_when_build_succeeds? && merge_request.open?
    merge_request.reset_merge_when_build_succeeds
    SystemNoteService.cancel_merge_when_build_succeeds(merge_request, @project, @current_user)

    success
  else
    error("Can't cancel the automatic merge", 406)
  end
end

#execute(merge_request) ⇒ Object

Marks the passed `merge_request` to be merged when the build succeeds or updates the params for the automatic merge


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/services/merge_requests/merge_when_build_succeeds_service.rb', line 5

def execute(merge_request)
  merge_request.merge_params.merge!(params)

  # The service is also called when the merge params are updated.
  already_approved = merge_request.merge_when_build_succeeds?

  unless already_approved
    merge_request.merge_when_build_succeeds = true
    merge_request.merge_user                = @current_user

    SystemNoteService.merge_when_build_succeeds(merge_request, @project, @current_user, merge_request.last_commit)
  end

  merge_request.save
end

#trigger(commit_status) ⇒ Object

Triggers the automatic merge of merge_request once the build succeeds


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/services/merge_requests/merge_when_build_succeeds_service.rb', line 22

def trigger(commit_status)
  merge_requests = merge_request_from(commit_status)

  merge_requests.each do |merge_request|
    next unless merge_request.merge_when_build_succeeds?
    next unless merge_request.mergeable?

    ci_commit = merge_request.ci_commit
    next unless ci_commit
    next unless ci_commit.sha == commit_status.sha
    next unless ci_commit.success?

    MergeWorker.perform_async(merge_request.id, merge_request.merge_user_id, merge_request.merge_params)
  end
end