Skip to content

build front back

build front back #45

name: "Discord: repo activity"
on:
push:
branches: ["**"]
jobs:
notify:
runs-on: ubuntu-latest
steps:
- name: Build commit summary (for push)
id: build
run: |
EVENT='${{ toJSON(github.event) }}'
COUNT=$(jq -r '.commits | length' <<< "$EVENT")
REF=$(jq -r '.ref' <<< "$EVENT" | sed 's|refs/heads/||')
COMPARE=$(jq -r '.compare' <<< "$EVENT")
# List every commit with numbered list and clickable commit hash
REPO_URL="https://github.com/${{ github.repository }}"
COMMITS=$(jq -r --arg repo "$REPO_URL" '.commits | to_entries[] |
(1 + .key | tostring) + ". " +
(.value.message | gsub("\\r?\\n"; " ")) +
" [" + .value.id[0:7] + "](" + $repo + "/commit/" + .value.id + ") by " + .value.author.name + "\n"' <<< "$EVENT")
{
echo "COUNT=$COUNT"
echo "REF=$REF"
echo "COMPARE=$COMPARE"
echo 'COMMITS<<EOF'
echo "$COMMITS"
echo 'EOF'
} >> "$GITHUB_ENV"
- name: Post to Discord (smart split)
run: |
HEADER="πŸš€ **Push Event**
πŸ“¦ Repository: \`${{ github.repository }}\`
πŸ‘€ Author: \`${{ github.actor }}\`
🌿 Branch: \`${{ env.REF }}\`
πŸ“ Commits: \`${{ env.COUNT }}\`
πŸ”— [View Diff](${{ env.COMPARE }})
**Commit List:**
"
MAX_LEN=1900 # Leave buffer for safety
HEADER_LEN=${#HEADER}
# Split commits into chunks
CURRENT_CHUNK=""
CHUNK_NUM=1
while IFS= read -r line; do
TEST_MSG="${HEADER}${CURRENT_CHUNK}${line}"$'\n'
if [ ${#TEST_MSG} -gt $MAX_LEN ] && [ -n "$CURRENT_CHUNK" ]; then
# Send current chunk
if [ $CHUNK_NUM -eq 1 ]; then
MESSAGE="${HEADER}${CURRENT_CHUNK}"
else
MESSAGE="${CURRENT_CHUNK}"
fi
curl -H "Content-Type: application/json" \
-d "{\"content\": $(echo "$MESSAGE" | jq -Rs .)}" \
"${{ secrets.DISCORD_WEBHOOK }}"
sleep 1 # Rate limit protection
CHUNK_NUM=$((CHUNK_NUM + 1))
CURRENT_CHUNK="${line}"$'\n'
else
CURRENT_CHUNK="${CURRENT_CHUNK}${line}"$'\n'
fi
done <<< "${{ env.COMMITS }}"
# Send final chunk
if [ -n "$CURRENT_CHUNK" ]; then
if [ $CHUNK_NUM -eq 1 ]; then
MESSAGE="${HEADER}${CURRENT_CHUNK}"
else
MESSAGE="${CURRENT_CHUNK}"
fi
curl -H "Content-Type: application/json" \
-d "{\"content\": $(echo "$MESSAGE" | jq -Rs .)}" \
"${{ secrets.DISCORD_WEBHOOK }}"
fi