2016-12-31 18:06:29 +01:00
|
|
|
# Webhooks for external integrations.
|
2022-06-23 17:39:38 +02:00
|
|
|
from django.core.exceptions import ValidationError
|
2016-12-31 18:06:29 +01:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
|
2020-08-20 00:32:15 +02:00
|
|
|
from zerver.decorator import webhook_view
|
2017-10-31 04:25:48 +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-06-23 17:39:38 +02:00
|
|
|
from zerver.lib.validator import WildValue, check_string, to_wild_value
|
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-12-31 18:06:29 +01:00
|
|
|
|
2019-04-18 23:25:03 +02:00
|
|
|
MESSAGE_TEMPLATE = """
|
|
|
|
Build update (see [build log]({build_log_url})):
|
|
|
|
* **Author**: {author}
|
|
|
|
* **Commit**: [{commit_id}]({commit_url})
|
|
|
|
* **Status**: {status} {emoji}
|
|
|
|
""".strip()
|
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
ALL_EVENT_TYPES = ["first-fail", "stop", "received"]
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
|
|
|
|
@webhook_view("SolanoLabs", all_event_types=ALL_EVENT_TYPES)
|
2016-12-31 18:06:29 +01:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def api_solano_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2022-06-23 17:39:38 +02:00
|
|
|
payload: WildValue = REQ(argument_type="body", converter=to_wild_value),
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2022-06-23 17:39:38 +02:00
|
|
|
event = payload["event"].tame(check_string)
|
2021-02-12 08:20:45 +01:00
|
|
|
topic = "build update"
|
|
|
|
if event == "test":
|
2018-03-16 22:53:50 +01:00
|
|
|
return handle_test_event(request, user_profile, topic)
|
2016-12-31 18:06:29 +01:00
|
|
|
try:
|
2022-06-23 17:39:38 +02:00
|
|
|
author = payload["committers"][0].tame(check_string)
|
|
|
|
except ValidationError:
|
2021-02-12 08:20:45 +01:00
|
|
|
author = "Unknown"
|
2022-06-23 17:39:38 +02:00
|
|
|
status = payload["status"].tame(check_string)
|
|
|
|
build_log = payload["url"].tame(check_string)
|
|
|
|
repository = payload["repository"]["url"].tame(check_string)
|
|
|
|
commit_id = payload["commit_id"].tame(check_string)
|
2016-12-31 18:06:29 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
good_status = ["passed"]
|
|
|
|
bad_status = ["failed", "error"]
|
|
|
|
neutral_status = ["running"]
|
|
|
|
emoji = ""
|
2016-12-31 18:06:29 +01:00
|
|
|
if status in good_status:
|
2021-02-12 08:20:45 +01:00
|
|
|
emoji = ":thumbs_up:"
|
2016-12-31 18:06:29 +01:00
|
|
|
elif status in bad_status:
|
2021-02-12 08:20:45 +01:00
|
|
|
emoji = ":thumbs_down:"
|
2017-01-05 01:02:03 +01:00
|
|
|
elif status in neutral_status:
|
2021-02-12 08:20:45 +01:00
|
|
|
emoji = ":arrows_counterclockwise:"
|
2016-12-31 18:06:29 +01:00
|
|
|
|
2020-10-23 02:43:28 +02:00
|
|
|
# If the service is not one of the following, the URL is of the repository home, not the individual
|
2016-12-31 18:06:29 +01:00
|
|
|
# commit itself.
|
2021-02-12 08:20:45 +01:00
|
|
|
commit_url = repository.split("@")[1]
|
|
|
|
if "github" in repository:
|
|
|
|
commit_url += f"/commit/{commit_id}"
|
|
|
|
elif "bitbucket" in repository:
|
|
|
|
commit_url += f"/commits/{commit_id}"
|
|
|
|
elif "gitlab" in repository:
|
|
|
|
commit_url += f"/pipelines/{commit_id}"
|
2016-12-31 18:06:29 +01:00
|
|
|
|
2019-04-18 23:25:03 +02:00
|
|
|
body = MESSAGE_TEMPLATE.format(
|
2021-02-12 08:19:30 +01:00
|
|
|
author=author,
|
|
|
|
build_log_url=build_log,
|
|
|
|
commit_id=commit_id[:7],
|
|
|
|
commit_url=commit_url,
|
|
|
|
status=status,
|
|
|
|
emoji=emoji,
|
2019-04-18 23:25:03 +02:00
|
|
|
)
|
2016-12-31 18:06:29 +01:00
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
check_send_webhook_message(request, user_profile, topic, body, event)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2017-04-11 08:58:23 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
def handle_test_event(request: HttpRequest, user_profile: UserProfile, topic: str) -> HttpResponse:
|
2021-02-12 08:20:45 +01:00
|
|
|
body = "Solano webhook set up correctly."
|
2018-03-16 22:53:50 +01:00
|
|
|
check_send_webhook_message(request, user_profile, topic, body)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|