zerver/webhooks: Change use of typing.Text to str.

This commit is contained in:
Aditya Bansal
2018-05-11 05:13:34 +05:30
committed by Tim Abbott
parent d18b193b5b
commit a40ae4cae5
7 changed files with 96 additions and 96 deletions

View File

@@ -2,7 +2,7 @@
import base64
from functools import wraps
from typing import Any, Callable, Dict, Optional, Text, TypeVar
from typing import Any, Callable, Dict, Optional, TypeVar
from django.http import HttpRequest, HttpResponse
@@ -41,7 +41,7 @@ def beanstalk_decoder(view_func: ViewFuncT) -> ViewFuncT:
@has_request_variables
def api_beanstalk_webhook(request: HttpRequest, user_profile: UserProfile,
payload: Dict[str, Any]=REQ(validator=check_dict([])),
branches: Optional[Text]=REQ(default=None)) -> HttpResponse:
branches: Optional[str]=REQ(default=None)) -> HttpResponse:
# Beanstalk supports both SVN and git repositories
# We distinguish between the two by checking for a
# 'uri' key that is only present for git repos

View File

@@ -1,4 +1,4 @@
from typing import Any, Mapping, Optional, Text
from typing import Any, Mapping, Optional
from django.http import HttpRequest, HttpResponse
@@ -14,8 +14,8 @@ from zerver.models import UserProfile, get_client
@authenticated_rest_api_view(webhook_client_name="Bitbucket")
@has_request_variables
def api_bitbucket_webhook(request: HttpRequest, user_profile: UserProfile,
payload: Mapping[Text, Any]=REQ(validator=check_dict([])),
branches: Optional[Text]=REQ(default=None)) -> HttpResponse:
payload: Mapping[str, Any]=REQ(validator=check_dict([])),
branches: Optional[str]=REQ(default=None)) -> HttpResponse:
repository = payload['repository']
commits = [

View File

@@ -1,7 +1,7 @@
# Webhooks for external integrations.
import re
from functools import partial
from typing import Any, Callable, Dict, List, Optional, Text
from typing import Any, Callable, Dict, List, Optional
from django.http import HttpRequest, HttpResponse
from django.utils.translation import ugettext as _
@@ -49,7 +49,7 @@ class UnknownTriggerType(Exception):
@has_request_variables
def api_bitbucket2_webhook(request: HttpRequest, user_profile: UserProfile,
payload: Dict[str, Any]=REQ(argument_type='body'),
branches: Optional[Text]=REQ(default=None)) -> HttpResponse:
branches: Optional[str]=REQ(default=None)) -> HttpResponse:
type = get_type(request, payload)
if type != 'push':
subject = get_subject_based_on_type(payload, type)
@@ -71,7 +71,7 @@ def api_bitbucket2_webhook(request: HttpRequest, user_profile: UserProfile,
return json_success()
def get_subject_for_branch_specified_events(payload: Dict[str, Any],
branch_name: Optional[Text]=None) -> Text:
branch_name: Optional[str]=None) -> str:
return SUBJECT_WITH_BRANCH_TEMPLATE.format(
repo=get_repository_name(payload['repository']),
branch=get_branch_name_for_push_event(payload) if branch_name is None else branch_name
@@ -95,7 +95,7 @@ def get_subject(payload: Dict[str, Any]) -> str:
assert(payload['repository'] is not None)
return BITBUCKET_SUBJECT_TEMPLATE.format(repository_name=get_repository_name(payload['repository']))
def get_subject_based_on_type(payload: Dict[str, Any], type: str) -> Text:
def get_subject_based_on_type(payload: Dict[str, Any], type: str) -> str:
if type.startswith('pull_request'):
return SUBJECT_WITH_PR_OR_ISSUE_INFO_TEMPLATE.format(
repo=get_repository_name(payload['repository']),
@@ -144,12 +144,12 @@ def get_type(request: HttpRequest, payload: Dict[str, Any]) -> str:
raise UnknownTriggerType("We don't support {} event type".format(event_key))
def get_body_based_on_type(type: str) -> Callable[[Dict[str, Any]], Text]:
def get_body_based_on_type(type: str) -> Callable[[Dict[str, Any]], str]:
fn = GET_SINGLE_MESSAGE_BODY_DEPENDING_ON_TYPE_MAPPER.get(type)
assert callable(fn) # type parameter should be pre-checked, so not None
return fn
def get_push_bodies(payload: Dict[str, Any]) -> List[Text]:
def get_push_bodies(payload: Dict[str, Any]) -> List[str]:
messages_list = []
for change in payload['push']['changes']:
potential_tag = (change['new'] or change['old'] or {}).get('type')
@@ -164,13 +164,13 @@ def get_push_bodies(payload: Dict[str, Any]) -> List[Text]:
messages_list.append(get_normal_push_body(payload, change))
return messages_list
def get_remove_branch_push_body(payload: Dict[str, Any], change: Dict[str, Any]) -> Text:
def get_remove_branch_push_body(payload: Dict[str, Any], change: Dict[str, Any]) -> str:
return get_remove_branch_event_message(
get_user_username(payload),
change['old']['name'],
)
def get_force_push_body(payload: Dict[str, Any], change: Dict[str, Any]) -> Text:
def get_force_push_body(payload: Dict[str, Any], change: Dict[str, Any]) -> str:
return get_force_push_commits_event_message(
get_user_username(payload),
change['links']['html']['href'],
@@ -178,12 +178,12 @@ def get_force_push_body(payload: Dict[str, Any], change: Dict[str, Any]) -> Text
change['new']['target']['hash']
)
def get_commit_author_name(commit: Dict[str, Any]) -> Text:
def get_commit_author_name(commit: Dict[str, Any]) -> str:
if commit['author'].get('user'):
return commit['author']['user'].get('username')
return commit['author']['raw'].split()[0]
def get_normal_push_body(payload: Dict[str, Any], change: Dict[str, Any]) -> Text:
def get_normal_push_body(payload: Dict[str, Any], change: Dict[str, Any]) -> str:
commits_data = [{
'name': get_commit_author_name(commit),
'sha': commit.get('hash'),
@@ -207,7 +207,7 @@ def get_fork_body(payload: Dict[str, Any]) -> str:
fork_url=get_repository_url(payload['fork'])
)
def get_commit_comment_body(payload: Dict[str, Any]) -> Text:
def get_commit_comment_body(payload: Dict[str, Any]) -> str:
comment = payload['comment']
action = u'[commented]({})'.format(comment['links']['html']['href'])
return get_commits_comment_action_message(
@@ -234,11 +234,11 @@ def get_commit_status_changed_body(payload: Dict[str, Any]) -> str:
status=payload['commit_status']['state']
)
def get_issue_commented_body(payload: Dict[str, Any]) -> Text:
def get_issue_commented_body(payload: Dict[str, Any]) -> str:
action = '[commented]({}) on'.format(payload['comment']['links']['html']['href'])
return get_issue_action_body(payload, action)
def get_issue_action_body(payload: Dict[str, Any], action: str) -> Text:
def get_issue_action_body(payload: Dict[str, Any], action: str) -> str:
issue = payload['issue']
assignee = None
message = None
@@ -256,7 +256,7 @@ def get_issue_action_body(payload: Dict[str, Any], action: str) -> Text:
assignee
)
def get_pull_request_action_body(payload: Dict[str, Any], action: str) -> Text:
def get_pull_request_action_body(payload: Dict[str, Any], action: str) -> str:
pull_request = payload['pullrequest']
return get_pull_request_event_message(
get_user_username(payload),
@@ -265,7 +265,7 @@ def get_pull_request_action_body(payload: Dict[str, Any], action: str) -> Text:
pull_request.get('id')
)
def get_pull_request_created_or_updated_body(payload: Dict[str, Any], action: str) -> Text:
def get_pull_request_created_or_updated_body(payload: Dict[str, Any], action: str) -> str:
pull_request = payload['pullrequest']
assignee = None
if pull_request.get('reviewers'):
@@ -282,15 +282,15 @@ def get_pull_request_created_or_updated_body(payload: Dict[str, Any], action: st
assignee=assignee
)
def get_pull_request_comment_created_action_body(payload: Dict[str, Any]) -> Text:
def get_pull_request_comment_created_action_body(payload: Dict[str, Any]) -> str:
action = '[commented]({})'.format(payload['comment']['links']['html']['href'])
return get_pull_request_comment_action_body(payload, action)
def get_pull_request_deleted_or_updated_comment_action_body(payload: Dict[str, Any], action: Text) -> Text:
def get_pull_request_deleted_or_updated_comment_action_body(payload: Dict[str, Any], action: str) -> str:
action = "{} a [comment]({})".format(action, payload['comment']['links']['html']['href'])
return get_pull_request_comment_action_body(payload, action)
def get_pull_request_comment_action_body(payload: Dict[str, Any], action: str) -> Text:
def get_pull_request_comment_action_body(payload: Dict[str, Any], action: str) -> str:
action += ' on'
return get_pull_request_event_message(
get_user_username(payload),
@@ -300,10 +300,10 @@ def get_pull_request_comment_action_body(payload: Dict[str, Any], action: str) -
message=payload['comment']['content']['raw']
)
def get_push_tag_body(payload: Dict[str, Any], change: Dict[str, Any]) -> Text:
def get_push_tag_body(payload: Dict[str, Any], change: Dict[str, Any]) -> str:
if change.get('created'):
tag = change['new']
action = 'pushed' # type: Optional[Text]
action = 'pushed' # type: Optional[str]
elif change.get('closed'):
tag = change['old']
action = 'removed'
@@ -317,7 +317,7 @@ def get_push_tag_body(payload: Dict[str, Any], change: Dict[str, Any]) -> Text:
action=action
)
def get_repo_updated_body(payload: Dict[str, Any]) -> Text:
def get_repo_updated_body(payload: Dict[str, Any]) -> str:
changes = ['website', 'name', 'links', 'language', 'full_name', 'description']
body = ""
repo_name = payload['repository']['name']

View File

@@ -1,7 +1,7 @@
import logging
import re
from functools import partial
from typing import Any, Callable, Dict, Optional, Text
from typing import Any, Callable, Dict, Optional
from django.http import HttpRequest, HttpResponse
@@ -20,7 +20,7 @@ from zerver.models import UserProfile
class UnknownEventType(Exception):
pass
def get_opened_or_update_pull_request_body(payload: Dict[str, Any]) -> Text:
def get_opened_or_update_pull_request_body(payload: Dict[str, Any]) -> str:
pull_request = payload['pull_request']
action = payload['action']
if action == 'synchronize':
@@ -39,7 +39,7 @@ def get_opened_or_update_pull_request_body(payload: Dict[str, Any]) -> Text:
assignee=assignee
)
def get_assigned_or_unassigned_pull_request_body(payload: Dict[str, Any]) -> Text:
def get_assigned_or_unassigned_pull_request_body(payload: Dict[str, Any]) -> str:
pull_request = payload['pull_request']
assignee = pull_request.get('assignee')
if assignee is not None:
@@ -54,7 +54,7 @@ def get_assigned_or_unassigned_pull_request_body(payload: Dict[str, Any]) -> Tex
return "{} to {}".format(base_message, assignee)
return base_message
def get_closed_pull_request_body(payload: Dict[str, Any]) -> Text:
def get_closed_pull_request_body(payload: Dict[str, Any]) -> str:
pull_request = payload['pull_request']
action = 'merged' if pull_request['merged'] else 'closed without merge'
return get_pull_request_event_message(
@@ -63,7 +63,7 @@ def get_closed_pull_request_body(payload: Dict[str, Any]) -> Text:
pull_request['html_url'],
)
def get_membership_body(payload: Dict[str, Any]) -> Text:
def get_membership_body(payload: Dict[str, Any]) -> str:
action = payload['action']
member = payload['member']
scope = payload['scope']
@@ -78,7 +78,7 @@ def get_membership_body(payload: Dict[str, Any]) -> Text:
scope
)
def get_member_body(payload: Dict[str, Any]) -> Text:
def get_member_body(payload: Dict[str, Any]) -> str:
return u"{} {} [{}]({}) to [{}]({})".format(
get_sender_name(payload),
payload['action'],
@@ -88,7 +88,7 @@ def get_member_body(payload: Dict[str, Any]) -> Text:
payload['repository']['html_url']
)
def get_issue_body(payload: Dict[str, Any]) -> Text:
def get_issue_body(payload: Dict[str, Any]) -> str:
action = payload['action']
issue = payload['issue']
assignee = issue['assignee']
@@ -101,7 +101,7 @@ def get_issue_body(payload: Dict[str, Any]) -> Text:
assignee=assignee['login'] if assignee else None
)
def get_issue_comment_body(payload: Dict[str, Any]) -> Text:
def get_issue_comment_body(payload: Dict[str, Any]) -> str:
action = payload['action']
comment = payload['comment']
issue = payload['issue']
@@ -120,7 +120,7 @@ def get_issue_comment_body(payload: Dict[str, Any]) -> Text:
comment['body'],
)
def get_fork_body(payload: Dict[str, Any]) -> Text:
def get_fork_body(payload: Dict[str, Any]) -> str:
forkee = payload['forkee']
return u"{} forked [{}]({})".format(
get_sender_name(payload),
@@ -128,17 +128,17 @@ def get_fork_body(payload: Dict[str, Any]) -> Text:
forkee['html_url']
)
def get_deployment_body(payload: Dict[str, Any]) -> Text:
def get_deployment_body(payload: Dict[str, Any]) -> str:
return u'{} created new deployment'.format(
get_sender_name(payload),
)
def get_change_deployment_status_body(payload: Dict[str, Any]) -> Text:
def get_change_deployment_status_body(payload: Dict[str, Any]) -> str:
return u'Deployment changed status to {}'.format(
payload['deployment_status']['state'],
)
def get_create_or_delete_body(payload: Dict[str, Any], action: Text) -> Text:
def get_create_or_delete_body(payload: Dict[str, Any], action: str) -> str:
ref_type = payload['ref_type']
return u'{} {} {} {}'.format(
get_sender_name(payload),
@@ -147,7 +147,7 @@ def get_create_or_delete_body(payload: Dict[str, Any], action: Text) -> Text:
payload['ref']
).rstrip()
def get_commit_comment_body(payload: Dict[str, Any]) -> Text:
def get_commit_comment_body(payload: Dict[str, Any]) -> str:
comment = payload['comment']
comment_url = comment['html_url']
commit_url = comment_url.split('#', 1)[0]
@@ -160,14 +160,14 @@ def get_commit_comment_body(payload: Dict[str, Any]) -> Text:
comment['body'],
)
def get_push_tags_body(payload: Dict[str, Any]) -> Text:
def get_push_tags_body(payload: Dict[str, Any]) -> str:
return get_push_tag_event_message(
get_sender_name(payload),
get_tag_name_from_ref(payload['ref']),
action='pushed' if payload.get('created') else 'removed'
)
def get_push_commits_body(payload: Dict[str, Any]) -> Text:
def get_push_commits_body(payload: Dict[str, Any]) -> str:
commits_data = [{
'name': (commit.get('author').get('username') or
commit.get('author').get('name')),
@@ -183,13 +183,13 @@ def get_push_commits_body(payload: Dict[str, Any]) -> Text:
deleted=payload['deleted']
)
def get_public_body(payload: Dict[str, Any]) -> Text:
def get_public_body(payload: Dict[str, Any]) -> str:
return u"{} made [the repository]({}) public".format(
get_sender_name(payload),
payload['repository']['html_url'],
)
def get_wiki_pages_body(payload: Dict[str, Any]) -> Text:
def get_wiki_pages_body(payload: Dict[str, Any]) -> str:
wiki_page_info_template = u"* {action} [{title}]({url})\n"
wiki_info = u''
for page in payload['pages']:
@@ -200,32 +200,32 @@ def get_wiki_pages_body(payload: Dict[str, Any]) -> Text:
)
return u"{}:\n{}".format(get_sender_name(payload), wiki_info.rstrip())
def get_watch_body(payload: Dict[str, Any]) -> Text:
def get_watch_body(payload: Dict[str, Any]) -> str:
return u"{} starred [the repository]({})".format(
get_sender_name(payload),
payload['repository']['html_url']
)
def get_repository_body(payload: Dict[str, Any]) -> Text:
def get_repository_body(payload: Dict[str, Any]) -> str:
return u"{} {} [the repository]({})".format(
get_sender_name(payload),
payload.get('action'),
payload['repository']['html_url']
)
def get_add_team_body(payload: Dict[str, Any]) -> Text:
def get_add_team_body(payload: Dict[str, Any]) -> str:
return u"[The repository]({}) was added to team {}".format(
payload['repository']['html_url'],
payload['team']['name']
)
def get_release_body(payload: Dict[str, Any]) -> Text:
def get_release_body(payload: Dict[str, Any]) -> str:
return u"{} published [the release]({})".format(
get_sender_name(payload),
payload['release']['html_url'],
)
def get_page_build_body(payload: Dict[str, Any]) -> Text:
def get_page_build_body(payload: Dict[str, Any]) -> str:
build = payload['build']
action = build['status']
if action == 'null':
@@ -243,7 +243,7 @@ def get_page_build_body(payload: Dict[str, Any]) -> Text:
action
)
def get_status_body(payload: Dict[str, Any]) -> Text:
def get_status_body(payload: Dict[str, Any]) -> str:
if payload['target_url']:
status = '[{}]({})'.format(
payload['state'],
@@ -257,7 +257,7 @@ def get_status_body(payload: Dict[str, Any]) -> Text:
status
)
def get_pull_request_review_body(payload: Dict[str, Any]) -> Text:
def get_pull_request_review_body(payload: Dict[str, Any]) -> str:
return get_pull_request_event_message(
get_sender_name(payload),
'submitted',
@@ -265,7 +265,7 @@ def get_pull_request_review_body(payload: Dict[str, Any]) -> Text:
type='PR Review'
)
def get_pull_request_review_comment_body(payload: Dict[str, Any]) -> Text:
def get_pull_request_review_comment_body(payload: Dict[str, Any]) -> str:
action = payload['action']
message = None
if action == 'created':
@@ -279,28 +279,28 @@ def get_pull_request_review_comment_body(payload: Dict[str, Any]) -> Text:
type='PR Review Comment'
)
def get_ping_body(payload: Dict[str, Any]) -> Text:
def get_ping_body(payload: Dict[str, Any]) -> str:
return get_setup_webhook_message('GitHub', get_sender_name(payload))
def get_repository_name(payload: Dict[str, Any]) -> Text:
def get_repository_name(payload: Dict[str, Any]) -> str:
return payload['repository']['name']
def get_organization_name(payload: Dict[str, Any]) -> Text:
def get_organization_name(payload: Dict[str, Any]) -> str:
return payload['organization']['login']
def get_sender_name(payload: Dict[str, Any]) -> Text:
def get_sender_name(payload: Dict[str, Any]) -> str:
return payload['sender']['login']
def get_branch_name_from_ref(ref_string: Text) -> Text:
def get_branch_name_from_ref(ref_string: str) -> str:
return re.sub(r'^refs/heads/', '', ref_string)
def get_tag_name_from_ref(ref_string: Text) -> Text:
def get_tag_name_from_ref(ref_string: str) -> str:
return re.sub(r'^refs/tags/', '', ref_string)
def is_commit_push_event(payload: Dict[str, Any]) -> bool:
return bool(re.match(r'^refs/heads/', payload['ref']))
def get_subject_based_on_type(payload: Dict[str, Any], event: Text) -> Text:
def get_subject_based_on_type(payload: Dict[str, Any], event: str) -> str:
if 'pull_request' in event:
return SUBJECT_WITH_PR_OR_ISSUE_INFO_TEMPLATE.format(
repo=get_repository_name(payload),
@@ -371,7 +371,7 @@ EVENT_FUNCTION_MAPPER = {
def api_github_webhook(
request: HttpRequest, user_profile: UserProfile,
payload: Dict[str, Any]=REQ(argument_type='body'),
branches: Text=REQ(default=None)) -> HttpResponse:
branches: str=REQ(default=None)) -> HttpResponse:
event = get_event(request, payload, branches)
if event is not None:
subject = get_subject_based_on_type(payload, event)
@@ -379,7 +379,7 @@ def api_github_webhook(
check_send_webhook_message(request, user_profile, subject, body)
return json_success()
def get_event(request: HttpRequest, payload: Dict[str, Any], branches: Text) -> Optional[str]:
def get_event(request: HttpRequest, payload: Dict[str, Any], branches: str) -> Optional[str]:
event = validate_extract_webhook_http_header(request, 'X_GITHUB_EVENT', 'GitHub')
if event == 'pull_request':
action = payload['action']

View File

@@ -1,5 +1,5 @@
from functools import partial
from typing import Any, Dict, Iterable, Optional, Text
from typing import Any, Dict, Iterable, Optional
import re
from django.http import HttpRequest, HttpResponse
@@ -20,12 +20,12 @@ class UnknownEventType(Exception):
pass
def get_push_event_body(payload: Dict[str, Any]) -> Text:
def get_push_event_body(payload: Dict[str, Any]) -> str:
if payload.get('after') == EMPTY_SHA:
return get_remove_branch_event_body(payload)
return get_normal_push_event_body(payload)
def get_normal_push_event_body(payload: Dict[str, Any]) -> Text:
def get_normal_push_event_body(payload: Dict[str, Any]) -> str:
compare_url = u'{}/compare/{}...{}'.format(
get_repository_homepage(payload),
payload['before'],
@@ -49,20 +49,20 @@ def get_normal_push_event_body(payload: Dict[str, Any]) -> Text:
commits
)
def get_remove_branch_event_body(payload: Dict[str, Any]) -> Text:
def get_remove_branch_event_body(payload: Dict[str, Any]) -> str:
return get_remove_branch_event_message(
get_user_name(payload),
get_branch_name(payload)
)
def get_tag_push_event_body(payload: Dict[str, Any]) -> Text:
def get_tag_push_event_body(payload: Dict[str, Any]) -> str:
return get_push_tag_event_message(
get_user_name(payload),
get_tag_name(payload),
action="pushed" if payload.get('checkout_sha') else "removed"
)
def get_issue_created_event_body(payload: Dict[str, Any]) -> Text:
def get_issue_created_event_body(payload: Dict[str, Any]) -> str:
description = payload['object_attributes'].get('description')
# Filter out multiline hidden comments
if description is not None:
@@ -77,7 +77,7 @@ def get_issue_created_event_body(payload: Dict[str, Any]) -> Text:
get_objects_assignee(payload)
)
def get_issue_event_body(payload: Dict[str, Any], action: Text) -> Text:
def get_issue_event_body(payload: Dict[str, Any], action: str) -> str:
return get_issue_event_message(
get_issue_user_name(payload),
action,
@@ -85,12 +85,12 @@ def get_issue_event_body(payload: Dict[str, Any], action: Text) -> Text:
payload['object_attributes'].get('iid'),
)
def get_merge_request_updated_event_body(payload: Dict[str, Any]) -> Text:
def get_merge_request_updated_event_body(payload: Dict[str, Any]) -> str:
if payload['object_attributes'].get('oldrev'):
return get_merge_request_event_body(payload, "added commit(s) to")
return get_merge_request_open_or_updated_body(payload, "updated")
def get_merge_request_event_body(payload: Dict[str, Any], action: Text) -> Text:
def get_merge_request_event_body(payload: Dict[str, Any], action: str) -> str:
pull_request = payload['object_attributes']
return get_pull_request_event_message(
get_issue_user_name(payload),
@@ -100,7 +100,7 @@ def get_merge_request_event_body(payload: Dict[str, Any], action: Text) -> Text:
type='MR',
)
def get_merge_request_open_or_updated_body(payload: Dict[str, Any], action: Text) -> Text:
def get_merge_request_open_or_updated_body(payload: Dict[str, Any], action: str) -> str:
pull_request = payload['object_attributes']
return get_pull_request_event_message(
get_issue_user_name(payload),
@@ -114,13 +114,13 @@ def get_merge_request_open_or_updated_body(payload: Dict[str, Any], action: Text
type='MR',
)
def get_objects_assignee(payload: Dict[str, Any]) -> Optional[Text]:
def get_objects_assignee(payload: Dict[str, Any]) -> Optional[str]:
assignee_object = payload.get('assignee')
if assignee_object:
return assignee_object.get('name')
return None
def get_commented_commit_event_body(payload: Dict[str, Any]) -> Text:
def get_commented_commit_event_body(payload: Dict[str, Any]) -> str:
comment = payload['object_attributes']
action = u'[commented]({})'.format(comment['url'])
return get_commits_comment_action_message(
@@ -131,7 +131,7 @@ def get_commented_commit_event_body(payload: Dict[str, Any]) -> Text:
comment['note'],
)
def get_commented_merge_request_event_body(payload: Dict[str, Any]) -> Text:
def get_commented_merge_request_event_body(payload: Dict[str, Any]) -> str:
comment = payload['object_attributes']
action = u'[commented]({}) on'.format(comment['url'])
url = u'{}/merge_requests/{}'.format(
@@ -147,7 +147,7 @@ def get_commented_merge_request_event_body(payload: Dict[str, Any]) -> Text:
type='MR'
)
def get_commented_issue_event_body(payload: Dict[str, Any]) -> Text:
def get_commented_issue_event_body(payload: Dict[str, Any]) -> str:
comment = payload['object_attributes']
action = u'[commented]({}) on'.format(comment['url'])
url = u'{}/issues/{}'.format(
@@ -163,7 +163,7 @@ def get_commented_issue_event_body(payload: Dict[str, Any]) -> Text:
type='Issue'
)
def get_commented_snippet_event_body(payload: Dict[str, Any]) -> Text:
def get_commented_snippet_event_body(payload: Dict[str, Any]) -> str:
comment = payload['object_attributes']
action = u'[commented]({}) on'.format(comment['url'])
url = u'{}/snippets/{}'.format(
@@ -179,7 +179,7 @@ def get_commented_snippet_event_body(payload: Dict[str, Any]) -> Text:
type='Snippet'
)
def get_wiki_page_event_body(payload: Dict[str, Any], action: Text) -> Text:
def get_wiki_page_event_body(payload: Dict[str, Any], action: str) -> str:
return u"{} {} [Wiki Page \"{}\"]({}).".format(
get_issue_user_name(payload),
action,
@@ -187,7 +187,7 @@ def get_wiki_page_event_body(payload: Dict[str, Any], action: Text) -> Text:
payload['object_attributes'].get('url'),
)
def get_build_hook_event_body(payload: Dict[str, Any]) -> Text:
def get_build_hook_event_body(payload: Dict[str, Any]) -> str:
build_status = payload.get('build_status')
if build_status == 'created':
action = 'was created'
@@ -201,11 +201,11 @@ def get_build_hook_event_body(payload: Dict[str, Any]) -> Text:
action
)
def get_test_event_body(payload: Dict[str, Any]) -> Text:
def get_test_event_body(payload: Dict[str, Any]) -> str:
return u"Webhook for **{repo}** has been configured successfully! :tada:".format(
repo=get_repo_name(payload))
def get_pipeline_event_body(payload: Dict[str, Any]) -> Text:
def get_pipeline_event_body(payload: Dict[str, Any]) -> str:
pipeline_status = payload['object_attributes'].get('status')
if pipeline_status == 'pending':
action = 'was created'
@@ -219,28 +219,28 @@ def get_pipeline_event_body(payload: Dict[str, Any]) -> Text:
builds_status += u"* {} - {}\n".format(build.get('name'), build.get('status'))
return u"Pipeline {} with build(s):\n{}.".format(action, builds_status[:-1])
def get_repo_name(payload: Dict[str, Any]) -> Text:
def get_repo_name(payload: Dict[str, Any]) -> str:
return payload['project']['name']
def get_user_name(payload: Dict[str, Any]) -> Text:
def get_user_name(payload: Dict[str, Any]) -> str:
return payload['user_name']
def get_issue_user_name(payload: Dict[str, Any]) -> Text:
def get_issue_user_name(payload: Dict[str, Any]) -> str:
return payload['user']['name']
def get_repository_homepage(payload: Dict[str, Any]) -> Text:
def get_repository_homepage(payload: Dict[str, Any]) -> str:
return payload['repository']['homepage']
def get_branch_name(payload: Dict[str, Any]) -> Text:
def get_branch_name(payload: Dict[str, Any]) -> str:
return payload['ref'].replace('refs/heads/', '')
def get_tag_name(payload: Dict[str, Any]) -> Text:
def get_tag_name(payload: Dict[str, Any]) -> str:
return payload['ref'].replace('refs/tags/', '')
def get_object_iid(payload: Dict[str, Any]) -> Text:
def get_object_iid(payload: Dict[str, Any]) -> str:
return payload['object_attributes']['iid']
def get_object_url(payload: Dict[str, Any]) -> Text:
def get_object_url(payload: Dict[str, Any]) -> str:
return payload['object_attributes']['url']
EVENT_FUNCTION_MAPPER = {
@@ -272,7 +272,7 @@ EVENT_FUNCTION_MAPPER = {
@has_request_variables
def api_gitlab_webhook(request: HttpRequest, user_profile: UserProfile,
payload: Dict[str, Any]=REQ(argument_type='body'),
branches: Optional[Text]=REQ(default=None)) -> HttpResponse:
branches: Optional[str]=REQ(default=None)) -> HttpResponse:
event = get_event(request, payload, branches)
if event is not None:
body = get_body_based_on_event(event)(payload)
@@ -283,7 +283,7 @@ def api_gitlab_webhook(request: HttpRequest, user_profile: UserProfile,
def get_body_based_on_event(event: str) -> Any:
return EVENT_FUNCTION_MAPPER[event]
def get_subject_based_on_event(event: str, payload: Dict[str, Any]) -> Text:
def get_subject_based_on_event(event: str, payload: Dict[str, Any]) -> str:
if event == 'Push Hook':
return u"{} / {}".format(get_repo_name(payload), get_branch_name(payload))
elif event == 'Job Hook' or event == 'Build Hook':
@@ -330,7 +330,7 @@ def get_subject_based_on_event(event: str, payload: Dict[str, Any]) -> Text:
)
return get_repo_name(payload)
def get_event(request: HttpRequest, payload: Dict[str, Any], branches: Optional[Text]) -> Optional[str]:
def get_event(request: HttpRequest, payload: Dict[str, Any], branches: Optional[str]) -> Optional[str]:
# if there is no 'action' attribute, then this is a test payload
# and we should ignore it
event = validate_extract_webhook_http_header(request, 'X_GITLAB_EVENT', 'GitLab')

View File

@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
from typing import Any, Dict, Iterable, Optional, Text
from typing import Any, Dict, Iterable, Optional
from django.http import HttpRequest, HttpResponse
from django.utils.translation import ugettext as _
@@ -15,7 +15,7 @@ from zerver.lib.webhooks.git import SUBJECT_WITH_BRANCH_TEMPLATE, \
get_pull_request_event_message, get_push_commits_event_message
from zerver.models import UserProfile
def format_push_event(payload: Dict[str, Any]) -> Text:
def format_push_event(payload: Dict[str, Any]) -> str:
for commit in payload['commits']:
commit['sha'] = commit['id']
@@ -31,7 +31,7 @@ def format_push_event(payload: Dict[str, Any]) -> Text:
return get_push_commits_event_message(**data)
def format_new_branch_event(payload: Dict[str, Any]) -> Text:
def format_new_branch_event(payload: Dict[str, Any]) -> str:
branch_name = payload['ref']
url = '{}/src/{}'.format(payload['repository']['html_url'], branch_name)
@@ -43,7 +43,7 @@ def format_new_branch_event(payload: Dict[str, Any]) -> Text:
}
return get_create_branch_event_message(**data)
def format_pull_request_event(payload: Dict[str, Any]) -> Text:
def format_pull_request_event(payload: Dict[str, Any]) -> str:
data = {
'user_name': payload['pull_request']['user']['username'],
@@ -64,7 +64,7 @@ def format_pull_request_event(payload: Dict[str, Any]) -> Text:
@has_request_variables
def api_gogs_webhook(request: HttpRequest, user_profile: UserProfile,
payload: Dict[str, Any]=REQ(argument_type='body'),
branches: Optional[Text]=REQ(default=None)) -> HttpResponse:
branches: Optional[str]=REQ(default=None)) -> HttpResponse:
repo = payload['repository']['name']
event = validate_extract_webhook_http_header(request, 'X_GOGS_EVENT', 'Gogs')

View File

@@ -1,5 +1,5 @@
# Webhooks for external integrations.
from typing import Any, Callable, Dict, Iterable, Optional, Text, Tuple
from typing import Any, Callable, Dict, Iterable, Optional, Tuple
from django.http import HttpRequest, HttpResponse
from django.utils.translation import ugettext as _