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
|
2020-06-29 11:08:56 -04:00
|
|
|
extend ::Gitlab::Utils::Override
|
2019-09-25 12:25:40 -04:00
|
|
|
include MergeRequests::AssignsMergeParams
|
|
|
|
|
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: {})
|
2019-08-22 02:14:51 -04:00
|
|
|
merge_data = hook_data(merge_request, action, old_rev: old_rev, old_associations: old_associations)
|
|
|
|
merge_request.project.execute_hooks(merge_data, :merge_request_hooks)
|
|
|
|
merge_request.project.execute_services(merge_data, :merge_request_hooks)
|
2020-09-01 08:11:01 -04:00
|
|
|
|
|
|
|
enqueue_jira_connect_messages_for(merge_request)
|
2014-04-02 14:51:17 -04:00
|
|
|
end
|
2015-07-24 15:46:40 -04:00
|
|
|
|
2019-04-24 07:37:29 -04:00
|
|
|
def cleanup_environments(merge_request)
|
|
|
|
Ci::StopEnvironmentsService.new(merge_request.source_project, current_user)
|
|
|
|
.execute_for_merge_request(merge_request)
|
|
|
|
end
|
|
|
|
|
2020-06-26 08:08:51 -04:00
|
|
|
def cancel_review_app_jobs!(merge_request)
|
|
|
|
environments = merge_request.environments.in_review_folder.available
|
|
|
|
environments.each { |environment| environment.cancel_deployment_jobs! }
|
|
|
|
end
|
|
|
|
|
2019-11-15 13:06:24 -05:00
|
|
|
def source_project
|
|
|
|
@source_project ||= merge_request.source_project
|
|
|
|
end
|
|
|
|
|
|
|
|
def target_project
|
|
|
|
@target_project ||= merge_request.target_project
|
|
|
|
end
|
|
|
|
|
|
|
|
# Don't try to print expensive instance variables.
|
|
|
|
def inspect
|
2020-05-15 08:08:28 -04:00
|
|
|
return "#<#{self.class}>" unless respond_to?(:merge_request)
|
|
|
|
|
2019-11-15 13:06:24 -05:00
|
|
|
"#<#{self.class} #{merge_request.to_reference(full: true)}>"
|
|
|
|
end
|
|
|
|
|
2015-07-24 15:46:40 -04:00
|
|
|
private
|
|
|
|
|
2020-09-01 08:11:01 -04:00
|
|
|
def enqueue_jira_connect_messages_for(merge_request)
|
|
|
|
return unless project.jira_subscription_exists?
|
|
|
|
|
|
|
|
if Atlassian::JiraIssueKeyExtractor.has_keys?(merge_request.title, merge_request.description)
|
|
|
|
JiraConnect::SyncMergeRequestWorker.perform_async(merge_request.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-25 12:25:40 -04:00
|
|
|
def create(merge_request)
|
|
|
|
self.params = assign_allowed_merge_params(merge_request, params)
|
|
|
|
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def update(merge_request)
|
|
|
|
self.params = assign_allowed_merge_params(merge_request, params)
|
|
|
|
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2020-06-29 11:08:56 -04:00
|
|
|
override :handle_quick_actions
|
|
|
|
def handle_quick_actions(merge_request)
|
|
|
|
super
|
|
|
|
handle_wip_event(merge_request)
|
|
|
|
end
|
|
|
|
|
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
|
2020-09-07 08:08:27 -04:00
|
|
|
|
|
|
|
filter_reviewer(merge_request)
|
|
|
|
end
|
|
|
|
|
|
|
|
def filter_reviewer(merge_request)
|
|
|
|
return if params[:reviewer_ids].blank?
|
|
|
|
|
|
|
|
unless can_admin_issuable?(merge_request) && merge_request.allows_reviewers?
|
|
|
|
params.delete(:reviewer_ids)
|
|
|
|
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2020-10-01 14:10:20 -04:00
|
|
|
unless merge_request.allows_multiple_reviewers?
|
|
|
|
params[:reviewer_ids] = params[:reviewer_ids].first(1)
|
|
|
|
end
|
|
|
|
|
2020-09-07 08:08:27 -04:00
|
|
|
reviewer_ids = params[:reviewer_ids].select { |reviewer_id| user_can_read?(merge_request, reviewer_id) }
|
|
|
|
|
|
|
|
if params[:reviewer_ids].map(&:to_s) == [IssuableFinder::Params::NONE]
|
|
|
|
params[:reviewer_ids] = []
|
|
|
|
elsif reviewer_ids.any?
|
|
|
|
params[:reviewer_ids] = reviewer_ids
|
|
|
|
else
|
|
|
|
params.delete(:reviewer_ids)
|
|
|
|
end
|
2018-02-23 12:30:37 -05:00
|
|
|
end
|
|
|
|
|
2017-12-07 12:41:30 -05:00
|
|
|
def merge_request_metrics_service(merge_request)
|
|
|
|
MergeRequestMetricsService.new(merge_request.metrics)
|
|
|
|
end
|
|
|
|
|
2019-04-07 14:35:16 -04:00
|
|
|
def create_assignee_note(merge_request, old_assignees)
|
|
|
|
SystemNoteService.change_issuable_assignees(
|
|
|
|
merge_request, merge_request.project, current_user, old_assignees)
|
2017-05-04 08:11:15 -04:00
|
|
|
end
|
|
|
|
|
2020-09-24 05:09:35 -04:00
|
|
|
def create_reviewer_note(merge_request, old_reviewers)
|
|
|
|
SystemNoteService.change_issuable_reviewers(
|
|
|
|
merge_request, merge_request.project, current_user, old_reviewers)
|
|
|
|
end
|
|
|
|
|
2019-03-21 09:08:32 -04:00
|
|
|
def create_pipeline_for(merge_request, user)
|
2019-05-27 08:26:39 -04:00
|
|
|
MergeRequests::CreatePipelineService.new(project, user).execute(merge_request)
|
2019-03-28 04:55:35 -04:00
|
|
|
end
|
|
|
|
|
2019-06-26 08:24:09 -04:00
|
|
|
def abort_auto_merge(merge_request, reason)
|
|
|
|
AutoMergeService.new(project, current_user).abort(merge_request, reason)
|
2019-06-10 07:10:02 -04:00
|
|
|
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)
|
2019-04-03 05:44:51 -04:00
|
|
|
pipeline.all_merge_requests.opened.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
|
2020-05-07 11:09:29 -04:00
|
|
|
|
|
|
|
def log_error(exception:, message:, save_message_on_model: false)
|
|
|
|
reference = merge_request.to_reference(full: true)
|
|
|
|
data = {
|
|
|
|
class: self.class.name,
|
|
|
|
message: message,
|
|
|
|
merge_request_id: merge_request.id,
|
|
|
|
merge_request: reference,
|
|
|
|
save_message_on_model: save_message_on_model
|
|
|
|
}
|
|
|
|
|
|
|
|
if exception
|
|
|
|
Gitlab::ErrorTracking.with_context(current_user) do
|
|
|
|
Gitlab::ErrorTracking.track_exception(exception, data)
|
|
|
|
end
|
|
|
|
|
|
|
|
data[:"exception.message"] = exception.message
|
|
|
|
end
|
|
|
|
|
|
|
|
# TODO: Deprecate Gitlab::GitLogger since ErrorTracking should suffice:
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab/-/issues/216379
|
|
|
|
data[:message] = "#{self.class.name} error (#{reference}): #{message}"
|
|
|
|
Gitlab::GitLogger.error(data)
|
|
|
|
|
|
|
|
merge_request.update(merge_error: message) if save_message_on_model
|
|
|
|
end
|
2014-04-02 14:51:17 -04:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
|
|
MergeRequests::BaseService.prepend_if_ee('EE::MergeRequests::BaseService')
|