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
|
|
|
|
|
|
|
|
attr_accessor :current_user, :params
|
|
|
|
|
|
|
|
def initialize(user = nil, params = {})
|
|
|
|
@current_user, @params = user, params.dup
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-09-06 08:49:49 -04:00
|
|
|
def execute(type)
|
|
|
|
model_class = type.classify.constantize
|
|
|
|
update_class = type.classify.pluralize.constantize::UpdateService
|
|
|
|
|
|
|
|
ids = params.delete(:issuable_ids).split(",")
|
|
|
|
items = model_class.where(id: ids)
|
|
|
|
|
2017-05-08 10:58:42 -04:00
|
|
|
permitted_attrs(type).each do |key|
|
2016-09-06 08:49:49 -04:00
|
|
|
params.delete(key) unless params[key].present?
|
|
|
|
end
|
|
|
|
|
2017-05-04 08:11:15 -04:00
|
|
|
if params[:assignee_ids] == [IssuableFinder::NONE.to_s]
|
|
|
|
params[:assignee_ids] = []
|
|
|
|
end
|
|
|
|
|
2016-09-06 08:49:49 -04:00
|
|
|
items.each do |issuable|
|
|
|
|
next unless can?(current_user, :"update_#{type}", issuable)
|
|
|
|
|
|
|
|
update_class.new(issuable.project, current_user, params).execute(issuable)
|
|
|
|
end
|
|
|
|
|
|
|
|
{
|
|
|
|
count: items.count,
|
|
|
|
success: !items.count.zero?
|
|
|
|
}
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-05-08 10:58:42 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def permitted_attrs(type)
|
|
|
|
attrs = %i(state_event milestone_id assignee_id assignee_ids add_label_ids remove_label_ids subscription_event)
|
|
|
|
|
|
|
|
if type == 'issue'
|
|
|
|
attrs.push(:assignee_ids)
|
|
|
|
else
|
|
|
|
attrs.push(:assignee_id)
|
|
|
|
end
|
|
|
|
end
|
2016-09-06 08:49:49 -04:00
|
|
|
end
|
|
|
|
end
|