Skip to content
Snippets Groups Projects
Commit 8ab6490c authored by jurgenhaas's avatar jurgenhaas
Browse files

customer/bitegra/mobimo/drupal#240 Implement support for an optional project...

customer/bitegra/mobimo/drupal#240 Implement support for an optional project ID when sending alert from fluentd to alerta
parent e020924b
Branches
No related tags found
No related merge requests found
import gitlab
import json import json
import logging import logging
from alerta.plugins import PluginBase, app
import gitlab
from alerta.plugins import PluginBase
from gitlab.v4.objects import Issue
LOG = logging.getLogger('alerta.plugins') LOG = logging.getLogger('alerta.plugins')
...@@ -15,15 +17,16 @@ class GitlabIssue(PluginBase): ...@@ -15,15 +17,16 @@ class GitlabIssue(PluginBase):
name = 'alerta-gitlab' name = 'alerta-gitlab'
def __init__(self): def __init__(self):
super().__init__()
self.headers = {'Private-Token': GITLAB_ACCESS_TOKEN} self.headers = {'Private-Token': GITLAB_ACCESS_TOKEN}
def pre_receive(self, alert): def pre_receive(self, alert, **kwargs):
return alert return alert
def post_receive(self, alert): def post_receive(self, alert, **kwargs):
return alert return alert
def status_change(self, alert, status, text): def status_change(self, alert, status, text, **kwargs):
return alert, status, text return alert, status, text
def take_action(self, alert, action, text, **kwargs): def take_action(self, alert, action, text, **kwargs):
...@@ -33,7 +36,11 @@ class GitlabIssue(PluginBase): ...@@ -33,7 +36,11 @@ class GitlabIssue(PluginBase):
LOG.info('Create GitLab issue for %s' % alert.environment) LOG.info('Create GitLab issue for %s' % alert.environment)
git = gitlab.Gitlab(GITLAB_URL, GITLAB_ACCESS_TOKEN, ssl_verify=True) # type: gitlab git = gitlab.Gitlab(GITLAB_URL, GITLAB_ACCESS_TOKEN, ssl_verify=True) # type: gitlab
git.auth() git.auth()
project = git.projects.get('ansible-inventories/hosts/{}/{}'.format(alert.environment, alert.origin.split('/').pop()))
if 'gitlab_project_id' in alert.attributes:
project = git.projects.get(alert.attributes['gitlab_project_id'])
else:
project = git.projects.get('ansible-inventories/hosts/{}/{}'.format(alert.environment, alert.origin.split('/').pop()))
if project: if project:
LOG.info('Project ID %s' % project.id) LOG.info('Project ID %s' % project.id)
...@@ -48,8 +55,8 @@ class GitlabIssue(PluginBase): ...@@ -48,8 +55,8 @@ class GitlabIssue(PluginBase):
raw_data = alert.raw_data raw_data = alert.raw_data
issue = project.issues.create({ issue = project.issues.create({
'title': alert.text[0:128], 'title': alert.text[0:128],
'description': {}/alert/{}\n\nService: {}\n\nOrigin: {}\n\n```\n{}\n```\n.format(ALERTA_URL, alert.id, alert.service, alert.origin, raw_data) 'description': '{}/alert/{}\n\nService: {}\n\nOrigin: {}\n\n```\n{}\n```\n'.format(ALERTA_URL, alert.id, alert.service, alert.origin, raw_data)
}) # type: gitlab.Issue }) # type: Issue
alert.attributes['issue_iid'] = issue.id alert.attributes['issue_iid'] = issue.id
alert.attributes['gitlabUrl'] = '<a href="{}" target="_blank">Issue #{}</a>'.format( alert.attributes['gitlabUrl'] = '<a href="{}" target="_blank">Issue #{}</a>'.format(
issue.web_url, issue.web_url,
......
from setuptools import find_packages, setup from setuptools import find_packages, setup
version = '1.2.0' version = '1.3.0'
setup( setup(
name='alerta-gitlab', name='alerta-gitlab',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment