Commit e606683f authored by Sean Arnold's avatar Sean Arnold

Add alert details into issue markdown

- Use alert for create issue service
- Have Alerting::Alert take an alert
- Update tests
parent e1724883
......@@ -47,6 +47,14 @@ module AlertManagement
private
def details_url
::Gitlab::Routing.url_helpers.details_namespace_project_alert_management_url(
id: alert,
project_id: project,
namespace_id: project.namespace
)
end
attr_reader :alert, :project
def alerting_alert
......@@ -67,6 +75,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,16 @@ module Projects
end
end
def details_url
return unless alert
::Gitlab::Routing.url_helpers.details_namespace_project_alert_management_url(
id: alert,
project_id: project,
namespace_id: project.namespace
)
end
private
def alert_title
......@@ -97,6 +107,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 presented_alert.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
presented_alert.full_title
end
def issue_description
......@@ -45,16 +48,16 @@ module IncidentManagement
end
def alert_summary
alert.issue_summary_markdown
presented_alert.issue_summary_markdown
end
def alert_markdown
alert.alert_markdown
presented_alert.alert_markdown
end
def alert
strong_memoize(:alert) do
Gitlab::Alerting::Alert.new(project: project, payload: params).present
def presented_alert
strong_memoize(:presented_alert) 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
......
......@@ -7,7 +7,17 @@ module Gitlab
include Gitlab::Utils::StrongMemoize
include Presentable
attr_accessor :project, :payload
attr_accessor :project, :payload, :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, alert: alert)
end
def gitlab_alert
strong_memoize(:gitlab_alert) do
......
......@@ -16,6 +16,8 @@ RSpec.describe AlertManagement::AlertPresenter do
build(: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.id}/details" }
subject(:presenter) { described_class.new(alert) }
describe '#issue_description' do
......@@ -31,7 +33,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
......
......@@ -20,6 +20,8 @@ RSpec.describe AlertManagement::PrometheusAlertPresenter do
create(:alert_management_alert, :prometheus, project: project, payload: payload)
end
let(:alert_url) { "http://localhost/#{project.full_path}/-/alert_management/#{alert.id}/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
......
......@@ -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
......
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