Delete .github/workflows/telegram-commit-notifier.yml
This commit is contained in:
@@ -1,141 +0,0 @@
|
||||
name: Telegram commit notifier (polling)
|
||||
|
||||
on:
|
||||
schedule:
|
||||
- cron: "*/5 * * * *" # every 5 minutes
|
||||
workflow_dispatch: {}
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
|
||||
concurrency:
|
||||
group: telegram-commit-notifier
|
||||
cancel-in-progress: false
|
||||
|
||||
jobs:
|
||||
notify:
|
||||
runs-on: ubuntu-latest
|
||||
env:
|
||||
BRANCH: main
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 2
|
||||
|
||||
- name: Read last seen SHA (state)
|
||||
id: state
|
||||
shell: bash
|
||||
run: |
|
||||
set -euo pipefail
|
||||
LAST_FILE=".github/telegram_notifier_last_sha.txt"
|
||||
if [ -f "$LAST_FILE" ]; then
|
||||
echo "last_sha=$(tr -d '\n\r' < "$LAST_FILE")" >> "$GITHUB_OUTPUT"
|
||||
else
|
||||
echo "last_sha=" >> "$GITHUB_OUTPUT"
|
||||
fi
|
||||
|
||||
- name: Fetch commits + build message
|
||||
id: build
|
||||
shell: bash
|
||||
env:
|
||||
REPO: ${{ github.repository }}
|
||||
GH_TOKEN: ${{ github.token }}
|
||||
LAST_SHA: ${{ steps.state.outputs.last_sha }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
|
||||
API="https://api.github.com/repos/${REPO}/commits?sha=${BRANCH}&per_page=20"
|
||||
curl -sS \
|
||||
-H "Authorization: Bearer ${GH_TOKEN}" \
|
||||
-H "Accept: application/vnd.github+json" \
|
||||
"$API" > commits.json
|
||||
|
||||
python3 - <<'PY'
|
||||
import json, os
|
||||
|
||||
commits = json.load(open("commits.json"))
|
||||
last_sha = os.environ.get("LAST_SHA","").strip()
|
||||
repo = os.environ["REPO"]
|
||||
branch = os.environ["BRANCH"]
|
||||
|
||||
newest_sha = commits[0]["sha"] if commits else ""
|
||||
new = []
|
||||
for c in commits:
|
||||
if last_sha and c["sha"] == last_sha:
|
||||
break
|
||||
new.append(c)
|
||||
|
||||
has_new = bool(new)
|
||||
|
||||
lines = []
|
||||
for c in reversed(new): # oldest -> newest
|
||||
short = c["sha"][:7]
|
||||
msg = c["commit"]["message"].splitlines()[0]
|
||||
author = (c.get("author") or {}).get("login") or c["commit"]["author"].get("name","")
|
||||
url = c["html_url"]
|
||||
lines.append(f"- {short}: {msg} (by {author})\n{url}")
|
||||
|
||||
text = ""
|
||||
if has_new:
|
||||
text = f"New commits in {repo}@{branch}:\n\n" + "\n\n".join(lines)
|
||||
|
||||
out = os.environ["GITHUB_OUTPUT"]
|
||||
with open(out, "a", encoding="utf-8") as f:
|
||||
f.write(f"has_new={'true' if has_new else 'false'}\n")
|
||||
f.write(f"newest_sha={newest_sha}\n")
|
||||
f.write("text<<EOF\n")
|
||||
f.write(text + "\n")
|
||||
f.write("EOF\n")
|
||||
PY
|
||||
|
||||
- name: Send Telegram message (topic)
|
||||
if: steps.build.outputs.has_new == 'true'
|
||||
shell: bash
|
||||
env:
|
||||
TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }}
|
||||
TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }}
|
||||
TELEGRAM_TOPIC_ID: ${{ secrets.TELEGRAM_TOPIC_ID }}
|
||||
TEXT: ${{ steps.build.outputs.text }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
curl -sS -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
|
||||
-H "content-type: application/json" \
|
||||
-d "$(python3 - <<'PY'
|
||||
import json, os
|
||||
print(json.dumps({
|
||||
"chat_id": int(os.environ["TELEGRAM_CHAT_ID"]),
|
||||
"message_thread_id": int(os.environ["TELEGRAM_TOPIC_ID"]),
|
||||
"text": os.environ["TEXT"],
|
||||
"disable_web_page_preview": True
|
||||
}))
|
||||
PY
|
||||
)"
|
||||
|
||||
- name: Update last seen SHA
|
||||
if: steps.build.outputs.has_new == 'true'
|
||||
shell: bash
|
||||
run: |
|
||||
set -euo pipefail
|
||||
mkdir -p .github
|
||||
# Only update the file if SHA is different to avoid unnecessary commits
|
||||
if [ ! -f .github/telegram_notifier_last_sha.txt ] || [ "$(cat .github/telegram_notifier_last_sha.txt)" != "${{ steps.build.outputs.newest_sha }}" ]; then
|
||||
echo "${{ steps.build.outputs.newest_sha }}" > .github/telegram_notifier_last_sha.txt
|
||||
fi
|
||||
|
||||
- name: Commit state file
|
||||
if: steps.build.outputs.has_new == 'true'
|
||||
shell: bash
|
||||
run: |
|
||||
set -euo pipefail
|
||||
git config user.name "github-actions[bot]"
|
||||
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
|
||||
git add .github/telegram_notifier_last_sha.txt
|
||||
# Only commit if the state file changed
|
||||
if git diff --cached --quiet; then
|
||||
echo "No changes to commit."
|
||||
exit 0
|
||||
fi
|
||||
git commit -m "chore: update telegram notifier last sha"
|
||||
git push
|
||||
Reference in New Issue
Block a user