Skip to content

Commit ae938a3

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

File tree

1 file changed

+36
-56
lines changed

1 file changed

+36
-56
lines changed

.github/workflows/telegram-commits.yml

Lines changed: 36 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ permissions:
1010
jobs:
1111
send:
1212
runs-on: ubuntu-latest
13-
environment: SANDBOX # Must contain TG_BOT_TOKEN and TG_CHAT_ID
13+
environment: SANDBOX # Must contain TG_BOT_TOKEN and TG_CHAT_ID
1414
steps:
15-
- name: Checkout repository
15+
- name: Checkout
1616
uses: actions/checkout@v4
1717
with:
18-
fetch-depth: 50 # Fetch enough commits for recent history
18+
fetch-depth: 50 # enough for recent history
1919

20-
- name: Build commit message (plain text, IST timestamps)
20+
- name: Build message (plain text, IST timestamps)
2121
id: msg
2222
shell: bash
2323
env:
@@ -26,82 +26,62 @@ jobs:
2626
REF_NAME: ${{ github.ref_name }}
2727
REPO_URL: ${{ github.server_url }}/${{ github.repository }}
2828
SITE_URL: https://someshdiwan.github.io/JavaEvolution-Learning-Growing-Mastering/
29-
COMMIT_COUNT: 10 # Number of commits to fetch
3029
run: |
3130
set -euo pipefail
3231
33-
# Validate environment variables
34-
if [[ -z "${REPO:-}" || -z "${ACTOR:-}" || -z "${REF_NAME:-}" || -z "${REPO_URL:-}" ]]; then
35-
echo "Error: Missing required GitHub environment variables"
36-
exit 1
37-
fi
38-
39-
# Fetch last N commits with subject | short hash | IST datetime
40-
mapfile -t COMMITS < <(
32+
# Pull last 10 commits with subject | short hash | IST datetime
33+
# Use Git's format-local and force runner TZ to Asia/Kolkata
34+
mapfile -t LINES < <(
4135
TZ=Asia/Kolkata \
42-
git log -n "$COMMIT_COUNT" \
36+
git log -n 10 \
4337
--date=format-local:'%d %b %Y, %H:%M IST' \
44-
--pretty=format:'%s | %h | %ad' 2>/dev/null || true
38+
--pretty=format:'%s%x1f%h%x1f%ad' || true
4539
)
4640
47-
# Format commit message
48-
if [[ ${#COMMITS[@]} -eq 0 ]]; then
49-
COMMIT_MESSAGE="- No recent commits found"
41+
if [[ ${#LINES[@]} -eq 0 ]]; then
42+
COMMITS="- (no commit messages found)"
5043
else
51-
COMMIT_MESSAGE=""
52-
for row in "${COMMITS[@]}"; do
53-
IFS='|' read -r subj short when <<<"$row"
54-
COMMIT_MESSAGE+="- ${subj//|/} (${short}) — ${when}"$'\n\n'
44+
COMMITS=""
45+
for row in "${LINES[@]}"; do
46+
IFS=$'\x1f' read -r subj short when <<<"$row"
47+
# bullet + blank line after each item
48+
COMMITS+="- ${subj} (${short}) — ${when}"$'\n\n'
5549
done
56-
COMMIT_MESSAGE="${COMMIT_MESSAGE%$'\n\n'}" # Trim trailing newlines
50+
# trim final blank line
51+
COMMITS="${COMMITS%$'\n\n'}"
5752
fi
5853
59-
# Construct Telegram message
6054
{
6155
printf 'text<<MSGEOF\n'
62-
printf '📣 Recent Commits (%s)\n' "$COMMIT_COUNT"
56+
printf '📣 Last 10 commits\n'
6357
printf 'Site: %s\n' "$SITE_URL"
64-
printf 'Repository: %s\n' "$REPO"
58+
printf 'Repo: %s\n' "$REPO"
6559
printf 'Branch: %s\n' "$REF_NAME"
66-
printf 'Pushed by: %s\n' "$ACTOR"
67-
printf '\n%s\n' "$COMMIT_MESSAGE"
68-
printf '\n🔗 Repo: %s\n' "$REPO_URL"
60+
printf 'By: %s\n' "$ACTOR"
61+
printf '\n'
62+
printf '%s\n' "$COMMITS"
63+
printf '\n'
64+
printf '🔗 Repo: %s\n' "$REPO_URL"
6965
printf 'MSGEOF\n'
7066
} >> "$GITHUB_OUTPUT"
7167
72-
- name: Send to Telegram (plain text with retry)
68+
- name: Send to Telegram (plain text)
7369
shell: bash
7470
env:
7571
TG_BOT_TOKEN: ${{ secrets.TG_BOT_TOKEN }}
7672
TG_CHAT_ID: ${{ secrets.TG_CHAT_ID }}
7773
TEXT: ${{ steps.msg.outputs.text }}
78-
MAX_RETRIES: 3
79-
RETRY_DELAY: 5
8074
run: |
8175
set -euo pipefail
76+
[[ -n "${TG_BOT_TOKEN:-}" && -n "${TG_CHAT_ID:-}" ]] || { echo "Missing TG secrets"; exit 1; }
8277
83-
# Validate Telegram secrets
84-
if [[ -z "${TG_BOT_TOKEN:-}" || -z "${TG_CHAT_ID:-}" ]]; then
85-
echo "Error: Missing Telegram bot token or chat ID"
86-
exit 1
87-
fi
88-
89-
# Send message with retry logic
90-
for ((i=1; i<=MAX_RETRIES; i++)); do
91-
if curl -sS -X POST "https://api.telegram.org/bot${TG_BOT_TOKEN}/sendMessage" \
92-
-H 'Content-Type: application/json' \
93-
-d "$(jq -n \
94-
--arg chat_id "$TG_CHAT_ID" \
95-
--arg text "$TEXT" \
96-
'{chat_id:$chat_id, text:$text, disable_web_page_preview:true}')" \
97-
| jq -e '.ok == true' >/dev/null; then
98-
echo "✅ Successfully posted to Telegram (attempt $i)"
99-
exit 0
100-
else
101-
echo "⚠️ Telegram API request failed (attempt $i)"
102-
[[ $i -lt $MAX_RETRIES ]] && sleep "$RETRY_DELAY"
103-
fi
104-
done
78+
# Plain text: keeps your newlines and spacing as-is
79+
curl -sS -X POST "https://api.telegram.org/bot${TG_BOT_TOKEN}/sendMessage" \
80+
-H 'Content-Type: application/json' \
81+
-d "$(jq -n \
82+
--arg chat_id "$TG_CHAT_ID" \
83+
--arg text "$TEXT" \
84+
'{chat_id:$chat_id, text:$text, disable_web_page_preview:true}')" \
85+
| jq -e '.ok == true' >/dev/null
10586
106-
echo "❌ Failed to post to Telegram after $MAX_RETRIES attempts"
107-
exit 1
87+
echo "✅ Posted last 10 commits to Telegram."

0 commit comments

Comments
 (0)