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

66 lines
1.7 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 = {})
@parent, @current_user, @params = parent, user, params.dup
2019-09-30 21:07:59 +05:30
end
2016-09-29 09:46:39 +05:30
def execute(type)
model_class = type.classify.constantize
update_class = type.classify.pluralize.constantize::UpdateService
ids = params.delete(:issuable_ids).split(",")
2020-01-01 13:55:28 +05:30
items = find_issuables(parent, model_class, ids)
2016-09-29 09:46:39 +05:30
2020-06-23 00:09:42 +05:30
params.slice!(*permitted_attrs(type))
params.delete_if { |k, v| v.blank? }
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
items.each do |issuable|
next unless can?(current_user, :"update_#{type}", issuable)
2019-10-12 21:52:04 +05:30
update_class.new(issuable.issuing_parent, current_user, params).execute(issuable)
2016-09-29 09:46:39 +05:30
end
{
count: items.count,
success: !items.count.zero?
}
end
2017-08-17 22:00:37 +05:30
private
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)
issuable_specific_attrs(type, attrs)
end
2017-08-17 22:00:37 +05:30
2020-06-23 00:09:42 +05:30
def issuable_specific_attrs(type, attrs)
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
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
2016-09-29 09:46:39 +05:30
end
end
2020-01-01 13:55:28 +05:30
Issuable::BulkUpdateService.prepend_if_ee('EE::Issuable::BulkUpdateService')