Skip to content

Commit 4979098

Browse files
committed
fix: Handle Discord 2000 char limit by splitting large commit messages into multiple notifications
1 parent 9f77c85 commit 4979098

File tree

1 file changed

+56
-16
lines changed

1 file changed

+56
-16
lines changed

.github/workflows/discord-activity.yml

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,59 @@ jobs:
3232
echo 'EOF'
3333
} >> "$GITHUB_ENV"
3434
35-
- name: Post to Discord (with full commit list)
36-
uses: Ilshidur/action-discord@0.4.0
37-
env:
38-
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
39-
with:
40-
args: |
41-
🚀 **Push Event**
42-
📦 Repository: `${{ github.repository }}`
43-
👤 Author: `${{ github.actor }}`
44-
🌿 Branch: `${{ env.REF }}`
45-
📝 Commits: `${{ env.COUNT }}`
46-
47-
**Commit List:**
48-
${{ env.COMMITS }}
49-
50-
🔗 [View Diff](${{ env.COMPARE }})
35+
- name: Post to Discord (smart split)
36+
run: |
37+
HEADER="🚀 **Push Event**
38+
📦 Repository: \`${{ github.repository }}\`
39+
👤 Author: \`${{ github.actor }}\`
40+
🌿 Branch: \`${{ env.REF }}\`
41+
📝 Commits: \`${{ env.COUNT }}\`
42+
🔗 [View Diff](${{ env.COMPARE }})
43+
44+
**Commit List:**
45+
"
46+
47+
MAX_LEN=1900 # Leave buffer for safety
48+
HEADER_LEN=${#HEADER}
49+
50+
# Split commits into chunks
51+
CURRENT_CHUNK=""
52+
CHUNK_NUM=1
53+
54+
while IFS= read -r line; do
55+
TEST_MSG="${HEADER}${CURRENT_CHUNK}${line}"$'\n'
56+
57+
if [ ${#TEST_MSG} -gt $MAX_LEN ] && [ -n "$CURRENT_CHUNK" ]; then
58+
# Send current chunk
59+
if [ $CHUNK_NUM -eq 1 ]; then
60+
MESSAGE="${HEADER}${CURRENT_CHUNK}"
61+
else
62+
MESSAGE="**Commit List (continued ${CHUNK_NUM}):**
63+
${CURRENT_CHUNK}"
64+
fi
65+
66+
curl -H "Content-Type: application/json" \
67+
-d "{\"content\": $(echo "$MESSAGE" | jq -Rs .)}" \
68+
"${{ secrets.DISCORD_WEBHOOK }}"
69+
70+
sleep 1 # Rate limit protection
71+
CHUNK_NUM=$((CHUNK_NUM + 1))
72+
CURRENT_CHUNK="${line}"$'\n'
73+
else
74+
CURRENT_CHUNK="${CURRENT_CHUNK}${line}"$'\n'
75+
fi
76+
done <<< "${{ env.COMMITS }}"
77+
78+
# Send final chunk
79+
if [ -n "$CURRENT_CHUNK" ]; then
80+
if [ $CHUNK_NUM -eq 1 ]; then
81+
MESSAGE="${HEADER}${CURRENT_CHUNK}"
82+
else
83+
MESSAGE="**Commit List (continued ${CHUNK_NUM}):**
84+
${CURRENT_CHUNK}"
85+
fi
86+
87+
curl -H "Content-Type: application/json" \
88+
-d "{\"content\": $(echo "$MESSAGE" | jq -Rs .)}" \
89+
"${{ secrets.DISCORD_WEBHOOK }}"
90+
fi

0 commit comments

Comments
 (0)