debian-mirror-gitlab/app/services/merge_requests/base_service.rb

65 lines
2.2 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
module MergeRequests
2015-04-26 12:48:37 +05:30
class BaseService < ::IssuableBaseService
2017-09-10 17:25:29 +05:30
def create_note(merge_request, state = merge_request.state)
SystemNoteService.change_status(merge_request, merge_request.target_project, current_user, state, nil)
2014-09-02 18:07:02 +05:30
end
2018-03-17 18:26:18 +05:30
def hook_data(merge_request, action, old_rev: nil, old_associations: {})
hook_data = merge_request.to_hook_data(current_user, old_associations: old_associations)
2015-04-26 12:48:37 +05:30
hook_data[:object_attributes][:action] = action
2018-03-17 18:26:18 +05:30
if old_rev && !Gitlab::Git.blank_ref?(old_rev)
hook_data[:object_attributes][:oldrev] = old_rev
2016-09-13 17:45:13 +05:30
end
2018-03-17 18:26:18 +05:30
2015-04-26 12:48:37 +05:30
hook_data
2014-09-02 18:07:02 +05:30
end
2018-03-17 18:26:18 +05:30
def execute_hooks(merge_request, action = 'open', old_rev: nil, old_associations: {})
2015-04-26 12:48:37 +05:30
if merge_request.project
2018-03-17 18:26:18 +05:30
merge_data = hook_data(merge_request, action, old_rev: old_rev, old_associations: old_associations)
2015-04-26 12:48:37 +05:30
merge_request.project.execute_hooks(merge_data, :merge_request_hooks)
merge_request.project.execute_services(merge_data, :merge_request_hooks)
end
2014-09-02 18:07:02 +05:30
end
2015-09-11 14:41:01 +05:30
private
2018-03-17 18:26:18 +05:30
def merge_request_metrics_service(merge_request)
MergeRequestMetricsService.new(merge_request.metrics)
end
2017-08-17 22:00:37 +05:30
def create_assignee_note(merge_request)
SystemNoteService.change_assignee(
merge_request, merge_request.project, current_user, merge_request.assignee)
2015-09-11 14:41:01 +05:30
end
2016-06-02 11:05:42 +05:30
2017-08-17 22:00:37 +05:30
# Returns all origin and fork merge requests from `@project` satisfying passed arguments.
2017-09-10 17:25:29 +05:30
def merge_requests_for(source_branch, mr_states: [:opened])
2017-08-17 22:00:37 +05:30
MergeRequest
.with_state(mr_states)
.where(source_branch: source_branch, source_project_id: @project.id)
.preload(:source_project) # we don't need a #includes since we're just preloading for the #select
.select(&:source_project)
2016-11-03 12:29:30 +05:30
end
2016-06-02 11:05:42 +05:30
2016-11-03 12:29:30 +05:30
def pipeline_merge_requests(pipeline)
merge_requests_for(pipeline.ref).each do |merge_request|
2017-08-17 22:00:37 +05:30
next unless pipeline == merge_request.head_pipeline
2016-06-02 11:05:42 +05:30
2016-11-03 12:29:30 +05:30
yield merge_request
end
2016-06-02 11:05:42 +05:30
end
2016-11-03 12:29:30 +05:30
def commit_status_merge_requests(commit_status)
merge_requests_for(commit_status.ref).each do |merge_request|
2017-08-17 22:00:37 +05:30
pipeline = merge_request.head_pipeline
2016-06-02 11:05:42 +05:30
next unless pipeline
next unless pipeline.sha == commit_status.sha
2016-06-02 11:05:42 +05:30
2016-11-03 12:29:30 +05:30
yield merge_request
2016-06-02 11:05:42 +05:30
end
end
2014-09-02 18:07:02 +05:30
end
end