2018-07-17 12:50:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-04-02 14:51:17 -04:00
|
|
|
module MergeRequests
|
2014-10-07 10:09:28 -04:00
|
|
|
class BaseService < ::IssuableBaseService
|
2017-07-19 10:03:50 -04:00
|
|
|
def create_note(merge_request, state = merge_request.state)
|
|
|
|
SystemNoteService.change_status(merge_request, merge_request.target_project, current_user, state, nil)
|
2014-04-02 14:51:17 -04:00
|
|
|
end
|
|
|
|
|
2017-11-21 12:13:07 -05:00
|
|
|
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-02-19 00:02:57 -05:00
|
|
|
hook_data[:object_attributes][:action] = action
|
2017-09-15 13:08:27 -04:00
|
|
|
if old_rev && !Gitlab::Git.blank_ref?(old_rev)
|
|
|
|
hook_data[:object_attributes][:oldrev] = old_rev
|
2016-07-27 15:39:45 -04:00
|
|
|
end
|
2017-09-15 13:08:27 -04:00
|
|
|
|
2015-02-19 00:02:57 -05:00
|
|
|
hook_data
|
|
|
|
end
|
|
|
|
|
2017-11-21 12:13:07 -05:00
|
|
|
def execute_hooks(merge_request, action = 'open', old_rev: nil, old_associations: {})
|
2014-04-02 14:51:17 -04:00
|
|
|
if merge_request.project
|
2017-11-21 12:13:07 -05:00
|
|
|
merge_data = hook_data(merge_request, action, old_rev: old_rev, old_associations: old_associations)
|
2015-02-19 00:02:57 -05:00
|
|
|
merge_request.project.execute_hooks(merge_data, :merge_request_hooks)
|
|
|
|
merge_request.project.execute_services(merge_data, :merge_request_hooks)
|
2014-04-02 14:51:17 -04:00
|
|
|
end
|
|
|
|
end
|
2015-07-24 15:46:40 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-03-01 09:32:39 -05:00
|
|
|
def handle_wip_event(merge_request)
|
|
|
|
if wip_event = params.delete(:wip_event)
|
|
|
|
# We update the title that is provided in the params or we use the mr title
|
|
|
|
title = params[:title] || merge_request.title
|
|
|
|
params[:title] = case wip_event
|
|
|
|
when 'wip' then MergeRequest.wip_title(title)
|
|
|
|
when 'unwip' then MergeRequest.wipless_title(title)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-23 12:30:37 -05:00
|
|
|
def filter_params(merge_request)
|
|
|
|
super
|
|
|
|
|
2018-05-22 21:54:57 -04:00
|
|
|
unless merge_request.can_allow_collaboration?(current_user)
|
|
|
|
params.delete(:allow_collaboration)
|
2018-02-23 12:30:37 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-07 12:41:30 -05:00
|
|
|
def merge_request_metrics_service(merge_request)
|
|
|
|
MergeRequestMetricsService.new(merge_request.metrics)
|
|
|
|
end
|
|
|
|
|
2017-05-04 08:11:15 -04:00
|
|
|
def create_assignee_note(merge_request)
|
|
|
|
SystemNoteService.change_assignee(
|
|
|
|
merge_request, merge_request.project, current_user, merge_request.assignee)
|
|
|
|
end
|
|
|
|
|
2018-12-05 01:57:00 -05:00
|
|
|
def create_merge_request_pipeline(merge_request, user)
|
|
|
|
return unless Feature.enabled?(:ci_merge_request_pipeline,
|
|
|
|
merge_request.source_project,
|
|
|
|
default_enabled: true)
|
|
|
|
|
|
|
|
##
|
|
|
|
# UpdateMergeRequestsWorker could be retried by an exception.
|
|
|
|
# MR pipelines should not be recreated in such case.
|
|
|
|
return if merge_request.merge_request_pipeline_exists?
|
|
|
|
|
|
|
|
Ci::CreatePipelineService
|
|
|
|
.new(merge_request.source_project, user, ref: merge_request.source_branch)
|
|
|
|
.execute(:merge_request,
|
|
|
|
ignore_skip_ci: true,
|
|
|
|
save_on_errors: false,
|
|
|
|
merge_request: merge_request)
|
|
|
|
end
|
|
|
|
|
2017-01-13 08:35:33 -05:00
|
|
|
# Returns all origin and fork merge requests from `@project` satisfying passed arguments.
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-07-19 10:03:50 -04:00
|
|
|
def merge_requests_for(source_branch, mr_states: [:opened])
|
2018-09-28 05:00:30 -04:00
|
|
|
@project.source_of_merge_requests
|
2017-01-13 08:35:33 -05:00
|
|
|
.with_state(mr_states)
|
2018-09-28 05:00:30 -04:00
|
|
|
.where(source_branch: source_branch)
|
|
|
|
.preload(:source_project) # we don't need #includes since we're just preloading for the #select
|
2017-01-13 08:35:33 -05:00
|
|
|
.select(&:source_project)
|
2016-03-08 13:22:50 -05:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-03-08 13:22:50 -05:00
|
|
|
|
2016-10-04 09:59:11 -04:00
|
|
|
def pipeline_merge_requests(pipeline)
|
2016-10-06 06:31:39 -04:00
|
|
|
merge_requests_for(pipeline.ref).each do |merge_request|
|
2016-11-28 05:13:32 -05:00
|
|
|
next unless pipeline == merge_request.head_pipeline
|
2016-10-06 06:19:50 -04:00
|
|
|
|
2016-10-04 09:59:11 -04:00
|
|
|
yield merge_request
|
|
|
|
end
|
|
|
|
end
|
2016-03-08 13:22:50 -05:00
|
|
|
|
2016-10-04 09:59:11 -04:00
|
|
|
def commit_status_merge_requests(commit_status)
|
2016-10-06 06:31:39 -04:00
|
|
|
merge_requests_for(commit_status.ref).each do |merge_request|
|
2016-11-28 05:13:32 -05:00
|
|
|
pipeline = merge_request.head_pipeline
|
2016-10-06 06:31:39 -04:00
|
|
|
|
2016-10-06 06:19:50 -04:00
|
|
|
next unless pipeline
|
|
|
|
next unless pipeline.sha == commit_status.sha
|
|
|
|
|
2016-10-04 09:59:11 -04:00
|
|
|
yield merge_request
|
2016-03-08 13:22:50 -05:00
|
|
|
end
|
|
|
|
end
|
2014-04-02 14:51:17 -04:00
|
|
|
end
|
|
|
|
end
|