Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .github/workflows/discord.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Discord PR Notification

on:
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:

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 }}

92 changes: 92 additions & 0 deletions scripts/notfiy.py
Original file line number Diff line number Diff line change
@@ -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)
Loading