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
e1f3c35b
Commit
e1f3c35b
authored
6 years ago
by
jurgenhaas
Browse files
Options
Downloads
Patches
Plain Diff
#6
Functional Gitlab plugin
parent
7e9b9b2f
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
tasks/alerta.yml
+13
-0
13 additions, 0 deletions
tasks/alerta.yml
templates/gitlab/alerta_gitlab.py
+58
-0
58 additions, 0 deletions
templates/gitlab/alerta_gitlab.py
templates/gitlab/setup.py
+26
-0
26 additions, 0 deletions
templates/gitlab/setup.py
with
97 additions
and
0 deletions
tasks/alerta.yml
+
13
−
0
View file @
e1f3c35b
...
...
@@ -99,3 +99,16 @@
minute
:
'
0'
job
:
'
/usr/local/bin/alerta
housekeeping'
tags
:
'
cron'
-
name
:
"
Copy
Gitlab
plugin"
template
:
src
:
'
gitlab/{{
item
}}'
dest
:
'
/tmp/{{
item
}}'
with_items
:
-
'
alerta_gitlab.py'
-
'
setup.py'
-
name
:
"
Install
Gitlab
plugin"
shell
:
'
python
setup.py
install'
args
:
chdir
:
'
/tmp'
This diff is collapsed.
Click to expand it.
templates/gitlab/alerta_gitlab.py
0 → 100644
+
58
−
0
View file @
e1f3c35b
import
logging
import
requests
import
urllib.parse
from
alerta.plugins
import
PluginBase
,
app
LOG
=
logging
.
getLogger
(
'
alerta.plugins.gitlab
'
)
ALERTA_URL
=
'
https://{{ alerta_domain }}
'
GITLAB_URL
=
'
{{ gitlab_issue.url }}/api/v4
'
GITLAB_ACCESS_TOKEN
=
'
{{ gitlab_issue.token }}
'
class
GitlabIssue
(
PluginBase
):
def
__init__
(
self
):
self
.
headers
=
{
'
Private-Token
'
:
GITLAB_ACCESS_TOKEN
}
def
pre_receive
(
self
,
alert
):
return
alert
def
post_receive
(
self
,
alert
):
return
alert
def
status_change
(
self
,
alert
,
status
,
text
):
return
alert
,
status
,
text
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
)
url
=
BASE_URL
+
'
?search={}
'
.
format
(
alert
.
resource
)
r
=
requests
.
get
(
url
,
headers
=
self
.
headers
)
for
p
in
r
.
json
():
if
p
.
get
(
'
namespace
'
).
get
(
'
full_path
'
)
==
namespace
:
pid
=
p
.
get
(
'
id
'
,
None
)
break
if
pid
:
BASE_URL
=
'
{}/{}
'
.
format
(
BASE_URL
,
pid
)
if
action
==
'
createIssue
'
:
if
'
issue_iid
'
not
in
alert
.
attributes
:
query
=
urllib
.
parse
.
urlencode
({
'
title
'
:
alert
.
text
,
'
description
'
:
'
{}/#/alert/{}
'
.
format
(
ALERTA_URL
,
alert
.
id
)
})
url
=
'
{}/issues?{}
'
.
format
(
BASE_URL
,
query
)
r
=
requests
.
post
(
url
,
headers
=
self
.
headers
)
alert
.
attributes
[
'
issue_iid
'
]
=
r
.
json
().
get
(
'
iid
'
,
None
)
alert
.
attributes
[
'
gitlabUrl
'
]
=
'
<a href=
"
{}
"
target=
"
_blank
"
>Issue #{}</a>
'
.
format
(
r
.
json
().
get
(
'
web_url
'
,
None
),
r
.
json
().
get
(
'
iid
'
,
None
)
)
return
alert
,
action
,
text
This diff is collapsed.
Click to expand it.
templates/gitlab/setup.py
0 → 100644
+
26
−
0
View file @
e1f3c35b
from
setuptools
import
find_packages
,
setup
version
=
'
1.0.0
'
setup
(
name
=
'
alerta-gitlab
'
,
version
=
version
,
description
=
'
LakeDrops Alerta plugin for GitLab Issues
'
,
url
=
'
https://gitlab.lakedrops.com/ansible-roles/alerta
'
,
license
=
'
MIT
'
,
author
=
'
Jürgen Haas
'
,
author_email
=
'
juergen.haas@lakedrops.com
'
,
packages
=
find_packages
(),
py_modules
=
[
'
alerta_gitlab
'
],
install_requires
=
[
'
requests
'
],
include_package_data
=
True
,
zip_safe
=
True
,
entry_points
=
{
'
alerta.plugins
'
:
[
'
gitlab = alerta_gitlab:GitlabIssue
'
]
}
)
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