diff --git a/.github/workflows/notify-telegram-on-pr.yml b/.github/workflows/notify-telegram-on-pr.yml index 2c13c72..7459f75 100644 --- a/.github/workflows/notify-telegram-on-pr.yml +++ b/.github/workflows/notify-telegram-on-pr.yml @@ -2,16 +2,21 @@ name: Notify Telegram on PR on: pull_request: - types: [opened, reopened, ready_for_review] + # Trigger on opened, reopened, ready_for_review AND closed + types: [opened, reopened, ready_for_review, closed] jobs: notify: runs-on: ubuntu-latest - # Skip if secrets are missing (e.g., forked PRs) if: ${{ secrets.TELEGRAM_BOT_TOKEN != '' && secrets.TELEGRAM_CHAT_ID != '' }} steps: - - name: Send Telegram message + # ---------------------------------------------------------------- + # 1. NOTIFY ON NEW / UPDATED PR + # ---------------------------------------------------------------- + - name: Send Telegram (New PR) + # Run only if the PR is NOT closed + if: github.event.action != 'closed' env: TG_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }} CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }} @@ -24,30 +29,83 @@ jobs: HEAD_BRANCH: ${{ github.event.pull_request.head.ref }} run: | set -euo pipefail - API_URL="https://api.telegram.org/bot${TG_TOKEN}/sendMessage" MSG="\New Pull Request\ -Repo: \${REPO}\ -By: \${ACTOR}\ -PR: \#${PR_NUM}\ — ${PR_TITLE} -Branch: \${HEAD_BRANCH}\ → \${BASE_BRANCH}\ -Link: ${PR_URL}" + Repo: \${REPO}\ + By: \${ACTOR}\ + PR: \#${PR_NUM}\ — ${PR_TITLE} + Branch: \${HEAD_BRANCH}\ → \${BASE_BRANCH}\ + Link: ${PR_URL}" - # Show short debug (lengths only; won’t leak secrets) - echo "Token length: ${#TG_TOKEN}, Chat ID: ${CHAT_ID}" + curl -sS -X POST "$API_URL" \ + -d "chat_id=${CHAT_ID}" \ + -d "parse_mode=HTML" \ + -d "disable_web_page_preview=true" \ + --data-urlencode "text=${MSG}" - # Send and capture HTTP code + body - HTTP_CODE=$(curl -sS -w "%{http_code}" -o /tmp/resp.json "$API_URL" \ + # ---------------------------------------------------------------- + # 2. NOTIFY ON MERGE SUCCESS + # ---------------------------------------------------------------- + - name: Send Telegram (Merge Successful) + # Run only if the PR is Closed AND Merged + if: github.event.action == 'closed' && github.event.pull_request.merged == true + env: + TG_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }} + CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }} + REPO: ${{ github.repository }} + ACTOR: ${{ github.actor }} + PR_NUM: ${{ github.event.pull_request.number }} + PR_TITLE: ${{ github.event.pull_request.title }} + PR_URL: ${{ github.event.pull_request.html_url }} + BASE_BRANCH: ${{ github.event.pull_request.base.ref }} + HEAD_BRANCH: ${{ github.event.pull_request.head.ref }} + run: | + set -euo pipefail + API_URL="https://api.telegram.org/bot${TG_TOKEN}/sendMessage" + + MSG="\💜 Merge Successful\ + Repo: \${REPO}\ + By: \${ACTOR}\ + PR: \#${PR_NUM}\ — ${PR_TITLE} + + Branch: \${HEAD_BRANCH}\ → \${BASE_BRANCH}\ + + Link: ${PR_URL}" + + curl -sS -X POST "$API_URL" \ -d "chat_id=${CHAT_ID}" \ -d "parse_mode=HTML" \ -d "disable_web_page_preview=true" \ - --data-urlencode "text=${MSG}") + --data-urlencode "text=${MSG}" + + # ---------------------------------------------------------------- + # 3. NOTIFY ON FAILURE + # ---------------------------------------------------------------- + - name: Notify Telegram on Failure + # Run only if a previous step failed + if: failure() + env: + TG_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }} + CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }} + REPO: ${{ github.repository }} + ACTOR: ${{ github.actor }} + PR_NUM: ${{ github.event.pull_request.number }} + PR_TITLE: ${{ github.event.pull_request.title }} + RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} + run: | + set -euo pipefail + API_URL="https://api.telegram.org/bot${TG_TOKEN}/sendMessage" - echo "Telegram response:" - cat /tmp/resp.json - echo - echo "HTTP ${HTTP_CODE}" + MSG="\❌ Action Failed\ + Repo: \${REPO}\ + PR: \#${PR_NUM}\ — ${PR_TITLE} + By: ${ACTOR} + + 👉 \View Error Logs\" - # Fail if Telegram didn’t return 200 - [ "$HTTP_CODE" = "200" ] || exit 1 + curl -sS -X POST "$API_URL" \ + -d "chat_id=${CHAT_ID}" \ + -d "parse_mode=HTML" \ + -d "disable_web_page_preview=true" \ + --data-urlencode "text=${MSG}" \ No newline at end of file