Commit 6aeea689 authored by Sean McGivern's avatar Sean McGivern

Merge branch 'pl-alert-management-fix-multiple-issue-creation' into 'master'

Fix multiple issue creation for Generic Alerts

Closes #220088

See merge request gitlab-org/gitlab!33647
parents bcf3d865 0a5d9c10
...@@ -12,7 +12,7 @@ module IncidentManagement ...@@ -12,7 +12,7 @@ module IncidentManagement
return unless project return unless project
new_issue = create_issue(project, alert_payload) new_issue = create_issue(project, alert_payload)
return unless am_alert_id && new_issue.persisted? return unless am_alert_id && new_issue&.persisted?
link_issue_with_alert(am_alert_id, new_issue.id) link_issue_with_alert(am_alert_id, new_issue.id)
end end
...@@ -27,6 +27,7 @@ module IncidentManagement ...@@ -27,6 +27,7 @@ module IncidentManagement
IncidentManagement::CreateIssueService IncidentManagement::CreateIssueService
.new(project, alert_payload) .new(project, alert_payload)
.execute .execute
.dig(:issue)
end end
def link_issue_with_alert(alert_id, issue_id) def link_issue_with_alert(alert_id, issue_id)
......
---
title: Fix linking alerts to created issues for the Generic alerts intergration
merge_request: 33647
author:
type: fixed
...@@ -7,40 +7,39 @@ describe IncidentManagement::ProcessAlertWorker do ...@@ -7,40 +7,39 @@ describe IncidentManagement::ProcessAlertWorker do
describe '#perform' do describe '#perform' do
let(:alert_management_alert_id) { nil } let(:alert_management_alert_id) { nil }
let(:alert_payload) { { alert: 'payload' } } let(:alert_payload) do
let(:new_issue) { create(:issue, project: project) } {
let(:create_issue_service) { instance_double(IncidentManagement::CreateIssueService, execute: new_issue) } 'annotations' => { 'title' => 'title' },
'startsAt' => Time.now.rfc3339
}
end
let(:created_issue) { Issue.last }
subject { described_class.new.perform(project.id, alert_payload, alert_management_alert_id) } subject { described_class.new.perform(project.id, alert_payload, alert_management_alert_id) }
before do before do
allow(IncidentManagement::CreateIssueService) allow(IncidentManagement::CreateIssueService)
.to receive(:new).with(project, alert_payload) .to receive(:new).with(project, alert_payload)
.and_return(create_issue_service) .and_call_original
end end
it 'calls create issue service' do it 'creates an issue' do
expect(Project).to receive(:find_by_id).and_call_original
expect(IncidentManagement::CreateIssueService) expect(IncidentManagement::CreateIssueService)
.to receive(:new).with(project, alert_payload) .to receive(:new).with(project, alert_payload)
.and_return(create_issue_service)
expect(create_issue_service).to receive(:execute)
subject expect { subject }.to change { Issue.count }.by(1)
end end
context 'with invalid project' do context 'with invalid project' do
let(:invalid_project_id) { 0 } let(:invalid_project_id) { non_existing_record_id }
subject { described_class.new.perform(invalid_project_id, alert_payload) } subject { described_class.new.perform(invalid_project_id, alert_payload) }
it 'does not create issues' do it 'does not create issues' do
expect(Project).to receive(:find_by_id).and_call_original
expect(IncidentManagement::CreateIssueService).not_to receive(:new) expect(IncidentManagement::CreateIssueService).not_to receive(:new)
subject expect { subject }.not_to change { Issue.count }
end end
end end
...@@ -59,7 +58,9 @@ describe IncidentManagement::ProcessAlertWorker do ...@@ -59,7 +58,9 @@ describe IncidentManagement::ProcessAlertWorker do
context 'when alert can be updated' do context 'when alert can be updated' do
it 'updates AlertManagement::Alert#issue_id' do it 'updates AlertManagement::Alert#issue_id' do
expect { subject }.to change { alert.reload.issue_id }.to(new_issue.id) subject
expect(alert.reload.issue_id).to eq(created_issue.id)
end end
it 'does not write a warning to log' do it 'does not write a warning to log' do
...@@ -76,12 +77,12 @@ describe IncidentManagement::ProcessAlertWorker do ...@@ -76,12 +77,12 @@ describe IncidentManagement::ProcessAlertWorker do
expect { subject }.not_to change { alert.reload.issue_id } expect { subject }.not_to change { alert.reload.issue_id }
end end
it 'writes a worning to log' do it 'logs a warning' do
subject subject
expect(Gitlab::AppLogger).to have_received(:warn).with( expect(Gitlab::AppLogger).to have_received(:warn).with(
message: 'Cannot link an Issue with Alert', message: 'Cannot link an Issue with Alert',
issue_id: new_issue.id, issue_id: created_issue.id,
alert_id: alert_management_alert_id, alert_id: alert_management_alert_id,
alert_errors: { hosts: ['hosts array is over 255 chars'] } alert_errors: { hosts: ['hosts array is over 255 chars'] }
) )
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment