Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
alerta
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Service Desk
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ansible
Roles
alerta
Commits
16f6013c
Commit
16f6013c
authored
5 years ago
by
jurgenhaas
Browse files
Options
Downloads
Patches
Plain Diff
ansible-roles/alerta#9 Utilize GitLab python library
parent
ffc14076
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
tasks/alerta.yml
+1
-0
1 addition, 0 deletions
tasks/alerta.yml
templates/gitlab/alerta_gitlab.py
+13
-24
13 additions, 24 deletions
templates/gitlab/alerta_gitlab.py
with
14 additions
and
24 deletions
tasks/alerta.yml
+
1
−
0
View file @
16f6013c
...
...
@@ -21,6 +21,7 @@
-
'
alerta-server'
-
'
alerta'
-
'
uwsgi'
-
'
python-gitlab'
-
name
:
"
Remove
directories
for
web
UI"
file
:
...
...
This diff is collapsed.
Click to expand it.
templates/gitlab/alerta_gitlab.py
+
13
−
24
View file @
16f6013c
import
gitlab
import
json
import
logging
import
requests
...
...
@@ -7,7 +8,7 @@ from alerta.plugins import PluginBase, app
LOG
=
logging
.
getLogger
(
'
alerta.plugins
'
)
ALERTA_URL
=
'
https://{{ alerta_domain }}
'
GITLAB_URL
=
'
{{ gitlab_issue.url }}
/api/v4
'
GITLAB_URL
=
'
{{ gitlab_issue.url }}
'
GITLAB_ACCESS_TOKEN
=
'
{{ gitlab_issue.token }}
'
...
...
@@ -30,21 +31,13 @@ class GitlabIssue(PluginBase):
def
take_action
(
self
,
alert
,
action
,
text
,
**
kwargs
):
"""
should return internal id of external system
"""
namespace
=
'
ansible-inventories/hosts/{}
'
.
format
(
alert
.
environment
)
base_url
=
'
{}/projects
'
.
format
(
GITLAB_URL
)
LOG
.
info
(
'
Create GitLab issue for %s
'
%
alert
.
environment
)
git
=
gitlab
.
Gitlab
(
GITLAB_URL
,
GITLAB_ACCESS_TOKEN
,
ssl_verify
=
True
)
# type: gitlab
git
.
auth
()
project
=
git
.
projects
.
get
(
'
ansible-inventories/hosts/{}/{}
'
.
format
(
alert
.
environment
,
alert
.
origin
.
split
(
'
/
'
).
pop
()))
origin
=
alert
.
origin
.
split
(
'
/
'
).
pop
()
url
=
base_url
+
'
?search={}
'
.
format
(
origin
)
r
=
requests
.
get
(
url
,
headers
=
self
.
headers
)
pid
=
False
for
p
in
r
.
json
():
if
p
.
get
(
'
namespace
'
).
get
(
'
full_path
'
)
==
namespace
:
pid
=
p
.
get
(
'
id
'
,
None
)
break
LOG
.
info
(
'
Create GitLab issue for %s / %s / %s
'
%
(
alert
.
environment
,
origin
,
pid
))
if
pid
:
project_url
=
'
{}/{}
'
.
format
(
base_url
,
pid
)
if
project
:
LOG
.
info
(
'
Project ID %s
'
%
project
.
id
)
if
action
==
'
createIssue
'
:
if
'
issue_iid
'
not
in
alert
.
attributes
:
...
...
@@ -54,18 +47,14 @@ class GitlabIssue(PluginBase):
except
Exception
as
e
:
LOG
.
info
(
'
Exception %s
'
%
str
(
e
))
raw_data
=
alert
.
raw_data
query
=
urllib
.
parse
.
urlencod
e
({
issue
=
project
.
issues
.
creat
e
({
'
title
'
:
alert
.
text
[
0
:
128
],
'
description
'
:
'
{}/alert/{}
\n\n
Service: {}
\n\n
Origin: {}
\n\n
```
\n
{}
\n
```
\n
'
.
format
(
ALERTA_URL
,
alert
.
id
,
alert
.
service
,
alert
.
origin
,
raw_data
)
})
url
=
'
{}/issues?{}
'
.
format
(
project_url
,
query
)
#LOG.info('Post to %s' % url)
r
=
requests
.
post
(
url
,
headers
=
self
.
headers
)
#LOG.info('Response %s' % r.headers)
alert
.
attributes
[
'
issue_iid
'
]
=
r
.
json
().
get
(
'
iid
'
,
None
)
})
# type: gitlab.Issue
alert
.
attributes
[
'
issue_iid
'
]
=
issue
.
id
alert
.
attributes
[
'
gitlabUrl
'
]
=
'
<a href=
"
{}
"
target=
"
_blank
"
>Issue #{}</a>
'
.
format
(
r
.
json
().
get
(
'
web_url
'
,
None
)
,
r
.
json
().
get
(
'
iid
'
,
None
)
issue
.
web_url
,
issue
.
iid
)
return
alert
,
action
,
text
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment