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
return unless project
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)
end
......@@ -27,6 +27,7 @@ module IncidentManagement
IncidentManagement::CreateIssueService
.new(project, alert_payload)
.execute
.dig(:issue)
end
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
describe '#perform' do
let(:alert_management_alert_id) { nil }
let(:alert_payload) { { alert: 'payload' } }
let(:new_issue) { create(:issue, project: project) }
let(:create_issue_service) { instance_double(IncidentManagement::CreateIssueService, execute: new_issue) }
let(:alert_payload) do
{
'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) }
before do
allow(IncidentManagement::CreateIssueService)
.to receive(:new).with(project, alert_payload)
.and_return(create_issue_service)
.and_call_original
end
it 'calls create issue service' do
expect(Project).to receive(:find_by_id).and_call_original
it 'creates an issue' do
expect(IncidentManagement::CreateIssueService)
.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
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) }
it 'does not create issues' do
expect(Project).to receive(:find_by_id).and_call_original
expect(IncidentManagement::CreateIssueService).not_to receive(:new)
subject
expect { subject }.not_to change { Issue.count }
end
end
......@@ -59,7 +58,9 @@ describe IncidentManagement::ProcessAlertWorker do
context 'when alert can be updated' 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
it 'does not write a warning to log' do
......@@ -76,12 +77,12 @@ describe IncidentManagement::ProcessAlertWorker do
expect { subject }.not_to change { alert.reload.issue_id }
end
it 'writes a worning to log' do
it 'logs a warning' do
subject
expect(Gitlab::AppLogger).to have_received(:warn).with(
message: 'Cannot link an Issue with Alert',
issue_id: new_issue.id,
issue_id: created_issue.id,
alert_id: alert_management_alert_id,
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