TSHARPS-CI/ci-notify.sh

62 lines
1.8 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
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
exit 0