Commit 2bc020a1 authored by Peter Leitzen's avatar Peter Leitzen

Merge branch 'ck3g-get-rid-of-alert-manegement-alert-status-events-constant' into 'master'

Get rid of AlertManagement::Alert::STATUS_EVENTS constant

See merge request gitlab-org/gitlab!44309
parents ca22188e 272e47d5
...@@ -21,13 +21,6 @@ module AlertManagement ...@@ -21,13 +21,6 @@ module AlertManagement
ignored: 3 ignored: 3
}.freeze }.freeze
STATUS_EVENTS = {
triggered: :trigger,
acknowledged: :acknowledge,
resolved: :resolve,
ignored: :ignore
}.freeze
OPEN_STATUSES = [ OPEN_STATUSES = [
:triggered, :triggered,
:acknowledged :acknowledged
...@@ -190,6 +183,15 @@ module AlertManagement ...@@ -190,6 +183,15 @@ module AlertManagement
reference.to_i > 0 && reference.to_i <= Gitlab::Database::MAX_INT_VALUE reference.to_i > 0 && reference.to_i <= Gitlab::Database::MAX_INT_VALUE
end end
def status_event_for(status)
self.class.state_machines[:status].events.transitions_for(self, to: status.to_s.to_sym).first&.event
end
def change_status_to(new_status)
event = status_event_for(new_status)
event && fire_status_event(event)
end
def prometheus? def prometheus?
monitoring_tool == Gitlab::AlertManagement::Payload::MONITORING_TOOLS[:prometheus] monitoring_tool == Gitlab::AlertManagement::Payload::MONITORING_TOOLS[:prometheus]
end end
......
...@@ -112,7 +112,7 @@ module AlertManagement ...@@ -112,7 +112,7 @@ module AlertManagement
def filter_status def filter_status
return unless params[:status] return unless params[:status]
status_event = AlertManagement::Alert::STATUS_EVENTS[status_key] status_event = alert.status_event_for(status_key)
unless status_event unless status_event
param_errors << _('Invalid status') param_errors << _('Invalid status')
......
...@@ -133,7 +133,7 @@ RSpec.describe AlertManagement::Alert do ...@@ -133,7 +133,7 @@ RSpec.describe AlertManagement::Alert do
let(:new_alert) { build(:alert_management_alert, new_status, fingerprint: fingerprint, project: project) } let(:new_alert) { build(:alert_management_alert, new_status, fingerprint: fingerprint, project: project) }
before do before do
existing_alert.public_send(described_class::STATUS_EVENTS[existing_status]) existing_alert.change_status_to(existing_status)
end end
if params[:valid] if params[:valid]
...@@ -453,4 +453,54 @@ RSpec.describe AlertManagement::Alert do ...@@ -453,4 +453,54 @@ RSpec.describe AlertManagement::Alert do
expect { subject }.to change { alert.events }.by(1) expect { subject }.to change { alert.events }.by(1)
end end
end end
describe '#status_event_for' do
using RSpec::Parameterized::TableSyntax
where(:for_status, :event) do
:triggered | :trigger
'triggered' | :trigger
:acknowledged | :acknowledge
'acknowledged' | :acknowledge
:resolved | :resolve
'resolved' | :resolve
:ignored | :ignore
'ignored' | :ignore
:unknown | nil
nil | nil
'' | nil
1 | nil
end
with_them do
let(:alert) { build(:alert_management_alert, project: project) }
it 'returns event by status name' do
expect(alert.status_event_for(for_status)).to eq(event)
end
end
end
describe '#change_status_to' do
let_it_be_with_reload(:alert) { create(:alert_management_alert, project: project) }
context 'with valid statuses' do
it 'changes the status to triggered' do
alert.acknowledge! # change to non-triggered status
expect { alert.change_status_to(:triggered) }.to change { alert.triggered? }.to(true)
end
%i(acknowledged resolved ignored).each do |status|
it "changes the status to #{status}" do
expect { alert.change_status_to(status) }.to change { alert.public_send(:"#{status}?") }.to(true)
end
end
end
context 'with invalid status' do
it 'does not change the current status' do
expect { alert.change_status_to(nil) }.not_to change { alert.status }
end
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