Skip to content

Commit 5df32ac

Browse files
committed
feat: testing workflows for telegram commits
Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent d424ae5 commit 5df32ac

File tree

1 file changed

+36
-17
lines changed

1 file changed

+36
-17
lines changed
Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Telegram – Last 10 Commits
1+
name: Telegram – Last 10 Commits (Pretty)
22

33
on:
44
push:
@@ -11,14 +11,14 @@ permissions:
1111
jobs:
1212
send:
1313
runs-on: ubuntu-latest
14-
environment: SANDBOX # uses TG_BOT_TOKEN and TG_CHAT_ID secrets from your env
14+
environment: SANDBOX # uses TG_BOT_TOKEN and TG_CHAT_ID
1515
steps:
1616
- name: Checkout repo
1717
uses: actions/checkout@v4
1818
with:
19-
fetch-depth: 50 # fetch enough history for git log
19+
fetch-depth: 50
2020

21-
- name: Build message
21+
- name: Build message (HTML)
2222
id: msg
2323
shell: bash
2424
run: |
@@ -29,15 +29,33 @@ jobs:
2929
REF_NAME="${{ github.ref_name }}"
3030
RUN_URL="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
3131
REPO_URL="${{ github.server_url }}/${{ github.repository }}"
32+
COMMIT_BASE="${{ github.server_url }}/${{ github.repository }}/commit"
3233
33-
# Last 10 commits
34-
COMMITS="$(git log -n 10 --pretty=format:'- %s (%h) by %an' || true)"
35-
[[ -z "$COMMITS" ]] && COMMITS="- (no commit messages found)"
34+
# Escape minimal HTML for Telegram parse_mode=HTML
35+
esc() { sed -e 's/&/\&amp;/g' -e 's/</\&lt;/g' -e 's/>/\&gt;/g'; }
3636
37-
MESSAGE=$(
38-
printf "📣 Last 10 commits\nRepo: %s\nBy: %s\nRef: %s\nRun: %s\n\n%s\n\nRepo: %s\n" \
39-
"$REPO" "$ACTOR" "$REF_NAME" "$RUN_URL" "$COMMITS" "$REPO_URL"
40-
)
37+
# Last 10 commits: subject, short hash, author, full hash for link
38+
mapfile -t LINES < <(git log -n 10 --pretty=format:'%s%x1f%h%x1f%an%x1f%H' || true)
39+
40+
if [[ ${#LINES[@]} -eq 0 ]]; then
41+
COMMITS_HTML="- (no commit messages found)"
42+
else
43+
COMMITS_HTML=""
44+
for row in "${LINES[@]}"; do
45+
IFS=$'\x1f' read -r subj short author full <<<"$row"
46+
subj_esc="$(printf '%s' "$subj" | esc)"
47+
author_esc="$(printf '%s' "$author" | esc)"
48+
COMMITS_HTML+=$'- ' # bullet
49+
COMMITS_HTML+="<a href=\"${COMMIT_BASE}/${full}\">${subj_esc}</a> "
50+
COMMITS_HTML+="(<code>${short}</code>) by ${author_esc}\n"
51+
done
52+
fi
53+
54+
HEADER="<b>📣 Last 10 commits</b>\n"
55+
META="Repo: <a href=\"${REPO_URL}\">${REPO}</a>\nBy: ${ACTOR}\nRef: <code>${REF_NAME}</code>\nRun: <a href=\"${RUN_URL}\">view run</a>\n\n"
56+
FOOTER="\nRepo: <a href=\"${REPO_URL}\">${REPO_URL}</a>"
57+
58+
MESSAGE="${HEADER}${META}${COMMITS_HTML}${FOOTER}"
4159
4260
{
4361
echo "text<<EOF"
@@ -53,16 +71,17 @@ jobs:
5371
shell: bash
5472
run: |
5573
set -euo pipefail
56-
5774
if [[ -z "${TG_BOT_TOKEN:-}" || -z "${TG_CHAT_ID:-}" ]]; then
58-
echo "❌ Telegram secrets missing"
59-
exit 1
75+
echo "❌ Telegram secrets missing"; exit 1
6076
fi
6177
62-
# Send message
78+
# Send as HTML so links and <code> render nicely
6379
curl -sS -X POST "https://api.telegram.org/bot${TG_BOT_TOKEN}/sendMessage" \
6480
-H 'Content-Type: application/json' \
65-
-d "$(jq -n --arg chat_id "$TG_CHAT_ID" --arg text "$TEXT" \
66-
'{chat_id:$chat_id, text:$text, disable_web_page_preview:true}')"
81+
-d "$(jq -n \
82+
--arg chat_id "$TG_CHAT_ID" \
83+
--arg text "$TEXT" \
84+
--arg pm "HTML" \
85+
'{chat_id:$chat_id, text:$text, parse_mode:$pm, disable_web_page_preview:true}')"
6786
6887
echo "✅ Posted last 10 commits to Telegram channel."

0 commit comments

Comments
 (0)