Skip to content

Commit 5e0ab01

Browse files
fix(github-notifications-bot): Add escape_markdown for sending commit messages
1 parent 887aca6 commit 5e0ab01

File tree

1 file changed

+47
-14
lines changed

1 file changed

+47
-14
lines changed

.github/workflows/github-notifications-bot.yml

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,32 @@ jobs:
1717
BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }}
1818
TELEGRAM_TARGETS: ${{ vars.TELEGRAM_TARGETS }}
1919
run: |
20+
# Function to escape special characters for Telegram MarkdownV2
21+
escape_markdown() {
22+
local input="$1"
23+
# Escape: _ * [ ] ( ) ~ ` > # + - = | { } . !
24+
printf '%s' "$input" | sed 's/[[\*_\(\)\~\`\>\#\+\-\=\|\{\}\.\!]/\\&/g'
25+
}
26+
2027
# Determine event type and create appropriate message
2128
if [[ "${{ github.event_name }}" == "push" ]]; then
22-
# Push event
2329
EVENT_STATUS="🔔 *New Push to ${{ github.ref_name }}*"
30+
EVENT_STATUS_ESCAPED=$(escape_markdown "$EVENT_STATUS")
31+
32+
# Extract and escape commit messages
33+
COMMITS_RAW=$(echo '${{ toJSON(github.event.commits) }}' | jq -r '.[] | "- " + .message + " (`" + .id[0:7] + "`)"' | head -n 10)
34+
ESCAPED_COMMITS=""
35+
while IFS= read -r line; do
36+
if [[ -n "$line" ]]; then
37+
escaped_line=$(escape_markdown "$line")
38+
ESCAPED_COMMITS+="$escaped_line"$'\n'
39+
fi
40+
done <<< "$COMMITS_RAW"
41+
ESCAPED_COMMITS=${ESCAPED_COMMITS%$'\n'}
2442
25-
# Get commit messages from the push
26-
COMMITS=$(echo '${{ toJSON(github.event.commits) }}' | jq -r '.[] | "- " + .message + " (`" + .id[0:7] + "`)"' | head -n 10)
43+
COMPARE_URL="${{ github.event.compare }}"
44+
MESSAGE="$EVENT_STATUS_ESCAPED%0A%0A👤 By: ${{ github.actor }}%0A🔗 [View Commits]($COMPARE_URL)%0A%0A*Commits:*%0A$ESCAPED_COMMITS"
2745
28-
MESSAGE="$EVENT_STATUS%0A%0A👤 By: ${{ github.actor }}%0A🔗 [View Commits](${{ github.event.compare }})%0A%0A*Commits:*%0A$COMMITS"
2946
else
3047
# Pull request event
3148
if [[ "${{ github.event.action }}" == "opened" ]]; then
@@ -36,14 +53,29 @@ jobs:
3653
PR_STATUS="❌ *Pull Request Closed Without Merging*"
3754
fi
3855
56+
PR_STATUS_ESCAPED=$(escape_markdown "$PR_STATUS")
57+
PR_TITLE_ESCAPED=$(escape_markdown "${{ github.event.pull_request.title }}")
58+
PR_URL="${{ github.event.pull_request.html_url }}"
59+
ACTOR="${{ github.actor }}"
60+
3961
# Fetch commits for this PR
40-
COMMITS=$(curl -s \
62+
COMMITS_API="https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/commits"
63+
COMMITS_RAW=$(curl -s \
4164
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
42-
"https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/commits" \
65+
"$COMMITS_API" \
4366
| jq -r '.[] | "- " + .commit.message + " (`" + .sha[0:7] + "`)"' \
44-
| head -n 10) # limit to 10 commits for readability
67+
| head -n 10)
4568
46-
MESSAGE="$PR_STATUS%0A%0A👤 By: ${{ github.actor }}%0ATitle: ${{ github.event.pull_request.title }}%0A🔗 [View Pull Request](${{ github.event.pull_request.html_url }})%0A%0A*Commits:*%0A$COMMITS"
69+
ESCAPED_COMMITS=""
70+
while IFS= read -r line; do
71+
if [[ -n "$line" ]]; then
72+
escaped_line=$(escape_markdown "$line")
73+
ESCAPED_COMMITS+="$escaped_line"$'\n'
74+
fi
75+
done <<< "$COMMITS_RAW"
76+
ESCAPED_COMMITS=${ESCAPED_COMMITS%$'\n'}
77+
78+
MESSAGE="$PR_STATUS_ESCAPED%0A%0A👤 By: $ACTOR%0ATitle: $PR_TITLE_ESCAPED%0A🔗 [View Pull Request]($PR_URL)%0A%0A*Commits:*%0A$ESCAPED_COMMITS"
4779
fi
4880
4981
# Parse and loop through targets in format: chat_id^topic_id,chat_id^topic_id
@@ -53,15 +85,16 @@ jobs:
5385
TOPIC_ID=$(echo "$TARGET" | cut -d'^' -f2)
5486
5587
# Build curl command
56-
CURL_CMD="curl -s -X POST \"https://api.telegram.org/bot$BOT_TOKEN/sendMessage\" -d chat_id=\"$CHAT_ID\""
88+
CURL_CMD="curl -s -X POST \"https://api.telegram.org/bot${BOT_TOKEN}/sendMessage\""
89+
CURL_CMD="$CURL_CMD -d chat_id=\"$CHAT_ID\""
90+
CURL_CMD="$CURL_CMD -d parse_mode=\"MarkdownV2\""
91+
CURL_CMD="$CURL_CMD -d text=\"$MESSAGE\""
5792
58-
# Add topic_id only if it's not empty
59-
if [[ -n "$TOPIC_ID" ]]; then
93+
# Add message_thread_id only if topic_id is provided
94+
if [[ -n "$TOPIC_ID" && "$TOPIC_ID" != " " ]]; then
6095
CURL_CMD="$CURL_CMD -d message_thread_id=\"$TOPIC_ID\""
6196
fi
6297
63-
CURL_CMD="$CURL_CMD -d parse_mode=\"Markdown\" -d text=\"$MESSAGE\""
64-
6598
# Execute the command
6699
eval $CURL_CMD
67-
done
100+
done

0 commit comments

Comments
 (0)