def main(): """Main function to handle all requests.""" if request.method == "POST": # GitHub sends the secret key in the payload header if utils.match_webhook_secret(request): event = request.headers["X-GitHub-Event"] app.logger.debug(f"Request Headers:\n{request.headers}") app.logger.debug(f"Request body:\n{request.json}") event_to_action = { "pull_request": handlers.handle_pull_request, "integration_installation": handlers.handle_integration_installation, "integration_installation_repositories": handlers.handle_integration_installation_repo, "installation_repositories": handlers.handle_integration_installation_repo, "ping": handlers.handle_ping, "issue_comment": handlers.handle_issue_comment, "installation": handlers.handle_installation, } supported_event = event in event_to_action if supported_event: return event_to_action[event](request) else: return handlers.handle_unsupported_requests(request) else: app.logger.info("Received an unauthorized request") return handlers.handle_unauthorized_requests() else: return redirect("https://pep8speaks.com")
def main(): if request.method == "GET": return redirect("https://pep8speaks.com") elif request.method == "POST": # GitHub sends the secret key in the payload header if utils.match_webhook_secret(request): event = request.headers["X-GitHub-Event"] event_to_action = { "pull_request": handlers.handle_pull_request, "integration_installation": handlers.handle_integration_installation, "integration_installation_repositories": handlers.handle_integration_installation_repo, "installation_repositories": handlers.handle_integration_installation_repo, "ping": handlers.handle_ping, "issue_comment": handlers.handle_issue_comment, "installation": handlers.handle_installation, } try: return event_to_action[event](request) except KeyError: handlers.handle_unsupported_requests(request) else: return render_template('index.html')
def test_match_webhook_secret(self, monkeypatch, request_ctx): assert match_webhook_secret(request_ctx) is True monkeypatch.setenv('OVER_HEROKU', False) request_ctx.headers = {'Header1': True} with pytest.raises(werkzeug.exceptions.Forbidden): match_webhook_secret(request_ctx) request_ctx.headers = {'X-Hub-Signature': None} with pytest.raises(werkzeug.exceptions.Forbidden): match_webhook_secret(request_ctx) key, data = 'testkey', 'testdata' hmac_obj = hmac.new(key.encode(), data.encode()) request_ctx.headers = { 'X-Hub-Signature': '{}={}'.format(hmac_obj.name, hmac_obj.hexdigest()) } with pytest.raises(werkzeug.exceptions.NotImplemented): match_webhook_secret(request_ctx) hmac_obj = hmac.new(key.encode(), data.encode(), digestmod="sha1") request_ctx.headers = { 'X-Hub-Signature': 'sha1={}'.format(hmac_obj.hexdigest()) } request_ctx.data = data.encode() monkeypatch.setenv('GITHUB_PAYLOAD_SECRET', 'wrongkey') with pytest.raises(werkzeug.exceptions.Forbidden): match_webhook_secret(request_ctx) monkeypatch.setenv('GITHUB_PAYLOAD_SECRET', key) assert match_webhook_secret(request_ctx) is True