2016-03-11 22:12:41 +01:00
|
|
|
from __future__ import absolute_import
|
|
|
|
from django.conf import settings
|
2016-06-07 05:52:33 +02:00
|
|
|
from zerver.models import get_client, UserProfile
|
2016-03-11 22:12:41 +01:00
|
|
|
from zerver.lib.response import json_success
|
|
|
|
from zerver.lib.validator import check_dict
|
|
|
|
from zerver.decorator import authenticated_api_view, REQ, has_request_variables, to_non_negative_int, flexible_boolean
|
|
|
|
from zerver.views.messages import send_message_backend
|
2016-10-05 19:26:09 +02:00
|
|
|
from zerver.lib.webhooks.git import get_push_commits_event_message, SUBJECT_WITH_BRANCH_TEMPLATE
|
2016-03-11 22:12:41 +01:00
|
|
|
import logging
|
|
|
|
import re
|
|
|
|
import ujson
|
|
|
|
|
2016-06-07 05:52:33 +02:00
|
|
|
from six import text_type
|
|
|
|
from typing import Any, Mapping, Optional, Sequence, Tuple
|
2016-06-26 10:34:24 +02:00
|
|
|
from zerver.lib.str_utils import force_str
|
2016-06-07 05:52:33 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2016-06-26 10:34:24 +02:00
|
|
|
|
2016-04-20 21:24:01 +02:00
|
|
|
COMMITS_IN_LIST_LIMIT = 10
|
|
|
|
ZULIP_TEST_REPO_NAME = 'zulip-test'
|
|
|
|
ZULIP_TEST_REPO_ID = 6893087
|
2016-03-11 22:12:41 +01:00
|
|
|
|
2016-04-20 21:25:53 +02:00
|
|
|
def is_test_repository(repository):
|
2016-06-07 05:52:33 +02:00
|
|
|
# type: (Mapping[text_type, Any]) -> bool
|
2016-04-20 21:25:53 +02:00
|
|
|
return repository['name'] == ZULIP_TEST_REPO_NAME and repository['id'] == ZULIP_TEST_REPO_ID
|
|
|
|
|
2016-04-20 21:34:16 +02:00
|
|
|
class UnknownEventType(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2016-03-11 22:12:41 +01:00
|
|
|
def github_generic_subject(noun, topic_focus, blob):
|
2016-06-07 05:52:33 +02:00
|
|
|
# type: (text_type, text_type, Mapping[text_type, Any]) -> text_type
|
2016-03-11 22:12:41 +01:00
|
|
|
# issue and pull_request objects have the same fields we're interested in
|
2016-06-26 10:23:21 +02:00
|
|
|
return u'%s: %s %d: %s' % (topic_focus, noun, blob['number'], blob['title'])
|
2016-03-11 22:12:41 +01:00
|
|
|
|
|
|
|
def github_generic_content(noun, payload, blob):
|
2016-06-07 05:52:33 +02:00
|
|
|
# type: (text_type, Mapping[text_type, Any], Mapping[text_type, Any]) -> text_type
|
2016-04-20 21:30:55 +02:00
|
|
|
action = 'synchronized' if payload['action'] == 'synchronize' else payload['action']
|
2016-03-11 22:12:41 +01:00
|
|
|
|
|
|
|
# issue and pull_request objects have the same fields we're interested in
|
2016-06-26 10:23:21 +02:00
|
|
|
content = (u'%s %s [%s %s](%s)'
|
2016-03-11 22:12:41 +01:00
|
|
|
% (payload['sender']['login'],
|
|
|
|
action,
|
|
|
|
noun,
|
|
|
|
blob['number'],
|
|
|
|
blob['html_url']))
|
|
|
|
if payload['action'] in ('opened', 'reopened'):
|
2016-06-26 10:23:21 +02:00
|
|
|
content += u'\n\n~~~ quote\n%s\n~~~' % (blob['body'],)
|
2016-03-11 22:12:41 +01:00
|
|
|
return content
|
|
|
|
|
|
|
|
|
|
|
|
def api_github_v1(user_profile, event, payload, branches, stream, **kwargs):
|
2016-06-07 05:52:33 +02:00
|
|
|
# type: (UserProfile, text_type, Mapping[text_type, Any], text_type, text_type, **Any) -> Tuple[text_type, text_type, text_type]
|
2016-03-11 22:12:41 +01:00
|
|
|
"""
|
|
|
|
processes github payload with version 1 field specification
|
|
|
|
`payload` comes in unmodified from github
|
|
|
|
`stream` is set to 'commits' if otherwise unset
|
|
|
|
"""
|
|
|
|
commit_stream = stream
|
|
|
|
issue_stream = 'issues'
|
|
|
|
return api_github_v2(user_profile, event, payload, branches, stream, commit_stream, issue_stream, **kwargs)
|
|
|
|
|
|
|
|
|
2016-07-17 19:49:53 +02:00
|
|
|
def api_github_v2(user_profile, event, payload, branches, default_stream,
|
|
|
|
commit_stream, issue_stream, topic_focus = None):
|
2016-06-07 05:52:33 +02:00
|
|
|
# type: (UserProfile, text_type, Mapping[text_type, Any], text_type, text_type, text_type, text_type, Optional[text_type]) -> Tuple[text_type, text_type, text_type]
|
2016-03-11 22:12:41 +01:00
|
|
|
"""
|
|
|
|
processes github payload with version 2 field specification
|
|
|
|
`payload` comes in unmodified from github
|
|
|
|
`default_stream` is set to what `stream` is in v1 above
|
|
|
|
`commit_stream` and `issue_stream` fall back to `default_stream` if they are empty
|
|
|
|
This and allowing alternative endpoints is what distinguishes v1 from v2 of the github configuration
|
|
|
|
"""
|
2016-04-20 21:30:55 +02:00
|
|
|
target_stream = commit_stream if commit_stream else default_stream
|
|
|
|
issue_stream = issue_stream if issue_stream else default_stream
|
2016-03-11 22:12:41 +01:00
|
|
|
repository = payload['repository']
|
2016-04-20 21:30:55 +02:00
|
|
|
topic_focus = topic_focus if topic_focus else repository['name']
|
2016-03-11 22:12:41 +01:00
|
|
|
|
|
|
|
# Event Handlers
|
|
|
|
if event == 'pull_request':
|
|
|
|
pull_req = payload['pull_request']
|
|
|
|
subject = github_generic_subject('pull request', topic_focus, pull_req)
|
|
|
|
content = github_generic_content('pull request', payload, pull_req)
|
|
|
|
elif event == 'issues':
|
|
|
|
# in v1, we assume that this stream exists since it is
|
|
|
|
# deprecated and the few realms that use it already have the
|
|
|
|
# stream
|
|
|
|
target_stream = issue_stream
|
|
|
|
issue = payload['issue']
|
|
|
|
subject = github_generic_subject('issue', topic_focus, issue)
|
|
|
|
content = github_generic_content('issue', payload, issue)
|
|
|
|
elif event == 'issue_comment':
|
|
|
|
# Comments on both issues and pull requests come in as issue_comment events
|
|
|
|
issue = payload['issue']
|
|
|
|
if 'pull_request' not in issue or issue['pull_request']['diff_url'] is None:
|
|
|
|
# It's an issues comment
|
|
|
|
target_stream = issue_stream
|
|
|
|
noun = 'issue'
|
|
|
|
else:
|
|
|
|
# It's a pull request comment
|
|
|
|
noun = 'pull request'
|
|
|
|
|
|
|
|
subject = github_generic_subject(noun, topic_focus, issue)
|
|
|
|
comment = payload['comment']
|
2016-06-26 10:23:21 +02:00
|
|
|
content = (u'%s [commented](%s) on [%s %d](%s)\n\n~~~ quote\n%s\n~~~'
|
2016-03-11 22:12:41 +01:00
|
|
|
% (comment['user']['login'],
|
|
|
|
comment['html_url'],
|
|
|
|
noun,
|
|
|
|
issue['number'],
|
|
|
|
issue['html_url'],
|
|
|
|
comment['body']))
|
|
|
|
elif event == 'push':
|
|
|
|
subject, content = build_message_from_gitlog(user_profile, topic_focus,
|
|
|
|
payload['ref'], payload['commits'],
|
|
|
|
payload['before'], payload['after'],
|
|
|
|
payload['compare'],
|
|
|
|
payload['pusher']['name'],
|
|
|
|
forced=payload['forced'],
|
|
|
|
created=payload['created'])
|
|
|
|
elif event == 'commit_comment':
|
|
|
|
comment = payload['comment']
|
2016-06-26 10:23:21 +02:00
|
|
|
subject = u'%s: commit %s' % (topic_focus, comment['commit_id'])
|
2016-03-11 22:12:41 +01:00
|
|
|
|
2016-06-26 10:23:21 +02:00
|
|
|
content = (u'%s [commented](%s)'
|
2016-03-11 22:12:41 +01:00
|
|
|
% (comment['user']['login'],
|
|
|
|
comment['html_url']))
|
|
|
|
|
|
|
|
if comment['line'] is not None:
|
2016-06-26 10:23:21 +02:00
|
|
|
content += u' on `%s`, line %d' % (comment['path'], comment['line'])
|
2016-03-11 22:12:41 +01:00
|
|
|
|
2016-06-26 10:23:21 +02:00
|
|
|
content += u'\n\n~~~ quote\n%s\n~~~' % (comment['body'],)
|
2016-03-11 22:12:41 +01:00
|
|
|
|
2016-04-20 21:34:16 +02:00
|
|
|
else:
|
2016-06-26 10:34:24 +02:00
|
|
|
raise UnknownEventType(force_str(u'Event %s is unknown and cannot be handled' % (event,)))
|
2016-04-20 21:34:16 +02:00
|
|
|
|
2016-04-20 21:27:51 +02:00
|
|
|
return target_stream, subject, content
|
2016-03-11 22:12:41 +01:00
|
|
|
|
2016-05-18 20:35:35 +02:00
|
|
|
@authenticated_api_view(is_webhook=True)
|
2016-03-11 22:12:41 +01:00
|
|
|
@has_request_variables
|
2016-05-31 16:29:39 +02:00
|
|
|
def api_github_landing(request, user_profile, event=REQ(),
|
2016-03-11 22:12:41 +01:00
|
|
|
payload=REQ(validator=check_dict([])),
|
|
|
|
branches=REQ(default=''),
|
|
|
|
stream=REQ(default=''),
|
|
|
|
version=REQ(converter=to_non_negative_int, default=1),
|
|
|
|
commit_stream=REQ(default=''),
|
|
|
|
issue_stream=REQ(default=''),
|
|
|
|
exclude_pull_requests=REQ(converter=flexible_boolean, default=False),
|
|
|
|
exclude_issues=REQ(converter=flexible_boolean, default=False),
|
|
|
|
exclude_commits=REQ(converter=flexible_boolean, default=False),
|
|
|
|
emphasize_branch_in_topic=REQ(converter=flexible_boolean, default=False),
|
|
|
|
):
|
2016-06-07 05:52:33 +02:00
|
|
|
# type: (HttpRequest, UserProfile, text_type, Mapping[text_type, Any], text_type, text_type, int, text_type, text_type, bool, bool, bool, bool) -> HttpResponse
|
2016-03-11 22:12:41 +01:00
|
|
|
|
|
|
|
repository = payload['repository']
|
|
|
|
|
|
|
|
# Special hook for capturing event data. If we see our special test repo, log the payload from github.
|
|
|
|
try:
|
2016-04-20 21:25:53 +02:00
|
|
|
if is_test_repository(repository) and settings.PRODUCTION:
|
2016-03-11 22:12:41 +01:00
|
|
|
with open('/var/log/zulip/github-payloads', 'a') as f:
|
|
|
|
f.write(ujson.dumps({'event': event,
|
|
|
|
'payload': payload,
|
|
|
|
'branches': branches,
|
|
|
|
'stream': stream,
|
|
|
|
'version': version,
|
|
|
|
'commit_stream': commit_stream,
|
|
|
|
'issue_stream': issue_stream,
|
|
|
|
'exclude_pull_requests': exclude_pull_requests,
|
|
|
|
'exclude_issues': exclude_issues,
|
|
|
|
'exclude_commits': exclude_commits,
|
|
|
|
'emphasize_branch_in_topic': emphasize_branch_in_topic,
|
|
|
|
}))
|
2016-04-20 21:35:38 +02:00
|
|
|
f.write('\n')
|
2016-03-11 22:12:41 +01:00
|
|
|
except Exception:
|
2016-04-20 21:35:38 +02:00
|
|
|
logging.exception('Error while capturing Github event')
|
2016-03-11 22:12:41 +01:00
|
|
|
|
|
|
|
if not stream:
|
|
|
|
stream = 'commits'
|
|
|
|
|
2016-04-20 21:35:38 +02:00
|
|
|
short_ref = re.sub(r'^refs/heads/', '', payload.get('ref', ''))
|
2016-03-11 22:12:41 +01:00
|
|
|
kwargs = dict()
|
|
|
|
|
|
|
|
if emphasize_branch_in_topic and short_ref:
|
|
|
|
kwargs['topic_focus'] = short_ref
|
|
|
|
|
|
|
|
allowed_events = set()
|
|
|
|
if not exclude_pull_requests:
|
|
|
|
allowed_events.add('pull_request')
|
|
|
|
|
|
|
|
if not exclude_issues:
|
2016-04-20 21:35:38 +02:00
|
|
|
allowed_events.add('issues')
|
|
|
|
allowed_events.add('issue_comment')
|
2016-03-11 22:12:41 +01:00
|
|
|
|
|
|
|
if not exclude_commits:
|
2016-04-20 21:35:38 +02:00
|
|
|
allowed_events.add('push')
|
|
|
|
allowed_events.add('commit_comment')
|
2016-03-11 22:12:41 +01:00
|
|
|
|
|
|
|
if event not in allowed_events:
|
|
|
|
return json_success()
|
|
|
|
|
|
|
|
# We filter issue_comment events for issue creation events
|
|
|
|
if event == 'issue_comment' and payload['action'] != 'created':
|
|
|
|
return json_success()
|
|
|
|
|
|
|
|
if event == 'push':
|
|
|
|
# If we are given a whitelist of branches, then we silently ignore
|
|
|
|
# any push notification on a branch that is not in our whitelist.
|
|
|
|
if branches and short_ref not in re.split('[\s,;|]+', branches):
|
|
|
|
return json_success()
|
|
|
|
|
|
|
|
# Map payload to the handler with the right version
|
|
|
|
if version == 2:
|
2016-07-17 19:49:53 +02:00
|
|
|
target_stream, subject, content = api_github_v2(user_profile, event, payload, branches,
|
|
|
|
stream, commit_stream, issue_stream,
|
|
|
|
**kwargs)
|
2016-03-11 22:12:41 +01:00
|
|
|
else:
|
2016-07-17 19:49:53 +02:00
|
|
|
target_stream, subject, content = api_github_v1(user_profile, event, payload, branches,
|
|
|
|
stream, **kwargs)
|
2016-03-11 22:12:41 +01:00
|
|
|
|
2016-04-20 21:35:38 +02:00
|
|
|
request.client = get_client('ZulipGitHubWebhook')
|
2016-03-11 22:12:41 +01:00
|
|
|
return send_message_backend(request, user_profile,
|
2016-04-20 21:35:38 +02:00
|
|
|
message_type_name='stream',
|
2016-03-11 22:12:41 +01:00
|
|
|
message_to=[target_stream],
|
|
|
|
forged=False, subject_name=subject,
|
|
|
|
message_content=content)
|
|
|
|
|
|
|
|
def build_message_from_gitlog(user_profile, name, ref, commits, before, after, url, pusher, forced=None, created=None):
|
2016-09-26 17:28:14 +02:00
|
|
|
# type: (UserProfile, text_type, text_type, List[Dict[str, str]], text_type, text_type, text_type, text_type, Optional[text_type], Optional[text_type]) -> Tuple[text_type, text_type]
|
2016-03-11 22:12:41 +01:00
|
|
|
short_ref = re.sub(r'^refs/heads/', '', ref)
|
2016-10-05 19:26:09 +02:00
|
|
|
subject = SUBJECT_WITH_BRANCH_TEMPLATE.format(repo=name, branch=short_ref)
|
2016-03-11 22:12:41 +01:00
|
|
|
|
|
|
|
if re.match(r'^0+$', after):
|
2016-06-26 10:23:21 +02:00
|
|
|
content = u'%s deleted branch %s' % (pusher,
|
2016-03-11 22:12:41 +01:00
|
|
|
short_ref)
|
|
|
|
# 'created' and 'forced' are github flags; the second check is for beanstalk
|
|
|
|
elif (forced and not created) or (forced is None and len(commits) == 0):
|
2016-06-26 10:23:21 +02:00
|
|
|
content = (u'%s [force pushed](%s) to branch %s. Head is now %s'
|
2016-03-11 22:12:41 +01:00
|
|
|
% (pusher,
|
|
|
|
url,
|
|
|
|
short_ref,
|
|
|
|
after[:7]))
|
|
|
|
else:
|
2016-09-26 17:28:14 +02:00
|
|
|
commits = _transform_commits_list_to_common_format(commits)
|
|
|
|
content = get_push_commits_event_message(pusher, url, short_ref, commits)
|
2016-03-11 22:12:41 +01:00
|
|
|
|
2016-04-20 21:27:51 +02:00
|
|
|
return subject, content
|
2016-09-26 17:28:14 +02:00
|
|
|
|
|
|
|
def _transform_commits_list_to_common_format(commits):
|
|
|
|
# type: (List[Dict[str, str]]) -> List[Dict[str, str]]
|
|
|
|
new_commits_list = []
|
|
|
|
for commit in commits:
|
|
|
|
new_commits_list.append({
|
|
|
|
'sha': commit.get('id'),
|
|
|
|
'url': commit.get('url'),
|
|
|
|
'message': commit.get('message'),
|
|
|
|
})
|
|
|
|
return new_commits_list
|