summaryrefslogtreecommitdiff
path: root/app/graphql/mutations/issues/update.rb
blob: b5af048dc07ed53018c6e1fffa09460581da77a3 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# frozen_string_literal: true

module Mutations
  module Issues
    class Update < Base
      graphql_name 'UpdateIssue'

      include CommonMutationArguments

      argument :title, GraphQL::Types::String,
               required: false,
               description: copy_field_description(Types::IssueType, :title)

      argument :milestone_id, GraphQL::Types::ID, # rubocop: disable Graphql/IDType
               required: false,
               description: 'ID of the milestone to assign to the issue. On update milestone will be removed if set to null.'

      argument :add_label_ids, [GraphQL::Types::ID],
               required: false,
               description: 'IDs of labels to be added to the issue.'

      argument :remove_label_ids, [GraphQL::Types::ID],
               required: false,
               description: 'IDs of labels to be removed from the issue.'

      argument :label_ids, [GraphQL::Types::ID],
               required: false,
               description: 'IDs of labels to be set. Replaces existing issue labels.'

      argument :state_event, Types::IssueStateEventEnum,
               description: 'Close or reopen an issue.',
               required: false

      argument :time_estimate, GraphQL::Types::String,
               required: false,
               description: 'Estimated time to complete the issue, or `0` to remove the current estimate.'

      def resolve(project_path:, iid:, **args)
        issue = authorized_find!(project_path: project_path, iid: iid)
        project = issue.project

        args = parse_arguments(args)

        spam_params = ::Spam::SpamParams.new_from_request(request: context[:request])
        ::Issues::UpdateService.new(container: project, current_user: current_user, params: args, spam_params: spam_params).execute(issue)

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

      def ready?(label_ids: [], add_label_ids: [], remove_label_ids: [], time_estimate: nil, **args)
        if label_ids.any? && (add_label_ids.any? || remove_label_ids.any?)
          raise Gitlab::Graphql::Errors::ArgumentError, 'labelIds is mutually exclusive with any of addLabelIds or removeLabelIds'
        end

        if !time_estimate.nil? && Gitlab::TimeTrackingFormatter.parse(time_estimate, keep_zero: true).nil?
          raise Gitlab::Graphql::Errors::ArgumentError, 'timeEstimate must be formatted correctly, for example `1h 30m`'
        end

        super
      end

      private

      def parse_arguments(args)
        args[:add_label_ids] = parse_label_ids(args[:add_label_ids])
        args[:remove_label_ids] = parse_label_ids(args[:remove_label_ids])
        args[:label_ids] = parse_label_ids(args[:label_ids])

        unless args[:time_estimate].nil?
          args[:time_estimate] = Gitlab::TimeTrackingFormatter.parse(args[:time_estimate], keep_zero: true)
        end

        args
      end

      def parse_label_ids(ids)
        ids&.map do |gid|
          GitlabSchema.parse_gid(gid, expected_type: ::Label).model_id
        rescue Gitlab::Graphql::Errors::ArgumentError
          gid
        end
      end
    end
  end
end

Mutations::Issues::Update.prepend_mod_with('Mutations::Issues::Update')