debian-mirror-gitlab/app/services/issuable/bulk_update_service.rb

125 lines
3.4 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2016-09-29 09:46:39 +05:30
module Issuable
2019-09-30 21:07:59 +05:30
class BulkUpdateService
include Gitlab::Allowable
2020-01-01 13:55:28 +05:30
attr_accessor :parent, :current_user, :params
2019-09-30 21:07:59 +05:30
2020-01-01 13:55:28 +05:30
def initialize(parent, user = nil, params = {})
2021-04-29 21:17:54 +05:30
@parent = parent
@current_user = user
@params = params.dup
2019-09-30 21:07:59 +05:30
end
2016-09-29 09:46:39 +05:30
def execute(type)
2023-04-23 21:23:45 +05:30
model_ids = ids_from_params(params.delete(:issuable_ids))
2020-07-28 23:09:34 +05:30
set_update_params(type)
2023-04-23 21:23:45 +05:30
updated_issuables = update_issuables(type, model_ids)
2016-09-29 09:46:39 +05:30
2021-06-08 01:23:25 +05:30
if updated_issuables.present? && requires_count_cache_reset?(type)
schedule_group_issues_count_reset(updated_issuables)
end
response_success(payload: { count: updated_issuables.size })
2020-07-28 23:09:34 +05:30
rescue ArgumentError => e
response_error(e.message, 422)
end
private
2023-04-23 21:23:45 +05:30
def ids_from_params(issuable_ids)
return issuable_ids if issuable_ids.is_a?(Array)
issuable_ids.split(',')
end
2020-07-28 23:09:34 +05:30
def set_update_params(type)
2020-06-23 00:09:42 +05:30
params.slice!(*permitted_attrs(type))
2016-09-29 09:46:39 +05:30
2020-04-22 19:07:51 +05:30
if params[:assignee_ids] == [IssuableFinder::Params::NONE.to_s]
2017-08-17 22:00:37 +05:30
params[:assignee_ids] = []
end
2016-09-29 09:46:39 +05:30
end
2017-08-17 22:00:37 +05:30
def permitted_attrs(type)
2020-06-23 00:09:42 +05:30
attrs = %i(state_event milestone_id add_label_ids remove_label_ids subscription_event)
if type == 'issue' || type == 'merge_request'
2017-08-17 22:00:37 +05:30
attrs.push(:assignee_ids)
else
attrs.push(:assignee_id)
end
end
2020-01-01 13:55:28 +05:30
2020-07-28 23:09:34 +05:30
def update_issuables(type, ids)
model_class = type.classify.constantize
update_class = type.classify.pluralize.constantize::UpdateService
items = find_issuables(parent, model_class, ids)
2023-04-23 21:23:45 +05:30
authorized_issuables = []
2020-07-28 23:09:34 +05:30
items.each do |issuable|
next unless can?(current_user, :"update_#{type}", issuable)
2023-04-23 21:23:45 +05:30
authorized_issuables << issuable
2021-11-11 11:23:49 +05:30
update_class.new(
**update_class.constructor_container_arg(issuable.issuing_parent),
current_user: current_user,
params: dup_params
).execute(issuable)
2020-07-28 23:09:34 +05:30
end
2023-04-23 21:23:45 +05:30
authorized_issuables
2020-07-28 23:09:34 +05:30
end
2020-01-01 13:55:28 +05:30
def find_issuables(parent, model_class, ids)
2023-04-23 21:23:45 +05:30
issuables = model_class.id_in(ids)
2023-01-13 00:05:48 +05:30
case parent
when Project
2023-04-23 21:23:45 +05:30
issuables = issuables.of_projects(parent)
2023-01-13 00:05:48 +05:30
when Group
2023-04-23 21:23:45 +05:30
issuables = issuables.of_projects(parent.all_projects)
2021-04-29 21:17:54 +05:30
else
2023-04-23 21:23:45 +05:30
raise ArgumentError, _('A parent must be provided when bulk updating issuables')
2020-01-01 13:55:28 +05:30
end
2021-04-29 21:17:54 +05:30
2023-04-23 21:23:45 +05:30
issuables.includes_for_bulk_update
2020-01-01 13:55:28 +05:30
end
2020-07-28 23:09:34 +05:30
2021-11-11 11:23:49 +05:30
# Duplicates params and its top-level values
# We cannot use deep_dup because ActiveRecord objects will result
# to new records with no id assigned
def dup_params
dup = HashWithIndifferentAccess.new
params.each do |key, value|
dup[key] = value.is_a?(ActiveRecord::Base) ? value : value.dup
end
dup
end
2020-07-28 23:09:34 +05:30
def response_success(message: nil, payload: nil)
ServiceResponse.success(message: message, payload: payload)
end
def response_error(message, http_status)
ServiceResponse.error(message: message, http_status: http_status)
end
2021-06-08 01:23:25 +05:30
def requires_count_cache_reset?(type)
type.to_sym == :issue && params.include?(:state_event)
end
def schedule_group_issues_count_reset(updated_issuables)
group_ids = updated_issuables.map(&:project).map(&:namespace_id)
return if group_ids.empty?
Issuables::ClearGroupsIssueCounterWorker.perform_async(group_ids)
end
2016-09-29 09:46:39 +05:30
end
end
2020-01-01 13:55:28 +05:30
2021-06-08 01:23:25 +05:30
Issuable::BulkUpdateService.prepend_mod_with('Issuable::BulkUpdateService')