diff --git a/app/services/system_notes/issuables_service.rb b/app/services/system_notes/issuables_service.rb index 1442628853456c7232545eb45772961aa99271ee..2e2cd0c89a6c70efdf7ba1ca0348be38bb43b64d 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 518f9720f17cd085a82e96796009344f1f9fd331..f6506a2bba61b12e2a014d97b9579131e32c16ba 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 9d4f7f2e0f46efccf1be22e8470fdc4c1ed06727..9b785e2afadd844d0c685116ede71fbc910da0e9 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 eff3036442bdf56cb9d1005ac578ac1cc3e475cb..2acb0d605768d5ae57573b0ce560691fa16ac408 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