2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
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-27 19:54:05 +05:30
|
|
|
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
|
|
|
|
|
|
|
|
def filter_params(merge_request)
|
|
|
|
super
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
unless merge_request.can_allow_collaboration?(current_user)
|
|
|
|
params.delete(:allow_collaboration)
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
2019-05-30 16:15:17 +05:30
|
|
|
def create_merge_request_pipeline(merge_request, user)
|
|
|
|
return unless Feature.enabled?(:ci_merge_request_pipeline,
|
|
|
|
merge_request.source_project,
|
|
|
|
default_enabled: true)
|
2019-02-15 15:39:39 +05:30
|
|
|
|
|
|
|
##
|
|
|
|
# UpdateMergeRequestsWorker could be retried by an exception.
|
2019-05-30 16:15:17 +05:30
|
|
|
# MR pipelines should not be recreated in such case.
|
|
|
|
return if merge_request.merge_request_pipeline_exists?
|
|
|
|
return if merge_request.has_no_commits?
|
|
|
|
|
|
|
|
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)
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
# Returns all origin and fork merge requests from `@project` satisfying passed arguments.
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-09-10 17:25:29 +05:30
|
|
|
def merge_requests_for(source_branch, mr_states: [:opened])
|
2018-12-05 23:21:45 +05:30
|
|
|
@project.source_of_merge_requests
|
2017-08-17 22:00:37 +05:30
|
|
|
.with_state(mr_states)
|
2018-12-05 23:21:45 +05:30
|
|
|
.where(source_branch: source_branch)
|
|
|
|
.preload(:source_project) # we don't need #includes since we're just preloading for the #select
|
2017-08-17 22:00:37 +05:30
|
|
|
.select(&:source_project)
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
def pipeline_merge_requests(pipeline)
|
2019-05-30 16:15:17 +05:30
|
|
|
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
|
2019-05-30 16:15:17 +05:30
|
|
|
|
|
|
|
def commit_status_merge_requests(commit_status)
|
|
|
|
merge_requests_for(commit_status.ref).each do |merge_request|
|
|
|
|
pipeline = merge_request.head_pipeline
|
|
|
|
|
|
|
|
next unless pipeline
|
|
|
|
next unless pipeline.sha == commit_status.sha
|
|
|
|
|
|
|
|
yield merge_request
|
|
|
|
end
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|