github/webhook_request.py: test sending webhook requests

Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
Jakub Sokołowski 2024-08-12 20:25:15 +02:00
parent 388bf9f474
commit 3f419d36b0
No known key found for this signature in database
GPG Key ID: FE65CD384D5BF7B4
1 changed files with 74 additions and 0 deletions

74
github/webhook_request.py Executable file
View File

@ -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()