|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Mutations |
| 4 | + module WorkItems |
| 5 | + class BulkUpdate < BaseMutation |
| 6 | + graphql_name 'WorkItemBulkUpdate' |
| 7 | + |
| 8 | + include ::Gitlab::Utils::StrongMemoize |
| 9 | + |
| 10 | + MAX_WORK_ITEMS = 100 |
| 11 | + |
| 12 | + description 'Allows updating several properties for a set of issues. ' \ |
| 13 | + 'Does nothing if the `bulk_update_issues_mutation` feature flag is disabled.' |
| 14 | + |
| 15 | + argument :ids, [::Types::GlobalIDType[::WorkItem]], |
| 16 | + required: true, |
| 17 | + description: 'Global ID array of the issues that will be updated. ' \ |
| 18 | + "IDs that the user can\'t update will be ignored. A max of #{MAX_WORK_ITEMS} can be provided." |
| 19 | + argument :parent_id, ::Types::GlobalIDType[::WorkItems::Parent], |
| 20 | + required: true, |
| 21 | + description: 'Global ID of the parent to which the bulk update will be scoped. ' \ |
| 22 | + 'The parent can be a project. The parent can also be a group (Premium and Ultimate only). ' \ |
| 23 | + 'Example `WorkItemsParentID` are `"gid://gitlab/Project/1"` and `"gid://gitlab/Group/1"`.' |
| 24 | + |
| 25 | + argument :labels_widget, |
| 26 | + ::Types::WorkItems::Widgets::LabelsUpdateInputType, |
| 27 | + required: false, |
| 28 | + description: 'Input for labels widget.', |
| 29 | + prepare: ->(input, _) { input.to_h } |
| 30 | + |
| 31 | + field :updated_work_item_count, GraphQL::Types::Int, |
| 32 | + null: true, |
| 33 | + description: 'Number of work items that were successfully updated.' |
| 34 | + |
| 35 | + def ready?(**args) |
| 36 | + if Feature.disabled?(:bulk_update_work_items_mutation, parent_for!(args[:parent_id])) |
| 37 | + raise_resource_not_available_error!('`bulk_update_work_items_mutation` feature flag is disabled.') |
| 38 | + end |
| 39 | + |
| 40 | + if args[:ids].size > MAX_WORK_ITEMS |
| 41 | + raise Gitlab::Graphql::Errors::ArgumentError, |
| 42 | + format( |
| 43 | + _('No more than %{max_work_items} work items can be updated at the same time'), |
| 44 | + max_work_items: MAX_WORK_ITEMS |
| 45 | + ) |
| 46 | + end |
| 47 | + |
| 48 | + super |
| 49 | + end |
| 50 | + |
| 51 | + def resolve(ids:, parent_id:, **attributes) |
| 52 | + parent = parent_for!(parent_id) |
| 53 | + |
| 54 | + result = ::WorkItems::BulkUpdateService.new( |
| 55 | + parent: parent, |
| 56 | + current_user: current_user, |
| 57 | + work_item_ids: ids.map(&:model_id), |
| 58 | + widget_params: attributes |
| 59 | + ).execute |
| 60 | + |
| 61 | + if result.success? |
| 62 | + { updated_work_item_count: result[:updated_work_item_count], errors: result.errors } |
| 63 | + else |
| 64 | + { errors: result.errors } |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + private |
| 69 | + |
| 70 | + def parent_for!(parent_id) |
| 71 | + strong_memoize_with(:parent_for, parent_id) do |
| 72 | + parent = GitlabSchema.find_by_gid(parent_id).sync |
| 73 | + raise_resource_not_available_error! unless current_user.can?("read_#{parent.to_ability_name}", parent) |
| 74 | + |
| 75 | + parent |
| 76 | + end |
| 77 | + end |
| 78 | + end |
| 79 | + end |
| 80 | +end |
| 81 | + |
| 82 | +Mutations::WorkItems::BulkUpdate.prepend_mod |
0 commit comments