Add slack webhook
authorTom Powell <tom@powell.io>
Mon, 29 Oct 2018 01:47:45 +0000 (18:47 -0700)
committerTom Powell <tom@powell.io>
Mon, 29 Oct 2018 01:47:45 +0000 (18:47 -0700)
ui/config.py
ui/gitlab/webhooks.py
ui/slack/__init__.py [new file with mode: 0644]
ui/slack/webhook.py [new file with mode: 0644]

index 74c624d15bf11f34aff1902d256a62b5058c063f..2657aab3905cd5aa937e8fd668a01de34d32954a 100644 (file)
@@ -6,3 +6,5 @@ CACHE_TYPE = 'simple'
 GITLAB_WEBHOOK_TOKEN = os.environ.get("GITLAB_WEBHOOK_TOKEN", "secret")
 
 PRESERVE_CONTEXT_ON_EXCEPTION = False
+
+SLACK_WEBHOOK_URL = os.environ.get("SLACK_WEBHOOK_URL", None)
index 2750d9406e9f9abf58c44f2f7e139bbd906cedcb..4c4461dca42c91f4f3fe1e6d492e3be7e542c591 100644 (file)
@@ -3,6 +3,7 @@ from flask import abort
 from ui import config
 from ui.gitlab import api
 from ui.models import Build, Runner, db
+from ui.slack.webhook import post_build
 
 def process(request):
     if request.headers.get('X-Gitlab-Token', None) != config.GITLAB_WEBHOOK_TOKEN:
@@ -34,3 +35,5 @@ def process(request):
                 build.build_runner.runner_name = runner.get("description")
         db.session.add(build)
         db.session.commit()
+        if build.build_status in ['failed', 'canceled']:
+            post_build(build.build_status, build.build_device, build.build_version, build.build_type, build.build_id)
diff --git a/ui/slack/__init__.py b/ui/slack/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/ui/slack/webhook.py b/ui/slack/webhook.py
new file mode 100644 (file)
index 0000000..ae8014c
--- /dev/null
@@ -0,0 +1,32 @@
+import requests
+
+from ui import config
+
+
+def post_build(status, device, version, btype, id_):
+    if not config.SLACK_WEBHOOK_URL:
+        return
+    if status == "failed":
+        state = "has failed for"
+    elif status == "canceled":
+        state = "was canceled on"
+    else:
+        return
+    version = version.split("-")[1]
+    text = f"LineageOS {version} {state} {device} ({btype})"
+    data = {
+        "attachments": [
+            {
+                "fallback": text,
+                "author_name": "buildbot",
+                "title": text,
+                "text": f"https://gitlab.com/lineageos/builder/android/pipelines/{id_}",
+                "color": "danger" if status == "failed" else "#020202"
+            }
+        ]
+    }
+    requests.post(config.SLACK_WEBHOOK_URL, json=data)
+
+if __name__ == "__main__":
+    post_build('failed', 'mako', 'lineage-40.1', 'userdebug', 4)
+    post_build('canceled', 'mako', 'lineage-40.1', 'stable', 5)