2016-06-05 23:13:50 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2016-06-05 00:38:26 +02:00
|
|
|
|
2017-10-31 04:25:48 +01:00
|
|
|
from zerver.decorator import authenticated_rest_api_view
|
2017-11-16 00:43:10 +01:00
|
|
|
from zerver.lib.response import json_success
|
2023-09-27 19:01:31 +02:00
|
|
|
from zerver.lib.typed_endpoint import JsonBodyPayload, typed_endpoint
|
2023-08-12 09:34:31 +02:00
|
|
|
from zerver.lib.validator import WildValue, check_string
|
2018-03-13 23:43:02 +01:00
|
|
|
from zerver.lib.webhooks.common import check_send_webhook_message
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.webhooks.git import TOPIC_WITH_BRANCH_TEMPLATE, get_push_commits_event_message
|
2019-02-02 23:53:55 +01:00
|
|
|
from zerver.models import UserProfile
|
2016-03-13 13:22:33 +01:00
|
|
|
|
2020-01-14 22:06:24 +01:00
|
|
|
|
2018-03-16 23:37:32 +01:00
|
|
|
@authenticated_rest_api_view(webhook_client_name="Bitbucket")
|
2023-08-12 09:34:31 +02:00
|
|
|
@typed_endpoint
|
2021-02-12 08:19:30 +01:00
|
|
|
def api_bitbucket_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2023-08-12 09:34:31 +02:00
|
|
|
*,
|
2023-09-27 19:01:31 +02:00
|
|
|
payload: JsonBodyPayload[WildValue],
|
2024-07-12 02:30:23 +02:00
|
|
|
branches: str | None = None,
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2021-02-12 08:20:45 +01:00
|
|
|
repository = payload["repository"]
|
2016-10-01 11:03:07 +02:00
|
|
|
|
|
|
|
commits = [
|
|
|
|
{
|
2024-01-29 00:32:21 +01:00
|
|
|
"name": (
|
|
|
|
commit["author"].tame(check_string)
|
|
|
|
if "author" in commit
|
|
|
|
else payload.get("user", "Someone").tame(check_string)
|
|
|
|
),
|
2022-01-11 09:10:13 +01:00
|
|
|
"sha": commit["raw_node"].tame(check_string),
|
|
|
|
"message": commit["message"].tame(check_string),
|
2021-02-12 08:20:45 +01:00
|
|
|
"url": "{}{}commits/{}".format(
|
2022-01-11 09:10:13 +01:00
|
|
|
payload["canon_url"].tame(check_string),
|
|
|
|
repository["absolute_url"].tame(check_string),
|
|
|
|
commit["raw_node"].tame(check_string),
|
2021-02-12 08:19:30 +01:00
|
|
|
),
|
2016-10-01 11:03:07 +02:00
|
|
|
}
|
2021-02-12 08:20:45 +01:00
|
|
|
for commit in payload["commits"]
|
2016-10-01 11:03:07 +02:00
|
|
|
]
|
2016-03-13 13:22:33 +01:00
|
|
|
|
|
|
|
if len(commits) == 0:
|
|
|
|
# Bitbucket doesn't give us enough information to really give
|
|
|
|
# a useful message :/
|
2024-01-17 15:53:30 +01:00
|
|
|
topic_name = repository["name"].tame(check_string)
|
2020-06-14 02:57:50 +02:00
|
|
|
content = "{} [force pushed]({}).".format(
|
2022-01-11 09:10:13 +01:00
|
|
|
payload.get("user", "Someone").tame(check_string),
|
|
|
|
payload["canon_url"].tame(check_string) + repository["absolute_url"].tame(check_string),
|
2020-06-14 02:57:50 +02:00
|
|
|
)
|
2016-03-13 13:22:33 +01:00
|
|
|
else:
|
2022-01-11 09:10:13 +01:00
|
|
|
branch = payload["commits"][-1]["branch"].tame(check_string)
|
2017-04-05 02:52:31 +02:00
|
|
|
if branches is not None and branches.find(branch) == -1:
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2019-03-16 18:39:03 +01:00
|
|
|
|
2022-01-11 09:10:13 +01:00
|
|
|
committer = payload.get("user", "Someone").tame(check_string)
|
|
|
|
content = get_push_commits_event_message(committer, None, branch, commits)
|
2024-01-17 15:53:30 +01:00
|
|
|
topic_name = TOPIC_WITH_BRANCH_TEMPLATE.format(
|
2022-01-11 09:10:13 +01:00
|
|
|
repo=repository["name"].tame(check_string), branch=branch
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2016-03-13 13:22:33 +01:00
|
|
|
|
2024-01-17 15:53:30 +01:00
|
|
|
check_send_webhook_message(
|
|
|
|
request, user_profile, topic_name, content, unquote_url_parameters=True
|
|
|
|
)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|