-
Notifications
You must be signed in to change notification settings - Fork 1
163 lines (146 loc) · 7.6 KB
/
Copy pathdiscord-notify.yml
File metadata and controls
163 lines (146 loc) · 7.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
name: Discord Push Notification
on:
push:
branches:
- '**' # Fire on EVERY branch push
jobs:
notify:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Gather commit and branch info
id: info
run: |
BRANCH_NAME="${GITHUB_REF#refs/heads/}"
COMMIT_SHA="${{ github.sha }}"
COMMIT_URL="${{ github.server_url }}/${{ github.repository }}/commit/${{ github.sha }}"
REPO_URL="${{ github.server_url }}/${{ github.repository }}"
PR_URL="${{ github.server_url }}/${{ github.repository }}/compare/${BRANCH_NAME}?expand=1"
COMMIT_MESSAGE=$(git log -1 --format=%B | head -5)
COMMIT_AUTHOR=$(git log -1 --format=%an)
FILES_CHANGED=$(git diff --name-only HEAD~1 HEAD 2>/dev/null | wc -l | xargs)
INSERTIONS=$(git diff --numstat HEAD~1 HEAD 2>/dev/null | awk '{sum+=$1} END {print sum+0}')
DELETIONS=$(git diff --numstat HEAD~1 HEAD 2>/dev/null | awk '{sum+=$2} END {print sum+0}')
CHANGED_FILES=$(git diff --name-only HEAD~1 HEAD 2>/dev/null | head -6 | sed 's/^/∙ /')
# Branch-type emoji + color
if echo "$BRANCH_NAME" | grep -qE "^(main|master)$"; then
BRANCH_EMOJI="🟢"
EMBED_COLOR=3066993
TITLE_EMOJI="🟢"
elif echo "$BRANCH_NAME" | grep -q "^feature/"; then
BRANCH_EMOJI="🔵"
EMBED_COLOR=3447003
TITLE_EMOJI="🚀"
elif echo "$BRANCH_NAME" | grep -qE "^(fix|bugfix|hotfix)/"; then
BRANCH_EMOJI="🔴"
EMBED_COLOR=15158332
TITLE_EMOJI="🐛"
elif echo "$BRANCH_NAME" | grep -qE "^(chore|docs|refactor)/"; then
BRANCH_EMOJI="🟡"
EMBED_COLOR=15844367
TITLE_EMOJI="🔧"
else
BRANCH_EMOJI="⚪"
EMBED_COLOR=9807270
TITLE_EMOJI="📝"
fi
echo "branch_name=$BRANCH_NAME" >> $GITHUB_OUTPUT
echo "commit_sha=${COMMIT_SHA:0:7}" >> $GITHUB_OUTPUT
echo "commit_url=$COMMIT_URL" >> $GITHUB_OUTPUT
echo "repo_url=$REPO_URL" >> $GITHUB_OUTPUT
echo "pr_url=$PR_URL" >> $GITHUB_OUTPUT
echo "commit_author=$COMMIT_AUTHOR" >> $GITHUB_OUTPUT
echo "files_changed=$FILES_CHANGED" >> $GITHUB_OUTPUT
echo "insertions=$INSERTIONS" >> $GITHUB_OUTPUT
echo "deletions=$DELETIONS" >> $GITHUB_OUTPUT
echo "embed_color=$EMBED_COLOR" >> $GITHUB_OUTPUT
echo "branch_emoji=$BRANCH_EMOJI" >> $GITHUB_OUTPUT
echo "title_emoji=$TITLE_EMOJI" >> $GITHUB_OUTPUT
echo "commit_message<<EOF" >> $GITHUB_OUTPUT
echo "$COMMIT_MESSAGE" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
echo "changed_files<<EOF" >> $GITHUB_OUTPUT
echo "$CHANGED_FILES" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Send Discord notification
env:
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }}
MENTION_USER_ID: ${{ secrets.DISCORD_MENTION_USER_ID }}
COMMIT_MESSAGE: ${{ steps.info.outputs.commit_message }}
COMMIT_SHA_SHORT: ${{ steps.info.outputs.commit_sha }}
COMMIT_URL: ${{ steps.info.outputs.commit_url }}
REPO_URL: ${{ steps.info.outputs.repo_url }}
PR_URL: ${{ steps.info.outputs.pr_url }}
COMMIT_AUTHOR: ${{ steps.info.outputs.commit_author }}
BRANCH_NAME: ${{ steps.info.outputs.branch_name }}
FILES_CHANGED: ${{ steps.info.outputs.files_changed }}
INSERTIONS: ${{ steps.info.outputs.insertions }}
DELETIONS: ${{ steps.info.outputs.deletions }}
CHANGED_FILES: ${{ steps.info.outputs.changed_files }}
EMBED_COLOR: ${{ steps.info.outputs.embed_color }}
BRANCH_EMOJI: ${{ steps.info.outputs.branch_emoji }}
TITLE_EMOJI: ${{ steps.info.outputs.title_emoji }}
run: |
TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
EMBED_JSON=$(jq -n \
--arg title "$TITLE_EMOJI Push → $BRANCH_NAME" \
--arg desc "$COMMIT_MESSAGE" \
--arg url "$COMMIT_URL" \
--arg c_sha "$COMMIT_SHA_SHORT" \
--arg c_author "$COMMIT_AUTHOR" \
--arg branch "$BRANCH_EMOJI $BRANCH_NAME" \
--arg stats "**Files:** $FILES_CHANGED **+$INSERTIONS -$DELETIONS**" \
--arg files "$CHANGED_FILES" \
--arg pr_url "$PR_URL" \
--arg repo_url "$REPO_URL" \
--argjson color "$EMBED_COLOR" \
--arg run_num "#${{ github.run_number }}" \
--arg timestamp "$TIMESTAMP" \
'{
"title": $title,
"description": $desc,
"url": $url,
"color": $color,
"fields": [
{"name": "🔗 Commit", "value": "[`\($c_sha)`](\($url))", "inline": true},
{"name": "👤 Author", "value": $c_author, "inline": true},
{"name": "🌿 Branch", "value": "`\($branch)`", "inline": true},
{"name": "📊 Changes", "value": $stats, "inline": false},
{"name": "📄 Files Changed", "value": $files, "inline": false},
{"name": "🔀 Open PR", "value": "[Create Pull Request](\($pr_url))", "inline": true},
{"name": "📁 Repository", "value": "[Project-EL](\($repo_url))", "inline": true}
],
"footer": {"text": "Project-EL CI • Run \($run_num)"},
"timestamp": $timestamp
}')
if [ -n "$MENTION_USER_ID" ]; then
JSON_PAYLOAD=$(jq -n \
--arg content "<@$MENTION_USER_ID> new push on **$BRANCH_NAME**" \
--argjson allowed_mentions "{\"users\":[\"$MENTION_USER_ID\"]}" \
--arg username "Project-EL Bot" \
--arg avatar_url "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png" \
--argjson embed "$EMBED_JSON" \
'{username: $username, avatar_url: $avatar_url, content: $content, allowed_mentions: $allowed_mentions, embeds: [$embed]}')
else
JSON_PAYLOAD=$(jq -n \
--arg username "Project-EL Bot" \
--arg avatar_url "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png" \
--argjson embed "$EMBED_JSON" \
'{username: $username, avatar_url: $avatar_url, embeds: [$embed]}')
fi
RESPONSE=$(curl -s -w "\n%{http_code}" \
-X POST \
-H 'Content-Type: application/json' \
--data-raw "$JSON_PAYLOAD" \
"$DISCORD_WEBHOOK_URL")
HTTP_STATUS=$(echo "$RESPONSE" | tail -n1)
BODY=$(echo "$RESPONSE" | sed '$d')
echo "Discord status: $HTTP_STATUS"
echo "Discord body: $BODY"
if [ "$HTTP_STATUS" -ne 204 ] && [ "$HTTP_STATUS" -ne 200 ]; then
echo "::error::Discord webhook failed with HTTP $HTTP_STATUS" >&2
exit 1
fi