From dfcecc6b598857f040af6fa6ae7aad4ce4459115 Mon Sep 17 00:00:00 2001 From: Nasser Zahrani Date: Tue, 9 Dec 2025 14:57:41 -0500 Subject: [PATCH] Instrument work item references --- .../system_notes/issuables_service.rb | 22 ++++++++++++- .../ee/system_notes/issuables_service_spec.rb | 20 +++++++++++ .../instrumentation/event_actions.rb | 2 ++ .../system_notes/issuables_service_spec.rb | 33 +++++++++++++++++++ 4 files changed, 76 insertions(+), 1 deletion(-) diff --git a/app/services/system_notes/issuables_service.rb b/app/services/system_notes/issuables_service.rb index 1442628853456c..2e2cd0c89a6c70 100644 --- a/app/services/system_notes/issuables_service.rb +++ b/app/services/system_notes/issuables_service.rb @@ -267,7 +267,10 @@ def cross_reference(mentioned_in) track_cross_reference_action created_at = mentioned_in.created_at if USE_COMMIT_DATE_FOR_CROSS_REFERENCE_NOTE && mentioned_in.is_a?(Commit) - create_note(NoteSummary.new(noteable, noteable.project, author, body, action: 'cross_reference', created_at: created_at), skip_touch_noteable: true) + note = create_note(NoteSummary.new(noteable, noteable.project, author, body, action: 'cross_reference', created_at: created_at), skip_touch_noteable: true) + track_cross_reference(mentioned_in, note) + + note end end @@ -518,6 +521,23 @@ def track_cross_reference_action track_issue_event(:track_issue_cross_referenced_action) end + def track_cross_reference(mentioned_in, note) + work_item = case mentioned_in + when Epic + mentioned_in.issue + when WorkItem, Issue + mentioned_in + end + + return unless work_item + + ::Gitlab::WorkItems::Instrumentation::TrackingService.new( + work_item: work_item, + current_user: note.author, + event: Gitlab::WorkItems::Instrumentation::EventActions::REFERENCE_ADD + ).execute + end + def hierarchy_note_params(action, parent, child) return {} unless child && parent diff --git a/ee/spec/services/ee/system_notes/issuables_service_spec.rb b/ee/spec/services/ee/system_notes/issuables_service_spec.rb index 518f9720f17cd0..f6506a2bba61b1 100644 --- a/ee/spec/services/ee/system_notes/issuables_service_spec.rb +++ b/ee/spec/services/ee/system_notes/issuables_service_spec.rb @@ -333,6 +333,26 @@ subject end + + context 'with work item instrumentation tracking' do + before do + allow_next_instance_of(described_class) do |instance| + allow(instance).to receive(:cross_reference_disallowed?).and_return(false) + end + end + + context 'when mentioned_in is an Epic' do + let_it_be(:epic) { create(:epic, group: group) } + let_it_be(:epic_work_item) { create(:work_item, project: project) } + let(:mentioned_in) { epic } + + before do + allow(epic).to receive(:issue).and_return(epic_work_item) + end + + it_behaves_like 'tracks work item event', :epic_work_item, :author, 'work_item_reference_add', :subject + end + end end context 'with project and group having the same path name' do diff --git a/lib/gitlab/work_items/instrumentation/event_actions.rb b/lib/gitlab/work_items/instrumentation/event_actions.rb index 9d4f7f2e0f46ef..9b785e2afadd84 100644 --- a/lib/gitlab/work_items/instrumentation/event_actions.rb +++ b/lib/gitlab/work_items/instrumentation/event_actions.rb @@ -15,6 +15,7 @@ module EventActions NOTE_CREATE = 'work_item_note_create' NOTE_DESTROY = 'work_item_note_destroy' NOTE_UPDATE = 'work_item_note_update' + REFERENCE_ADD = 'work_item_reference_add' REOPEN = 'work_item_reopen' ALL_EVENTS = [ @@ -29,6 +30,7 @@ module EventActions NOTE_CREATE, NOTE_DESTROY, NOTE_UPDATE, + REFERENCE_ADD, REOPEN ].freeze diff --git a/spec/services/system_notes/issuables_service_spec.rb b/spec/services/system_notes/issuables_service_spec.rb index eff3036442bdf5..2acb0d605768d5 100644 --- a/spec/services/system_notes/issuables_service_spec.rb +++ b/spec/services/system_notes/issuables_service_spec.rb @@ -521,6 +521,39 @@ def build_note(added_count, removed_count) subject end end + + context 'with work item instrumentation tracking' do + before do + allow_next_instance_of(described_class) do |instance| + allow(instance).to receive(:cross_reference_disallowed?).and_return(false) + end + end + + context 'when mentioned_in is a WorkItem' do + let(:mentioned_in) { create(:work_item, project: project) } + + it_behaves_like 'tracks work item event', :mentioned_in, :author, 'work_item_reference_add', :subject + end + + context 'when mentioned_in is an Issue' do + let(:mentioned_in) { create(:issue, project: project) } + let(:work_item) { WorkItem.find(mentioned_in.id) } + + it_behaves_like 'tracks work item event', :work_item, :author, 'work_item_reference_add', :subject + end + + context 'when mentioned_in is a MergeRequest' do + let(:mentioned_in) { create(:merge_request, :simple, source_project: project) } + + it_behaves_like 'does not track work item event', :subject + end + + context 'when mentioned_in is a Commit' do + let(:mentioned_in) { project.repository.commit } + + it_behaves_like 'does not track work item event', :subject + end + end end end -- GitLab