Some checks failed
Code scanning / CodeQL (push) Failing after 15s
Zulip production suite / Ubuntu 22.04 production build (push) Failing after 26s
Zulip production suite / Debian 12 production install with custom db name and user (push) Has been skipped
Zulip production suite / Ubuntu 22.04 production install and PostgreSQL upgrade with pgroonga (push) Has been skipped
Zulip production suite / Ubuntu 24.04 production install (push) Has been skipped
Zulip production suite / Ubuntu 26.04 production install (push) Has been skipped
Zulip production suite / Debian 13 production install (push) Has been skipped
Zulip production suite / 7.0 Version Upgrade (push) Has been skipped
Zulip production suite / 8.0 Version Upgrade (push) Has been skipped
Zulip production suite / 6.0 Version Upgrade (push) Has been skipped
Zulip production suite / 10.0 Version Upgrade (push) Has been skipped
Zulip production suite / 9.0 Version Upgrade (push) Has been skipped
Zulip production suite / 12.0 Version Upgrade (push) Has been skipped
Zulip production suite / 11.0 Version Upgrade (push) Has been skipped
GitHub Actions Security Analysis with zizmor / zizmor (push) Failing after 3s
Zulip CI / Debian 12 (Python 3.11, backend + documentation) (push) Failing after 26s
Zulip CI / Ubuntu 22.04 (Python 3.10, backend + frontend) (push) Failing after 3s
Zulip CI / Ubuntu 24.04 (Python 3.12, backend) (push) Failing after 25s
Zulip CI / Ubuntu 26.04 (Python 3.14, backend) (push) Failing after 25s
Zulip CI / Debian 13 (Python 3.13, backend) (push) Failing after 28s
Zulip production suite / Required jobs (push) Has been cancelled
Zulip CI / Required jobs (push) Has been cancelled
API Documentation Update Check / check-feature-level-updated (push) Has been cancelled
API Documentation Update Check / notify-if-api-docs-changed (push) Has been cancelled
102 lines
3.3 KiB
Python
102 lines
3.3 KiB
Python
from collections.abc import Callable
|
|
|
|
from django.core.exceptions import ValidationError
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
from zerver.decorator import webhook_view
|
|
from zerver.lib.exceptions import UnsupportedWebhookEventTypeError
|
|
from zerver.lib.response import json_success
|
|
from zerver.lib.typed_endpoint import JsonBodyPayload, typed_endpoint
|
|
from zerver.lib.validator import WildValue, check_string
|
|
from zerver.lib.webhooks.common import check_send_webhook_message
|
|
from zerver.lib.webhooks.git import (
|
|
TOPIC_WITH_BRANCH_TEMPLATE,
|
|
get_push_commits_event_message,
|
|
is_branch_name_notifiable,
|
|
)
|
|
from zerver.models import UserProfile
|
|
|
|
|
|
def get_push_commits_body(payload: WildValue) -> str:
|
|
commits_data = [
|
|
{
|
|
"name": commit["author"].tame(check_string),
|
|
"sha": commit["raw_id"].tame(check_string),
|
|
"url": commit["url"].tame(check_string),
|
|
"message": commit["message"].tame(check_string),
|
|
}
|
|
for commit in payload["event"]["push"]["commits"]
|
|
]
|
|
return get_push_commits_event_message(
|
|
get_user_name(payload),
|
|
None,
|
|
get_push_branch_name(payload),
|
|
commits_data,
|
|
)
|
|
|
|
|
|
def get_user_name(payload: WildValue) -> str:
|
|
return payload["event"]["actor"]["username"].tame(check_string)
|
|
|
|
|
|
def get_push_branch_name(payload: WildValue) -> str:
|
|
branches = payload["event"]["push"]["branches"]
|
|
try:
|
|
return branches[0]["name"].tame(check_string)
|
|
# this error happens when the event is a push to delete remote branch, where
|
|
# branches will be an empty list
|
|
except ValidationError:
|
|
return payload["event"]["push"]["commits"][0]["raw_id"].tame(check_string).split("=>")[1]
|
|
|
|
|
|
def get_event_name(payload: WildValue, branches: str | None) -> str | None:
|
|
event_name = payload["event"]["name"].tame(check_string)
|
|
if event_name == "repo-push" and branches is not None:
|
|
branch = get_push_branch_name(payload)
|
|
if not is_branch_name_notifiable(branch, branches):
|
|
return None
|
|
if event_name in EVENT_FUNCTION_MAPPER:
|
|
return event_name
|
|
raise UnsupportedWebhookEventTypeError(event_name)
|
|
|
|
|
|
def get_repository_name(payload: WildValue) -> str:
|
|
return payload["event"]["repo"]["repo_name"].tame(check_string)
|
|
|
|
|
|
def get_topic_based_on_event(payload: WildValue, event: str) -> str:
|
|
if event == "repo-push":
|
|
return TOPIC_WITH_BRANCH_TEMPLATE.format(
|
|
repo=get_repository_name(payload), branch=get_push_branch_name(payload)
|
|
)
|
|
return get_repository_name(payload) # nocoverage
|
|
|
|
|
|
EVENT_FUNCTION_MAPPER: dict[str, Callable[[WildValue], str]] = {
|
|
"repo-push": get_push_commits_body,
|
|
}
|
|
|
|
ALL_EVENT_TYPES = list(EVENT_FUNCTION_MAPPER.keys())
|
|
|
|
|
|
@webhook_view("RhodeCode", all_event_types=ALL_EVENT_TYPES)
|
|
@typed_endpoint
|
|
def api_rhodecode_webhook(
|
|
request: HttpRequest,
|
|
user_profile: UserProfile,
|
|
*,
|
|
payload: JsonBodyPayload[WildValue],
|
|
branches: str | None = None,
|
|
) -> HttpResponse:
|
|
event = get_event_name(payload, branches)
|
|
if event is None:
|
|
return json_success(request)
|
|
|
|
topic_name = get_topic_based_on_event(payload, event)
|
|
|
|
body_function = EVENT_FUNCTION_MAPPER[event]
|
|
body = body_function(payload)
|
|
|
|
check_send_webhook_message(request, user_profile, topic_name, body)
|
|
return json_success(request)
|