Refactor Discord issue webhook to Python script

This commit is contained in:
David Montero Crespo 2026-03-13 17:06:34 -03:00 committed by GitHub
parent a606e42b45
commit f35a9b18d3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 71 additions and 74 deletions

View File

@ -11,85 +11,82 @@ jobs:
- name: Send issue to Discord - name: Send issue to Discord
env: env:
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK_ISSUES }} DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK_ISSUES }}
ISSUE_NUMBER: ${{ github.event.issue.number }}
ISSUE_TITLE: ${{ github.event.issue.title }}
ISSUE_URL: ${{ github.event.issue.html_url }}
ISSUE_BODY: ${{ github.event.issue.body }}
ISSUE_USER: ${{ github.event.issue.user.login }}
ISSUE_AVATAR: ${{ github.event.issue.user.avatar_url }}
ISSUE_CREATED_AT: ${{ github.event.issue.created_at }}
REPO: ${{ github.repository }}
LABELS_JSON: ${{ toJson(github.event.issue.labels) }}
run: | run: |
ISSUE_NUMBER="${{ github.event.issue.number }}" python3 - <<'PYEOF'
ISSUE_TITLE="${{ github.event.issue.title }}" import json, os, urllib.request
ISSUE_URL="${{ github.event.issue.html_url }}"
ISSUE_BODY="${{ github.event.issue.body }}"
ISSUE_USER="${{ github.event.issue.user.login }}"
ISSUE_AVATAR="${{ github.event.issue.user.avatar_url }}"
REPO="${{ github.repository }}"
# Truncate body to 300 chars to keep embed clean number = os.environ["ISSUE_NUMBER"]
SHORT_BODY=$(echo "$ISSUE_BODY" | head -c 300) title = os.environ["ISSUE_TITLE"]
if [ ${#ISSUE_BODY} -gt 300 ]; then url = os.environ["ISSUE_URL"]
SHORT_BODY="${SHORT_BODY}..." body = os.environ.get("ISSUE_BODY") or ""
fi user = os.environ["ISSUE_USER"]
avatar = os.environ["ISSUE_AVATAR"]
repo = os.environ["REPO"]
created_at = os.environ["ISSUE_CREATED_AT"]
webhook = os.environ["DISCORD_WEBHOOK"]
# Build labels list (may be empty) # Truncate body
LABELS_JSON='${{ toJson(github.event.issue.labels) }}' short_body = body[:300] + ("..." if len(body) > 300 else "")
LABELS=$(echo "$LABELS_JSON" | python3 -c " if not short_body.strip():
import json, sys short_body = "*(no description)*"
labels = json.load(sys.stdin)
if labels:
print(', '.join(f'\`{l[\"name\"]}\`' for l in labels))
else:
print('*(none)*')
" 2>/dev/null || echo "*(none)*")
# Discord embed payload # Labels
PAYLOAD=$(python3 -c " try:
import json, sys labels_raw = json.loads(os.environ.get("LABELS_JSON", "[]"))
labels = ", ".join(f"`{l['name']}`" for l in labels_raw) if labels_raw else "*(none)*"
except Exception:
labels = "*(none)*"
payload = { payload = {
'content': '@everyone 📢 **New issue reported on Velxio!**', "content": "@everyone 📢 **New issue reported on Velxio!**",
'embeds': [{ "embeds": [{
'title': f'🐛 Issue #{sys.argv[1]}: {sys.argv[2]}', "title": f"🐛 Issue #{number}: {title}",
'url': sys.argv[3], "url": url,
'description': sys.argv[4] if sys.argv[4].strip() else '*(no description)*', "description": short_body,
'color': 0xE74C3C, "color": 0xE74C3C,
'fields': [ "fields": [
{ {
'name': '📦 Repository', "name": "📦 Repository",
'value': f'[\`{sys.argv[7]}\`](https://github.com/{sys.argv[7]})', "value": f"[`{repo}`](https://github.com/{repo})",
'inline': True "inline": True
}, },
{ {
'name': '🏷️ Labels', "name": "🏷️ Labels",
'value': sys.argv[6], "value": labels,
'inline': True "inline": True
} }
], ],
'author': { "author": {
'name': sys.argv[5], "name": user,
'url': f'https://github.com/{sys.argv[5]}', "url": f"https://github.com/{user}",
'icon_url': sys.argv[8] "icon_url": avatar
}, },
'footer': { "footer": {"text": "Velxio · GitHub Issues"},
'text': 'Velxio · GitHub Issues' "timestamp": created_at
}, }]
'timestamp': '${{ github.event.issue.created_at }}'
}]
} }
print(json.dumps(payload))
" \ data = json.dumps(payload).encode()
"$ISSUE_NUMBER" \ req = urllib.request.Request(
"$ISSUE_TITLE" \ webhook,
"$ISSUE_URL" \ data=data,
"$SHORT_BODY" \ headers={"Content-Type": "application/json"},
"$ISSUE_USER" \ method="POST"
"$LABELS" \
"$REPO" \
"$ISSUE_AVATAR"
) )
try:
HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" \ with urllib.request.urlopen(req) as resp:
-H "Content-Type: application/json" \ print(f"Discord response: {resp.status}")
-X POST \ except urllib.error.HTTPError as e:
-d "$PAYLOAD" \ body_err = e.read().decode()
"$DISCORD_WEBHOOK") print(f"Discord error: {e.code} {e.reason} — {body_err}")
raise SystemExit(1)
echo "Discord response: $HTTP_STATUS" PYEOF
if [ "$HTTP_STATUS" -ne 204 ]; then
echo "::warning::Discord webhook returned $HTTP_STATUS"
fi