diff --git a/app/services/issues/base_service.rb b/app/services/issues/base_service.rb index f564914352bbd5cf90e0735770672e60fe92ca2c..d87dc013c7fc1c0524db04bb06db922d2633872e 100644 --- a/app/services/issues/base_service.rb +++ b/app/services/issues/base_service.rb @@ -31,6 +31,16 @@ def rebalance_if_needed(issue) Issues::RebalancingWorker.perform_async(nil, *issue.project.self_or_root_group_ids) end + def execute_hooks(issue, action = 'open', old_associations: {}) + issue_data = Gitlab::Lazy.new { hook_data(issue, action, old_associations: old_associations) } + hooks_scope = issue.confidential? ? :confidential_issue_hooks : :issue_hooks + issue.namespace.execute_hooks(issue_data, hooks_scope) + issue.namespace.execute_integrations(issue_data, hooks_scope) + + execute_incident_hooks(issue, issue_data) if issue.work_item_type&.incident? + execute_group_mention_hooks(issue, issue_data) if action == 'open' + end + private # overriding this because IssuableBaseService#constructor_container_arg returns { project: value } @@ -105,16 +115,6 @@ def create_assignee_note(issue, old_assignees) issue, issue.project, current_user, old_assignees) end - def execute_hooks(issue, action = 'open', old_associations: {}) - issue_data = Gitlab::Lazy.new { hook_data(issue, action, old_associations: old_associations) } - hooks_scope = issue.confidential? ? :confidential_issue_hooks : :issue_hooks - issue.namespace.execute_hooks(issue_data, hooks_scope) - issue.namespace.execute_integrations(issue_data, hooks_scope) - - execute_incident_hooks(issue, issue_data) if issue.work_item_type&.incident? - execute_group_mention_hooks(issue, issue_data) if action == 'open' - end - # We can remove this code after proposal in # https://gitlab.com/gitlab-org/gitlab/-/issues/367550#proposal is updated. def execute_incident_hooks(issue, issue_data) diff --git a/app/services/timelogs/create_service.rb b/app/services/timelogs/create_service.rb index 19428864fa94c086180d7e6d136eda36d9a2f7ca..f65f9482d769105eb52483aec105506735219fda 100644 --- a/app/services/timelogs/create_service.rb +++ b/app/services/timelogs/create_service.rb @@ -37,12 +37,33 @@ def execute note: nil ) + old_associations = { total_time_spent: issuable.total_time_spent } + if !timelog.save error_in_save(timelog) else SystemNoteService.created_timelog(issuable, issuable.project, current_user, timelog) + + issuable_base_service.execute_hooks(issuable, 'update', old_associations: old_associations) + success(timelog) end end + + private + + def issuable_base_service + if issuable.is_a?(Issue) + Issues::BaseService.new( + container: issuable.project, + current_user: current_user + ) + else + MergeRequests::BaseService.new( + project: issuable.project, + current_user: current_user + ) + end + end end end diff --git a/spec/support/shared_examples/services/timelogs/create_service_shared_examples.rb b/spec/support/shared_examples/services/timelogs/create_service_shared_examples.rb index 00d4224f021d4172b20016b3fac464552b62cf46..301c603df713cc080a80de9e115142597d6dc251 100644 --- a/spec/support/shared_examples/services/timelogs/create_service_shared_examples.rb +++ b/spec/support/shared_examples/services/timelogs/create_service_shared_examples.rb @@ -9,6 +9,11 @@ shared_examples 'success_response' do it 'sucessfully saves the timelog' do + expect(Projects::TriggeredHooks).to receive(:new).with( + issuable.is_a?(Issue) ? :issue_hooks : :merge_request_hooks, + a_hash_including(changes: a_hash_including(total_time_spent: { previous: 0, current: time_spent })) + ).and_call_original + is_expected.to be_success timelog = subject.payload[:timelog]