Commit 04cf1dbb authored by Sean Arnold's avatar Sean Arnold

Create system note for alert when issue closing

- Update specs
- Move issue closing and system note to close service
parent a9c5768b
...@@ -146,10 +146,6 @@ class Issue < ApplicationRecord ...@@ -146,10 +146,6 @@ class Issue < ApplicationRecord
issue.closed_at = nil issue.closed_at = nil
issue.closed_by = nil issue.closed_by = nil
end end
after_transition any => :closed do |issue|
issue.resolve_associated_alert_management_alert
end
end end
# Alias to state machine .with_state_id method # Alias to state machine .with_state_id method
...@@ -363,9 +359,13 @@ class Issue < ApplicationRecord ...@@ -363,9 +359,13 @@ class Issue < ApplicationRecord
@design_collection ||= ::DesignManagement::DesignCollection.new(self) @design_collection ||= ::DesignManagement::DesignCollection.new(self)
end end
def resolve_associated_alert_management_alert def resolve_associated_alert_management_alert(user)
return unless alert_management_alert return unless alert_management_alert
return if alert_management_alert.resolve
if alert_management_alert.resolve
SystemNoteService.closed_alert_issue(alert_management_alert, self, user)
return
end
Gitlab::AppLogger.warn( Gitlab::AppLogger.warn(
message: 'Cannot resolve an associated Alert Management alert', message: 'Cannot resolve an associated Alert Management alert',
......
...@@ -33,6 +33,7 @@ module Issues ...@@ -33,6 +33,7 @@ module Issues
notification_service.async.close_issue(issue, current_user, closed_via: closed_via) if notifications notification_service.async.close_issue(issue, current_user, closed_via: closed_via) if notifications
todo_service.close_issue(issue, current_user) todo_service.close_issue(issue, current_user)
issue.resolve_associated_alert_management_alert(current_user)
execute_hooks(issue, 'close') execute_hooks(issue, 'close')
invalidate_cache_counts(issue, users: issue.assignees) invalidate_cache_counts(issue, users: issue.assignees)
issue.update_project_counter_caches issue.update_project_counter_caches
......
...@@ -300,6 +300,10 @@ module SystemNoteService ...@@ -300,6 +300,10 @@ module SystemNoteService
::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(alert, issue)
end end
def closed_alert_issue(alert, issue, author)
::SystemNotes::AlertManagementService.new(noteable: alert, project: alert.project, author: author).closed_alert_issue(alert, issue)
end
private private
def merge_requests_service(noteable, project, author) def merge_requests_service(noteable, project, author)
......
...@@ -33,5 +33,22 @@ module SystemNotes ...@@ -33,5 +33,22 @@ module SystemNotes
create_note(NoteSummary.new(noteable, project, author, body, action: 'alert_issue_added')) create_note(NoteSummary.new(noteable, project, author, body, action: 'alert_issue_added'))
end end
# Called when an AlertManagement::Alert is resolved due to the associated alert being closed
#
# alert - AlertManagement::Alert object.
# issue - Issue object.
#
# Example Note text:
#
# "resolved this alert due to 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)}"
create_note(NoteSummary.new(noteable, project, author, body, action: 'alert_issue_added'))
end
end end
end end
...@@ -5,6 +5,8 @@ require 'spec_helper' ...@@ -5,6 +5,8 @@ require 'spec_helper'
RSpec.describe Issue do RSpec.describe Issue do
include ExternalAuthorizationServiceHelpers include ExternalAuthorizationServiceHelpers
let_it_be(:user) { create(:user) }
describe "Associations" do describe "Associations" do
it { is_expected.to belong_to(:milestone) } it { is_expected.to belong_to(:milestone) }
it { is_expected.to belong_to(:iteration) } it { is_expected.to belong_to(:iteration) }
...@@ -172,7 +174,13 @@ RSpec.describe Issue do ...@@ -172,7 +174,13 @@ RSpec.describe Issue do
let!(:alert) { create(:alert_management_alert, project: issue.project, issue: issue) } let!(:alert) { create(:alert_management_alert, project: issue.project, issue: issue) }
it 'resolves an alert' do it 'resolves an alert' do
expect { issue.close }.to change { alert.reload.resolved? }.to(true) 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
end end
...@@ -184,7 +192,7 @@ RSpec.describe Issue do ...@@ -184,7 +192,7 @@ RSpec.describe Issue do
end end
it 'writes a warning into the log' do it 'writes a warning into the log' do
issue.close close
expect(Gitlab::AppLogger).to have_received(:warn).with( expect(Gitlab::AppLogger).to have_received(:warn).with(
message: 'Cannot resolve an associated Alert Management alert', message: 'Cannot resolve an associated Alert Management alert',
...@@ -198,7 +206,6 @@ RSpec.describe Issue do ...@@ -198,7 +206,6 @@ RSpec.describe Issue do
end end
describe '#reopen' do describe '#reopen' do
let(:user) { create(:user) }
let(:issue) { create(:issue, state: 'closed', closed_at: Time.current, closed_by: user) } let(:issue) { create(:issue, state: 'closed', closed_at: Time.current, closed_by: user) }
it 'sets closed_at to nil when an issue is reopend' do it 'sets closed_at to nil when an issue is reopend' do
...@@ -282,7 +289,6 @@ RSpec.describe Issue do ...@@ -282,7 +289,6 @@ RSpec.describe Issue do
end end
describe '#assignee_or_author?' do describe '#assignee_or_author?' do
let(:user) { create(:user) }
let(:issue) { create(:issue) } let(:issue) { create(:issue) }
it 'returns true for a user that is assigned to an issue' do it 'returns true for a user that is assigned to an issue' do
...@@ -303,7 +309,6 @@ RSpec.describe Issue do ...@@ -303,7 +309,6 @@ RSpec.describe Issue do
end end
describe '#can_move?' do describe '#can_move?' do
let(:user) { create(:user) }
let(:issue) { create(:issue) } let(:issue) { create(:issue) }
subject { issue.can_move?(user) } subject { issue.can_move?(user) }
......
...@@ -252,6 +252,14 @@ RSpec.describe Issues::CloseService do ...@@ -252,6 +252,14 @@ RSpec.describe Issues::CloseService do
expect(todo.reload).to be_done expect(todo.reload).to be_done
end end
it 'resolves associated alert' do
alert = create(:alert_management_alert, issue: issue, project: project)
close_issue
expect(alert.reload.resolved?).to eq(true)
end
it 'deletes milestone issue counters cache' do it 'deletes milestone issue counters cache' do
issue.update(milestone: create(:milestone, project: project)) issue.update(milestone: create(:milestone, project: project))
......
...@@ -705,4 +705,16 @@ RSpec.describe SystemNoteService do ...@@ -705,4 +705,16 @@ RSpec.describe SystemNoteService do
described_class.new_alert_issue(alert, alert.issue, author) described_class.new_alert_issue(alert, alert.issue, author)
end end
end end
describe '.closed_alert_issue' do
let(:alert) { build(:alert_management_alert, :with_issue) }
it 'calls AlertManagementService' do
expect_next_instance_of(SystemNotes::AlertManagementService) do |service|
expect(service).to receive(:closed_alert_issue).with(alert, alert.issue)
end
described_class.closed_alert_issue(alert, alert.issue, author)
end
end
end end
...@@ -32,4 +32,18 @@ RSpec.describe ::SystemNotes::AlertManagementService do ...@@ -32,4 +32,18 @@ RSpec.describe ::SystemNotes::AlertManagementService do
expect(subject.note).to eq("created issue #{issue.to_reference(project)} for this alert") expect(subject.note).to eq("created issue #{issue.to_reference(project)} for this alert")
end end
end end
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) }
it_behaves_like 'a system note' do
let(:action) { 'alert_issue_added' }
end
it 'has the appropriate message' do
expect(subject.note).to eq("resolved this alert due to closing issue #{issue.to_reference(project)}")
end
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