Skip to content

Merge pull request #244 from oparada1988/main #54

Merge pull request #244 from oparada1988/main

Merge pull request #244 from oparada1988/main #54

name: Notify Discord about plugin update
on:
push:
branches:
- main
paths:
- 'Plugins.json'
jobs:
notify:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v4
with:
fetch-depth: 2
- name: Extract Hash Diff
id: diff
shell: python
run: |
import json
import os
import subprocess
# Get historical versions of Plugins.json
try:
old_content = subprocess.check_output(
["git", "show", "HEAD~1:Plugins.json"],
stderr=subprocess.DEVNULL
).decode("utf-8")
old_data = json.loads(old_content)
except (subprocess.CalledProcessError, json.JSONDecodeError) as e:
print(f"Could not read or parse HEAD~1:Plugins.json ({e}) — skipping update notification.")
old_data = None
if old_data is not None:
try:
new_content = subprocess.check_output(
["git", "show", "HEAD:Plugins.json"],
stderr=subprocess.DEVNULL
).decode("utf-8")
new_data = json.loads(new_content)
except (subprocess.CalledProcessError, json.JSONDecodeError) as e:
print(f"Could not read or parse HEAD:Plugins.json ({e}) — skipping update notification.")
new_data = None
if new_data is not None:
old_dict = {item['url']: item['commits'] for item in old_data if isinstance(item, dict) and 'url' in item and 'commits' in item}
new_dict = {item['url']: item['commits'] for item in new_data if isinstance(item, dict) and 'url' in item and 'commits' in item}
found = False
for url, new_commits in new_dict.items():
if url in old_dict and isinstance(new_commits, dict):
old_commits = old_dict[url]
if not isinstance(old_commits, dict):
continue
# Get the single version key and its new SHA
for version, new_sha in new_commits.items():
old_values = list(old_commits.values())
if not old_values:
continue
# Fallback to absolute positional value if the app version key itself was updated
old_sha = old_commits.get(version) or old_values[0]
# Trigger if the commit hash changed
if old_sha != new_sha:
plugin_name = url.split('/')[-1]
changelog_url = f"{url}/compare/{old_sha}...{new_sha}"
# Creates a clean display string like: "a459c67...bd2cfc3"
display_text = f"{old_sha[:7]}...{new_sha[:7]}"
with open(os.environ['GITHUB_OUTPUT'], 'a') as f:
f.write(f"plugin_name={plugin_name}\n")
f.write(f"changelog_url={changelog_url}\n")
f.write(f"display_text={display_text}\n")
found = True
break
if found:
break
- name: Send Message via Discord Bot
if: steps.diff.outputs.plugin_name != ''
env:
BOT_TOKEN: ${{ secrets.DISCORD_BOT_TOKEN }}
CHANNEL_ID: ${{ secrets.DISCORD_CHANNEL_ID }}
PLUGIN_NAME: ${{ steps.diff.outputs.plugin_name }}
CHANGELOG_URL: ${{ steps.diff.outputs.changelog_url }}
DISPLAY_TEXT: ${{ steps.diff.outputs.display_text }}
run: |
# Formats neatly in Discord markdown with embed-suppressing brackets < >
PAYLOAD=$(jq -n \
--arg msg "# 🎉 $PLUGIN_NAME updated"$'\n\n'"Changelog: [$DISPLAY_TEXT](<$CHANGELOG_URL>)" \
'{content: $msg}')
curl -X POST \
-H "Authorization: Bot $BOT_TOKEN" \
-H "Content-Type: application/json" \
-d "$PAYLOAD" \
"https://discord.com/api/v10/channels/$CHANNEL_ID/messages"