diff --git a/webhooks.py b/webhooks.py index 121e824..cdc584c 100644 --- a/webhooks.py +++ b/webhooks.py @@ -1,5 +1,7 @@ import logging import base64 +import hmac +import hashlib import requests import yaml import os @@ -27,6 +29,101 @@ srht_public_key = Ed25519PublicKey.from_public_bytes( base64.b64decode(config['srht_payload_sig'])) +@app.route('/gitea/', methods=['POST']) +def gitea_event(gateway=None): + secret = bytes(config["gitea_secret"], "utf-8") + signature = request.headers["X-Gitea-Signature"] + event = request.headers["X-Gitea-Event"] + + sigcheck = hmac.new(secret, msg=request.data, digestmod=hashlib.sha256).hexdigest() + + if gateway is None: + logging.info("MISSING GATEWAY") + abort(400) + + if sigcheck != signature: + logging.info("INVALID GITEA SIGNATURE") + abort(401) + + if event == "push": + logging.debug(f"Gitea - {event} >> {gateway} ") + gt_push_event(gateway, request.json) + + elif event == "issues": + logging.debug(f"Gitea - {event} >> {gateway} ") + gt_issue_event(gateway, request.json) + + elif event == "issue_comment": + logging.debug(f"Gitea - {event} >> {gateway} ") + gt_comment_event(gateway, request.json) + + else: + logging.info("UNKNOWN GITEA EVENT") + logging.debug(f"Gitea - {event} >> {gateway} ") + logging.debug(request.json) + + return "", 200 + +def gt_push_event(gateway, data): + logging.debug(f">> {gateway}") + # logging.debug(data) + project = data["repository"]["full_name"] + username = data["pusher"]["username"] + branch = data['ref'][len('refs/heads/'):] + count = len(data["commits"]) + s = "s" if count > 1 else "" + + body = f"[{project}] {username} pushed {count} commit{s} to {branch}:\n" + for commit in data["commits"]: + cid = commit["id"][:8] + title = commit["message"] + url = commit["url"] + body += f"- [{cid}]({url}) {title}\n" + + logging.debug(body) + mb_url = config["codeberg_gateway"] + payload = { 'gateway': gateway, 'username': "codeberg", 'text': body.rstrip() } + r = requests.post(mb_url, json=payload) + +def gt_issue_event(gateway, data): + logging.debug(f">> {gateway}") + # logging.debug(data) + project = data["repository"]["full_name"] + issue = data["issue"] + username = issue["user"]["username"] + number = issue["number"] + title = issue["title"] + description = issue["body"] + action = data["action"] + + body = f"[{project}] {username} {action} issue #{number}: {title}\n" + body += f"{description}" + + logging.debug(body) + mb_url = config["codeberg_gateway"] + payload = { 'gateway': gateway, 'username': "codeberg", 'text': body.rstrip() } + r = requests.post(mb_url, json=payload) + +def gt_comment_event(gateway, data): + logging.debug(f">> {gateway}") + # logging.debug(data) + project = data["repository"]["full_name"] + issue = data["issue"] + comment = data["comment"] + username = comment["user"]["username"] + number = issue["number"] + title = issue["title"] + action = data["action"] + note = data["comment"]["body"] + + body = f"[{project}] {username} commented on issue #{number}: {title}\n" + body += f"> {note}" + + logging.debug(body) + mb_url = config["codeberg_gateway"] + payload = { 'gateway': gateway, 'username': "codeberg", 'text': body.rstrip() } + r = requests.post(mb_url, json=payload) + @app.route('/gitlab/', methods=['POST']) def gitlab_event(gateway=None): token = request.headers['X-Gitlab-Token']