Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
e90c1b4a
Commit
e90c1b4a
authored
Jun 15, 2020
by
Vitali Tatarintev
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Extract AlertPresenter from CreateAlertIssue
Extract presentation logic from `CreateAlertIssueService`
parent
e1c83c24
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
136 additions
and
57 deletions
+136
-57
app/models/alert_management/alert.rb
app/models/alert_management/alert.rb
+1
-0
app/presenters/alert_management/alert_presenter.rb
app/presenters/alert_management/alert_presenter.rb
+45
-0
app/services/alert_management/create_alert_issue_service.rb
app/services/alert_management/create_alert_issue_service.rb
+8
-49
spec/presenters/alert_management/alert_presenter_spec.rb
spec/presenters/alert_management/alert_presenter_spec.rb
+78
-0
spec/services/alert_management/create_alert_issue_service_spec.rb
...vices/alert_management/create_alert_issue_service_spec.rb
+4
-8
No files found.
app/models/alert_management/alert.rb
View file @
e90c1b4a
...
...
@@ -10,6 +10,7 @@ module AlertManagement
include
Sortable
include
Noteable
include
Gitlab
::
SQL
::
Pattern
include
Presentable
STATUSES
=
{
triggered:
0
,
...
...
app/presenters/alert_management/alert_presenter.rb
0 → 100644
View file @
e90c1b4a
# frozen_string_literal: true
module
AlertManagement
class
AlertPresenter
<
Gitlab
::
View
::
Presenter
::
Delegated
include
Gitlab
::
Utils
::
StrongMemoize
include
IncidentManagement
::
Settings
def
initialize
(
alert
,
_attributes
=
{})
super
@alert
=
alert
@project
=
alert
.
project
end
delegate
:alert_markdown
,
:issue_summary_markdown
,
to: :alerting_alert
def
issue_description
horizontal_line
=
"
\n\n
---
\n\n
"
[
issue_summary_markdown
,
alert_markdown
,
incident_management_setting
.
issue_template_content
].
compact
.
join
(
horizontal_line
)
end
private
attr_reader
:alert
,
:project
def
alert_payload
if
alert
.
prometheus?
alert
.
payload
else
Gitlab
::
Alerting
::
NotificationPayloadParser
.
call
(
alert
.
payload
.
to_h
)
end
end
def
alerting_alert
strong_memoize
(
:alertign_alert
)
do
Gitlab
::
Alerting
::
Alert
.
new
(
project:
project
,
payload:
alert_payload
).
present
end
end
end
end
app/services/alert_management/create_alert_issue_service.rb
View file @
e90c1b4a
...
...
@@ -17,7 +17,7 @@ module AlertManagement
return
error_no_permissions
unless
allowed?
return
error_issue_already_exists
if
alert
.
issue
result
=
create_issue
(
user
,
alert_payload
)
result
=
create_issue
@issue
=
result
.
payload
[
:issue
]
return
error
(
result
.
message
)
if
result
.
error?
...
...
@@ -36,7 +36,7 @@ module AlertManagement
user
.
can?
(
:create_issue
,
project
)
end
def
create_issue
(
user
,
alert_payload
)
def
create_issue
issue
=
do_create_issue
(
label_ids:
issue_label_ids
)
# Create an unlabelled issue if we couldn't create the issue
...
...
@@ -53,14 +53,6 @@ module AlertManagement
success
end
def
alert_payload
if
alert
.
prometheus?
alert
.
payload
else
Gitlab
::
Alerting
::
NotificationPayloadParser
.
call
(
alert
.
payload
.
to_h
)
end
end
def
update_alert_issue_id
alert
.
update
(
issue_id:
issue
.
id
)
end
...
...
@@ -85,24 +77,16 @@ module AlertManagement
Issues
::
CreateService
.
new
(
project
,
user
,
title:
issue_
title
,
description:
issue_description
,
title:
alert_presenter
.
title
,
description:
alert_presenter
.
issue_description
,
**
params
).
execute
end
def
issue_title
alert_presenter
.
full_title
end
def
issue_description
horizontal_line
=
"
\n\n
---
\n\n
"
[
alert_summary
,
alert_markdown
,
issue_template_content
].
compact
.
join
(
horizontal_line
)
def
alert_presenter
strong_memoize
(
:alert_presenter
)
do
alert
.
present
end
end
def
issue_label_ids
...
...
@@ -117,31 +101,6 @@ module AlertManagement
.
execute
end
def
alert_summary
alert_presenter
.
issue_summary_markdown
end
def
alert_markdown
alert_presenter
.
alert_markdown
end
def
alert_presenter
strong_memoize
(
:alert_presenter
)
do
Gitlab
::
Alerting
::
Alert
.
new
(
project:
project
,
payload:
alert_payload
).
present
end
end
def
issue_template_content
incident_management_setting
.
issue_template_content
end
def
incident_management_setting
strong_memoize
(
:incident_management_setting
)
do
project
.
incident_management_setting
||
project
.
build_incident_management_setting
end
end
def
issue_errors
(
issue
)
issue
.
errors
.
full_messages
.
to_sentence
end
...
...
spec/presenters/alert_management/alert_presenter_spec.rb
0 → 100644
View file @
e90c1b4a
# frozen_string_literal: true
require
'spec_helper'
RSpec
.
describe
AlertManagement
::
AlertPresenter
do
let_it_be
(
:project
)
{
create
(
:project
)
}
let_it_be
(
:generic_payload
)
do
{
'title'
=>
'Alert title'
,
'start_time'
=>
'2020-04-27T10:10:22.265949279Z'
,
'custom'
=>
{
'param'
=>
73
}
}
end
let_it_be
(
:generic_alert
)
do
create
(
:alert_management_alert
,
project:
project
,
payload:
generic_payload
)
end
let_it_be
(
:prometheus_payload
)
do
{
'annotations'
=>
{
'title'
=>
'Alert title'
,
'gitlab_incident_markdown'
=>
'**`markdown example`**'
},
'startsAt'
=>
'2020-04-27T10:10:22.265949279Z'
,
'generatorURL'
=>
'http://8d467bd4607a:9090/graph?g0.expr=vector%281%29&g0.tab=1'
}
end
let_it_be
(
:prometheus_alert
)
do
create
(
:alert_management_alert
,
:prometheus
,
project:
project
,
payload:
prometheus_payload
)
end
let
(
:alert
)
{
generic_alert
}
let
(
:presenter
)
{
described_class
.
new
(
alert
)
}
describe
'#issue_description'
do
let
(
:markdown_line_break
)
{
' '
}
context
'with generic alert'
do
let
(
:alert
)
{
generic_alert
}
let
(
:parsed_payload
)
{
Gitlab
::
Alerting
::
NotificationPayloadParser
.
call
(
generic_payload
.
to_h
)
}
let
(
:alert_presenter
)
{
Gitlab
::
Alerting
::
Alert
.
new
(
project:
project
,
payload:
parsed_payload
).
present
}
it
'returns an alert issue description'
do
expect
(
presenter
.
issue_description
).
to
eq
(
<<~
MARKDOWN
.
chomp
#### Summary
**Start time:**
#{
alert_presenter
.
start_time
}
#### Alert Details
**custom.param:** 73
#{
markdown_line_break
}
**severity:** critical
MARKDOWN
)
end
end
context
'with prometheus alert'
do
let
(
:alert
)
{
prometheus_alert
}
let
(
:alert_presenter
)
{
Gitlab
::
Alerting
::
Alert
.
new
(
project:
project
,
payload:
prometheus_payload
).
present
}
it
'returns an alert issue description'
do
expect
(
presenter
.
issue_description
).
to
eq
(
<<~
MARKDOWN
.
chomp
#### Summary
**Start time:**
#{
alert_presenter
.
start_time
}#{
markdown_line_break
}
**full_query:** `vector(1)`
---
**`markdown example`**
MARKDOWN
)
end
end
end
end
spec/services/alert_management/create_alert_issue_service_spec.rb
View file @
e90c1b4a
...
...
@@ -8,10 +8,6 @@ RSpec.describe AlertManagement::CreateAlertIssueService do
let_it_be
(
:project
)
{
create
(
:project
,
group:
group
)
}
let_it_be
(
:payload
)
do
{
'title'
=>
'Alert title'
,
'annotations'
=>
{
'title'
=>
'Alert title'
},
'startsAt'
=>
'2020-04-27T10:10:22.265949279Z'
,
'generatorURL'
=>
'http://8d467bd4607a:9090/graph?g0.expr=vector%281%29&g0.tab=1'
}
...
...
@@ -61,7 +57,7 @@ RSpec.describe AlertManagement::CreateAlertIssueService do
end
it
'sets the issue title'
do
expect
(
created_issue
.
title
).
to
eq
(
alert
_presenter
.
title
)
expect
(
created_issue
.
title
).
to
eq
(
alert
.
title
)
end
it
'sets the issue description'
do
...
...
@@ -187,11 +183,11 @@ RSpec.describe AlertManagement::CreateAlertIssueService do
end
context
'when issue cannot be created'
do
let
(
:alert
)
{
prometheus
_alert
}
let
(
:alert
)
{
generic
_alert
}
before
do
#
set invalid payload for Prometheus
alert
alert
.
update
!
(
payload:
{}
)
#
Invalid
alert
alert
.
update
_columns
(
title:
''
)
end
it
'has an unsuccessful status'
do
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment