diff --git a/.github/workflow_data/discord_push.py b/.github/workflow_data/discord_push.py new file mode 100644 index 000000000..edc3e4cc9 --- /dev/null +++ b/.github/workflow_data/discord_push.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python +import requests +import json +import os + + +if __name__ == "__main__": + with open(os.environ["GITHUB_EVENT_PATH"], "r") as f: + push = json.load(f) + + count = len(push['commits']) + branch = push["ref"].removeprefix("refs/heads/") + change = "Force Push" if push["forced"] and not count else f"{count} New Commit{'' if count == 1 else 's'}" + desc = f"[**{change}**]({push['compare']}) on [{branch}]({push['repository']['html_url']}/tree/{branch}) branch\n" + + for commit in push["commits"]: + desc += f"\n[`{commit['id'][:7]}`]({commit['url']}): {commit['message'].splitlines()[0]} - [__{commit['author']['username']}__](https://github.com/{commit['author']['username']})" + + requests.post( + os.environ["DEV_DISCORD_WEBHOOK"], + headers={ + "Accept": "application/json", + "Content-Type": "application/json" + }, + json={ + "content": None, + "embeds": [ + { + "description": desc, + "url": push["compare"], + "color": 16711680 if push["forced"] else 3669797, + "author": { + "name": push["sender"]["login"], + "url": push["sender"]["html_url"], + "icon_url": push["sender"]["avatar_url"] + } + } + ], + "attachments": [] + } + ) diff --git a/.github/workflows/discord.yml b/.github/workflows/discord.yml new file mode 100644 index 000000000..f3631d6ac --- /dev/null +++ b/.github/workflows/discord.yml @@ -0,0 +1,16 @@ +name: Discord webhook + +on: [push] + +jobs: + webhook: + runs-on: ubuntu-latest + steps: + + - name: 'Checkout code' + uses: actions/checkout@v3 + + - name: Send webhook + env: + DEV_DISCORD_WEBHOOK: "https://discord.com/api/webhooks/${{ secrets.DEV_WEBHOOK_ID }}/${{ secrets.DEV_WEBHOOK_TOKEN }}" + run: python .github/workflow_data/discord_push.py