Commit cf00a6bc authored by Peter Leitzen's avatar Peter Leitzen

Merge branch 'sy-integrate-generic-environments' into 'master'

Support environment for generic alerts in new alert payload class

See merge request gitlab-org/gitlab!40021
parents 947d0787 6b3a2158
......@@ -104,7 +104,20 @@ module Gitlab
def gitlab_fingerprint
strong_memoize(:gitlab_fingerprint) do
Gitlab::AlertManagement::Fingerprint.generate(plain_gitlab_fingerprint) if plain_gitlab_fingerprint
next unless plain_gitlab_fingerprint
Gitlab::AlertManagement::Fingerprint.generate(plain_gitlab_fingerprint)
end
end
def environment
strong_memoize(:environment) do
next unless environment_name
EnvironmentsFinder
.new(project, nil, { name: environment_name })
.find
.first
end
end
......
......@@ -8,6 +8,7 @@ module Gitlab
DEFAULT_TITLE = 'New: Incident'
DEFAULT_SEVERITY = 'critical'
attribute :environment_name, paths: 'gitlab_environment_name'
attribute :hosts, paths: 'hosts'
attribute :monitoring_tool, paths: 'monitoring_tool'
attribute :runbook, paths: 'runbook'
......
......@@ -49,14 +49,6 @@ module Gitlab
rescue URI::InvalidURIError, KeyError
end
def environment
return unless environment_name
EnvironmentsFinder.new(project, nil, { name: environment_name })
.find
&.first
end
def metrics_dashboard_url
return unless environment && full_query && title
......
......@@ -3,7 +3,7 @@
require 'spec_helper'
RSpec.describe Gitlab::AlertManagement::Payload::Base do
let_it_be(:project) { build_stubbed(:project) }
let_it_be(:project) { create(:project) }
let(:raw_payload) { {} }
let(:payload_class) { described_class }
......@@ -120,16 +120,9 @@ RSpec.describe Gitlab::AlertManagement::Payload::Base do
end
describe '#alert_params' do
let(:payload_class) do
Class.new(described_class) do
def title
'title'
end
def description
'description'
end
end
before do
allow(parsed_payload).to receive(:title).and_return('title')
allow(parsed_payload).to receive(:description).and_return('description')
end
subject { parsed_payload.alert_params }
......@@ -143,12 +136,10 @@ RSpec.describe Gitlab::AlertManagement::Payload::Base do
it { is_expected.to be_nil }
context 'when plain_gitlab_fingerprint is defined' do
let(:payload_class) do
Class.new(described_class) do
def plain_gitlab_fingerprint
'fingerprint'
end
end
before do
allow(parsed_payload)
.to receive(:plain_gitlab_fingerprint)
.and_return('fingerprint')
end
it 'returns a fingerprint' do
......@@ -156,4 +147,32 @@ RSpec.describe Gitlab::AlertManagement::Payload::Base do
end
end
end
describe '#environment' do
let_it_be(:environment) { create(:environment, project: project, name: 'production') }
subject { parsed_payload.environment }
before do
allow(parsed_payload).to receive(:environment_name).and_return(environment_name)
end
context 'without an environment name' do
let(:environment_name) { nil }
it { is_expected.to be_nil }
end
context 'with a non-matching environment name' do
let(:environment_name) { 'other_environment' }
it { is_expected.to be_nil }
end
context 'with a matching environment name' do
let(:environment_name) { 'production' }
it { is_expected.to eq(environment) }
end
end
end
......@@ -80,4 +80,10 @@ RSpec.describe Gitlab::AlertManagement::Payload::Generic do
is_expected.to eq(Digest::SHA1.hexdigest(plain_fingerprint))
end
end
describe '#environment_name' do
subject { parsed_payload.environment_name }
it_behaves_like 'parsable alert payload field', 'gitlab_environment_name'
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