From 425cd068098b3bbca66e0df5e2f6425d63422d95 Mon Sep 17 00:00:00 2001 From: Yash-Rawat-IIT-D Date: Tue, 17 Mar 2026 14:40:31 +0000 Subject: [PATCH 1/2] Added Github PR Discord Integration --- .github/workflows/discord.yml | 36 ++++++++++++++ scripts/notfiy.py | 92 +++++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 .github/workflows/discord.yml create mode 100644 scripts/notfiy.py diff --git a/.github/workflows/discord.yml b/.github/workflows/discord.yml new file mode 100644 index 0000000..707fc91 --- /dev/null +++ b/.github/workflows/discord.yml @@ -0,0 +1,36 @@ +name: Discord PR Notification + +on: + pull_request: + # Trigger the workflow when a pull request is closed (merged or not merged) // Shall Filter in scripts/notify.py + types: [closed] + workflow_dispatch: + +jobs: + notify: + runs-on: ubuntu-latest + steps: + - name: Checkout Repo + uses: actions/checkout@v4 # Load the repo to access the scripts/notify.py + + - name: Install Python-Dependencies + run: pip install requests + + - name: Notify Discord by running the script + run: python scripts/notify.py + env: + # Setup Webhook + WEBHOOK: ${{ secrets.DISCORD }} + + # Event type + EVENT_NAME: ${{ github.event_name }} + + # PR data + PR_TITLE: ${{ github.event.pull_request.title }} + PR_NUMBER: ${{ github.event.pull_request.number }} + PR_URL: ${{ github.event.pull_request.html_url }} + PR_AUTHOR: ${{ github.event.pull_request.user.login }} + PR_STATE: ${{ github.event.pull_request.state }} + PR_MERGED: ${{ github.event.pull_request.merged }} + TARGET_BRANCH: ${{ github.event.pull_request.base.ref }} + \ No newline at end of file diff --git a/scripts/notfiy.py b/scripts/notfiy.py new file mode 100644 index 0000000..8f95091 --- /dev/null +++ b/scripts/notfiy.py @@ -0,0 +1,92 @@ +import os +import requests + +def send_notification(web_hook, pay_load): + resp = requests.post(web_hook, json=pay_load) + if resp.status_code != 204: + print(f"Failed to send notification.\n Status code: {resp.status_code} \n Response: {resp.text}") + exit(1) + + +def get_discord_payload_v0(title, url, author, branch): + payload = { + "embeds": [ + { + "title": "PR Merged", + "url": url, + "color": 3066993, # green in decimal (0x2ecc71 in hex) + "fields": [ + { + "name": "Title", + "value": title, + "inline": False + }, + { + "name": "Author", + "value": author, + "inline": True + }, + { + "name": "Target Branch", + "value": branch, + "inline": True + } + ], + "footer": { + "text": f"Branch: {branch}" + } + } + ] + } + + return + +def get_discord_payload_v1(title, url, author, branch, number): + payload = { + "embeds": [ + { + "title": f"PR #{number}: {title}", + "url": url, + "color": 3066993, + "description": f"Merged into **{branch}**", + "fields": [ + { + "name": "Author", + "value": author, + "inline": True + }, + { + "name": "Branch", + "value": branch, + "inline": True + } + ], + "footer": { + "text": "GitHub PR Notification" + } + } + ] + } + return payload + +event = os.environ.get("EVENT_NAME") +web_hook = os.environ["WEB_HOOK"] # Discord Webhook URL presented as an environment variable + +if event == "pull_request_target": + merged = (os.environ.get("PR_MERGED") == "true") + + # Only act notify if the pull request is merged + if not merged: + print("Pull request is not merged. No notification will be sent.") + exit(0) + + pr_title = os.environ.get("PR_TITLE") + pr_url = os.environ.get("PR_URL") + pr_author = os.environ.get("PR_AUTHOR") + pr_target_branch = os.environ.get("TARGET_BRANCH") + pr_number = os.environ.get("PR_NUMBER") + + payload = get_discord_payload_v1(pr_title, pr_url, pr_author, + pr_target_branch,pr_number) + + send_notification(web_hook, payload) \ No newline at end of file From 05699e8987b5029beccd15d1f12787bac226415e Mon Sep 17 00:00:00 2001 From: Yash-Rawat-IIT-D Date: Tue, 17 Mar 2026 16:56:21 +0000 Subject: [PATCH 2/2] Updated workflow to use pull_request_target --- .github/workflows/discord.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/discord.yml b/.github/workflows/discord.yml index 707fc91..ba45ac0 100644 --- a/.github/workflows/discord.yml +++ b/.github/workflows/discord.yml @@ -1,7 +1,7 @@ name: Discord PR Notification on: - pull_request: + pull_request_target: # Trigger the workflow when a pull request is closed (merged or not merged) // Shall Filter in scripts/notify.py types: [closed] workflow_dispatch: