2018-07-16 12:31:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-06 08:49:49 -04:00
|
|
|
module Issuable
|
2019-07-09 07:46:16 -04:00
|
|
|
class BulkUpdateService
|
|
|
|
include Gitlab::Allowable
|
|
|
|
|
2019-12-02 04:06:58 -05:00
|
|
|
attr_accessor :parent, :current_user, :params
|
2019-07-09 07:46:16 -04:00
|
|
|
|
2019-12-02 04:06:58 -05:00
|
|
|
def initialize(parent, user = nil, params = {})
|
|
|
|
@parent, @current_user, @params = parent, user, params.dup
|
2019-07-09 07:46:16 -04:00
|
|
|
end
|
|
|
|
|
2016-09-06 08:49:49 -04:00
|
|
|
def execute(type)
|
|
|
|
ids = params.delete(:issuable_ids).split(",")
|
2020-07-06 05:09:20 -04:00
|
|
|
set_update_params(type)
|
|
|
|
items = update_issuables(type, ids)
|
2016-09-06 08:49:49 -04:00
|
|
|
|
2020-07-06 05:09:20 -04:00
|
|
|
response_success(payload: { count: items.count })
|
|
|
|
rescue ArgumentError => e
|
|
|
|
response_error(e.message, 422)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def set_update_params(type)
|
2020-06-05 11:08:23 -04:00
|
|
|
params.slice!(*permitted_attrs(type))
|
|
|
|
params.delete_if { |k, v| v.blank? }
|
2016-09-06 08:49:49 -04:00
|
|
|
|
2020-03-30 17:08:01 -04:00
|
|
|
if params[:assignee_ids] == [IssuableFinder::Params::NONE.to_s]
|
2017-05-04 08:11:15 -04:00
|
|
|
params[:assignee_ids] = []
|
|
|
|
end
|
2016-09-06 08:49:49 -04:00
|
|
|
end
|
2017-05-08 10:58:42 -04:00
|
|
|
|
|
|
|
def permitted_attrs(type)
|
2020-06-01 14:08:07 -04:00
|
|
|
attrs = %i(state_event milestone_id add_label_ids remove_label_ids subscription_event)
|
2017-05-08 10:58:42 -04:00
|
|
|
|
2020-06-01 14:08:07 -04:00
|
|
|
if type == 'issue' || type == 'merge_request'
|
2017-05-08 10:58:42 -04:00
|
|
|
attrs.push(:assignee_ids)
|
|
|
|
else
|
|
|
|
attrs.push(:assignee_id)
|
|
|
|
end
|
|
|
|
end
|
2019-12-02 04:06:58 -05:00
|
|
|
|
2020-07-06 05:09:20 -04:00
|
|
|
def update_issuables(type, ids)
|
|
|
|
model_class = type.classify.constantize
|
|
|
|
update_class = type.classify.pluralize.constantize::UpdateService
|
|
|
|
items = find_issuables(parent, model_class, ids)
|
|
|
|
|
|
|
|
items.each do |issuable|
|
|
|
|
next unless can?(current_user, :"update_#{type}", issuable)
|
|
|
|
|
|
|
|
update_class.new(issuable.issuing_parent, current_user, params).execute(issuable)
|
|
|
|
end
|
|
|
|
|
|
|
|
items
|
|
|
|
end
|
|
|
|
|
2019-12-02 04:06:58 -05:00
|
|
|
def find_issuables(parent, model_class, ids)
|
|
|
|
if parent.is_a?(Project)
|
|
|
|
model_class.id_in(ids).of_projects(parent)
|
|
|
|
elsif parent.is_a?(Group)
|
|
|
|
model_class.id_in(ids).of_projects(parent.all_projects)
|
|
|
|
end
|
|
|
|
end
|
2020-07-06 05:09:20 -04:00
|
|
|
|
|
|
|
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
|
2016-09-06 08:49:49 -04:00
|
|
|
end
|
|
|
|
end
|
2019-12-02 04:06:58 -05:00
|
|
|
|
|
|
|
Issuable::BulkUpdateService.prepend_if_ee('EE::Issuable::BulkUpdateService')
|