def hooklistener(): """Listen for the "issues" webhook event. By default, we return a 403 HTTP response. """ if not is_github_hook(request): return ('Nothing to see here', 401, {'Content-Type': 'text/plain'}) payload = json.loads(request.data) event_type = request.headers.get('X-GitHub-Event') # Treating events related to issues if event_type == 'issues': issue = get_issue_info(payload) # A new issue has been created. # In the future we can add new type of actions. if issue['action'] == 'opened': # we are setting labels on each new open issues response = new_opened_issue(payload) if response.status_code == 200: return ('gracias, amigo.', 200, {'Content-Type': 'text/plain'}) else: log = app.logger log.setLevel(logging.INFO) msg = 'failed to set labels on issue {issue}'.format( issue=issue['number']) log.info(msg) return ('ooops', 400, {'Content-Type': 'text/plain'}) elif event_type == 'ping': return ('pong', 200, {'Content-Type': 'text/plain'}) # If nothing worked as expected, the default response is 403. return ('Not an interesting hook', 403, {'Content-Type': 'text/plain'})
def test_is_github_hook_missing_x_hub_signature(self): """Validation tests for GitHub Webhooks: Missing X-Hub-Signature.""" json_event, signature = event_data('new_event_invalid.json') # Lack the X-Hub-Signature with self.app as client: headers = self.headers.copy() headers.update({'X-GitHub-Event': 'issues'}) client.post(self.test_url, data=json_event, headers=headers) webhook_request = helpers.is_github_hook(flask.request) self.assertFalse(webhook_request, 'X-Hub-Signature is missing')
def test_is_github_hook(self): """Validation tests for GitHub Webhooks.""" json_event, signature = event_data('new_event_invalid.json') # Lack the X-GitHub-Event with self.app as client: headers = self.headers.copy() headers.update({'X-Hub-Signature': signature}) client.post(self.test_url, data=json_event, headers=headers) webhook_request = helpers.is_github_hook(flask.request) self.assertFalse(webhook_request, 'X-GitHub-Event is missing') # Lack the X-Hub-Signature with self.app as client: headers = self.headers.copy() headers.update({'X-GitHub-Event': 'issues'}) client.post(self.test_url, data=json_event, headers=headers) webhook_request = helpers.is_github_hook(flask.request) self.assertFalse(webhook_request, 'X-Hub-Signature is missing') # X-Hub-Signature is wrong with self.app as client: headers = self.headers.copy() headers.update({'X-GitHub-Event': 'issues', 'X-Hub-Signature': 'failme'}) client.post(self.test_url, data=json_event, headers=headers) webhook_request = helpers.is_github_hook(flask.request) self.assertFalse(webhook_request, 'X-Hub-Signature is wrong') # Everything is fine with self.app as client: headers = self.headers.copy() headers.update({'X-GitHub-Event': 'issues', 'X-Hub-Signature': signature}) client.post(self.test_url, data=json_event, headers=headers) webhook_request = helpers.is_github_hook(flask.request) self.assertTrue(webhook_request, 'X-GitHub-Event and X-Hub-Signature are correct')
def test_is_github_hook(self): """Validation tests for GitHub Webhooks.""" json_event, signature = event_data('new_event_invalid.json') # Lack the X-GitHub-Event with self.app as client: headers = self.headers.copy() headers.update({'X-Hub-Signature': signature}) client.post(self.test_url, data=json_event, headers=headers) webhook_request = helpers.is_github_hook(flask.request) self.assertFalse(webhook_request, 'X-GitHub-Event is missing') # Lack the X-Hub-Signature with self.app as client: headers = self.headers.copy() headers.update({'X-GitHub-Event': 'issues'}) client.post(self.test_url, data=json_event, headers=headers) webhook_request = helpers.is_github_hook(flask.request) self.assertFalse(webhook_request, 'X-Hub-Signature is missing') # X-Hub-Signature is wrong with self.app as client: headers = self.headers.copy() headers.update({'X-GitHub-Event': 'issues', 'X-Hub-Signature': 'failme'}) client.post(self.test_url, data=json_event, headers=headers) webhook_request = helpers.is_github_hook(flask.request) self.assertFalse(webhook_request, 'X-Hub-Signature is wrong') # Everything is fine with self.app as client: headers = self.headers.copy() headers.update({'X-GitHub-Event': 'issues', 'X-Hub-Signature': signature}) client.post(self.test_url, data=json_event, headers=headers) webhook_request = helpers.is_github_hook(flask.request) self.assertTrue(webhook_request, 'X-GitHub-Event and X-Hub-Signature are correct')
def hooklistener(): """Listen for the "issues" webhook event. By default, we return a 403 HTTP response. """ # webcompat/webcompat-tests/issues if not is_github_hook(request): return make_response('Nothing to see here', 401) payload = json.loads(request.data) event_type = request.headers.get('X-GitHub-Event') # Treating events related to issues if event_type == 'issues': webhook_issue = WebHookIssue.from_dict(payload) # we process the action return webhook_issue.process_issue_action() elif event_type == 'ping': return make_response('pong', 200) # If nothing worked as expected, the default response is 403. return make_response('Not an interesting hook', 403)
def hooklistener(): """Listen for the "issues" webhook event. By default, we return a 403 HTTP response. """ # webcompat/webcompat-tests/issues if not is_github_hook(request): return ('Nothing to see here', 401, {'Content-Type': 'text/plain'}) payload = json.loads(request.data) event_type = request.headers.get('X-GitHub-Event') # Treating events related to issues if event_type == 'issues': issue_info = get_issue_info(payload) # we process the action response = process_issue_action(issue_info) return response elif event_type == 'ping': return ('pong', 200, {'Content-Type': 'text/plain'}) # If nothing worked as expected, the default response is 403. return ('Not an interesting hook', 403, {'Content-Type': 'text/plain'})