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
|
2020-07-28 23:09:34 +05:30
|
|
|
extend ::Gitlab::Utils::Override
|
2019-10-31 01:37:42 +05:30
|
|
|
include MergeRequests::AssignsMergeParams
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
delegate :repository, to: :project
|
|
|
|
|
|
|
|
def initialize(project:, current_user: nil, params: {})
|
|
|
|
super(container: project, current_user: current_user, params: params)
|
|
|
|
end
|
|
|
|
|
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: {})
|
2023-01-13 00:05:48 +05:30
|
|
|
merge_data = Gitlab::Lazy.new { hook_data(merge_request, action, old_rev: old_rev, old_associations: old_associations) }
|
2019-12-04 20:38:33 +05:30
|
|
|
merge_request.project.execute_hooks(merge_data, :merge_request_hooks)
|
2021-09-30 23:02:18 +05:30
|
|
|
merge_request.project.execute_integrations(merge_data, :merge_request_hooks)
|
2020-11-24 15:15:51 +05:30
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
execute_external_hooks(merge_request, merge_data)
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
enqueue_jira_connect_messages_for(merge_request)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2015-09-11 14:41:01 +05:30
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
def execute_external_hooks(merge_request, merge_data)
|
|
|
|
# Implemented in EE
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_changes(merge_request, options)
|
|
|
|
old_associations = options.fetch(:old_associations, {})
|
|
|
|
old_assignees = old_associations.fetch(:assignees, [])
|
|
|
|
old_reviewers = old_associations.fetch(:reviewers, [])
|
|
|
|
|
|
|
|
handle_assignees_change(merge_request, old_assignees) if merge_request.assignees != old_assignees
|
|
|
|
handle_reviewers_change(merge_request, old_reviewers) if merge_request.reviewers != old_reviewers
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_assignees_change(merge_request, old_assignees)
|
|
|
|
MergeRequests::HandleAssigneesChangeService
|
|
|
|
.new(project: project, current_user: current_user)
|
|
|
|
.async_execute(merge_request, old_assignees)
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_reviewers_change(merge_request, old_reviewers)
|
|
|
|
affected_reviewers = (old_reviewers + merge_request.reviewers) - (old_reviewers & merge_request.reviewers)
|
|
|
|
create_reviewer_note(merge_request, old_reviewers)
|
|
|
|
notification_service.async.changed_reviewer_of_merge_request(merge_request, current_user, old_reviewers)
|
|
|
|
todo_service.reassigned_reviewable(merge_request, current_user, old_reviewers)
|
|
|
|
invalidate_cache_counts(merge_request, users: affected_reviewers.compact)
|
|
|
|
|
|
|
|
new_reviewers = merge_request.reviewers - old_reviewers
|
|
|
|
merge_request_activity_counter.track_users_review_requested(users: new_reviewers)
|
|
|
|
merge_request_activity_counter.track_reviewers_changed_action(user: current_user)
|
2022-11-25 23:54:43 +05:30
|
|
|
trigger_merge_request_reviewers_updated(merge_request)
|
2023-03-04 22:38:38 +05:30
|
|
|
|
|
|
|
capture_suggested_reviewers_accepted(merge_request)
|
2021-09-04 01:27:46 +05:30
|
|
|
end
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
def cleanup_environments(merge_request)
|
2021-10-27 15:23:28 +05:30
|
|
|
Environments::StopService.new(merge_request.source_project, current_user)
|
2022-07-16 23:28:13 +05:30
|
|
|
.execute_for_merge_request_pipeline(merge_request)
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
def cancel_review_app_jobs!(merge_request)
|
2022-06-21 17:19:12 +05:30
|
|
|
environments = merge_request.environments_in_head_pipeline.in_review_folder.available
|
2020-07-28 23:09:34 +05:30
|
|
|
environments.each { |environment| environment.cancel_deployment_jobs! }
|
|
|
|
end
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
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-24 23:13:21 +05:30
|
|
|
return "#<#{self.class}>" unless respond_to?(:merge_request)
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
"#<#{self.class} #{merge_request.to_reference(full: true)}>"
|
|
|
|
end
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
def merge_request_activity_counter
|
|
|
|
Gitlab::UsageDataCounters::MergeRequestActivityUniqueCounter
|
|
|
|
end
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
private
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
def self.constructor_container_arg(value)
|
|
|
|
{ project: value }
|
|
|
|
end
|
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
def refresh_pipelines_on_merge_requests(merge_request, allow_duplicate: false)
|
|
|
|
create_pipeline_for(merge_request, current_user, async: true, allow_duplicate: allow_duplicate)
|
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
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)
|
2021-02-22 17:27:13 +05:30
|
|
|
JiraConnect::SyncMergeRequestWorker.perform_async(merge_request.id, Atlassian::JiraConnect::Client.generate_update_sequence_id)
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-31 01:37:42 +05:30
|
|
|
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-07-28 23:09:34 +05:30
|
|
|
override :handle_quick_actions
|
|
|
|
def handle_quick_actions(merge_request)
|
|
|
|
super
|
2022-07-23 23:45:48 +05:30
|
|
|
handle_draft_event(merge_request)
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
def handle_draft_event(merge_request)
|
|
|
|
if draft_event = params.delete(:wip_event)
|
2018-03-27 19:54:05 +05:30
|
|
|
# We update the title that is provided in the params or we use the mr title
|
|
|
|
title = params[:title] || merge_request.title
|
2022-07-23 23:45:48 +05:30
|
|
|
params[:title] = case draft_event
|
|
|
|
when 'draft' then MergeRequest.draft_title(title)
|
|
|
|
when 'ready' then MergeRequest.draftless_title(title)
|
2018-03-27 19:54:05 +05:30
|
|
|
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
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
filter_reviewer(merge_request)
|
2023-03-04 22:38:38 +05:30
|
|
|
filter_suggested_reviewers
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def filter_reviewer(merge_request)
|
|
|
|
return if params[:reviewer_ids].blank?
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
unless can_admin_issuable?(merge_request)
|
2020-11-24 15:15:51 +05:30
|
|
|
params.delete(:reviewer_ids)
|
|
|
|
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
unless merge_request.allows_multiple_reviewers?
|
|
|
|
params[:reviewer_ids] = params[:reviewer_ids].first(1)
|
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
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-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
def filter_suggested_reviewers
|
|
|
|
# Implemented in EE
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def merge_request_metrics_service(merge_request)
|
|
|
|
MergeRequestMetricsService.new(merge_request.metrics)
|
|
|
|
end
|
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
def create_assignee_note(merge_request, old_assignees)
|
|
|
|
SystemNoteService.change_issuable_assignees(
|
|
|
|
merge_request, merge_request.project, current_user, old_assignees)
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def create_reviewer_note(merge_request, old_reviewers)
|
|
|
|
SystemNoteService.change_issuable_reviewers(
|
|
|
|
merge_request, merge_request.project, current_user, old_reviewers)
|
|
|
|
end
|
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
def create_pipeline_for(merge_request, user, async: false, allow_duplicate: false)
|
|
|
|
create_pipeline_params = params.slice(:push_options).merge(allow_duplicate: allow_duplicate)
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
if async
|
2022-08-13 15:12:31 +05:30
|
|
|
MergeRequests::CreatePipelineWorker.perform_async(
|
|
|
|
project.id,
|
|
|
|
user.id,
|
|
|
|
merge_request.id,
|
2023-03-17 16:20:25 +05:30
|
|
|
create_pipeline_params.deep_stringify_keys)
|
2021-04-29 21:17:54 +05:30
|
|
|
else
|
2022-07-23 23:45:48 +05:30
|
|
|
MergeRequests::CreatePipelineService
|
2023-03-17 16:20:25 +05:30
|
|
|
.new(project: project, current_user: user, params: create_pipeline_params)
|
2022-07-23 23:45:48 +05:30
|
|
|
.execute(merge_request)
|
2021-04-29 21:17:54 +05:30
|
|
|
end
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
def abort_auto_merge(merge_request, reason)
|
|
|
|
AutoMergeService.new(project, current_user).abort(merge_request, reason)
|
2019-09-04 21:01:54 +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-07-07 11:18:12 +05:30
|
|
|
pipeline.all_merge_requests.opened.each do |merge_request|
|
2021-04-29 21:17:54 +05:30
|
|
|
next unless pipeline.id == merge_request.head_pipeline_id
|
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
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
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
|
2021-04-17 20:07:23 +05:30
|
|
|
Gitlab::ApplicationContext.with_context(user: current_user) do
|
2020-05-24 23:13:21 +05:30
|
|
|
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
|
2021-04-29 21:17:54 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
def trigger_merge_request_reviewers_updated(merge_request)
|
|
|
|
GraphqlTriggers.merge_request_reviewers_updated(merge_request)
|
|
|
|
end
|
2023-01-13 00:05:48 +05:30
|
|
|
|
|
|
|
def trigger_merge_request_merge_status_updated(merge_request)
|
|
|
|
GraphqlTriggers.merge_request_merge_status_updated(merge_request)
|
|
|
|
end
|
2023-03-04 22:38:38 +05:30
|
|
|
|
|
|
|
def trigger_merge_request_approval_state_updated(merge_request)
|
|
|
|
GraphqlTriggers.merge_request_approval_state_updated(merge_request)
|
|
|
|
end
|
|
|
|
|
|
|
|
def capture_suggested_reviewers_accepted(merge_request)
|
|
|
|
# Implemented in EE
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
MergeRequests::BaseService.prepend_mod_with('MergeRequests::BaseService')
|