2018-01-28 18:09:08 +01:00
|
|
|
# Webhooks for external integrations.
|
|
|
|
import json
|
|
|
|
import os
|
2019-02-02 23:53:55 +01:00
|
|
|
from typing import Any, Dict
|
2018-01-28 18:09:08 +01:00
|
|
|
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
|
2020-08-20 00:32:15 +02:00
|
|
|
from zerver.decorator import webhook_view
|
2018-01-28 18:09:08 +01:00
|
|
|
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-01-28 18:09:08 +01:00
|
|
|
from zerver.lib.webhooks.common import check_send_webhook_message
|
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
2021-02-12 03:52:14 +01:00
|
|
|
MESSAGE_TEMPLATE = """\
|
|
|
|
Author: {}
|
|
|
|
Build status: {} {}
|
|
|
|
Details: [build log]({})
|
|
|
|
Comment: {}"""
|
2018-01-28 18:09:08 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
@webhook_view("Gocd")
|
2018-01-28 18:09:08 +01:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def api_gocd_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2021-02-12 08:20:45 +01:00
|
|
|
payload: Dict[str, Any] = REQ(argument_type="body"),
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2018-01-28 18:09:08 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
modifications = payload["build_cause"]["material_revisions"][0]["modifications"][0]
|
|
|
|
result = payload["stages"][0]["result"]
|
|
|
|
material = payload["build_cause"]["material_revisions"][0]["material"]
|
2018-01-28 18:09:08 +01:00
|
|
|
|
|
|
|
if result == "Passed":
|
2021-02-12 08:20:45 +01:00
|
|
|
emoji = ":thumbs_up:"
|
2018-01-28 18:09:08 +01:00
|
|
|
elif result == "Failed":
|
2021-02-12 08:20:45 +01:00
|
|
|
emoji = ":thumbs_down:"
|
2018-01-28 18:09:08 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
build_details_file = os.path.join(os.path.dirname(__file__), "fixtures/build_details.json")
|
2018-01-28 18:09:08 +01:00
|
|
|
|
2020-04-09 21:51:58 +02:00
|
|
|
with open(build_details_file) as f:
|
2018-01-28 18:09:08 +01:00
|
|
|
contents = json.load(f)
|
|
|
|
build_link = contents["build_details"]["_links"]["pipeline"]["href"]
|
|
|
|
|
|
|
|
body = MESSAGE_TEMPLATE.format(
|
2021-02-12 08:20:45 +01:00
|
|
|
modifications["user_name"],
|
2018-01-28 18:09:08 +01:00
|
|
|
result,
|
|
|
|
emoji,
|
|
|
|
build_link,
|
2021-02-12 08:20:45 +01:00
|
|
|
modifications["comment"],
|
2018-01-28 18:09:08 +01:00
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
branch = material["description"].split(",")
|
2018-01-28 18:09:08 +01:00
|
|
|
topic = branch[0].split(" ")[1]
|
|
|
|
|
|
|
|
check_send_webhook_message(request, user_profile, topic, body)
|
|
|
|
|
|
|
|
return json_success()
|