2018-05-11 01:43:34 +02:00
|
|
|
from typing import Any, Mapping, Optional
|
2016-06-05 00:38:26 +02:00
|
|
|
|
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
|
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2017-11-16 00:43:10 +01:00
|
|
|
from zerver.lib.response import json_success
|
2016-03-13 13:22:33 +01:00
|
|
|
from zerver.lib.validator import check_dict
|
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")
|
2016-03-13 13:22:33 +01:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def api_bitbucket_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
|
|
|
payload: Mapping[str, Any] = REQ(validator=check_dict([])),
|
|
|
|
branches: Optional[str] = REQ(default=None),
|
|
|
|
) -> HttpResponse:
|
2021-02-12 08:20:45 +01:00
|
|
|
repository = payload["repository"]
|
2016-10-01 11:03:07 +02:00
|
|
|
|
|
|
|
commits = [
|
|
|
|
{
|
2021-02-12 08:20:45 +01:00
|
|
|
"name": commit.get("author") or payload.get("user"),
|
|
|
|
"sha": commit.get("raw_node"),
|
|
|
|
"message": commit.get("message"),
|
|
|
|
"url": "{}{}commits/{}".format(
|
|
|
|
payload.get("canon_url"), repository.get("absolute_url"), commit.get("raw_node")
|
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 :/
|
2021-02-12 08:20:45 +01:00
|
|
|
subject = repository["name"]
|
2020-06-14 02:57:50 +02:00
|
|
|
content = "{} [force pushed]({}).".format(
|
2021-02-12 08:20:45 +01:00
|
|
|
payload.get("user", "Someone"),
|
|
|
|
payload["canon_url"] + repository["absolute_url"],
|
2020-06-14 02:57:50 +02:00
|
|
|
)
|
2016-03-13 13:22:33 +01:00
|
|
|
else:
|
2021-02-12 08:20:45 +01:00
|
|
|
branch = payload["commits"][-1]["branch"]
|
2017-04-05 02:52:31 +02:00
|
|
|
if branches is not None and branches.find(branch) == -1:
|
|
|
|
return json_success()
|
2019-03-16 18:39:03 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
committer = payload.get("user")
|
2019-03-16 18:39:03 +01:00
|
|
|
content = get_push_commits_event_message(
|
2021-02-12 08:20:45 +01:00
|
|
|
committer if committer is not None else "Someone", None, branch, commits
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
subject = TOPIC_WITH_BRANCH_TEMPLATE.format(repo=repository["name"], branch=branch)
|
2016-03-13 13:22:33 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
check_send_webhook_message(request, user_profile, subject, content, unquote_url_parameters=True)
|
2016-03-13 13:22:33 +01:00
|
|
|
return json_success()
|