Skip to content
Open
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
376 changes: 187 additions & 189 deletions .github/workflows/discord-commit.yml
Original file line number Diff line number Diff line change
@@ -1,194 +1,192 @@
name: Send Commit Message to Discord

on:
push:
branches:
- '**'
workflow_dispatch:
inputs:
sha:
description: 'Commit SHA to run against (optional).'
required: false
type: string
push:
branches:
- "**"
workflow_dispatch:

jobs:
notify-discord:
runs-on: blacksmith-2vcpu-ubuntu-2404

steps:
- name: Checkout repo
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ inputs.sha || github.sha }}

- name: Prepare Data
shell: bash
run: |
set -euo pipefail

HEAD_SHA="$(git rev-parse HEAD)"
echo "HEAD_SHA=$HEAD_SHA" >> "$GITHUB_ENV"
echo "COMMIT_URL=https://github.com/${GITHUB_REPOSITORY}/commit/${HEAD_SHA}" >> "$GITHUB_ENV"
echo "TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ")" >> "$GITHUB_ENV"
echo "BRANCH=${GITHUB_REF#refs/heads/}" >> "$GITHUB_ENV"
echo "REPO_FULL=${GITHUB_REPOSITORY}" >> "$GITHUB_ENV"
echo "REPO_NAME=${GITHUB_REPOSITORY#*/}" >> "$GITHUB_ENV"

declare -A USERS=(
["EpsilonPhoenix"]="<@899647076370092042>"
["qxionr"]="<@790294721943699477>"
["quiteboring"]="<@1441859003708866601>"
["rdbtCVS"]="<@777346923232886784>"
["IcyHenryT"]="<@1160548899711356958>"
["oblongboot"]="<@768481984242253904>"
["axlecoffee"]="<@790736254714642453>"
)

ACTOR="$(git log -1 --pretty=%an)"
echo "DISCORD_AUTHOR=${USERS[$ACTOR]:-$ACTOR}" >> "$GITHUB_ENV"

BASE_SHA="$(git rev-parse "${HEAD_SHA}^" 2>/dev/null || echo "4b825dc642cb6eb9a060e54bf8d69288fbee4904")"

STATS="$(git diff --shortstat "$BASE_SHA" "$HEAD_SHA" || echo "0 files changed")"
echo "STATS_TEXT=$STATS" >> "$GITHUB_ENV"

EXT_LIST="$(
git diff --name-only "$BASE_SHA" "$HEAD_SHA" | awk '
{
f=$0
n=split(f,a,"/")
b=a[n]
if (index(b,".")==0) ext="noext"
else { ext=b; sub(/^.*\./,"",ext) }
print ext
}
' | sort | uniq -c | sort -nr | head -n 10 \
| awk '{print $2 " (" $1 ")"}' \
| paste -sd ", " -
)"
[[ -z "${EXT_LIST:-}" ]] && EXT_LIST="No specific types"
EXT_LIST="${EXT_LIST:0:1024}"
echo "FILE_TYPES=$EXT_LIST" >> "$GITHUB_ENV"

- name: Send to Discord
env:
WEBHOOK: ${{ secrets.PUSH_NOTIFIER }}
shell: bash
run: |
set -euo pipefail

COMMIT_MSG="$(git log -1 --pretty=%B)"
export CHUNK_LIMIT=3500
export COMMIT_MSG

mapfile -d '' MSG_CHUNKS < <(
python3 - << 'PY'
import os
import sys

s = os.environ.get("COMMIT_MSG", "")
limit = int(os.environ.get("CHUNK_LIMIT", "3500"))

if not s:
sys.stdout.write("No commit message.\0")
sys.exit(0)

start = 0
s_len = len(s)

while start < s_len:
end = start + limit
if end >= s_len:
sys.stdout.write(s[start:] + '\0')
break

chunk = s[start:end]
last_newline = chunk.rfind('\n')

if last_newline != -1:
cut_point = last_newline + 1
else:
cut_point = limit

sys.stdout.write(chunk[:cut_point] + '\0')
start += cut_point
PY
)

if [[ "${#MSG_CHUNKS[@]}" -eq 0 ]]; then
MSG_CHUNKS=("No commit message.")
fi

TOTAL="${#MSG_CHUNKS[@]}"

FIRST_MSG="${MSG_CHUNKS[0]}"

PAYLOAD="$(jq -cn \
--arg msg "$FIRST_MSG" \
--arg url "$COMMIT_URL" \
--arg author "$DISCORD_AUTHOR" \
--arg branch "$BRANCH" \
--arg time "$TIMESTAMP" \
--arg stats "$STATS_TEXT" \
--arg types "$FILE_TYPES" \
--arg repo "$REPO_NAME" \
--arg repofull "$REPO_FULL" \
--arg idx "1" \
--arg total "$TOTAL" \
'{
username: ($repo + " Commits"),
avatar_url: "https://p7.hiclipart.com/preview/227/12/28/catgirl-anime-kawaii-manga-cat.jpg",
embeds: [{
title: ("[" + $repo + "] New Commit" + (if ($total|tonumber) > 1 then " (" + $idx + "/" + $total + ")" else "" end)),
description: ("📝 **Summary**\n" + $msg + "\n\n[View Commit](" + $url + ")"),
color: 3447003,
timestamp: $time,
fields: [
{name: "Repository", value: $repofull, inline: true},
{name: "Author", value: $author, inline: true},
{name: "Branch", value: $branch, inline: true},
{name: "📊 Changes", value: $stats, inline: false},
{name: "📂 File Types", value: $types, inline: false}
]
}]
}'
)"

curl -sS --fail-with-body \
-H "Content-Type: application/json" \
-d "$PAYLOAD" \
"$WEBHOOK"

for ((i = 1; i < TOTAL; i++)); do
PART="${MSG_CHUNKS[$i]}"
IDX="$((i+1))"

CONT_PAYLOAD="$(jq -cn \
--arg msg "$PART" \
--arg url "$COMMIT_URL" \
--arg time "$TIMESTAMP" \
--arg repo "$REPO_NAME" \
--arg repofull "$REPO_FULL" \
--arg idx "$IDX" \
--arg total "$TOTAL" \
'{
username: ($repo + " Commits"),
avatar_url: "https://p7.hiclipart.com/preview/227/12/28/catgirl-anime-kawaii-manga-cat.jpg",
embeds: [{
title: ("[" + $repo + "] New Commit (continued " + $idx + "/" + $total + ")"),
description: ($msg + "\n\n[View Commit](" + $url + ")"),
color: 3447003,
timestamp: $time,
fields: [
{name: "Repository", value: $repofull, inline: true}
]
}]
}'
)"

curl -sS --fail-with-body \
-H "Content-Type: application/json" \
-d "$CONT_PAYLOAD" \
"$WEBHOOK"
done
notify-discord:
runs-on: blacksmith-2vcpu-ubuntu-2404
if: |
github.event_name == 'workflow_dispatch' ||
(github.event_name == 'push' && !contains(github.event.head_commit.message, '[skip-native-auto]'))

steps:
- name: Checkout repo
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.sha }}

- name: Prepare Data
shell: bash
run: |
set -euo pipefail

HEAD_SHA="$(git rev-parse HEAD)"
echo "HEAD_SHA=$HEAD_SHA" >> "$GITHUB_ENV"
echo "COMMIT_URL=https://github.com/${GITHUB_REPOSITORY}/commit/${HEAD_SHA}" >> "$GITHUB_ENV"
echo "TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ")" >> "$GITHUB_ENV"
echo "BRANCH=${GITHUB_REF#refs/heads/}" >> "$GITHUB_ENV"
echo "REPO_FULL=${GITHUB_REPOSITORY}" >> "$GITHUB_ENV"
echo "REPO_NAME=${GITHUB_REPOSITORY#*/}" >> "$GITHUB_ENV"

declare -A USERS=(
["EpsilonPhoenix"]="<@899647076370092042>"
["qxionr"]="<@790294721943699477>"
["quiteboring"]="<@1441859003708866601>"
["rdbtCVS"]="<@777346923232886784>"
["IcyHenryT"]="<@1160548899711356958>"
["oblongboot"]="<@768481984242253904>"
["axlecoffee"]="<@790736254714642453>"
)

ACTOR="$(git log -1 --pretty=%an)"
echo "DISCORD_AUTHOR=${USERS[$ACTOR]:-$ACTOR}" >> "$GITHUB_ENV"

BASE_SHA="$(git rev-parse "${HEAD_SHA}^" 2>/dev/null || echo "4b825dc642cb6eb9a060e54bf8d69288fbee4904")"

STATS="$(git diff --shortstat "$BASE_SHA" "$HEAD_SHA" || echo "0 files changed")"
echo "STATS_TEXT=$STATS" >> "$GITHUB_ENV"

EXT_LIST="$(
git diff --name-only "$BASE_SHA" "$HEAD_SHA" | awk '
{
f=$0
n=split(f,a,"/")
b=a[n]
if (index(b,".")==0) ext="noext"
else { ext=b; sub(/^.*\./,"",ext) }
print ext
}
' | sort | uniq -c | sort -nr | head -n 10 \
| awk '{print $2 " (" $1 ")"}' \
| paste -sd ", " -
)"
[[ -z "${EXT_LIST:-}" ]] && EXT_LIST="No specific types"
EXT_LIST="${EXT_LIST:0:1024}"
echo "FILE_TYPES=$EXT_LIST" >> "$GITHUB_ENV"

- name: Send to Discord
env:
WEBHOOK: ${{ secrets.PUSH_NOTIFIER }}
shell: bash
run: |
set -euo pipefail

COMMIT_MSG="$(git log -1 --pretty=%B)"
export CHUNK_LIMIT=3500
export COMMIT_MSG

mapfile -d '' MSG_CHUNKS < <(
python3 - << 'PY'
import os
import sys

s = os.environ.get("COMMIT_MSG", "")
limit = int(os.environ.get("CHUNK_LIMIT", "3500"))

if not s:
sys.stdout.write("No commit message.\0")
sys.exit(0)

start = 0
s_len = len(s)

while start < s_len:
end = start + limit
if end >= s_len:
sys.stdout.write(s[start:] + '\0')
break

chunk = s[start:end]
last_newline = chunk.rfind('\n')

if last_newline != -1:
cut_point = last_newline + 1
else:
cut_point = limit

sys.stdout.write(chunk[:cut_point] + '\0')
start += cut_point
PY
)

if [[ "${#MSG_CHUNKS[@]}" -eq 0 ]]; then
MSG_CHUNKS=("No commit message.")
fi

TOTAL="${#MSG_CHUNKS[@]}"

FIRST_MSG="${MSG_CHUNKS[0]}"

PAYLOAD="$(jq -cn \
--arg msg "$FIRST_MSG" \
--arg url "$COMMIT_URL" \
--arg author "$DISCORD_AUTHOR" \
--arg branch "$BRANCH" \
--arg time "$TIMESTAMP" \
--arg stats "$STATS_TEXT" \
--arg types "$FILE_TYPES" \
--arg repo "$REPO_NAME" \
--arg repofull "$REPO_FULL" \
--arg idx "1" \
--arg total "$TOTAL" \
'{
username: ($repo + " Commits"),
avatar_url: "https://p7.hiclipart.com/preview/227/12/28/catgirl-anime-kawaii-manga-cat.jpg",
embeds: [{
title: ("[" + $repo + "] New Commit" + (if ($total|tonumber) > 1 then " (" + $idx + "/" + $total + ")" else "" end)),
description: ("📝 **Summary**\n" + $msg + "\n\n[View Commit](" + $url + ")"),
color: 3447003,
timestamp: $time,
fields: [
{name: "Repository", value: $repofull, inline: true},
{name: "Author", value: $author, inline: true},
{name: "Branch", value: $branch, inline: true},
{name: "📊 Changes", value: $stats, inline: false},
{name: "📂 File Types", value: $types, inline: false}
]
}]
}'
)"

curl -sS --fail-with-body \
-H "Content-Type: application/json" \
-d "$PAYLOAD" \
"$WEBHOOK"

for ((i = 1; i < TOTAL; i++)); do
PART="${MSG_CHUNKS[$i]}"
IDX="$((i+1))"

CONT_PAYLOAD="$(jq -cn \
--arg msg "$PART" \
--arg url "$COMMIT_URL" \
--arg time "$TIMESTAMP" \
--arg repo "$REPO_NAME" \
--arg repofull "$REPO_FULL" \
--arg idx "$IDX" \
--arg total "$TOTAL" \
'{
username: ($repo + " Commits"),
avatar_url: "https://p7.hiclipart.com/preview/227/12/28/catgirl-anime-kawaii-manga-cat.jpg",
embeds: [{
title: ("[" + $repo + "] New Commit (continued " + $idx + "/" + $total + ")"),
description: ($msg + "\n\n[View Commit](" + $url + ")"),
color: 3447003,
timestamp: $time,
fields: [
{name: "Repository", value: $repofull, inline: true}
]
}]
}'
)"

curl -sS --fail-with-body \
-H "Content-Type: application/json" \
-d "$CONT_PAYLOAD" \
"$WEBHOOK"
done
Loading
Loading