2017-11-16 00:43:10 +01:00
|
|
|
import re
|
2016-10-25 14:50:42 +02:00
|
|
|
from functools import partial
|
2019-02-02 23:53:55 +01:00
|
|
|
from typing import Any, Dict, Optional
|
2018-07-20 21:36:18 +02:00
|
|
|
from inspect import signature
|
2017-11-16 00:43:10 +01:00
|
|
|
|
2016-10-25 14:50:42 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2017-11-16 00:43:10 +01:00
|
|
|
|
2017-10-31 04:25:48 +01:00
|
|
|
from zerver.decorator import api_key_only_webhook_view
|
2019-02-02 23:53:55 +01:00
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2016-10-25 14:50:42 +02:00
|
|
|
from zerver.lib.response import json_success
|
2018-04-24 20:54:13 +02:00
|
|
|
from zerver.lib.webhooks.common import check_send_webhook_message, \
|
2018-05-22 16:46:45 +02:00
|
|
|
validate_extract_webhook_http_header, UnexpectedWebhookEventType
|
2017-11-16 00:43:10 +01:00
|
|
|
from zerver.lib.webhooks.git import CONTENT_MESSAGE_TEMPLATE, \
|
2018-11-09 20:59:15 +01:00
|
|
|
TOPIC_WITH_BRANCH_TEMPLATE, TOPIC_WITH_PR_OR_ISSUE_INFO_TEMPLATE, \
|
2017-11-16 00:43:10 +01:00
|
|
|
get_commits_comment_action_message, get_issue_event_message, \
|
|
|
|
get_pull_request_event_message, get_push_commits_event_message, \
|
|
|
|
get_push_tag_event_message, get_setup_webhook_message
|
2017-05-02 01:00:50 +02:00
|
|
|
from zerver.models import UserProfile
|
2016-10-25 14:50:42 +02:00
|
|
|
|
|
|
|
class UnknownEventType(Exception):
|
|
|
|
pass
|
|
|
|
|
2018-07-20 21:36:18 +02:00
|
|
|
def get_opened_or_update_pull_request_body(payload: Dict[str, Any],
|
|
|
|
include_title: Optional[bool]=False) -> str:
|
2016-10-25 14:50:42 +02:00
|
|
|
pull_request = payload['pull_request']
|
|
|
|
action = payload['action']
|
2017-02-02 20:09:57 +01:00
|
|
|
if action == 'synchronize':
|
2016-10-25 14:50:42 +02:00
|
|
|
action = 'updated'
|
|
|
|
assignee = None
|
|
|
|
if pull_request.get('assignee'):
|
|
|
|
assignee = pull_request['assignee']['login']
|
|
|
|
|
|
|
|
return get_pull_request_event_message(
|
|
|
|
get_sender_name(payload),
|
|
|
|
action,
|
|
|
|
pull_request['html_url'],
|
|
|
|
target_branch=pull_request['head']['ref'],
|
|
|
|
base_branch=pull_request['base']['ref'],
|
|
|
|
message=pull_request['body'],
|
2018-07-20 21:36:18 +02:00
|
|
|
assignee=assignee,
|
|
|
|
number=pull_request['number'],
|
|
|
|
title=pull_request['title'] if include_title else None
|
2016-10-25 14:50:42 +02:00
|
|
|
)
|
|
|
|
|
2018-07-20 21:36:18 +02:00
|
|
|
def get_assigned_or_unassigned_pull_request_body(payload: Dict[str, Any],
|
|
|
|
include_title: Optional[bool]=False) -> str:
|
2017-02-08 20:41:43 +01:00
|
|
|
pull_request = payload['pull_request']
|
2017-08-25 01:01:46 +02:00
|
|
|
assignee = pull_request.get('assignee')
|
|
|
|
if assignee is not None:
|
|
|
|
assignee = assignee.get('login')
|
2017-02-08 20:41:43 +01:00
|
|
|
|
|
|
|
base_message = get_pull_request_event_message(
|
|
|
|
get_sender_name(payload),
|
|
|
|
payload['action'],
|
|
|
|
pull_request['html_url'],
|
2018-07-20 21:36:18 +02:00
|
|
|
number=pull_request['number'],
|
|
|
|
title=pull_request['title'] if include_title else None
|
2017-02-08 20:41:43 +01:00
|
|
|
)
|
2017-08-25 01:01:46 +02:00
|
|
|
if assignee is not None:
|
2017-02-08 20:41:43 +01:00
|
|
|
return "{} to {}".format(base_message, assignee)
|
|
|
|
return base_message
|
|
|
|
|
2018-07-20 21:36:18 +02:00
|
|
|
def get_closed_pull_request_body(payload: Dict[str, Any],
|
|
|
|
include_title: Optional[bool]=False) -> str:
|
2016-10-25 14:50:42 +02:00
|
|
|
pull_request = payload['pull_request']
|
|
|
|
action = 'merged' if pull_request['merged'] else 'closed without merge'
|
|
|
|
return get_pull_request_event_message(
|
|
|
|
get_sender_name(payload),
|
|
|
|
action,
|
|
|
|
pull_request['html_url'],
|
2018-07-20 21:36:18 +02:00
|
|
|
number=pull_request['number'],
|
|
|
|
title=pull_request['title'] if include_title else None
|
2016-10-25 14:50:42 +02:00
|
|
|
)
|
|
|
|
|
2018-05-11 01:43:34 +02:00
|
|
|
def get_membership_body(payload: Dict[str, Any]) -> str:
|
2016-10-25 14:50:42 +02:00
|
|
|
action = payload['action']
|
|
|
|
member = payload['member']
|
|
|
|
scope = payload['scope']
|
|
|
|
scope_object = payload[scope]
|
|
|
|
|
|
|
|
return u"{} {} [{}]({}) to {} {}".format(
|
|
|
|
get_sender_name(payload),
|
|
|
|
action,
|
|
|
|
member['login'],
|
|
|
|
member['html_url'],
|
|
|
|
scope_object['name'],
|
|
|
|
scope
|
|
|
|
)
|
|
|
|
|
2018-05-11 01:43:34 +02:00
|
|
|
def get_member_body(payload: Dict[str, Any]) -> str:
|
2016-10-25 14:50:42 +02:00
|
|
|
return u"{} {} [{}]({}) to [{}]({})".format(
|
|
|
|
get_sender_name(payload),
|
|
|
|
payload['action'],
|
|
|
|
payload['member']['login'],
|
|
|
|
payload['member']['html_url'],
|
|
|
|
get_repository_name(payload),
|
|
|
|
payload['repository']['html_url']
|
|
|
|
)
|
|
|
|
|
2018-07-20 21:36:18 +02:00
|
|
|
def get_issue_body(payload: Dict[str, Any],
|
|
|
|
include_title: Optional[bool]=False) -> str:
|
2016-10-25 14:50:42 +02:00
|
|
|
action = payload['action']
|
|
|
|
issue = payload['issue']
|
|
|
|
assignee = issue['assignee']
|
|
|
|
return get_issue_event_message(
|
|
|
|
get_sender_name(payload),
|
|
|
|
action,
|
|
|
|
issue['html_url'],
|
|
|
|
issue['number'],
|
|
|
|
issue['body'],
|
2018-07-20 21:36:18 +02:00
|
|
|
assignee=assignee['login'] if assignee else None,
|
|
|
|
title=issue['title'] if include_title else None
|
2016-10-25 14:50:42 +02:00
|
|
|
)
|
|
|
|
|
2018-07-20 21:36:18 +02:00
|
|
|
def get_issue_comment_body(payload: Dict[str, Any],
|
|
|
|
include_title: Optional[bool]=False) -> str:
|
2016-10-25 14:50:42 +02:00
|
|
|
action = payload['action']
|
|
|
|
comment = payload['comment']
|
|
|
|
issue = payload['issue']
|
|
|
|
|
|
|
|
if action == 'created':
|
|
|
|
action = '[commented]'
|
|
|
|
else:
|
2018-09-27 18:57:09 +02:00
|
|
|
action = '{} a [comment]'.format(action)
|
2016-10-25 14:50:42 +02:00
|
|
|
action += '({}) on'.format(comment['html_url'])
|
|
|
|
|
|
|
|
return get_issue_event_message(
|
|
|
|
get_sender_name(payload),
|
|
|
|
action,
|
|
|
|
issue['html_url'],
|
|
|
|
issue['number'],
|
|
|
|
comment['body'],
|
2018-07-20 21:36:18 +02:00
|
|
|
title=issue['title'] if include_title else None
|
2016-10-25 14:50:42 +02:00
|
|
|
)
|
|
|
|
|
2018-05-11 01:43:34 +02:00
|
|
|
def get_fork_body(payload: Dict[str, Any]) -> str:
|
2016-10-25 14:50:42 +02:00
|
|
|
forkee = payload['forkee']
|
|
|
|
return u"{} forked [{}]({})".format(
|
|
|
|
get_sender_name(payload),
|
|
|
|
forkee['name'],
|
|
|
|
forkee['html_url']
|
|
|
|
)
|
|
|
|
|
2018-05-11 01:43:34 +02:00
|
|
|
def get_deployment_body(payload: Dict[str, Any]) -> str:
|
2016-10-25 14:50:42 +02:00
|
|
|
return u'{} created new deployment'.format(
|
|
|
|
get_sender_name(payload),
|
|
|
|
)
|
|
|
|
|
2018-05-11 01:43:34 +02:00
|
|
|
def get_change_deployment_status_body(payload: Dict[str, Any]) -> str:
|
2016-10-25 14:50:42 +02:00
|
|
|
return u'Deployment changed status to {}'.format(
|
|
|
|
payload['deployment_status']['state'],
|
|
|
|
)
|
|
|
|
|
2018-05-11 01:43:34 +02:00
|
|
|
def get_create_or_delete_body(payload: Dict[str, Any], action: str) -> str:
|
2016-10-25 14:50:42 +02:00
|
|
|
ref_type = payload['ref_type']
|
|
|
|
return u'{} {} {} {}'.format(
|
|
|
|
get_sender_name(payload),
|
|
|
|
action,
|
|
|
|
ref_type,
|
|
|
|
payload['ref']
|
2017-01-24 06:34:26 +01:00
|
|
|
).rstrip()
|
2016-10-25 14:50:42 +02:00
|
|
|
|
2018-05-11 01:43:34 +02:00
|
|
|
def get_commit_comment_body(payload: Dict[str, Any]) -> str:
|
2016-10-25 14:50:42 +02:00
|
|
|
comment = payload['comment']
|
|
|
|
comment_url = comment['html_url']
|
|
|
|
commit_url = comment_url.split('#', 1)[0]
|
|
|
|
action = u'[commented]({})'.format(comment_url)
|
|
|
|
return get_commits_comment_action_message(
|
|
|
|
get_sender_name(payload),
|
|
|
|
action,
|
|
|
|
commit_url,
|
|
|
|
comment.get('commit_id'),
|
|
|
|
comment['body'],
|
|
|
|
)
|
|
|
|
|
2018-05-11 01:43:34 +02:00
|
|
|
def get_push_tags_body(payload: Dict[str, Any]) -> str:
|
2016-10-25 14:50:42 +02:00
|
|
|
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'
|
|
|
|
)
|
|
|
|
|
2018-05-11 01:43:34 +02:00
|
|
|
def get_push_commits_body(payload: Dict[str, Any]) -> str:
|
2016-10-25 14:50:42 +02:00
|
|
|
commits_data = [{
|
2017-04-29 08:16:07 +02:00
|
|
|
'name': (commit.get('author').get('username') or
|
|
|
|
commit.get('author').get('name')),
|
2016-10-25 14:50:42 +02:00
|
|
|
'sha': commit['id'],
|
|
|
|
'url': commit['url'],
|
|
|
|
'message': commit['message']
|
|
|
|
} for commit in payload['commits']]
|
|
|
|
return get_push_commits_event_message(
|
|
|
|
get_sender_name(payload),
|
|
|
|
payload['compare'],
|
|
|
|
get_branch_name_from_ref(payload['ref']),
|
2017-05-16 04:38:25 +02:00
|
|
|
commits_data,
|
|
|
|
deleted=payload['deleted']
|
2016-10-25 14:50:42 +02:00
|
|
|
)
|
|
|
|
|
2018-05-11 01:43:34 +02:00
|
|
|
def get_public_body(payload: Dict[str, Any]) -> str:
|
2016-10-25 14:50:42 +02:00
|
|
|
return u"{} made [the repository]({}) public".format(
|
|
|
|
get_sender_name(payload),
|
|
|
|
payload['repository']['html_url'],
|
|
|
|
)
|
|
|
|
|
2018-05-11 01:43:34 +02:00
|
|
|
def get_wiki_pages_body(payload: Dict[str, Any]) -> str:
|
2016-10-25 14:50:42 +02:00
|
|
|
wiki_page_info_template = u"* {action} [{title}]({url})\n"
|
|
|
|
wiki_info = u''
|
|
|
|
for page in payload['pages']:
|
|
|
|
wiki_info += wiki_page_info_template.format(
|
|
|
|
action=page['action'],
|
|
|
|
title=page['title'],
|
|
|
|
url=page['html_url'],
|
|
|
|
)
|
|
|
|
return u"{}:\n{}".format(get_sender_name(payload), wiki_info.rstrip())
|
|
|
|
|
2018-05-11 01:43:34 +02:00
|
|
|
def get_watch_body(payload: Dict[str, Any]) -> str:
|
2016-10-25 14:50:42 +02:00
|
|
|
return u"{} starred [the repository]({})".format(
|
|
|
|
get_sender_name(payload),
|
|
|
|
payload['repository']['html_url']
|
|
|
|
)
|
|
|
|
|
2018-05-11 01:43:34 +02:00
|
|
|
def get_repository_body(payload: Dict[str, Any]) -> str:
|
2016-10-25 14:50:42 +02:00
|
|
|
return u"{} {} [the repository]({})".format(
|
|
|
|
get_sender_name(payload),
|
|
|
|
payload.get('action'),
|
|
|
|
payload['repository']['html_url']
|
|
|
|
)
|
|
|
|
|
2018-05-11 01:43:34 +02:00
|
|
|
def get_add_team_body(payload: Dict[str, Any]) -> str:
|
2016-10-25 14:50:42 +02:00
|
|
|
return u"[The repository]({}) was added to team {}".format(
|
|
|
|
payload['repository']['html_url'],
|
|
|
|
payload['team']['name']
|
|
|
|
)
|
|
|
|
|
2018-05-11 01:43:34 +02:00
|
|
|
def get_release_body(payload: Dict[str, Any]) -> str:
|
2016-10-25 14:50:42 +02:00
|
|
|
return u"{} published [the release]({})".format(
|
|
|
|
get_sender_name(payload),
|
|
|
|
payload['release']['html_url'],
|
|
|
|
)
|
|
|
|
|
2018-05-11 01:43:34 +02:00
|
|
|
def get_page_build_body(payload: Dict[str, Any]) -> str:
|
2016-10-25 14:50:42 +02:00
|
|
|
build = payload['build']
|
2018-09-27 21:55:45 +02:00
|
|
|
status = build['status']
|
|
|
|
actions = {
|
|
|
|
'null': 'has yet to be built',
|
|
|
|
'building': 'is being built',
|
|
|
|
'errored': 'has failed{}',
|
|
|
|
'built': 'has finished building',
|
|
|
|
}
|
|
|
|
|
|
|
|
action = actions.get(status, 'is {}'.format(status))
|
|
|
|
action.format(
|
|
|
|
CONTENT_MESSAGE_TEMPLATE.format(message=build['error']['message'])
|
|
|
|
)
|
|
|
|
|
2016-10-25 14:50:42 +02:00
|
|
|
return u"Github Pages build, trigerred by {}, {}".format(
|
|
|
|
payload['build']['pusher']['login'],
|
|
|
|
action
|
|
|
|
)
|
|
|
|
|
2018-05-11 01:43:34 +02:00
|
|
|
def get_status_body(payload: Dict[str, Any]) -> str:
|
2016-10-25 14:50:42 +02:00
|
|
|
if payload['target_url']:
|
|
|
|
status = '[{}]({})'.format(
|
|
|
|
payload['state'],
|
|
|
|
payload['target_url']
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
status = payload['state']
|
2017-06-30 02:09:18 +02:00
|
|
|
return u"[{}]({}) changed its status to {}".format(
|
2016-10-25 14:50:42 +02:00
|
|
|
payload['sha'][:7], # TODO
|
|
|
|
payload['commit']['html_url'],
|
|
|
|
status
|
|
|
|
)
|
|
|
|
|
2018-07-20 21:36:18 +02:00
|
|
|
def get_pull_request_review_body(payload: Dict[str, Any],
|
|
|
|
include_title: Optional[bool]=False) -> str:
|
|
|
|
title = "for #{} {}".format(
|
|
|
|
payload['pull_request']['number'],
|
|
|
|
payload['pull_request']['title']
|
|
|
|
)
|
2016-10-25 14:50:42 +02:00
|
|
|
return get_pull_request_event_message(
|
|
|
|
get_sender_name(payload),
|
|
|
|
'submitted',
|
|
|
|
payload['review']['html_url'],
|
2018-07-20 21:36:18 +02:00
|
|
|
type='PR Review',
|
|
|
|
title=title if include_title else None
|
2016-10-25 14:50:42 +02:00
|
|
|
)
|
|
|
|
|
2018-07-20 21:36:18 +02:00
|
|
|
def get_pull_request_review_comment_body(payload: Dict[str, Any],
|
|
|
|
include_title: Optional[bool]=False) -> str:
|
2016-10-25 14:50:42 +02:00
|
|
|
action = payload['action']
|
|
|
|
message = None
|
|
|
|
if action == 'created':
|
|
|
|
message = payload['comment']['body']
|
|
|
|
|
2018-07-20 21:36:18 +02:00
|
|
|
title = "on #{} {}".format(
|
|
|
|
payload['pull_request']['number'],
|
|
|
|
payload['pull_request']['title']
|
|
|
|
)
|
|
|
|
|
2016-10-25 14:50:42 +02:00
|
|
|
return get_pull_request_event_message(
|
|
|
|
get_sender_name(payload),
|
|
|
|
action,
|
|
|
|
payload['comment']['html_url'],
|
|
|
|
message=message,
|
2018-07-20 21:36:18 +02:00
|
|
|
type='PR Review Comment',
|
|
|
|
title=title if include_title else None
|
2016-10-25 14:50:42 +02:00
|
|
|
)
|
|
|
|
|
2018-07-20 21:36:18 +02:00
|
|
|
def get_pull_request_review_requested_body(payload: Dict[str, Any],
|
|
|
|
include_title: Optional[bool]=False) -> str:
|
2018-10-23 20:05:14 +02:00
|
|
|
requested_reviewers = (payload['pull_request']['requested_reviewers'] or
|
|
|
|
[payload['requested_reviewer']])
|
2018-07-01 20:40:08 +02:00
|
|
|
sender = get_sender_name(payload)
|
|
|
|
pr_number = payload['pull_request']['number']
|
|
|
|
pr_url = payload['pull_request']['html_url']
|
|
|
|
message = "**{sender}** requested {reviewers} for a review on [PR #{pr_number}]({pr_url})."
|
2018-07-20 21:36:18 +02:00
|
|
|
message_with_title = ("**{sender}** requested {reviewers} for a review on "
|
|
|
|
"[PR #{pr_number} {title}]({pr_url}).")
|
|
|
|
body = message_with_title if include_title else message
|
|
|
|
|
2018-07-01 20:40:08 +02:00
|
|
|
reviewers = ""
|
|
|
|
if len(requested_reviewers) == 1:
|
|
|
|
reviewers = "[{login}]({html_url})".format(**requested_reviewers[0])
|
|
|
|
else:
|
|
|
|
for reviewer in requested_reviewers[:-1]:
|
|
|
|
reviewers += "[{login}]({html_url}), ".format(**reviewer)
|
|
|
|
reviewers += "and [{login}]({html_url})".format(**requested_reviewers[-1])
|
|
|
|
|
2018-07-20 21:36:18 +02:00
|
|
|
return body.format(
|
2018-07-01 20:40:08 +02:00
|
|
|
sender=sender,
|
|
|
|
reviewers=reviewers,
|
|
|
|
pr_number=pr_number,
|
|
|
|
pr_url=pr_url,
|
2018-07-20 21:36:18 +02:00
|
|
|
title=payload['pull_request']['title'] if include_title else None
|
2018-07-01 20:40:08 +02:00
|
|
|
)
|
|
|
|
|
2018-05-11 01:43:34 +02:00
|
|
|
def get_ping_body(payload: Dict[str, Any]) -> str:
|
2017-03-17 00:18:25 +01:00
|
|
|
return get_setup_webhook_message('GitHub', get_sender_name(payload))
|
|
|
|
|
2018-05-11 01:43:34 +02:00
|
|
|
def get_repository_name(payload: Dict[str, Any]) -> str:
|
2016-10-25 14:50:42 +02:00
|
|
|
return payload['repository']['name']
|
|
|
|
|
2018-05-11 01:43:34 +02:00
|
|
|
def get_organization_name(payload: Dict[str, Any]) -> str:
|
2017-07-17 04:03:54 +02:00
|
|
|
return payload['organization']['login']
|
|
|
|
|
2018-05-11 01:43:34 +02:00
|
|
|
def get_sender_name(payload: Dict[str, Any]) -> str:
|
2016-10-25 14:50:42 +02:00
|
|
|
return payload['sender']['login']
|
|
|
|
|
2018-05-11 01:43:34 +02:00
|
|
|
def get_branch_name_from_ref(ref_string: str) -> str:
|
2016-10-25 14:50:42 +02:00
|
|
|
return re.sub(r'^refs/heads/', '', ref_string)
|
|
|
|
|
2018-05-11 01:43:34 +02:00
|
|
|
def get_tag_name_from_ref(ref_string: str) -> str:
|
2016-10-25 14:50:42 +02:00
|
|
|
return re.sub(r'^refs/tags/', '', ref_string)
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def is_commit_push_event(payload: Dict[str, Any]) -> bool:
|
2016-10-25 14:50:42 +02:00
|
|
|
return bool(re.match(r'^refs/heads/', payload['ref']))
|
|
|
|
|
2018-05-11 01:43:34 +02:00
|
|
|
def get_subject_based_on_type(payload: Dict[str, Any], event: str) -> str:
|
2016-10-25 14:50:42 +02:00
|
|
|
if 'pull_request' in event:
|
2018-11-09 20:59:15 +01:00
|
|
|
return TOPIC_WITH_PR_OR_ISSUE_INFO_TEMPLATE.format(
|
2016-10-25 14:50:42 +02:00
|
|
|
repo=get_repository_name(payload),
|
|
|
|
type='PR',
|
|
|
|
id=payload['pull_request']['number'],
|
|
|
|
title=payload['pull_request']['title']
|
|
|
|
)
|
|
|
|
elif event.startswith('issue'):
|
2018-11-09 20:59:15 +01:00
|
|
|
return TOPIC_WITH_PR_OR_ISSUE_INFO_TEMPLATE.format(
|
2016-10-25 14:50:42 +02:00
|
|
|
repo=get_repository_name(payload),
|
|
|
|
type='Issue',
|
|
|
|
id=payload['issue']['number'],
|
|
|
|
title=payload['issue']['title']
|
|
|
|
)
|
|
|
|
elif event.startswith('deployment'):
|
|
|
|
return u"{} / Deployment on {}".format(
|
|
|
|
get_repository_name(payload),
|
|
|
|
payload['deployment']['environment']
|
|
|
|
)
|
|
|
|
elif event == 'membership':
|
|
|
|
return u"{} organization".format(payload['organization']['login'])
|
|
|
|
elif event == 'push_commits':
|
2018-11-09 20:59:15 +01:00
|
|
|
return TOPIC_WITH_BRANCH_TEMPLATE.format(
|
2016-10-25 14:50:42 +02:00
|
|
|
repo=get_repository_name(payload),
|
|
|
|
branch=get_branch_name_from_ref(payload['ref'])
|
|
|
|
)
|
|
|
|
elif event == 'gollum':
|
2018-11-09 20:59:15 +01:00
|
|
|
return TOPIC_WITH_BRANCH_TEMPLATE.format(
|
2016-10-25 14:50:42 +02:00
|
|
|
repo=get_repository_name(payload),
|
|
|
|
branch='Wiki Pages'
|
|
|
|
)
|
2017-07-17 04:03:54 +02:00
|
|
|
elif event == 'ping':
|
|
|
|
if payload.get('repository') is None:
|
|
|
|
return get_organization_name(payload)
|
2016-10-25 14:50:42 +02:00
|
|
|
return get_repository_name(payload)
|
|
|
|
|
|
|
|
EVENT_FUNCTION_MAPPER = {
|
|
|
|
'team_add': get_add_team_body,
|
|
|
|
'commit_comment': get_commit_comment_body,
|
|
|
|
'closed_pull_request': get_closed_pull_request_body,
|
|
|
|
'create': partial(get_create_or_delete_body, action='created'),
|
|
|
|
'delete': partial(get_create_or_delete_body, action='deleted'),
|
|
|
|
'deployment': get_deployment_body,
|
|
|
|
'deployment_status': get_change_deployment_status_body,
|
|
|
|
'fork': get_fork_body,
|
|
|
|
'gollum': get_wiki_pages_body,
|
|
|
|
'issue_comment': get_issue_comment_body,
|
2017-06-29 07:28:37 +02:00
|
|
|
'issues': get_issue_body,
|
2016-10-25 14:50:42 +02:00
|
|
|
'member': get_member_body,
|
|
|
|
'membership': get_membership_body,
|
|
|
|
'opened_or_update_pull_request': get_opened_or_update_pull_request_body,
|
2017-02-08 20:41:43 +01:00
|
|
|
'assigned_or_unassigned_pull_request': get_assigned_or_unassigned_pull_request_body,
|
2016-10-25 14:50:42 +02:00
|
|
|
'page_build': get_page_build_body,
|
2017-03-17 00:18:25 +01:00
|
|
|
'ping': get_ping_body,
|
2016-10-25 14:50:42 +02:00
|
|
|
'public': get_public_body,
|
|
|
|
'pull_request_review': get_pull_request_review_body,
|
|
|
|
'pull_request_review_comment': get_pull_request_review_comment_body,
|
2018-07-01 20:40:08 +02:00
|
|
|
'pull_request_review_requested': get_pull_request_review_requested_body,
|
2016-10-25 14:50:42 +02:00
|
|
|
'push_commits': get_push_commits_body,
|
|
|
|
'push_tags': get_push_tags_body,
|
|
|
|
'release': get_release_body,
|
|
|
|
'repository': get_repository_body,
|
|
|
|
'status': get_status_body,
|
|
|
|
'watch': get_watch_body,
|
|
|
|
}
|
|
|
|
|
2019-02-19 20:17:40 +01:00
|
|
|
IGNORED_EVENTS = [
|
2019-02-19 22:26:11 +01:00
|
|
|
'repository_vulnerability_alert',
|
|
|
|
'project_card',
|
2019-02-19 20:17:40 +01:00
|
|
|
]
|
|
|
|
|
2018-11-15 05:35:50 +01:00
|
|
|
@api_key_only_webhook_view('GitHub', notify_bot_owner_on_invalid_json=True)
|
2016-10-25 14:50:42 +02:00
|
|
|
@has_request_variables
|
|
|
|
def api_github_webhook(
|
2018-03-16 22:53:50 +01:00
|
|
|
request: HttpRequest, user_profile: UserProfile,
|
|
|
|
payload: Dict[str, Any]=REQ(argument_type='body'),
|
2018-07-20 21:36:18 +02:00
|
|
|
branches: str=REQ(default=None),
|
|
|
|
user_specified_topic: Optional[str]=REQ("topic", default=None)) -> HttpResponse:
|
2017-03-25 03:23:15 +01:00
|
|
|
event = get_event(request, payload, branches)
|
2017-03-17 00:18:25 +01:00
|
|
|
if event is not None:
|
2017-02-01 00:37:13 +01:00
|
|
|
subject = get_subject_based_on_type(payload, event)
|
2018-07-20 21:36:18 +02:00
|
|
|
body_function = get_body_function_based_on_type(event)
|
|
|
|
if 'include_title' in signature(body_function).parameters:
|
|
|
|
body = body_function(
|
|
|
|
payload,
|
|
|
|
include_title=user_specified_topic is not None
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
body = body_function(payload)
|
2018-03-16 22:53:50 +01:00
|
|
|
check_send_webhook_message(request, user_profile, subject, body)
|
2016-10-25 14:50:42 +02:00
|
|
|
return json_success()
|
|
|
|
|
2018-05-11 01:43:34 +02:00
|
|
|
def get_event(request: HttpRequest, payload: Dict[str, Any], branches: str) -> Optional[str]:
|
2018-04-24 20:54:13 +02:00
|
|
|
event = validate_extract_webhook_http_header(request, 'X_GITHUB_EVENT', 'GitHub')
|
2016-10-25 14:50:42 +02:00
|
|
|
if event == 'pull_request':
|
|
|
|
action = payload['action']
|
2017-02-08 20:41:43 +01:00
|
|
|
if action in ('opened', 'synchronize', 'reopened', 'edited'):
|
2016-10-25 14:50:42 +02:00
|
|
|
return 'opened_or_update_pull_request'
|
2017-02-08 20:41:43 +01:00
|
|
|
if action in ('assigned', 'unassigned'):
|
|
|
|
return 'assigned_or_unassigned_pull_request'
|
2016-10-25 14:50:42 +02:00
|
|
|
if action == 'closed':
|
|
|
|
return 'closed_pull_request'
|
2018-07-01 20:40:08 +02:00
|
|
|
if action == 'review_requested':
|
|
|
|
return '{}_{}'.format(event, action)
|
2018-07-01 19:29:45 +02:00
|
|
|
# Unsupported pull_request events
|
|
|
|
if action in ('labeled', 'unlabeled', 'review_request_removed'):
|
|
|
|
return None
|
2016-10-25 14:50:42 +02:00
|
|
|
if event == 'push':
|
|
|
|
if is_commit_push_event(payload):
|
2017-03-25 03:23:15 +01:00
|
|
|
if branches is not None:
|
|
|
|
branch = get_branch_name_from_ref(payload['ref'])
|
2017-04-25 02:20:39 +02:00
|
|
|
if branches.find(branch) == -1:
|
2017-03-25 03:23:15 +01:00
|
|
|
return None
|
2016-10-25 14:50:42 +02:00
|
|
|
return "push_commits"
|
|
|
|
else:
|
|
|
|
return "push_tags"
|
2017-02-01 00:37:13 +01:00
|
|
|
elif event in list(EVENT_FUNCTION_MAPPER.keys()) or event == 'ping':
|
2016-10-25 14:50:42 +02:00
|
|
|
return event
|
2019-02-19 20:17:40 +01:00
|
|
|
elif event in IGNORED_EVENTS:
|
|
|
|
return None
|
2018-05-22 16:46:45 +02:00
|
|
|
|
|
|
|
raise UnexpectedWebhookEventType('GitHub', event)
|
2016-10-25 14:50:42 +02:00
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def get_body_function_based_on_type(type: str) -> Any:
|
2016-10-25 14:50:42 +02:00
|
|
|
return EVENT_FUNCTION_MAPPER.get(type)
|