Class: Mutations::MergeRequests::SetLabels

Inherits:
Base
  • Object
show all
Defined in:
app/graphql/mutations/merge_requests/set_labels.rb

Constant Summary

Constants inherited from BaseMutation

BaseMutation::ERROR_MESSAGE

Instance Method Summary collapse

Methods included from ResolvesIssuable

#resolve_issuable

Methods inherited from BaseMutation

#api_user?, #current_user, #errors_on_object, #ready?

Instance Method Details

#label_descendant?(gid) ⇒ Boolean

Returns:

  • (Boolean)

49
50
51
# File 'app/graphql/mutations/merge_requests/set_labels.rb', line 49

def label_descendant?(gid)
  gid&.model_class&.ancestors&.include?(Label)
end

#resolve(project_path:, iid:, label_ids:, operation_mode: Types::MutationOperationModeEnum.enum[:replace]) ⇒ Object


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
# File 'app/graphql/mutations/merge_requests/set_labels.rb', line 22

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

  label_ids = label_ids
                .map { |gid| GlobalID.parse(gid) }
                .select(&method(:label_descendant?))
                .map(&:model_id) # MergeRequests::UpdateService expects integers

  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