#!/usr/bin/env python import requests import json import sys import os if __name__ == "__main__": with open(os.environ["GITHUB_EVENT_PATH"], "r") as f: event = json.load(f) webhook = "DEV_WEBHOOK" title = desc = url = "" color = 0 fields = [] match os.environ["GITHUB_EVENT_NAME"]: case "push": webhook = "BUILD_WEBHOOK" count = len(event["commits"]) if count == 20: count = int(requests.get( event["compare"].replace("github.com", "api.github.com/repos"), headers={ "Accept": "application/vnd.github.v3+json", "Authorization": f"token {os.environ['GITHUB_TOKEN']}" } ).json()["total_commits"]) branch = event["ref"].removeprefix("refs/heads/") change = ( "Force Push" if event["forced"] and not count else f"{count} New Commit{'' if count == 1 else 's'}" ) desc = f"[**{change}**]({event['compare']}) | [{branch}]({event['repository']['html_url']}/tree/{branch})\n" for i, commit in enumerate(event["commits"]): msg = commit['message'].splitlines()[0].replace("`", "") msg = msg[:50] + ("..." if len(msg) > 50 else "") desc += f"\n[`{commit['id'][:7]}`]({commit['url']}): {msg} - [__{commit['author'].get('username')}__](https://github.com/{commit['author'].get('username')})" if len(desc) > 2020: desc = desc.rsplit("\n", 1)[0] + f"\n+ {count - i} more commits" break url = event["compare"] color = 16723712 if event["forced"] else 3669797 # case "pull_request": # pr = event["pull_request"] # url = pr["html_url"] # branch = pr["base"]["ref"] + ( # "" # if pr["base"]["repo"]["full_name"] != pr["head"]["repo"]["full_name"] # else f" <- {pr['head']['ref']}" # ) # name = pr["title"][:50] + ("..." if len(pr["title"]) > 50 else "") # title = f"Pull Request {event['action'].title()} ({branch}): {name}" # match event["action"]: # case "opened": # desc = (pr["body"][:2045] + "...") if len(pr["body"]) > 2048 else pr["body"] # color = 3669797 # fields.append( # { # "name": "Changed Files:", # "value": str(pr["changed_files"]), # "inline": True, # } # ) # fields.append( # { # "name": "Added:", # "value": "+" + str(pr["additions"]), # "inline": True, # } # ) # fields.append( # { # "name": "Removed:", # "value": "-" + str(pr["deletions"]), # "inline": True, # } # ) # case "closed": # color = 16723712 # case "reopened": # color = 16751872 # case _: # sys.exit(1) case "release": match event["action"]: case "published": webhook = "RELEASE_WEBHOOK" color = 13845998 title = f"New Release published: {event['name']}" desc += f"Changelog:" changelog = "".join( event["body"] .split("Changelog")[1] .split("