summaryrefslogtreecommitdiff
path: root/app/graphql/mutations/merge_requests/set_labels.rb
blob: 712c68c94255abdd58ca8328520d3afb10149d57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# frozen_string_literal: true

module Mutations
  module MergeRequests
    class SetLabels < Base
      graphql_name 'MergeRequestSetLabels'

      argument :label_ids,
               [::Types::GlobalIDType[Label]],
               required: true,
               description: <<~DESC
                            The Label IDs to set. Replaces existing labels by default.
               DESC

      argument :operation_mode,
               Types::MutationOperationModeEnum,
               required: false,
               description: <<~DESC
                            Changes the operation mode. Defaults to REPLACE.
               DESC

      def resolve(project_path:, iid:, label_ids:, operation_mode: Types::MutationOperationModeEnum.enum[:replace])
        merge_request = authorized_find!(project_path: project_path, iid: iid)
        project = merge_request.project

        # TODO: remove this line when the compatibility layer is removed:
        # See: https://gitlab.com/gitlab-org/gitlab/-/issues/257883
        label_ids = label_ids.map { |id| ::Types::GlobalIDType[::Label].coerce_isolated_input(id) }
        # MergeRequests::UpdateService expects integers
        label_ids = label_ids.compact.map(&:model_id)

        attribute_name = case operation_mode
                         when Types::MutationOperationModeEnum.enum[:append]
                           :add_label_ids
                         when Types::MutationOperationModeEnum.enum[:remove]
                           :remove_label_ids
                         else
                           :label_ids
                         end

        ::MergeRequests::UpdateService.new(project, current_user, attribute_name => label_ids)
          .execute(merge_request)

        {
          merge_request: merge_request,
          errors: errors_on_object(merge_request)
        }
      end
    end
  end
end