From 3f419d36b03a3558e30489692a663f9bf818a83f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Soko=C5=82owski?= Date: Mon, 12 Aug 2024 20:25:15 +0200 Subject: [PATCH] github/webhook_request.py: test sending webhook requests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jakub SokoĊ‚owski --- github/webhook_request.py | 74 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100755 github/webhook_request.py diff --git a/github/webhook_request.py b/github/webhook_request.py new file mode 100755 index 0000000..cff24c4 --- /dev/null +++ b/github/webhook_request.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python3 + +import sys +import json +import hmac +import hashlib +import requests +import argparse +from os import environ +from uuid import uuid4 +from github import Github + +HELP_DESCRIPTION='This a utility for adding accounts to private GitHub repos.' +HELP_EXAMPLE='Example: ./add_to_private.py -o "waku-org" -u "status-im-auto"' + +PAYLOAD={ + 'ref': 'refs/heads/deploy-develop', + 'before': '2ef2124e7e739d8fe8a14d7c1660c0fb29db247c', + 'after': '750c8bd47aaf0d6d619c64c67b7ec2e4044f11a7', +} + +def parse_args(): + parser = argparse.ArgumentParser( + description=HELP_DESCRIPTION, epilog=HELP_EXAMPLE + ) + parser.add_argument('-u', '--url', + help='Webhook URL.') + parser.add_argument('-e', '--event', default='push', + help='Webhook GitHub event.') + parser.add_argument('-s', '--secret', default=environ.get('WEBHOOK_SECRET'), + help='Webhook Secret.') + parser.add_argument('-p', '--payload', default=PAYLOAD, + help='Webhook Secret.') + parser.add_argument('-t', '--timeout', default=5, + help='Webhook request timeout.') + + args = parser.parse_args() + + return args + + +def main(): + args = parse_args() + + secret = args.secret.encode('utf-8') + data = json.dumps(args.payload).encode('utf-8') + + digest = lambda algo: hmac.new(secret, data, algo).hexdigest() + + headers = { + 'Content-Type': 'application/json', + 'X-GitHub-Event': args.event, + 'X-Hub-Signature': 'sha1=%s' % digest(hashlib.sha1), + 'X-Hub-Signature-256': 'sha256=%s' % digest(hashlib.sha256), + 'X-GitHub-Delivery': str(uuid4()), + } + + try: + rval = requests.post( + args.url, + headers=headers, + data=data, + timeout=args.timeout, + ) + except requests.exceptions.ConnectionError as ex: + print(ex) + sys.exit(1) + + print(rval.status_code, rval.reason) + print(rval.headers) + print(rval.text) + +if __name__ == '__main__': + main()