Commit 86ea121d authored by Sean Arnold's avatar Sean Arnold

Move system note send into close service

- Update issue spec
- Fix other specs
parent 04cf1dbb
......@@ -362,10 +362,7 @@ class Issue < ApplicationRecord
def resolve_associated_alert_management_alert(user)
return unless alert_management_alert
if alert_management_alert.resolve
SystemNoteService.closed_alert_issue(alert_management_alert, self, user)
return
end
return true if alert_management_alert.resolve
Gitlab::AppLogger.warn(
message: 'Cannot resolve an associated Alert Management alert',
......
......@@ -33,7 +33,7 @@ module Issues
notification_service.async.close_issue(issue, current_user, closed_via: closed_via) if notifications
todo_service.close_issue(issue, current_user)
issue.resolve_associated_alert_management_alert(current_user)
resolve_alert(issue)
execute_hooks(issue, 'close')
invalidate_cache_counts(issue, users: issue.assignees)
issue.update_project_counter_caches
......@@ -59,6 +59,12 @@ module Issues
SystemNoteService.change_status(issue, issue.project, current_user, issue.state, current_commit)
end
def resolve_alert(issue)
return unless issue.resolve_associated_alert_management_alert(current_user)
SystemNoteService.closed_alert_issue(issue.alert_management_alert, issue, current_user)
end
def store_first_mentioned_in_commit_at(issue, merge_request)
metrics = issue.metrics
return if metrics.nil? || metrics.first_mentioned_in_commit_at
......
......@@ -49,6 +49,5 @@ module SystemNotes
create_note(NoteSummary.new(noteable, project, author, body, action: 'alert_issue_added'))
end
end
end
......@@ -168,41 +168,6 @@ RSpec.describe Issue do
expect { issue.close }.to change { issue.state_id }.from(open_state).to(closed_state)
end
context 'when there is an associated Alert Management Alert' do
context 'when alert can be resolved' do
let!(:alert) { create(:alert_management_alert, project: issue.project, issue: issue) }
it 'resolves an alert' do
expect { issue.close(user) }.to change { alert.reload.resolved? }.to(true)
end
it 'creates a system note' do
expect(SystemNoteService).to receive(:closed_alert_issue).with(alert, issue, instance_of(User))
issue.close(user)
end
end
context 'when alert cannot be resolved' do
let!(:alert) { create(:alert_management_alert, :with_validation_errors, project: issue.project, issue: issue) }
before do
allow(Gitlab::AppLogger).to receive(:warn).and_call_original
end
it 'writes a warning into the log' do
close
expect(Gitlab::AppLogger).to have_received(:warn).with(
message: 'Cannot resolve an associated Alert Management alert',
issue_id: issue.id,
alert_id: alert.id,
alert_errors: { hosts: ['hosts array is over 255 chars'] }
)
end
end
end
end
describe '#reopen' do
......@@ -404,6 +369,41 @@ RSpec.describe Issue do
end
end
describe '#resolve_associated_alert_management_alert' do
let(:issue) { create(:issue) }
subject { issue.resolve_associated_alert_management_alert(user) }
context 'when there is an associated Alert Management Alert' do
context 'when alert can be resolved' do
let!(:alert) { create(:alert_management_alert, project: issue.project, issue: issue) }
it 'resolves an alert' do
expect { subject }.to change { alert.reload.resolved? }.to(true)
end
end
context 'when alert cannot be resolved' do
let!(:alert) { create(:alert_management_alert, :with_validation_errors, project: issue.project, issue: issue) }
before do
allow(Gitlab::AppLogger).to receive(:warn).and_call_original
end
it 'writes a warning into the log' do
subject
expect(Gitlab::AppLogger).to have_received(:warn).with(
message: 'Cannot resolve an associated Alert Management alert',
issue_id: issue.id,
alert_id: alert.id,
alert_errors: { hosts: ['hosts array is over 255 chars'] }
)
end
end
end
end
describe '#suggested_branch_name' do
let(:repository) { double }
......
......@@ -255,6 +255,8 @@ RSpec.describe Issues::CloseService do
it 'resolves associated alert' do
alert = create(:alert_management_alert, issue: issue, project: project)
expect(SystemNoteService).to receive(:closed_alert_issue).with(alert, issue, instance_of(User))
close_issue
expect(alert.reload.resolved?).to eq(true)
......
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