2016-06-17 19:53:21 +02:00
|
|
|
# Webhooks for external integrations.
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
|
|
|
from zerver.models import get_client, get_user_profile_by_email
|
|
|
|
from zerver.lib.actions import check_send_message
|
|
|
|
from zerver.lib.response import json_success, json_error
|
|
|
|
from zerver.decorator import REQ, has_request_variables, api_key_only_webhook_view
|
2017-05-02 01:00:50 +02:00
|
|
|
from zerver.models import UserProfile
|
2016-06-17 19:53:21 +02:00
|
|
|
import ujson
|
|
|
|
|
|
|
|
from typing import Any, Dict
|
|
|
|
|
|
|
|
|
|
|
|
@api_key_only_webhook_view('Semaphore')
|
|
|
|
@has_request_variables
|
2017-05-02 01:00:50 +02:00
|
|
|
def api_semaphore_webhook(request, user_profile,
|
2016-06-17 19:53:21 +02:00
|
|
|
payload=REQ(argument_type='body'),
|
|
|
|
stream=REQ(default='builds')):
|
2017-05-02 01:00:50 +02:00
|
|
|
# type: (HttpRequest, UserProfile, Dict[str, Any], str) -> HttpResponse
|
2016-06-17 19:53:21 +02:00
|
|
|
|
|
|
|
# semaphore only gives the last commit, even if there were multiple commits
|
|
|
|
# since the last build
|
|
|
|
try:
|
|
|
|
branch_name = payload["branch_name"]
|
|
|
|
project_name = payload["project_name"]
|
|
|
|
result = payload["result"]
|
|
|
|
event = payload["event"]
|
|
|
|
commit_id = payload["commit"]["id"]
|
|
|
|
commit_url = payload["commit"]["url"]
|
|
|
|
author_email = payload["commit"]["author_email"]
|
|
|
|
message = payload["commit"]["message"]
|
|
|
|
except KeyError as e:
|
2016-07-13 17:09:49 +02:00
|
|
|
return json_error(_("Missing key %s in JSON") % (str(e),))
|
2016-06-17 19:53:21 +02:00
|
|
|
|
|
|
|
if event == "build":
|
|
|
|
try:
|
|
|
|
build_url = payload["build_url"]
|
|
|
|
build_number = payload["build_number"]
|
|
|
|
except KeyError as e:
|
2016-07-13 17:09:49 +02:00
|
|
|
return json_error(_("Missing key %s in JSON") % (str(e),))
|
2016-06-17 19:53:21 +02:00
|
|
|
content = u"[build %s](%s): %s\n" % (build_number, build_url, result)
|
|
|
|
|
|
|
|
elif event == "deploy":
|
|
|
|
try:
|
|
|
|
build_url = payload["build_html_url"]
|
|
|
|
build_number = payload["build_number"]
|
|
|
|
deploy_url = payload["html_url"]
|
|
|
|
deploy_number = payload["number"]
|
|
|
|
server_name = payload["server_name"]
|
|
|
|
except KeyError as e:
|
2016-07-13 17:09:49 +02:00
|
|
|
return json_error(_("Missing key %s in JSON") % (str(e),))
|
2016-06-17 19:53:21 +02:00
|
|
|
content = u"[deploy %s](%s) of [build %s](%s) on server %s: %s\n" % \
|
|
|
|
(deploy_number, deploy_url, build_number, build_url, server_name, result)
|
|
|
|
|
|
|
|
else: # should never get here
|
|
|
|
content = u"%s: %s\n" % (event, result)
|
|
|
|
|
|
|
|
content += "!avatar(%s) [`%s`](%s): %s" % (author_email, commit_id[:7],
|
|
|
|
commit_url, message)
|
|
|
|
subject = u"%s/%s" % (project_name, branch_name)
|
|
|
|
|
2017-05-02 01:00:50 +02:00
|
|
|
check_send_message(user_profile, request.client, "stream",
|
2016-06-17 19:53:21 +02:00
|
|
|
[stream], subject, content)
|
|
|
|
return json_success()
|