diff --git a/app/graphql/mutations/timelogs/create.rb b/app/graphql/mutations/timelogs/create.rb index 1be023eed8a8b2da111aca8ee8e9e52820cd4ffd..4caea414175386e3175292a516c766d7feae15c6 100644 --- a/app/graphql/mutations/timelogs/create.rb +++ b/app/graphql/mutations/timelogs/create.rb @@ -12,8 +12,8 @@ class Create < Base argument :spent_at, Types::TimeType, - required: true, - description: 'When the time was spent.' + required: false, + description: 'Timestamp of when the time was spent. If empty, defaults to current time.' argument :summary, GraphQL::Types::String, @@ -27,7 +27,7 @@ class Create < Base authorize :create_timelog - def resolve(issuable_id:, time_spent:, spent_at:, summary:, **args) + 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.')] } @@ -35,6 +35,8 @@ def resolve(issuable_id:, time_spent:, spent_at:, summary:, **args) 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 diff --git a/doc/api/graphql/reference/index.md b/doc/api/graphql/reference/index.md index 0273b6bbfd2a1aedc8e8df4fba2b7258eb729fcf..0a8b39cf4059e4d5c67aa67b1a6f7c04d281514c 100644 --- a/doc/api/graphql/reference/index.md +++ b/doc/api/graphql/reference/index.md @@ -8212,7 +8212,7 @@ Input type: `TimelogCreateInput` | ---- | ---- | ----------- | | `clientMutationId` | [`String`](#string) | A unique identifier for the client performing the mutation. | | `issuableId` | [`IssuableID!`](#issuableid) | Global ID of the issuable (Issue, WorkItem or MergeRequest). | -| `spentAt` | [`Time!`](#time) | When the time was spent. | +| `spentAt` | [`Time`](#time) | Timestamp of when the time was spent. If empty, defaults to current time. | | `summary` | [`String!`](#string) | Summary of time spent. | | `timeSpent` | [`String!`](#string) | Amount of time spent. | diff --git a/doc/user/project/time_tracking.md b/doc/user/project/time_tracking.md index b15966722b66095003de042e48c43bba17e56cfd..03666abb09bc8f85d0d25b952e35ca6485a94995 100644 --- a/doc/user/project/time_tracking.md +++ b/doc/user/project/time_tracking.md @@ -83,6 +83,7 @@ Prerequisites: #### Using the user interface > - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/101563) in GitLab 15.7. +> - [Changed](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/150564) in GitLab 17.0. When you don't specify when time was spent, current time is used. To add a time entry using the user interface: @@ -90,7 +91,7 @@ To add a time entry using the user interface: 1. Enter: - The amount of time spent. - - Optional. When it was spent. + - Optional. When it was spent. If empty, uses current time. - Optional. A summary. 1. Select **Save**. diff --git a/spec/support/shared_examples/graphql/mutations/timelogs/create_shared_examples.rb b/spec/support/shared_examples/graphql/mutations/timelogs/create_shared_examples.rb index c6402a89f02dc2604ecb79aca6ad34007cc1a23f..23dd195e626c243941b47dfa6df98e406d11e49d 100644 --- a/spec/support/shared_examples/graphql/mutations/timelogs/create_shared_examples.rb +++ b/spec/support/shared_examples/graphql/mutations/timelogs/create_shared_examples.rb @@ -2,14 +2,16 @@ RSpec.shared_examples 'issuable supports timelog creation mutation' do let(:mutation_response) { graphql_mutation_response(:timelog_create) } - let(:mutation) do - variables = { + let(:spent_at) { '2022-11-16T12:59:35+0100' } + let(:mutation) { graphql_mutation(:timelogCreate, variables) } + + let(:variables) do + { 'time_spent' => time_spent, - 'spent_at' => '2022-11-16T12:59:35+0100', + 'spent_at' => spent_at, 'summary' => 'Test summary', 'issuable_id' => issuable.to_global_id.to_s } - graphql_mutation(:timelogCreate, variables) end context 'when the user is anonymous' do @@ -56,6 +58,30 @@ end end + context 'when spent_at is not provided', time_travel_to: '2024-04-23 22:50:00 +0200' do + let(:variables) do + { + 'time_spent' => time_spent, + 'summary' => 'Test summary', + 'issuable_id' => issuable.to_global_id.to_s + } + end + + it 'creates the timelog using the current time' do + expect do + post_graphql_mutation(mutation, current_user: current_user) + end.to change { Timelog.count }.by(1) + + expect(response).to have_gitlab_http_status(:success) + expect(mutation_response['errors']).to be_empty + expect(mutation_response['timelog']).to include( + 'timeSpent' => 3600, + 'spentAt' => '2024-04-23T20:50:00Z', + 'summary' => 'Test summary' + ) + end + end + context 'with invalid time_spent' do let(:time_spent) { '3h e' }