2016-06-17 19:53:21 +02:00
|
|
|
# Webhooks for external integrations.
|
2017-11-16 00:43:10 +01:00
|
|
|
from typing import Any, Dict
|
|
|
|
|
2016-06-17 19:53:21 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
|
2017-10-31 04:25:48 +01:00
|
|
|
from zerver.decorator import api_key_only_webhook_view
|
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2019-02-02 23:53:55 +01:00
|
|
|
from zerver.lib.response import json_success
|
2018-03-16 22:53:50 +01:00
|
|
|
from zerver.lib.webhooks.common import check_send_webhook_message
|
2019-02-02 23:53:55 +01:00
|
|
|
from zerver.models import UserProfile
|
2016-06-17 19:53:21 +02:00
|
|
|
|
2019-04-17 03:57:31 +02:00
|
|
|
BUILD_TEMPLATE = """
|
|
|
|
[Build {build_number}]({build_url}) {status}:
|
|
|
|
* **Commit**: [{commit_hash}: {commit_message}]({commit_url})
|
|
|
|
* **Author**: {email}
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
DEPLOY_TEMPLATE = """
|
|
|
|
[Deploy {deploy_number}]({deploy_url}) of [build {build_number}]({build_url}) {status}:
|
|
|
|
* **Commit**: [{commit_hash}: {commit_message}]({commit_url})
|
|
|
|
* **Author**: {email}
|
|
|
|
* **Server**: {server_name}
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
TOPIC_TEMPLATE = "{project}/{branch}"
|
|
|
|
|
2016-06-17 19:53:21 +02:00
|
|
|
@api_key_only_webhook_view('Semaphore')
|
|
|
|
@has_request_variables
|
2017-12-03 20:51:06 +01:00
|
|
|
def api_semaphore_webhook(request: HttpRequest, user_profile: UserProfile,
|
2018-03-16 22:53:50 +01:00
|
|
|
payload: Dict[str, Any]=REQ(argument_type='body')) -> 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
|
2017-08-24 17:31:04 +02:00
|
|
|
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"]
|
2016-06-17 19:53:21 +02:00
|
|
|
|
|
|
|
if event == "build":
|
2017-08-24 17:31:04 +02:00
|
|
|
build_url = payload["build_url"]
|
|
|
|
build_number = payload["build_number"]
|
2019-04-17 03:57:31 +02:00
|
|
|
content = BUILD_TEMPLATE.format(
|
|
|
|
build_number=build_number,
|
|
|
|
build_url=build_url,
|
|
|
|
status=result,
|
|
|
|
commit_hash=commit_id[:7],
|
|
|
|
commit_message=message,
|
|
|
|
commit_url=commit_url,
|
|
|
|
email=author_email
|
|
|
|
)
|
2016-06-17 19:53:21 +02:00
|
|
|
|
|
|
|
elif event == "deploy":
|
2017-08-24 17:31:04 +02:00
|
|
|
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"]
|
2019-04-17 03:57:31 +02:00
|
|
|
content = DEPLOY_TEMPLATE.format(
|
|
|
|
deploy_number=deploy_number,
|
|
|
|
deploy_url=deploy_url,
|
|
|
|
build_number=build_number,
|
|
|
|
build_url=build_url,
|
|
|
|
status=result,
|
|
|
|
commit_hash=commit_id[:7],
|
|
|
|
commit_message=message,
|
|
|
|
commit_url=commit_url,
|
|
|
|
email=author_email,
|
|
|
|
server_name=server_name
|
|
|
|
)
|
2016-06-17 19:53:21 +02:00
|
|
|
|
2017-05-07 21:37:02 +02:00
|
|
|
else: # should never get here
|
2019-04-17 03:57:31 +02:00
|
|
|
content = "{event}: {result}".format(
|
|
|
|
event=event, result=result)
|
2016-06-17 19:53:21 +02:00
|
|
|
|
2019-04-17 03:57:31 +02:00
|
|
|
subject = TOPIC_TEMPLATE.format(
|
|
|
|
project=project_name,
|
|
|
|
branch=branch_name
|
|
|
|
)
|
2016-06-17 19:53:21 +02:00
|
|
|
|
2018-03-16 22:53:50 +01:00
|
|
|
check_send_webhook_message(request, user_profile, subject, content)
|
2016-06-17 19:53:21 +02:00
|
|
|
return json_success()
|