Commit 7f2fce29 authored by Sean Arnold's avatar Sean Arnold

Move close issue logic to close service

- Amend system note text and icon
- Remove unused params being passed around
parent 301567d0
......@@ -359,19 +359,6 @@ class Issue < ApplicationRecord
@design_collection ||= ::DesignManagement::DesignCollection.new(self)
end
def resolve_associated_alert_management_alert(user)
return unless alert_management_alert
return true if alert_management_alert.resolve
Gitlab::AppLogger.warn(
message: 'Cannot resolve an associated Alert Management alert',
issue_id: id,
alert_id: alert_management_alert.id,
alert_errors: alert_management_alert.errors.messages
)
end
def from_service_desk?
author.id == User.support_bot.id
end
......
......@@ -60,9 +60,18 @@ module Issues
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)
return unless alert = issue.alert_management_alert
if alert.resolve
SystemNoteService.closed_alert_issue(alert, issue, current_user)
else
Gitlab::AppLogger.warn(
message: 'Cannot resolve an associated Alert Management alert',
issue_id: issue.id,
alert_id: alert.id,
alert_errors: alert.errors.messages
)
end
end
def store_first_mentioned_in_commit_at(issue, merge_request)
......
......@@ -297,11 +297,11 @@ module SystemNoteService
end
def new_alert_issue(alert, issue, author)
::SystemNotes::AlertManagementService.new(noteable: alert, project: alert.project, author: author).new_alert_issue(alert, issue)
::SystemNotes::AlertManagementService.new(noteable: alert, project: alert.project, author: author).new_alert_issue(issue)
end
def closed_alert_issue(alert, issue, author)
::SystemNotes::AlertManagementService.new(noteable: alert, project: alert.project, author: author).closed_alert_issue(alert, issue)
::SystemNotes::AlertManagementService.new(noteable: alert, project: alert.project, author: author).closed_alert_issue(issue)
end
private
......
......@@ -12,7 +12,7 @@ module SystemNotes
#
# Returns the created Note object
def change_alert_status(alert)
status = AlertManagement::Alert::STATUSES.key(alert.status).to_s.titleize
status = alert.state.to_s.titleize
body = "changed the status to **#{status}**"
create_note(NoteSummary.new(noteable, project, author, body, action: 'status'))
......@@ -28,7 +28,7 @@ module SystemNotes
# "created issue #17 for this alert"
#
# Returns the created Note object
def new_alert_issue(alert, issue)
def new_alert_issue(issue)
body = "created issue #{issue.to_reference(project)} for this alert"
create_note(NoteSummary.new(noteable, project, author, body, action: 'alert_issue_added'))
......@@ -41,13 +41,13 @@ module SystemNotes
#
# Example Note text:
#
# "resolved this alert due to closing issue #17"
# "changed the status to Resolved by closing issue #17"
#
# Returns the created Note object
def closed_alert_issue(alert, issue)
body = "resolved this alert due to closing issue #{issue.to_reference(project)}"
def closed_alert_issue(issue)
body = "changed the status to **Resolved** by closing issue #{issue.to_reference(project)}"
create_note(NoteSummary.new(noteable, project, author, body, action: 'alert_issue_added'))
create_note(NoteSummary.new(noteable, project, author, body, action: 'status'))
end
end
end
......@@ -369,41 +369,6 @@ 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 }
......
......@@ -252,14 +252,37 @@ RSpec.describe Issues::CloseService do
expect(todo.reload).to be_done
end
it 'resolves associated alert' do
alert = create(:alert_management_alert, issue: issue, project: project)
context 'when there is an associated Alert Management Alert' do
context 'when alert can be resolved' do
let!(:alert) { create(:alert_management_alert, issue: issue, project: project) }
expect(SystemNoteService).to receive(:closed_alert_issue).with(alert, issue, instance_of(User))
it 'resolves an alert and sends a system note' do
expect(SystemNoteService).to receive(:closed_alert_issue).with(alert, issue, instance_of(User))
close_issue
close_issue
expect(alert.reload.resolved?).to eq(true)
end
end
context 'when alert cannot be resolved' do
let!(:alert) { create(:alert_management_alert, :with_validation_errors, issue: issue, project: project) }
expect(alert.reload.resolved?).to eq(true)
before do
allow(Gitlab::AppLogger).to receive(:warn).and_call_original
end
it 'writes a warning into the log' do
close_issue
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
it 'deletes milestone issue counters cache' do
......
......@@ -699,7 +699,7 @@ RSpec.describe SystemNoteService do
it 'calls AlertManagementService' do
expect_next_instance_of(SystemNotes::AlertManagementService) do |service|
expect(service).to receive(:new_alert_issue).with(alert, alert.issue)
expect(service).to receive(:new_alert_issue).with(alert.issue)
end
described_class.new_alert_issue(alert, alert.issue, author)
......@@ -711,7 +711,7 @@ RSpec.describe SystemNoteService do
it 'calls AlertManagementService' do
expect_next_instance_of(SystemNotes::AlertManagementService) do |service|
expect(service).to receive(:closed_alert_issue).with(alert, alert.issue)
expect(service).to receive(:closed_alert_issue).with(alert.issue)
end
described_class.closed_alert_issue(alert, alert.issue, author)
......
......@@ -22,7 +22,7 @@ RSpec.describe ::SystemNotes::AlertManagementService do
describe '#new_alert_issue' do
let_it_be(:issue) { noteable.issue }
subject { described_class.new(noteable: noteable, project: project, author: author).new_alert_issue(noteable, issue) }
subject { described_class.new(noteable: noteable, project: project, author: author).new_alert_issue(issue) }
it_behaves_like 'a system note' do
let(:action) { 'alert_issue_added' }
......@@ -36,14 +36,14 @@ RSpec.describe ::SystemNotes::AlertManagementService do
describe '#closed_alert_issue' do
let_it_be(:issue) { noteable.issue }
subject { described_class.new(noteable: noteable, project: project, author: author).closed_alert_issue(noteable, issue) }
subject { described_class.new(noteable: noteable, project: project, author: author).closed_alert_issue(issue) }
it_behaves_like 'a system note' do
let(:action) { 'alert_issue_added' }
let(:action) { 'status' }
end
it 'has the appropriate message' do
expect(subject.note).to eq("resolved this alert due to closing issue #{issue.to_reference(project)}")
expect(subject.note).to eq("changed the status to **Resolved** by closing issue #{issue.to_reference(project)}")
end
end
end
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