-
Notifications
You must be signed in to change notification settings - Fork 102
150 lines (129 loc) · 5.83 KB
/
github-notifications-bot.yml
File metadata and controls
150 lines (129 loc) · 5.83 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
name: Telegram Notification on Push and Pull Request Events
on:
push:
branches:
- main
- dev
pull_request_target:
types: [opened, closed]
jobs:
notify-telegram:
runs-on: ubuntu-latest
steps:
- name: Send Telegram Message
env:
BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }}
TELEGRAM_TARGETS: ${{ vars.TELEGRAM_TARGETS }}
run: |
# CORRECT HTML ESCAPE FUNCTION — THIS WAS BROKEN BEFORE
html_escape() {
echo "$1" | sed 's/&/\&/g; s/</\</g; s/>/\>/g; s/"/\"/g; s/'"'"'/\'/g'
}
LOG_BUFFER=""
log() {
LOG_BUFFER+="$1"$'\n'
}
if [[ "${{ github.event_name }}" == "push" ]]; then
BRANCH=$(html_escape "${{ github.ref_name }}")
ACTOR=$(html_escape "${{ github.actor }}")
# TRIM SPACES FROM COMPARE URL — CRITICAL
COMPARE_URL="${{ github.event.compare }}"
COMPARE_URL="${COMPARE_URL#"${COMPARE_URL%%[![:space:]]*}"}" # trim leading
COMPARE_URL="${COMPARE_URL%"${COMPARE_URL##*[![:space:]]}"}" # trim trailing
EVENT_STATUS="🔔 <b>New Push to $BRANCH</b>"
# GET COMMITS SAFELY
COMMITS_RAW=$(echo '${{ toJSON(github.event.commits) }}' | jq -r '.[] | "- " + (.message | gsub("\n"; " ") | gsub("\r"; " ")) + " (" + (.id | .[0:7]) + ")"' | head -n 10)
ESCAPED_COMMITS=""
while IFS= read -r line; do
if [[ -n "$line" ]]; then
if [[ "$line" =~ \(([a-f0-9]{7})\)$ ]]; then
HASH="${BASH_REMATCH[1]}"
PREFIX="${line% (*}"
ESC_PREFIX=$(html_escape "$PREFIX")
ESC_HASH=$(html_escape "$HASH")
ESCAPED_COMMITS+="$ESC_PREFIX (<code>$ESC_HASH</code>)"$'\n'
else
ESCAPED_COMMITS+=$(html_escape "$line")$'\n'
fi
fi
done <<< "$COMMITS_RAW"
MESSAGE="$EVENT_STATUS"$'\n\n'"👤 By: $ACTOR"$'\n'"🔗 <a href=\"$COMPARE_URL\">View Commits</a>"$'\n\n'"<b>Commits:</b>"$'\n'"$ESCAPED_COMMITS"
else
ACTOR=$(html_escape "${{ github.actor }}")
PR_TITLE=$(html_escape "${{ github.event.pull_request.title }}")
PR_URL="${{ github.event.pull_request.html_url }}"
PR_NUMBER="${{ github.event.pull_request.number }}"
if [[ "${{ github.event.action }}" == "opened" ]]; then
PR_STATUS="📢 <b>New Pull Request Opened</b>"
elif [[ "${{ github.event.action }}" == "closed" && "${{ github.event.pull_request.merged }}" == "true" ]]; then
PR_STATUS="✅ <b>Pull Request Merged</b>"
else
PR_STATUS="❌ <b>Pull Request Closed Without Merging</b>"
fi
COMMITS_RAW=$(curl -s \
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/${{ github.repository }}/pulls/$PR_NUMBER/commits" \
| jq -r '.[] | "- " + (.commit.message | gsub("\n"; " ") | gsub("\r"; " ")) + " (" + (.sha | .[0:7]) + ")"' \
| head -n 10)
ESCAPED_COMMITS=""
while IFS= read -r line; do
if [[ -n "$line" ]]; then
if [[ "$line" =~ \(([a-f0-9]{7})\)$ ]]; then
HASH="${BASH_REMATCH[1]}"
PREFIX="${line% (*}"
ESC_PREFIX=$(html_escape "$PREFIX")
ESC_HASH=$(html_escape "$HASH")
ESCAPED_COMMITS+="$ESC_PREFIX (<code>$ESC_HASH</code>)"$'\n'
else
ESCAPED_COMMITS+=$(html_escape "$line")$'\n'
fi
fi
done <<< "$COMMITS_RAW"
MESSAGE="$PR_STATUS"$'\n\n'"👤 By: $ACTOR"$'\n'"Title: $PR_TITLE"$'\n'"🔗 <a href=\"$PR_URL\">View Pull Request</a>"$'\n\n'"<b>Commits:</b>"$'\n'"$ESCAPED_COMMITS"
fi
# Prepare debug info (only shown on failure)
DECODED_MESSAGE="$MESSAGE"
log "=== GitHub Event Details ==="
log "Event: ${{ github.event_name }}"
log "Action: ${{ github.event.action }}"
log "Actor: ${{ github.actor }}"
log "Repository: ${{ github.repository }}"
log "Ref: ${{ github.ref_name }}"
log ""
log "=== Telegram Message (decoded) ==="
log "$DECODED_MESSAGE"
log ""
IFS=',' read -ra TARGETS <<< "$TELEGRAM_TARGETS"
log "Targets: $TELEGRAM_TARGETS"
SEND_FAILED=false
for TARGET in "${TARGETS[@]}"; do
if [[ "$TARGET" == *"^"* ]]; then
CHAT_ID=$(echo "$TARGET" | cut -d'^' -f1)
TOPIC_ID=$(echo "$TARGET" | cut -d'^' -f2)
else
CHAT_ID="$TARGET"
TOPIC_ID=""
fi
# 🔥 NO SPACE AFTER "bot" — THIS WAS BREAKING THINGS
RESPONSE=$(curl -s -X POST "https://api.telegram.org/bot$BOT_TOKEN/sendMessage" \
-d "chat_id=$CHAT_ID" \
${TOPIC_ID:+-d "message_thread_id=$TOPIC_ID"} \
-d "parse_mode=HTML" \
--data-urlencode "text=$MESSAGE")
if echo "$RESPONSE" | grep -q '"ok":true'; then
echo "✅ Notification sent."
else
SEND_FAILED=true
ERROR_DESC=$(echo "$RESPONSE" | jq -r '.description // "unknown"' 2>/dev/null)
log "❌ Failed to $CHAT_ID: $ERROR_DESC"
log "Response: $RESPONSE"
fi
done
if [[ "$SEND_FAILED" == "true" ]]; then
echo "❌ Job failed. Debug logs:"
echo "$LOG_BUFFER"
exit 1
else
echo "✅ All notifications sent."
fi