File: create.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (54 lines) | stat: -rw-r--r-- 1,544 bytes parent folder | download
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
# frozen_string_literal: true

module Mutations
  module Timelogs
    class Create < Base
      graphql_name 'TimelogCreate'

      argument :time_spent,
        GraphQL::Types::String,
        required: true,
        description: 'Amount of time spent.'

      argument :spent_at,
        Types::TimeType,
        required: false,
        description: 'Timestamp of when the time was spent. If empty, defaults to current time.'

      argument :summary,
        GraphQL::Types::String,
        required: true,
        description: 'Summary of time spent.'

      argument :issuable_id,
        ::Types::GlobalIDType[::Issuable],
        required: true,
        description: 'Global ID of the issuable (Issue, WorkItem or MergeRequest).'

      authorize :create_timelog

      def resolve(issuable_id:, time_spent:, summary:, **args)
        parsed_time_spent = Gitlab::TimeTrackingFormatter.parse(time_spent)
        if parsed_time_spent.nil?
          return { timelog: nil, errors: [_('Time spent must be formatted correctly. For example: 1h 30m.')] }
        end

        issuable = authorized_find!(id: issuable_id)

        spent_at = args[:spent_at].nil? ? DateTime.current : args[:spent_at]

        result = ::Timelogs::CreateService.new(
          issuable, parsed_time_spent, spent_at, summary, current_user
        ).execute

        response(result)
      end

      private

      def find_object(id:)
        GitlabSchema.object_from_id(id, expected_type: [::Issue, ::WorkItem, ::MergeRequest]).sync
      end
    end
  end
end