Commit 24d4cebd authored by Vitali Tatarintev's avatar Vitali Tatarintev

Extract IncidentIssueDescription class

Extract
`Gitlab::IncidentManagement::PagerDuty::IncidentIssueDescription`
from
`IncidentManagement::PagerDuty::CreateIncidentIssueService`
parent 83b673e3
......@@ -54,44 +54,7 @@ module IncidentManagement
end
def issue_description
markdown_line_break = ' '
<<~MARKDOWN.chomp
**Incident:** #{markdown_incident}#{markdown_line_break}
**Incident number:** #{incident_payload['incident_number']}#{markdown_line_break}
**Urgency:** #{incident_payload['urgency']}#{markdown_line_break}
**Status:** #{incident_payload['status']}#{markdown_line_break}
**Incident key:** #{incident_payload['incident_key']}#{markdown_line_break}
**Created at:** #{markdown_incident_created_at}#{markdown_line_break}
**Assignees:** #{markdown_assignees.join(', ')}#{markdown_line_break}
**Impacted services:** #{markdown_impacted_services.join(', ')}
MARKDOWN
end
def markdown_incident
"[#{incident_payload['title']}](#{incident_payload['url']})"
end
def incident_created_at
Time.parse(incident_payload['created_at'])
rescue
Time.current
end
def markdown_incident_created_at
incident_created_at.strftime('%d %B %Y, %-l:%M%p (%Z)')
end
def markdown_assignees
Array(incident_payload['assignees']).map do |assignee|
"[#{assignee['summary']}](#{assignee['url']})"
end
end
def markdown_impacted_services
Array(incident_payload['impacted_services']).map do |is|
"[#{is['summary']}](#{is['url']})"
end
Gitlab::IncidentManagement::PagerDuty::IncidentIssueDescription.new(incident_payload).to_s
end
def success(issue)
......
# frozen_string_literal: true
module Gitlab
module IncidentManagement
module PagerDuty
class IncidentIssueDescription
def initialize(incident_payload)
@incident_payload = incident_payload
end
def to_s
markdown_line_break = " \n"
[
"**Incident:** #{markdown_incident}",
"**Incident number:** #{incident_payload['incident_number']}",
"**Urgency:** #{incident_payload['urgency']}",
"**Status:** #{incident_payload['status']}",
"**Incident key:** #{incident_payload['incident_key']}",
"**Created at:** #{markdown_incident_created_at}",
"**Assignees:** #{markdown_assignees.join(', ')}",
"**Impacted services:** #{markdown_impacted_services.join(', ')}"
].join(markdown_line_break)
end
private
attr_reader :incident_payload
def markdown_incident
markdown_link(incident_payload['title'], incident_payload['url'])
end
def incident_created_at
Time.parse(incident_payload['created_at'])
rescue
Time.current.utc # PagerDuty provides time in UTC
end
def markdown_incident_created_at
incident_created_at.strftime('%d %B %Y, %-l:%M%p (%Z)')
end
def markdown_assignees
Array(incident_payload['assignees']).map do |assignee|
markdown_link(assignee['summary'], assignee['url'])
end
end
def markdown_impacted_services
Array(incident_payload['impacted_services']).map do |is|
markdown_link(is['summary'], is['url'])
end
end
def markdown_link(label, url)
return label if url.blank?
"[#{label}](#{url})"
end
end
end
end
end
......@@ -124,7 +124,7 @@ module Gitlab
issues_created_manually_from_alerts: issues_created_manually_from_alerts,
incident_issues: alert_bot_incident_count,
alert_bot_incident_issues: alert_bot_incident_count,
incident_labeled_issues: count(::Issue.with_label_attributes(IncidentManagement::CreateIncidentLabelService::LABEL_PROPERTIES)),
incident_labeled_issues: count(::Issue.with_label_attributes(::IncidentManagement::CreateIncidentLabelService::LABEL_PROPERTIES)),
keys: count(Key),
label_lists: count(List.label),
lfs_objects: count(LfsObject),
......
# frozen_string_literal: true
require 'fast_spec_helper'
require 'timecop'
RSpec.describe Gitlab::IncidentManagement::PagerDuty::IncidentIssueDescription do
describe '#to_s' do
let(:markdown_line_break) { ' ' }
let(:created_at) { '2017-09-26T15:14:36Z' }
let(:assignees) do
[{ 'summary' => 'Laura Haley', 'url' => 'https://webdemo.pagerduty.com/users/P553OPV' }]
end
let(:impacted_services) do
[{ 'summary' => 'Production XDB Cluster', 'url' => 'https://webdemo.pagerduty.com/services/PN49J75' }]
end
let(:incident_payload) do
{
'url' => 'https://webdemo.pagerduty.com/incidents/PRORDTY',
'incident_number' => 33,
'title' => 'My new incident',
'status' => 'triggered',
'created_at' => created_at,
'urgency' => 'high',
'incident_key' => 'SOME-KEY',
'assignees' => assignees,
'impacted_services' => impacted_services
}
end
subject(:to_s) { described_class.new(incident_payload).to_s }
it 'returns description' do
expect(to_s).to eq(
<<~MARKDOWN.chomp
**Incident:** [My new incident](https://webdemo.pagerduty.com/incidents/PRORDTY)#{markdown_line_break}
**Incident number:** 33#{markdown_line_break}
**Urgency:** high#{markdown_line_break}
**Status:** triggered#{markdown_line_break}
**Incident key:** SOME-KEY#{markdown_line_break}
**Created at:** 26 September 2017, 3:14PM (UTC)#{markdown_line_break}
**Assignees:** [Laura Haley](https://webdemo.pagerduty.com/users/P553OPV)#{markdown_line_break}
**Impacted services:** [Production XDB Cluster](https://webdemo.pagerduty.com/services/PN49J75)
MARKDOWN
)
end
context 'when created_at is missing' do
let(:created_at) { nil }
it 'description contains current time in UTC' do
Timecop.freeze do
now = Time.current.utc.strftime('%d %B %Y, %-l:%M%p (%Z)')
expect(to_s).to include(
<<~MARKDOWN.chomp
**Created at:** #{now}#{markdown_line_break}
MARKDOWN
)
end
end
end
context 'when there are several assignees' do
let(:assignees) do
[
{ 'summary' => 'Laura Haley', 'url' => 'https://laura.pagerduty.com' },
{ 'summary' => 'John Doe', 'url' => 'https://john.pagerduty.com' }
]
end
it 'assignees is a list of links' do
expect(to_s).to include(
<<~MARKDOWN.chomp
**Assignees:** [Laura Haley](https://laura.pagerduty.com), [John Doe](https://john.pagerduty.com)#{markdown_line_break}
MARKDOWN
)
end
end
context 'when there are several impacted services' do
let(:impacted_services) do
[
{ 'summary' => 'XDB Cluster', 'url' => 'https://xdb.pagerduty.com' },
{ 'summary' => 'BRB Cluster', 'url' => 'https://brb.pagerduty.com' }
]
end
it 'impacted services is a list of links' do
expect(to_s).to include(
<<~MARKDOWN.chomp
**Impacted services:** [XDB Cluster](https://xdb.pagerduty.com), [BRB Cluster](https://brb.pagerduty.com)
MARKDOWN
)
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