From 8ab6490cf9a6218bb6a0d5e35378e5ab43672b43 Mon Sep 17 00:00:00 2001
From: jurgenhaas <juergen@paragon-es.de>
Date: Wed, 19 Jun 2019 16:11:18 +0200
Subject: [PATCH] customer/bitegra/mobimo/drupal#240 Implement support for an
 optional project ID when sending alert from fluentd to alerta

---
 templates/gitlab/alerta_gitlab.py | 23 +++++++++++++++--------
 templates/gitlab/setup.py         |  2 +-
 2 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/templates/gitlab/alerta_gitlab.py b/templates/gitlab/alerta_gitlab.py
index fe23a07..717c59b 100644
--- a/templates/gitlab/alerta_gitlab.py
+++ b/templates/gitlab/alerta_gitlab.py
@@ -1,7 +1,9 @@
-import gitlab
 import json
 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')
 
@@ -15,15 +17,16 @@ class GitlabIssue(PluginBase):
     name = 'alerta-gitlab'
 
     def __init__(self):
+        super().__init__()
         self.headers = {'Private-Token': GITLAB_ACCESS_TOKEN}
 
-    def pre_receive(self, alert):
+    def pre_receive(self, alert, **kwargs):
         return alert
 
-    def post_receive(self, alert):
+    def post_receive(self, alert, **kwargs):
         return alert
 
-    def status_change(self, alert, status, text):
+    def status_change(self, alert, status, text, **kwargs):
         return alert, status, text
 
     def take_action(self, alert, action, text, **kwargs):
@@ -33,7 +36,11 @@ class GitlabIssue(PluginBase):
             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()))
+
+            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:
                 LOG.info('Project ID %s' % project.id)
@@ -48,8 +55,8 @@ class GitlabIssue(PluginBase):
                         raw_data = alert.raw_data
                     issue = project.issues.create({
                         '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)
-                    }) # type: gitlab.Issue
+                        'description': '{}/alert/{}\n\nService: {}\n\nOrigin: {}\n\n```\n{}\n```\n'.format(ALERTA_URL, alert.id, alert.service, alert.origin, raw_data)
+                    }) # type: Issue
                     alert.attributes['issue_iid'] = issue.id
                     alert.attributes['gitlabUrl'] = '<a href="{}" target="_blank">Issue #{}</a>'.format(
                         issue.web_url,
diff --git a/templates/gitlab/setup.py b/templates/gitlab/setup.py
index 02edd36..8234091 100644
--- a/templates/gitlab/setup.py
+++ b/templates/gitlab/setup.py
@@ -1,7 +1,7 @@
 
 from setuptools import find_packages, setup
 
-version = '1.2.0'
+version = '1.3.0'
 
 setup(
     name='alerta-gitlab',
-- 
GitLab