TSHARPS-CI/ci-notify.sh
Claude BM ab89dc05b2 Generate HTML failure report and attach to Telegram on CI fail
When tests fail, ci-runner.sh generates an HTML report with:
- Build metadata (branch, commit, actor, duration)
- Test stats (passed/failed/skipped/errors)
- Failed test table (file + test name)
- Failure detail output

ci-notify.sh sends the HTML as a document attachment alongside
the text notification in the CICD Pipeline topic.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-21 18:33:38 +00:00

73 lines
2.2 KiB
Bash
Executable File

#!/bin/bash
# TSHARPS CI — Telegram Notification Sender
# Sends CI results to the CICD Pipeline Telegram topic.
# Called by ci-runner.sh with results.
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
CONFIG="$SCRIPT_DIR/ci-config.json"
BOT_TOKEN=$(python3 -c "import json; print(json.load(open('$CONFIG'))['telegram_bot_token'])")
CHAT_ID=$(python3 -c "import json; print(json.load(open('$CONFIG'))['telegram_chat_id'])")
TOPIC_ID=$(python3 -c "import json; print(json.load(open('$CONFIG'))['telegram_topic_id'])")
TAG_USERS=$(python3 -c "import json; users=json.load(open('$CONFIG')).get('tag_users_on_fail',[]); print(' '.join('@'+u for u in users))")
BRANCH="$1"
COMMIT="$2"
STATUS="$3" # "pass" or "fail" or "error"
SUMMARY="$4" # e.g., "940 passed, 0 failed, 393 skipped"
FEATURES="$5" # e.g., "31/31 features verified"
DURATION="$6" # e.g., "12.3s"
ACTOR="$7" # who pushed
REPORT_FILE="$8" # optional HTML failure report path
if [ "$STATUS" = "pass" ]; then
ICON="✅"
MSG="${ICON} CI PASSED on ${BRANCH} (${COMMIT})
Tests: ${SUMMARY}
Features: ${FEATURES}
Duration: ${DURATION}
Pushed by: ${ACTOR}
@TSHARPSbm_bot"
elif [ "$STATUS" = "fail" ]; then
ICON="❌"
MSG="${ICON} CI FAILED on ${BRANCH} (${COMMIT})
Tests: ${SUMMARY}
Features: ${FEATURES}
Duration: ${DURATION}
Pushed by: ${ACTOR}
${TAG_USERS} — this build needs attention.
@TSHARPSbm_bot"
elif [ "$STATUS" = "error" ]; then
ICON="🚨"
MSG="${ICON} CI RUNNER ERROR on ${BRANCH} (${COMMIT})
The CI runner itself failed — not a test failure.
Check logs: journalctl -u tsharps-ci --since '5 minutes ago'
${TAG_USERS} — runner needs attention.
@TSHARPSbm_bot"
fi
curl -s -X POST "https://api.telegram.org/bot${BOT_TOKEN}/sendMessage" \
-d "chat_id=${CHAT_ID}" \
-d "message_thread_id=${TOPIC_ID}" \
-d "text=${MSG}" > /dev/null 2>&1
# Send HTML failure report as attachment if available
if [ -n "$REPORT_FILE" ] && [ -f "$REPORT_FILE" ]; then
curl -s -X POST "https://api.telegram.org/bot${BOT_TOKEN}/sendDocument" \
-F "chat_id=${CHAT_ID}" \
-F "message_thread_id=${TOPIC_ID}" \
-F "document=@${REPORT_FILE}" \
-F "caption=CI Failure Report — ${BRANCH} (${COMMIT})" > /dev/null 2>&1
rm -f "$REPORT_FILE"
fi
exit 0