Skip to content

Commit 5d0b2be

Browse files
committed
fix(ci): sanitize Telegram notifications and switch to MarkdownV2
Use GITHUB_ACTOR for author (include GitHub link), escape Markdown special characters in release/error messages, and use GITHUB_RUN_URL for run links. Update parse_mode to MarkdownV2.
1 parent 83a4f32 commit 5d0b2be

File tree

1 file changed

+38
-12
lines changed

1 file changed

+38
-12
lines changed

.github/workflows/ci.yml

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,26 @@ jobs:
175175
TAG="${RELEASE_TAG}"
176176
fi
177177
178-
LAST_AUTHOR=$(git log -1 --pretty=format:'%an')
178+
LAST_AUTHOR="${GITHUB_ACTOR}"
179+
AUTHOR_LINK="https://github.com/${LAST_AUTHOR}"
180+
179181
DATE=$(date +"%Y-%m-%d %H:%M:%S UTC")
180182
181-
MESSAGE="🚀 *New release published!*
182-
🏷️ *Version:* ${TAG}
183-
👤 *Author:* ${LAST_AUTHOR}
184-
🕓 *Date:* ${DATE}
185-
🔗 [View GitHub Action Run](${ACTION_RUN_URL})"
183+
escape_md() {
184+
echo "$1" | sed -E 's/([_*\[\]()~`>#+=|{}.!-])/\\\1/g'
185+
}
186+
187+
eTAG=$(escape_md "$TAG")
188+
eAuthor=$(escape_md "$LAST_AUTHOR")
189+
eAuthorLink="$AUTHOR_LINK"
190+
eActionRun="$ACTION_RUN_URL"
191+
eDate=$(escape_md "$DATE")
192+
193+
MESSAGE="🚀 *New release published!*\n"\
194+
"🏷️ *Version:* ${eTAG}\n"\
195+
"👤 *Author:* [${eAuthor}](${eAuthorLink})\n"\
196+
"🕓 *Date:* ${eDate}\n"\
197+
"🔗 [View GitHub Action Run](${eActionRun})"
186198
187199
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
188200
-d "chat_id=${TELEGRAM_CHAT_ID}" \
@@ -205,7 +217,10 @@ jobs:
205217
env:
206218
TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }}
207219
TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }}
220+
GITHUB_ACTOR: ${{ github.actor }}
221+
GITHUB_RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
208222
run: |
223+
AUTHOR_LINK="https://github.com/${LAST_AUTHOR}"
209224
FAILED_STEPS=()
210225
211226
if [ "${{ needs.test.result }}" = "failure" ]; then
@@ -224,13 +239,24 @@ jobs:
224239
fi
225240
226241
FAILED_LIST=$(IFS=','; echo "${FAILED_STEPS[*]}")
227-
MESSAGE="❌ *Pipeline Error Detected*
228-
⚠️ Failed Steps: *$FAILED_LIST*
229-
🕓 Date: $(date +"%Y-%m-%d %H:%M:%S")
230-
👤 Triggered by: $GITHUB_ACTOR
231-
🔗 Run URL: $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID"
242+
243+
escape_md() {
244+
echo "$1" | sed -E 's/([_*\[\]()~`>#+=|{}.!-])/\\\1/g'
245+
}
246+
247+
eFailedList=$(escape_md "$FAILED_LIST")
248+
eActor=$(escape_md "$GITHUB_ACTOR")
249+
eAuthorLink="$AUTHOR_LINK"
250+
eRunURL="$GITHUB_RUN_URL"
251+
eDate=$(escape_md "$(date +"%Y-%m-%d %H:%M:%S")")
252+
253+
MESSAGE="❌ *Pipeline Error Detected*\n"\
254+
"⚠️ Failed Steps: *${eFailedList}*\n"\
255+
"🕓 Date: ${eDate}\n"\
256+
"👤 Triggered by: [${eActor}](${eAuthorLink})\n"\
257+
"🔗 Run URL: [View Run](${eRunURL})"
232258
233259
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
234260
-d "chat_id=${TELEGRAM_CHAT_ID}" \
235261
-d "text=$MESSAGE" \
236-
-d "parse_mode=Markdown"
262+
-d "parse_mode=MarkdownV2"

0 commit comments

Comments
 (0)