Commit ff11d3a2 authored by Nick Thomas's avatar Nick Thomas

Merge branch '215088-add-alert-url-to-issue-description' into 'master'

Add alert url into issue markdown

See merge request gitlab-org/gitlab!38649
parents e690390d 15672c17
......@@ -47,6 +47,13 @@ module AlertManagement
private
def details_url
::Gitlab::Routing.url_helpers.details_project_alert_management_url(
project,
alert.iid
)
end
attr_reader :alert, :project
def alerting_alert
......@@ -67,6 +74,7 @@ module AlertManagement
metadata << list_item('Monitoring tool', monitoring_tool) if monitoring_tool
metadata << list_item('Hosts', host_links) if hosts.any?
metadata << list_item('Description', description) if description.present?
metadata << list_item('GitLab alert', details_url) if details_url.present?
metadata.join(MARKDOWN_LINE_BREAK)
end
......
......@@ -77,6 +77,15 @@ module Projects
end
end
def details_url
return unless am_alert
::Gitlab::Routing.url_helpers.details_project_alert_management_url(
project,
am_alert.iid
)
end
private
def alert_title
......@@ -97,6 +106,7 @@ module Projects
metadata << list_item(service.label.humanize, service.value) if service
metadata << list_item(monitoring_tool.label.humanize, monitoring_tool.value) if monitoring_tool
metadata << list_item(hosts.label.humanize, host_links) if hosts
metadata << list_item('GitLab alert', details_url) if details_url
metadata.join(MARKDOWN_LINE_BREAK)
end
......
......@@ -5,13 +5,16 @@ module IncidentManagement
include Gitlab::Utils::StrongMemoize
include IncidentManagement::Settings
def initialize(project, params)
super(project, User.alert_bot, params)
attr_reader :alert
def initialize(project, alert)
super(project, User.alert_bot)
@alert = alert
end
def execute
return error('setting disabled') unless incident_management_setting.create_issue?
return error('invalid alert') unless alert.valid?
return error('invalid alert') unless alert_presenter.valid?
result = create_incident
return error(result.message, result.payload[:issue]) unless result.success?
......@@ -31,7 +34,7 @@ module IncidentManagement
end
def issue_title
alert.full_title
alert_presenter.full_title
end
def issue_description
......@@ -45,16 +48,16 @@ module IncidentManagement
end
def alert_summary
alert.issue_summary_markdown
alert_presenter.issue_summary_markdown
end
def alert_markdown
alert.alert_markdown
alert_presenter.alert_markdown
end
def alert
strong_memoize(:alert) do
Gitlab::Alerting::Alert.new(project: project, payload: params).present
def alert_presenter
strong_memoize(:alert_presenter) do
Gitlab::Alerting::Alert.for_alert_management_alert(project: project, alert: alert).present
end
end
......
......@@ -29,17 +29,9 @@ module IncidentManagement
AlertManagement::Alert.find_by_id(alert_id)
end
def parsed_payload(alert)
if alert.prometheus?
alert.payload
else
Gitlab::Alerting::NotificationPayloadParser.call(alert.payload.to_h, alert.project)
end
end
def create_issue_for(alert)
IncidentManagement::CreateIssueService
.new(alert.project, parsed_payload(alert))
.new(alert.project, alert)
.execute
end
......
---
title: Add alert url into incident issue markdown
merge_request: 38649
author:
type: added
......@@ -7,7 +7,17 @@ module Gitlab
include Gitlab::Utils::StrongMemoize
include Presentable
attr_accessor :project, :payload
attr_accessor :project, :payload, :am_alert
def self.for_alert_management_alert(project:, alert:)
params = if alert.prometheus?
alert.payload
else
Gitlab::Alerting::NotificationPayloadParser.call(alert.payload.to_h, alert.project)
end
self.new(project: project, payload: params, am_alert: alert)
end
def gitlab_alert
strong_memoize(:gitlab_alert) do
......
......@@ -4,6 +4,7 @@ require 'spec_helper'
RSpec.describe AlertManagement::AlertPresenter do
let_it_be(:project) { create(:project) }
let_it_be(:generic_payload) do
{
'title' => 'Alert title',
......@@ -12,10 +13,13 @@ RSpec.describe AlertManagement::AlertPresenter do
'runbook' => 'https://runbook.com'
}
end
let_it_be(:alert) do
build(:alert_management_alert, :with_description, :with_host, :with_service, :with_monitoring_tool, project: project, payload: generic_payload)
create(:alert_management_alert, :with_description, :with_host, :with_service, :with_monitoring_tool, project: project, payload: generic_payload)
end
let(:alert_url) { "http://localhost/#{project.full_path}/-/alert_management/#{alert.iid}/details" }
subject(:presenter) { described_class.new(alert) }
describe '#issue_description' do
......@@ -31,7 +35,8 @@ RSpec.describe AlertManagement::AlertPresenter do
**Service:** #{alert.service}#{markdown_line_break}
**Monitoring tool:** #{alert.monitoring_tool}#{markdown_line_break}
**Hosts:** #{alert.hosts.join(' ')}#{markdown_line_break}
**Description:** #{alert.description}
**Description:** #{alert.description}#{markdown_line_break}
**GitLab alert:** #{alert_url}
#### Alert Details
......
......@@ -16,10 +16,12 @@ RSpec.describe AlertManagement::PrometheusAlertPresenter do
}
end
let(:alert) do
let!(:alert) do
create(:alert_management_alert, :prometheus, project: project, payload: payload)
end
let(:alert_url) { "http://localhost/#{project.full_path}/-/alert_management/#{alert.iid}/details" }
subject(:presenter) { described_class.new(alert) }
describe '#issue_description' do
......@@ -33,7 +35,8 @@ RSpec.describe AlertManagement::PrometheusAlertPresenter do
**Start time:** #{presenter.start_time}#{markdown_line_break}
**Severity:** #{presenter.severity}#{markdown_line_break}
**full_query:** `vector(1)`#{markdown_line_break}
**Monitoring tool:** Prometheus
**Monitoring tool:** Prometheus#{markdown_line_break}
**GitLab alert:** #{alert_url}
#### Alert Details
......
......@@ -293,6 +293,19 @@ RSpec.describe Projects::Prometheus::AlertPresenter do
end
end
describe '#details_url' do
subject { presenter.details_url }
it { is_expected.to eq(nil) }
context 'alert management alert present' do
let_it_be(:am_alert) { create(:alert_management_alert, project: project) }
let(:alert) { create(:alerting_alert, project: project, payload: payload, am_alert: am_alert) }
it { is_expected.to eq("http://localhost/#{project.full_path}/-/alert_management/#{am_alert.iid}/details") }
end
end
context 'with gitlab alert' do
include_context 'gitlab alert'
......
......@@ -5,7 +5,6 @@ require 'spec_helper'
RSpec.describe IncidentManagement::CreateIssueService do
let(:project) { create(:project, :repository, :private) }
let_it_be(:user) { User.alert_bot }
let(:service) { described_class.new(project, alert_payload) }
let(:alert_starts_at) { Time.current }
let(:alert_title) { 'TITLE' }
let(:alert_annotations) { { title: alert_title } }
......@@ -17,8 +16,11 @@ RSpec.describe IncidentManagement::CreateIssueService do
)
end
let(:alert) { create(:alert_management_alert, :prometheus, project: project, payload: alert_payload) }
let(:service) { described_class.new(project, alert) }
let(:alert_presenter) do
Gitlab::Alerting::Alert.new(project: project, payload: alert_payload).present
Gitlab::Alerting::Alert.for_alert_management_alert(project: project, alert: alert).present
end
let!(:setting) do
......
......@@ -19,14 +19,14 @@ RSpec.describe IncidentManagement::ProcessAlertWorker do
allow(Gitlab::AppLogger).to receive(:warn).and_call_original
allow(IncidentManagement::CreateIssueService)
.to receive(:new).with(alert.project, parsed_payload)
.to receive(:new).with(alert.project, alert)
.and_call_original
end
shared_examples 'creates issue successfully' do
it 'creates an issue' do
expect(IncidentManagement::CreateIssueService)
.to receive(:new).with(alert.project, parsed_payload)
.to receive(:new).with(alert.project, alert)
expect { subject }.to change { Issue.count }.by(1)
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