Commit 10438b18 authored by Gabriel Mazetto's avatar Gabriel Mazetto

Merge branch '334713-resolve-manually-created-incident-on-close' into 'master'

Resolve escalation status when incident is closed

See merge request gitlab-org/gitlab!72614
parents bc0fe79a bcf9daa8
......@@ -62,6 +62,7 @@ module Issues
def perform_incident_management_actions(issue)
resolve_alert(issue)
resolve_incident(issue)
end
def close_external_issue(issue, closed_via)
......@@ -91,6 +92,14 @@ module Issues
end
end
def resolve_incident(issue)
return unless issue.incident?
status = issue.incident_management_issuable_escalation_status || issue.build_incident_management_issuable_escalation_status
SystemNoteService.resolve_incident_status(issue, current_user) if status.resolve
end
def store_first_mentioned_in_commit_at(issue, merge_request, max_commit_lookup: 100)
metrics = issue.metrics
return if metrics.nil? || metrics.first_mentioned_in_commit_at
......
......@@ -327,6 +327,10 @@ module SystemNoteService
::SystemNotes::IncidentService.new(noteable: incident, project: incident.project, author: author).change_incident_severity
end
def resolve_incident_status(incident, author)
::SystemNotes::IncidentService.new(noteable: incident, project: incident.project, author: author).resolve_incident_status
end
def log_resolving_alert(alert, monitoring_tool)
::SystemNotes::AlertManagementService.new(noteable: alert, project: alert.project).log_resolving_alert(monitoring_tool)
end
......
......@@ -25,5 +25,11 @@ module SystemNotes
)
end
end
def resolve_incident_status
body = 'changed the status to **Resolved** by closing the incident'
create_note(NoteSummary.new(noteable, project, author, body, action: 'status'))
end
end
end
......@@ -83,6 +83,14 @@ RSpec.describe Issues::CloseService do
service.execute(issue)
end
it 'does not change escalation status' do
resolved = IncidentManagement::Escalatable::STATUSES[:resolved]
expect { service.execute(issue) }
.to not_change { IncidentManagement::IssuableEscalationStatus.where(issue: issue).count }
.and not_change { IncidentManagement::IssuableEscalationStatus.where(status: resolved).count }
end
context 'issue is incident type' do
let(:issue) { create(:incident, project: project) }
let(:current_user) { user }
......@@ -90,6 +98,40 @@ RSpec.describe Issues::CloseService do
subject { service.execute(issue) }
it_behaves_like 'an incident management tracked event', :incident_management_incident_closed
it 'creates a new escalation resolved escalation status', :aggregate_failures do
expect { service.execute(issue) }.to change { IncidentManagement::IssuableEscalationStatus.where(issue: issue).count }.by(1)
expect(issue.incident_management_issuable_escalation_status).to be_resolved
end
context 'when there is an escalation status' do
before do
create(:incident_management_issuable_escalation_status, issue: issue)
end
it 'changes escalations status to resolved' do
expect { service.execute(issue) }.to change { issue.incident_management_issuable_escalation_status.reload.resolved? }.to(true)
end
it 'adds a system note', :aggregate_failures do
expect { service.execute(issue) }.to change { issue.notes.count }.by(1)
new_note = issue.notes.last
expect(new_note.note).to eq('changed the status to **Resolved** by closing the incident')
expect(new_note.author).to eq(user)
end
context 'when the escalation status did not change to resolved' do
let(:escalation_status) { instance_double('IncidentManagement::IssuableEscalationStatus', resolve: false) }
it 'does not create a system note' do
allow(issue).to receive(:incident_management_issuable_escalation_status).and_return(escalation_status)
expect { service.execute(issue) }.not_to change { issue.notes.count }
end
end
end
end
end
......@@ -237,7 +279,7 @@ RSpec.describe Issues::CloseService do
it 'verifies the number of queries' do
recorded = ActiveRecord::QueryRecorder.new { close_issue }
expected_queries = 27
expected_queries = 32
expect(recorded.count).to be <= expected_queries
expect(recorded.cached_count).to eq(0)
......
......@@ -781,6 +781,18 @@ RSpec.describe SystemNoteService do
end
end
describe '.resolve_incident_status' do
let(:incident) { build(:incident, :closed) }
it 'calls IncidentService' do
expect_next_instance_of(SystemNotes::IncidentService) do |service|
expect(service).to receive(:resolve_incident_status)
end
described_class.resolve_incident_status(incident, author)
end
end
describe '.log_resolving_alert' do
let(:alert) { build(:alert_management_alert) }
let(:monitoring_tool) { 'Prometheus' }
......
......@@ -56,4 +56,14 @@ RSpec.describe ::SystemNotes::IncidentService do
end
end
end
describe '#resolve_incident_status' do
subject(:resolve_incident_status) { described_class.new(noteable: noteable, project: project, author: author).resolve_incident_status }
it 'creates a new note about resolved incident', :aggregate_failures do
expect { resolve_incident_status }.to change { noteable.notes.count }.by(1)
expect(noteable.notes.last.note).to eq('changed the status to **Resolved** by closing the incident')
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