Skip to content

update(MediaPlugin): update hash for main (#193) #2

update(MediaPlugin): update hash for main (#193)

update(MediaPlugin): update hash for main (#193) #2

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
old_content = subprocess.check_output(["git", "show", "HEAD~1:Plugins.json"]).decode("utf-8")
new_content = subprocess.check_output(["git", "show", "HEAD:Plugins.json"]).decode("utf-8")
old_data = json.loads(old_content)
new_data = json.loads(new_content)
old_dict = {item['url']: item['commits'] for item in old_data}
new_dict = {item['url']: item['commits'] for item in new_data}
found = False
for url, new_commits in new_dict.items():
old_commits = old_dict.get(url, {})
for version, new_sha in new_commits.items():
old_sha = old_commits.get(version)
# Check if the commit hash for this version changed
if old_sha != new_sha:
plugin_name = url.split('/')[-1]
if old_sha is not None:
# Case 1: An existing version key was updated directly
changelog_url = f"{url}/compare/{old_sha}...{new_sha}"
display_text = f"Update ({new_sha[:7]})"
else:
# Case 2: A brand new version key was added
if old_commits:
# Grab the very last version name and its corresponding real hash
prev_version = list(old_commits.keys())[-1]
prev_sha = old_commits[prev_version]
changelog_url = f"{url}/compare/{prev_sha}...{new_sha}"
display_text = f"{prev_version}...{version}"
else:
# Fallback for the first ever release of a plugin
changelog_url = f"{url}/commit/{new_sha}"
display_text = f"Initial Commit ({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 as [1.5.0-beta.13...1.5.0-beta.14](<URL>)
# The < > inside the markdown parenthesis cleanly suppresses the Discord embed preview!
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"