From 089ea75bbbf80fef9e08513c577ae05035cd711f Mon Sep 17 00:00:00 2001 From: Tom Powell Date: Sun, 28 Oct 2018 18:47:45 -0700 Subject: [PATCH] Add slack webhook --- ui/config.py | 2 ++ ui/gitlab/webhooks.py | 3 +++ ui/slack/__init__.py | 0 ui/slack/webhook.py | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 37 insertions(+) create mode 100644 ui/slack/__init__.py create mode 100644 ui/slack/webhook.py diff --git a/ui/config.py b/ui/config.py index 74c624d..2657aab 100644 --- a/ui/config.py +++ b/ui/config.py @@ -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) diff --git a/ui/gitlab/webhooks.py b/ui/gitlab/webhooks.py index 2750d94..4c4461d 100644 --- a/ui/gitlab/webhooks.py +++ b/ui/gitlab/webhooks.py @@ -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 index 0000000..e69de29 diff --git a/ui/slack/webhook.py b/ui/slack/webhook.py new file mode 100644 index 0000000..ae8014c --- /dev/null +++ b/ui/slack/webhook.py @@ -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) -- 2.20.1