mirror of https://github.com/zulip/zulip.git
beanstalk: Strengthen types using WildValue.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
parent
2551320a3b
commit
950e3cfcaf
|
@ -2,7 +2,7 @@
|
|||
import base64
|
||||
import re
|
||||
from functools import wraps
|
||||
from typing import Any, Dict, List, Optional, Tuple, cast
|
||||
from typing import Dict, List, Optional, Tuple, cast
|
||||
|
||||
from django.http import HttpRequest, HttpResponse
|
||||
|
||||
|
@ -10,7 +10,7 @@ from zerver.decorator import authenticated_rest_api_view
|
|||
from zerver.lib.request import REQ, has_request_variables
|
||||
from zerver.lib.response import json_success
|
||||
from zerver.lib.types import ViewFuncT
|
||||
from zerver.lib.validator import check_dict
|
||||
from zerver.lib.validator import WildValue, check_int, check_string, to_wild_value
|
||||
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
|
||||
from zerver.models import UserProfile
|
||||
|
@ -20,7 +20,7 @@ def build_message_from_gitlog(
|
|||
user_profile: UserProfile,
|
||||
name: str,
|
||||
ref: str,
|
||||
commits: List[Dict[str, str]],
|
||||
commits: WildValue,
|
||||
before: str,
|
||||
after: str,
|
||||
url: str,
|
||||
|
@ -32,21 +32,21 @@ def build_message_from_gitlog(
|
|||
short_ref = re.sub(r"^refs/heads/", "", ref)
|
||||
subject = TOPIC_WITH_BRANCH_TEMPLATE.format(repo=name, branch=short_ref)
|
||||
|
||||
commits = _transform_commits_list_to_common_format(commits)
|
||||
content = get_push_commits_event_message(pusher, url, short_ref, commits, deleted=deleted)
|
||||
commits_data = _transform_commits_list_to_common_format(commits)
|
||||
content = get_push_commits_event_message(pusher, url, short_ref, commits_data, deleted=deleted)
|
||||
|
||||
return subject, content
|
||||
|
||||
|
||||
def _transform_commits_list_to_common_format(commits: List[Dict[str, Any]]) -> List[Dict[str, str]]:
|
||||
def _transform_commits_list_to_common_format(commits: WildValue) -> List[Dict[str, str]]:
|
||||
new_commits_list = []
|
||||
for commit in commits:
|
||||
new_commits_list.append(
|
||||
{
|
||||
"name": commit["author"]["name"],
|
||||
"sha": commit.get("id"),
|
||||
"url": commit.get("url"),
|
||||
"message": commit.get("message"),
|
||||
"name": commit["author"]["name"].tame(check_string),
|
||||
"sha": commit["id"].tame(check_string),
|
||||
"url": commit["url"].tame(check_string),
|
||||
"message": commit["message"].tame(check_string),
|
||||
}
|
||||
)
|
||||
return new_commits_list
|
||||
|
@ -79,7 +79,7 @@ def beanstalk_decoder(view_func: ViewFuncT) -> ViewFuncT:
|
|||
def api_beanstalk_webhook(
|
||||
request: HttpRequest,
|
||||
user_profile: UserProfile,
|
||||
payload: Dict[str, Any] = REQ(json_validator=check_dict([])),
|
||||
payload: WildValue = REQ(converter=to_wild_value),
|
||||
branches: Optional[str] = REQ(default=None),
|
||||
) -> HttpResponse:
|
||||
# Beanstalk supports both SVN and Git repositories
|
||||
|
@ -87,24 +87,24 @@ def api_beanstalk_webhook(
|
|||
# 'uri' key that is only present for Git repos
|
||||
git_repo = "uri" in payload
|
||||
if git_repo:
|
||||
if branches is not None and branches.find(payload["branch"]) == -1:
|
||||
if branches is not None and branches.find(payload["branch"].tame(check_string)) == -1:
|
||||
return json_success(request)
|
||||
|
||||
subject, content = build_message_from_gitlog(
|
||||
user_profile,
|
||||
payload["repository"]["name"],
|
||||
payload["ref"],
|
||||
payload["repository"]["name"].tame(check_string),
|
||||
payload["ref"].tame(check_string),
|
||||
payload["commits"],
|
||||
payload["before"],
|
||||
payload["after"],
|
||||
payload["repository"]["url"],
|
||||
payload["pusher_name"],
|
||||
payload["before"].tame(check_string),
|
||||
payload["after"].tame(check_string),
|
||||
payload["repository"]["url"].tame(check_string),
|
||||
payload["pusher_name"].tame(check_string),
|
||||
)
|
||||
else:
|
||||
author = payload.get("author_full_name")
|
||||
url = payload.get("changeset_url")
|
||||
revision = payload.get("revision")
|
||||
(short_commit_msg, _, _) = payload["message"].partition("\n")
|
||||
author = payload["author_full_name"].tame(check_string)
|
||||
url = payload["changeset_url"].tame(check_string)
|
||||
revision = payload["revision"].tame(check_int)
|
||||
(short_commit_msg, _, _) = payload["message"].tame(check_string).partition("\n")
|
||||
|
||||
subject = f"svn r{revision}"
|
||||
content = f"{author} pushed [revision {revision}]({url}):\n\n> {short_commit_msg}"
|
||||
|
|
Loading…
Reference in New Issue