mypy: Fix strict-optional errors in webhooks directory.

This commit is contained in:
umkay
2017-05-24 14:03:06 -07:00
committed by Tim Abbott
parent 8ab6a23a30
commit c1a8fb615c
14 changed files with 104 additions and 98 deletions

View File

@@ -68,7 +68,7 @@ def api_beanstalk_webhook(request, user_profile,
author = payload.get('author_full_name')
url = payload.get('changeset_url')
revision = payload.get('revision')
(short_commit_msg, _, _) = payload.get('message').partition("\n")
(short_commit_msg, _, _) = payload['message'].partition("\n")
subject = "svn r%s" % (revision,)
content = "%s pushed [revision %s](%s):\n\n> %s" % (author, revision, url, short_commit_msg)

View File

@@ -29,7 +29,7 @@ def api_bitbucket_webhook(request, user_profile, payload=REQ(validator=check_dic
repository.get('absolute_url'),
commit.get('raw_node'))
}
for commit in payload.get('commits')
for commit in payload['commits']
]
if len(commits) == 0:
@@ -43,7 +43,7 @@ def api_bitbucket_webhook(request, user_profile, payload=REQ(validator=check_dic
branch = payload['commits'][-1]['branch']
if branches is not None and branches.find(branch) == -1:
return json_success()
content = get_push_commits_event_message(payload.get('user'), None, branch, commits)
content = get_push_commits_event_message(payload['user'], None, branch, commits)
subject = SUBJECT_WITH_BRANCH_TEMPLATE.format(repo=repository['name'], branch=branch)
check_send_message(user_profile, get_client("ZulipBitBucketWebhook"), "stream",

View File

@@ -89,20 +89,21 @@ def get_push_subjects(payload):
def get_subject(payload):
# type: (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, type):
# type: (Dict[str, Any], str) -> Text
if type.startswith('pull_request'):
return SUBJECT_WITH_PR_OR_ISSUE_INFO_TEMPLATE.format(
repo=get_repository_name(payload.get('repository')),
repo=get_repository_name(payload['repository']),
type='PR',
id=payload['pullrequest']['id'],
title=payload['pullrequest']['title']
)
if type.startswith('issue'):
return SUBJECT_WITH_PR_OR_ISSUE_INFO_TEMPLATE.format(
repo=get_repository_name(payload.get('repository')),
repo=get_repository_name(payload['repository']),
type='Issue',
id=payload['issue']['id'],
title=payload['issue']['title']
@@ -172,9 +173,9 @@ def get_force_push_body(payload, change):
def get_commit_author_name(commit):
# type: (Dict[str, Any]) -> Text
if commit.get('author').get('user'):
return commit.get('author').get('user').get('username')
return commit.get('author').get('raw').split()[0]
if commit['author'].get('user'):
return commit['author']['user'].get('username')
return commit['author']['raw'].split()[0]
def get_normal_push_body(payload, change):
# type: (Dict[str, Any], Dict[str, Any]) -> Text
@@ -204,7 +205,7 @@ def get_fork_body(payload):
def get_commit_comment_body(payload):
# type: (Dict[str, Any]) -> Text
comment = payload.get('comment')
comment = payload['comment']
action = u'[commented]({})'.format(comment['links']['html']['href'])
return get_commits_comment_action_message(
get_user_username(payload),
@@ -305,18 +306,18 @@ def get_pull_request_comment_action_body(payload, action):
def get_push_tag_body(payload, change):
# type: (Dict[str, Any], Dict[str, Any]) -> Text
if change.get('created'):
tag = change.get('new')
action = 'pushed'
tag = change['new']
action = 'pushed' # type: Optional[Text]
elif change.get('closed'):
tag = change.get('old')
tag = change['old']
action = 'removed'
else:
tag = change.get('new')
tag = change['new']
action = None
return get_push_tag_event_message(
get_user_username(payload),
tag.get('name'),
tag_url=tag.get('links').get('html').get('href'),
tag_url=tag['links']['html'].get('href'),
action=action
)

View File

@@ -137,7 +137,8 @@ def api_github_v2(user_profile, event, payload, branches, default_stream,
target_stream = commit_stream if commit_stream else default_stream
issue_stream = issue_stream if issue_stream else default_stream
repository = payload['repository']
topic_focus = topic_focus if topic_focus else repository['name']
updated_topic_focus = topic_focus if topic_focus else repository['name']
# Event Handlers
if event == 'pull_request':
@@ -166,7 +167,7 @@ def api_github_v2(user_profile, event, payload, branches, default_stream,
content = github_object_commented_content(payload, type)
elif event == 'push':
subject, content = build_message_from_gitlog(user_profile, topic_focus,
subject, content = build_message_from_gitlog(user_profile, updated_topic_focus,
payload['ref'], payload['commits'],
payload['before'], payload['after'],
payload['compare'],
@@ -175,9 +176,9 @@ def api_github_v2(user_profile, event, payload, branches, default_stream,
created=payload['created'],
deleted=payload['deleted'])
elif event == 'commit_comment':
subject = topic_focus
subject = updated_topic_focus
comment = payload.get('comment')
comment = payload['comment']
action = u'[commented]({})'.format(comment['html_url'])
content = get_commits_comment_action_message(
comment['user']['login'],
@@ -301,7 +302,7 @@ def _transform_commits_list_to_common_format(commits):
new_commits_list = []
for commit in commits:
new_commits_list.append({
'name': commit.get('author').get('username'),
'name': commit['author'].get('username'),
'sha': commit.get('id'),
'url': commit.get('url'),
'message': commit.get('message'),

View File

@@ -38,7 +38,7 @@ def get_normal_push_event_body(payload):
'message': commit.get('message'),
'url': commit.get('url')
}
for commit in payload.get('commits')
for commit in payload['commits']
]
return get_push_commits_event_message(
@@ -69,8 +69,8 @@ def get_issue_created_event_body(payload):
get_issue_user_name(payload),
'created',
get_object_url(payload),
payload.get('object_attributes').get('iid'),
payload.get('object_attributes').get('description'),
payload['object_attributes'].get('iid'),
payload['object_attributes'].get('description'),
get_objects_assignee(payload)
)
@@ -80,18 +80,18 @@ def get_issue_event_body(payload, action):
get_issue_user_name(payload),
action,
get_object_url(payload),
payload.get('object_attributes').get('iid'),
payload['object_attributes'].get('iid'),
)
def get_merge_request_updated_event_body(payload):
# type: (Dict[str, Any]) -> Text
if payload.get('object_attributes').get('oldrev'):
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, action):
# type: (Dict[str, Any], Text) -> Text
pull_request = payload.get('object_attributes')
pull_request = payload['object_attributes']
return get_pull_request_event_message(
get_issue_user_name(payload),
action,
@@ -102,7 +102,7 @@ def get_merge_request_event_body(payload, action):
def get_merge_request_open_or_updated_body(payload, action):
# type: (Dict[str, Any], Text) -> Text
pull_request = payload.get('object_attributes')
pull_request = payload['object_attributes']
return get_pull_request_event_message(
get_issue_user_name(payload),
action,
@@ -124,63 +124,63 @@ def get_objects_assignee(payload):
def get_commented_commit_event_body(payload):
# type: (Dict[str, Any]) -> Text
comment = payload.get('object_attributes')
comment = payload['object_attributes']
action = u'[commented]({})'.format(comment['url'])
return get_commits_comment_action_message(
get_issue_user_name(payload),
action,
payload.get('commit').get('url'),
payload.get('commit').get('id'),
payload['commit'].get('url'),
payload['commit'].get('id'),
comment['note'],
)
def get_commented_merge_request_event_body(payload):
# type: (Dict[str, Any]) -> Text
comment = payload.get('object_attributes')
comment = payload['object_attributes']
action = u'[commented]({}) on'.format(comment['url'])
url = u'{}/merge_requests/{}'.format(
payload.get('project').get('web_url'),
payload.get('merge_request').get('iid')
payload['project'].get('web_url'),
payload['merge_request'].get('iid')
)
return get_pull_request_event_message(
get_issue_user_name(payload),
action,
url,
payload.get('merge_request').get('iid'),
payload['merge_request'].get('iid'),
message=comment['note'],
type='MR'
)
def get_commented_issue_event_body(payload):
# type: (Dict[str, Any]) -> Text
comment = payload.get('object_attributes')
comment = payload['object_attributes']
action = u'[commented]({}) on'.format(comment['url'])
url = u'{}/issues/{}'.format(
payload.get('project').get('web_url'),
payload.get('issue').get('iid')
payload['project'].get('web_url'),
payload['issue'].get('iid')
)
return get_pull_request_event_message(
get_issue_user_name(payload),
action,
url,
payload.get('issue').get('iid'),
payload['issue'].get('iid'),
message=comment['note'],
type='Issue'
)
def get_commented_snippet_event_body(payload):
# type: (Dict[str, Any]) -> Text
comment = payload.get('object_attributes')
comment = payload['object_attributes']
action = u'[commented]({}) on'.format(comment['url'])
url = u'{}/snippets/{}'.format(
payload.get('project').get('web_url'),
payload.get('snippet').get('id')
payload['project'].get('web_url'),
payload['snippet'].get('id')
)
return get_pull_request_event_message(
get_issue_user_name(payload),
action,
url,
payload.get('snippet').get('id'),
payload['snippet'].get('id'),
message=comment['note'],
type='Snippet'
)
@@ -190,8 +190,8 @@ def get_wiki_page_event_body(payload, action):
return u"{} {} [Wiki Page \"{}\"]({}).".format(
get_issue_user_name(payload),
action,
payload.get('object_attributes').get('title'),
payload.get('object_attributes').get('url'),
payload['object_attributes'].get('title'),
payload['object_attributes'].get('url'),
)
def get_build_hook_event_body(payload):
@@ -211,7 +211,7 @@ def get_build_hook_event_body(payload):
def get_pipeline_event_body(payload):
# type: (Dict[str, Any]) -> Text
pipeline_status = payload.get('object_attributes').get('status')
pipeline_status = payload['object_attributes'].get('status')
if pipeline_status == 'pending':
action = 'was created'
elif pipeline_status == 'running':
@@ -220,7 +220,7 @@ def get_pipeline_event_body(payload):
action = 'changed status to {}'.format(pipeline_status)
builds_status = u""
for build in payload.get('builds'):
for build in payload['builds']:
builds_status += u"* {} - {}\n".format(build.get('name'), build.get('status'))
return u"Pipeline {} with build(s):\n{}.".format(action, builds_status[:-1])
@@ -300,63 +300,63 @@ def get_subject_based_on_event(event, payload):
if event == 'Push Hook':
return u"{} / {}".format(get_repo_name(payload), get_branch_name(payload))
elif event == 'Build Hook':
return u"{} / {}".format(payload.get('repository').get('name'), get_branch_name(payload))
return u"{} / {}".format(payload['repository'].get('name'), get_branch_name(payload))
elif event == 'Pipeline Hook':
return u"{} / {}".format(
get_repo_name(payload),
payload.get('object_attributes').get('ref').replace('refs/heads/', ''))
payload['object_attributes'].get('ref').replace('refs/heads/', ''))
elif event.startswith('Merge Request Hook'):
return SUBJECT_WITH_PR_OR_ISSUE_INFO_TEMPLATE.format(
repo=get_repo_name(payload),
type='MR',
id=payload.get('object_attributes').get('iid'),
title=payload.get('object_attributes').get('title')
id=payload['object_attributes'].get('iid'),
title=payload['object_attributes'].get('title')
)
elif event.startswith('Issue Hook'):
return SUBJECT_WITH_PR_OR_ISSUE_INFO_TEMPLATE.format(
repo=get_repo_name(payload),
type='Issue',
id=payload.get('object_attributes').get('iid'),
title=payload.get('object_attributes').get('title')
id=payload['object_attributes'].get('iid'),
title=payload['object_attributes'].get('title')
)
elif event == 'Note Hook Issue':
return SUBJECT_WITH_PR_OR_ISSUE_INFO_TEMPLATE.format(
repo=get_repo_name(payload),
type='Issue',
id=payload.get('issue').get('iid'),
title=payload.get('issue').get('title')
id=payload['issue'].get('iid'),
title=payload['issue'].get('title')
)
elif event == 'Note Hook MergeRequest':
return SUBJECT_WITH_PR_OR_ISSUE_INFO_TEMPLATE.format(
repo=get_repo_name(payload),
type='MR',
id=payload.get('merge_request').get('iid'),
title=payload.get('merge_request').get('title')
id=payload['merge_request'].get('iid'),
title=payload['merge_request'].get('title')
)
elif event == 'Note Hook Snippet':
return SUBJECT_WITH_PR_OR_ISSUE_INFO_TEMPLATE.format(
repo=get_repo_name(payload),
type='Snippet',
id=payload.get('snippet').get('id'),
title=payload.get('snippet').get('title')
id=payload['snippet'].get('id'),
title=payload['snippet'].get('title')
)
return get_repo_name(payload)
def get_event(request, payload, branches):
# type: (HttpRequest, Dict[str, Any], Text) -> Optional[str]
# type: (HttpRequest, Dict[str, Any], Optional[Text]) -> Optional[str]
event = request.META['HTTP_X_GITLAB_EVENT']
if event == 'Issue Hook':
action = payload.get('object_attributes').get('action')
action = payload['object_attributes'].get('action')
event = "{} {}".format(event, action)
elif event == 'Note Hook':
action = payload.get('object_attributes').get('noteable_type')
action = payload['object_attributes'].get('noteable_type')
event = "{} {}".format(event, action)
elif event == 'Merge Request Hook':
action = payload.get('object_attributes').get('action')
action = payload['object_attributes'].get('action')
event = "{} {}".format(event, action)
elif event == 'Wiki Page Hook':
action = payload.get('object_attributes').get('action')
action = payload['object_attributes'].get('action')
event = "{} {}".format(event, action)
elif event == 'Push Hook':
if branches is not None:

View File

@@ -151,7 +151,7 @@ def get_sub_event_for_update_issue(payload):
return sub_event
def get_event_type(payload):
# type: (Dict[str, Any]) -> Text
# type: (Dict[str, Any]) -> Optional[Text]
event = payload.get('webhookEvent')
if event is None and payload.get('transition'):
event = 'jira:issue_updated'

View File

@@ -1,6 +1,6 @@
# Webhooks for external integrations.
from __future__ import absolute_import
from typing import Any, Callable, Dict, Iterable, Optional, Tuple
from typing import Any, Callable, Dict, Iterable, Optional, Tuple, Text
from django.utils.translation import ugettext as _
from django.http import HttpRequest, HttpResponse
@@ -14,10 +14,10 @@ from zerver.models import UserProfile, Stream
@api_key_only_webhook_view("NewRelic")
@has_request_variables
def api_newrelic_webhook(request, user_profile, stream=REQ(),
def api_newrelic_webhook(request, user_profile, stream=REQ(default='newrelic'),
alert=REQ(validator=check_dict([]), default=None),
deployment=REQ(validator=check_dict([]), default=None)):
# type: (HttpRequest, UserProfile, Optional[Stream], Optional[Dict[str, Any]], Optional[Dict[str, Any]]) -> HttpResponse
# type: (HttpRequest, UserProfile, Text, Optional[Dict[str, Any]], Optional[Dict[str, Any]]) -> HttpResponse
if alert:
# Use the message as the subject because it stays the same for
# "opened", "acknowledged", and "closed" messages that should be

View File

@@ -70,7 +70,7 @@ def build_pagerduty_formatdict(message):
def send_raw_pagerduty_json(user_profile, client, stream, message, topic):
# type: (UserProfile, Client, Text, Dict[str, Any], Text) -> None
# type: (UserProfile, Client, Text, Dict[str, Any], Optional[Text]) -> None
subject = topic or 'pagerduty'
body = (
u'Unknown pagerduty message\n'
@@ -82,7 +82,7 @@ def send_raw_pagerduty_json(user_profile, client, stream, message, topic):
def send_formated_pagerduty(user_profile, client, stream, message_type, format_dict, topic):
# type: (UserProfile, Client, Text, Text, Dict[str, Any], Text) -> None
# type: (UserProfile, Client, Text, Text, Dict[str, Any], Optional[Text]) -> None
if message_type in ('incident.trigger', 'incident.unacknowledge'):
template = (u':imp: Incident '
u'[{incident_num}]({incident_url}) {action} by '

View File

@@ -14,7 +14,7 @@ def api_sentry_webhook(request, user_profile,
stream=REQ(default='sentry')):
# type: (HttpRequest, UserProfile, Dict[str, Any], str) -> HttpResponse
subject = "{}".format(payload.get('project_name'))
body = "New {} [issue]({}): {}.".format(payload.get('level').upper(),
body = "New {} [issue]({}): {}.".format(payload['level'].upper(),
payload.get('url'),
payload.get('message'))
check_send_message(user_profile, request.client, 'stream', [stream], subject, body)

View File

@@ -165,7 +165,7 @@ def parse_create_or_delete(message):
def parse_change_event(change_type, message):
# type: (str, Mapping[str, Any]) -> Dict[str, Any]
# type: (str, Mapping[str, Any]) -> Optional[Dict[str, Any]]
""" Parses change event. """
evt = {} # type: Dict[str, Any]
values = {

View File

@@ -20,7 +20,7 @@ from .view.exceptions import UnsupportedAction
def api_trello_webhook(request, user_profile, payload=REQ(argument_type='body'), stream=REQ(default='trello')):
# type: (HttpRequest, UserProfile, Mapping[str, Any], Text) -> HttpResponse
payload = ujson.loads(request.body)
action_type = payload.get('action').get('type')
action_type = payload['action'].get('type')
try:
subject, body = get_subject_and_body(payload, action_type)
except UnsupportedAction:

View File

@@ -20,7 +20,7 @@ from .exceptions import UnsupportedAction
def api_trello_webhook(request, user_profile, payload=REQ(argument_type='body'), stream=REQ(default='trello')):
# type: (HttpRequest, UserProfile, Mapping[str, Any], Text) -> HttpResponse
payload = ujson.loads(request.body)
action_type = payload.get('action').get('type')
action_type = payload['action'].get('type')
try:
subject, body = get_subject_and_body(payload, action_type)
except UnsupportedAction:

View File

@@ -64,20 +64,23 @@ def get_proper_action(payload, action_type):
# type: (Mapping[str, Any], Text) -> Text
if action_type == 'updateCard':
data = get_action_data(payload)
old_data = data['old']
card_data = data['card']
if data.get('listBefore'):
return CHANGE_LIST
if data.get('old').get('name'):
if old_data.get('name'):
return CHANGE_NAME
if data.get('old').get('due', False) is None:
if old_data.get('due', False) is None:
return SET_DUE_DATE
if data.get('old').get('due'):
if data.get('card').get('due', False) is None:
if old_data.get('due'):
if card_data.get('due', False) is None:
return REMOVE_DUE_DATE
else:
return CHANGE_DUE_DATE
if data.get('old').get('closed') is False and data.get('card').get('closed'):
if old_data.get('closed') is False and card_data.get('closed'):
return ARCHIVE
if data.get('old').get('closed') and data.get('card').get('closed') is False:
if old_data.get('closed') and card_data.get('closed') is False:
return REOPEN
raise UnknownUpdateCardAction()
@@ -86,28 +89,28 @@ def get_proper_action(payload, action_type):
def get_subject(payload):
# type: (Mapping[str, Any]) -> Text
data = {
'board_name': get_action_data(payload).get('board').get('name')
'board_name': get_action_data(payload)['board'].get('name')
}
return TRELLO_SUBJECT_TEMPLATE.format(**data)
def get_body(payload, action_type):
# type: (Mapping[str, Any], Text) -> Text
message_body = ACTIONS_TO_FILL_BODY_MAPPER.get(action_type)(payload, action_type)
creator = payload.get('action').get('memberCreator').get('fullName')
message_body = ACTIONS_TO_FILL_BODY_MAPPER[action_type](payload, action_type)
creator = payload['action']['memberCreator'].get('fullName')
return TRELLO_MESSAGE_TEMPLATE.format(full_name=creator, rest=message_body)
def get_added_checklist_body(payload, action_type):
# type: (Mapping[str, Any], Text) -> Text
data = {
'checklist_name': get_action_data(payload).get('checklist').get('name'),
'checklist_name': get_action_data(payload)['checklist'].get('name'),
}
return fill_appropriate_message_content(payload, action_type, data)
def get_added_attachment_body(payload, action_type):
# type: (Mapping[str, Any], Text) -> Text
data = {
'attachment_url': get_action_data(payload).get('attachment').get('url'),
'attachment_name': get_action_data(payload).get('attachment').get('name'),
'attachment_url': get_action_data(payload)['attachment'].get('url'),
'attachment_name': get_action_data(payload)['attachment'].get('name'),
}
return fill_appropriate_message_content(payload, action_type, data)
@@ -115,16 +118,17 @@ def get_updated_card_body(payload, action_type):
# type: (Mapping[str, Any], Text) -> Text
data = {
'card_name': get_card_name(payload),
'old_list': get_action_data(payload).get('listBefore').get('name'),
'new_list': get_action_data(payload).get('listAfter').get('name'),
'old_list': get_action_data(payload)['listBefore'].get('name'),
'new_list': get_action_data(payload)['listAfter'].get('name'),
}
return fill_appropriate_message_content(payload, action_type, data)
def get_renamed_card_body(payload, action_type):
# type: (Mapping[str, Any], Text) -> Text
data = {
'old_name': get_action_data(payload).get('old').get('name'),
'new_name': get_action_data(payload).get('card').get('name'),
'old_name': get_action_data(payload)['old'].get('name'),
'new_name': get_action_data(payload)['old'].get('name'),
}
return fill_appropriate_message_content(payload, action_type, data)
@@ -139,22 +143,22 @@ def get_added_label_body(payload, action_type):
def get_managed_member_body(payload, action_type):
# type: (Mapping[str, Any], Text) -> Text
data = {
'member_name': payload.get('action').get('member').get('fullName')
'member_name': payload['action']['member'].get('fullName')
}
return fill_appropriate_message_content(payload, action_type, data)
def get_managed_due_date_body(payload, action_type):
# type: (Mapping[str, Any], Text) -> Text
data = {
'due_date': prettify_date(get_action_data(payload).get('card').get('due'))
'due_date': prettify_date(get_action_data(payload)['card'].get('due'))
}
return fill_appropriate_message_content(payload, action_type, data)
def get_changed_due_date_body(payload, action_type):
# type: (Mapping[str, Any], Text) -> Text
data = {
'due_date': prettify_date(get_action_data(payload).get('card').get('due')),
'old_due_date': prettify_date(get_action_data(payload).get('old').get('due'))
'due_date': prettify_date(get_action_data(payload)['card'].get('due')),
'old_due_date': prettify_date(get_action_data(payload)['old'].get('due'))
}
return fill_appropriate_message_content(payload, action_type, data)
@@ -175,19 +179,19 @@ def get_filled_card_url_template(payload):
def get_card_url(payload):
# type: (Mapping[str, Any]) -> Text
return u'https://trello.com/c/{}'.format(get_action_data(payload).get('card').get('shortLink'))
return u'https://trello.com/c/{}'.format(get_action_data(payload)['card'].get('shortLink'))
def get_message_body(action_type):
# type: (Text) -> Text
return ACTIONS_TO_MESSAGE_MAPPER.get(action_type)
return ACTIONS_TO_MESSAGE_MAPPER[action_type]
def get_card_name(payload):
# type: (Mapping[str, Any]) -> Text
return get_action_data(payload).get('card').get('name')
return get_action_data(payload)['card'].get('name')
def get_action_data(payload):
# type: (Mapping[str, Any]) -> Mapping[str, Any]
return payload.get('action').get('data')
return payload['action'].get('data')
ACTIONS_TO_FILL_BODY_MAPPER = {
CREATE: get_body_by_action_type_without_data,

View File

@@ -11,10 +11,10 @@ import ujson
@api_key_only_webhook_view('Yo')
@has_request_variables
def api_yo_app_webhook(request, user_profile, email=REQ(default=None),
def api_yo_app_webhook(request, user_profile, email=REQ(default=""),
username=REQ(default='Yo Bot'), topic=REQ(default=None),
user_ip=REQ(default=None)):
# type: (HttpRequest, UserProfile, Optional[str], str, Optional[str], Optional[str]) -> HttpResponse
# type: (HttpRequest, UserProfile, str, str, Optional[str], Optional[str]) -> HttpResponse
body = ('Yo from %s') % (username,)
check_send_message(user_profile, request.client, 'private', [email], topic, body)