2018-01-28 18:09:08 +01:00
|
|
|
# Webhooks for external integrations.
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
|
|
|
|
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
|
2022-10-08 20:22:51 +02:00
|
|
|
from zerver.lib.validator import WildValue, check_string, to_wild_value
|
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,
|
2022-10-08 20:22:51 +02:00
|
|
|
payload: WildValue = REQ(argument_type="body", converter=to_wild_value),
|
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]
|
2022-10-08 20:22:51 +02:00
|
|
|
result = payload["stages"][0]["result"].tame(check_string)
|
2021-02-12 08:20:45 +01:00
|
|
|
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(
|
2022-10-08 20:22:51 +02:00
|
|
|
modifications["user_name"].tame(check_string),
|
2018-01-28 18:09:08 +01:00
|
|
|
result,
|
|
|
|
emoji,
|
|
|
|
build_link,
|
2022-10-08 20:22:51 +02:00
|
|
|
modifications["comment"].tame(check_string),
|
2018-01-28 18:09:08 +01:00
|
|
|
)
|
2022-10-08 20:22:51 +02:00
|
|
|
branch = material["description"].tame(check_string).split(",")
|
2018-01-28 18:09:08 +01:00
|
|
|
topic = branch[0].split(" ")[1]
|
|
|
|
|
|
|
|
check_send_webhook_message(request, user_profile, topic, body)
|
|
|
|
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|