def handle_pull_request(request): """Handle pull request events. Verify that the pull request meets our criteria and notify about it, so subscribers can perform actions on it. """ # bail out early if it's just a github check payload = json.loads(request.POST['payload']) if 'action' not in payload: return json.dumps({'message': 'No action, nothing can be done'}) short_url = shorten_pull_request_url(payload['pull_request']['html_url']) action = payload['action'] pull_request = payload['pull_request'] logger.info(f'PR {short_url}: with action {action}') if action in ('opened', 'reopened'): request.registry.notify(NewPullRequest(pull_request, request)) elif action == 'synchronize': request.registry.notify(UpdatedPullRequest(pull_request, request)) elif action == 'closed' and pull_request['merged']: request.registry.notify(MergedPullRequest(pull_request, request)) else: msg = ( f'PR {short_url}: action "{action}" ' f'(merged: {pull_request["merged"]}) not handled' ) logger.info(msg) return json.dumps({'message': msg}) msg = 'Thanks! Handlers already took care of this pull request' return json.dumps({'message': msg})
def handle_comment(request): """Handle comments on issues/pull requests events. Do some verification and issue internal events that will do they own thing. """ # bail out early if it's just a github check ?? see pull requests payload = json.loads(request.POST['payload']) if 'action' not in payload: return 'No action' # handle github pings if 'comment' not in payload: return 'Comment is missing in payload. No action.' if 'issue' not in payload or 'pull_request' not in payload['issue']: return 'The comment is not from a pull request. No action.' action = payload['action'] comment_payload = payload['comment'] comment_short_url = shorten_comment_url(comment_payload['html_url']) comment_user_id = comment_payload['user']['login'] github_users = request.registry.settings['github_users'] if comment_user_id in github_users: logger.info( f'COMMENT {comment_short_url}: IGNORED as it is from {comment_user_id}' ) return f'Comment on PR {comment_short_url} ignored as is from {comment_user_id}. No action.' pull_request_payload = payload['issue']['pull_request'] pull_request_short_url = shorten_pull_request_url(pull_request_payload['html_url']) logger.info( u'COMMENT {0}: with action {1} on pull request {2}'.format( comment_short_url, action, pull_request_short_url ) ) if action == 'created': request.registry.notify( CommentOnPullRequest(comment_payload, pull_request_payload, request) ) msg = 'Thanks! Handlers already took care of this comment' return json.dumps({'message': msg})
def handle_comment(request): """Handle comments on issues/pull requests events. Do some verification and issue internal events that will do they own thing. """ # bail out early if it's just a github check ?? see pull requests payload = json.loads(request.POST['payload']) if 'action' not in payload: return 'No action' # handle github pings if 'comment' not in payload: return 'Comment is missing in payload. No action.' if 'issue' not in payload or 'pull_request' not in payload['issue']: return 'The comment is not from a pull request. No action.' action = payload['action'] comment_payload = payload['comment'] comment_short_url = shorten_comment_url(comment_payload['html_url']) comment_user_id = comment_payload['user']['login'] github_users = request.registry.settings['github_users'] if comment_user_id in github_users: logger.info( f'COMMENT {comment_short_url}: IGNORED as it is from {comment_user_id}' ) return f'Comment on PR {comment_short_url} ignored as is from {comment_user_id}. No action.' pull_request_payload = payload['issue']['pull_request'] pull_request_short_url = shorten_pull_request_url( pull_request_payload['html_url']) logger.info(u'COMMENT {0}: with action {1} on pull request {2}'.format( comment_short_url, action, pull_request_short_url)) if action == 'created': request.registry.notify( CommentOnPullRequest(comment_payload, pull_request_payload, request)) msg = 'Thanks! Handlers already took care of this comment' return json.dumps({'message': msg})
def test_fallback(self): url = 'https://github.com/plone/random/url' self.assertEqual(shorten_pull_request_url(url), url)
def test_shorten_url(self): url = 'https://github.com/plone/plone.app.registry/pull/20' self.assertEqual(shorten_pull_request_url(url), 'plone/plone.app.registry#20')
def short_url(self): if self._short_url is None: self._short_url = shorten_pull_request_url( self.event.pull_request['html_url'] ) return self._short_url
def short_url(self): if self._short_url is None: self._short_url = shorten_pull_request_url( self.event.pull_request['html_url']) return self._short_url