Skip to content

Keiyoushi Upstream Monitor #52

Keiyoushi Upstream Monitor

Keiyoushi Upstream Monitor #52

name: Keiyoushi Upstream Monitor
on:
schedule:
- cron: '0 6 * * *'
workflow_dispatch:
env:
TSBASE: ${{ github.workspace }}/scrapers/webtoons/definitions/webtoons
ANALYZER: ${{ github.workspace }}/scrapers/webtoons/sync/keiyoushi_analyze.py
CLOUDFLARE_CHECK: ${{ github.workspace }}/scrapers/webtoons/sync/cloudflare_check.py
PERSIST: ${{ github.workspace }}/scrapers/webtoons/sync/persist_commit.py
permissions:
issues: write
jobs:
check-upstream:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Restore last checked commit from cache
id: cache
uses: actions/cache@v4
with:
path: /tmp/keiyoushi_last_commit
key: keiyoushi-commit-cache-${{ github.run_id }}
restore-keys: keiyoushi-commit-cache-
- name: Install PostgreSQL client
run: |
sudo apt-get update -qq 2>/dev/null || true
sudo apt-get install -y -qq postgresql-client
- name: Check upstream for changes
id: monitor
env:
GH_TOKEN: ${{ github.token }}
SUPABASE_DATABASE_URL: ${{ secrets.SUPABASE_DATABASE_URL }}
run: |
STATE_FILE="/tmp/keiyoushi_last_commit"
REPO_API="https://api.github.com/repos/keiyoushi/extensions-source"
GH_HEADER="Authorization: Bearer $GH_TOKEN"
persist_supabase() {
local sha="$1"
echo "$sha" > "$STATE_FILE"
python3 "$PERSIST" write "$sha"
}
curl -sL -H "$GH_HEADER" "$REPO_API" -o /tmp/repo.json
python3 -c "import json; d=json.load(open('/tmp/repo.json')); assert 'default_branch' in d" 2>/dev/null || { echo "Error: invalid repo API response"; cat /tmp/repo.json; exit 1; }
DEFAULT_BRANCH=$(python3 -c "import json; print(json.load(open('/tmp/repo.json'))['default_branch'])")
curl -sL -H "$GH_HEADER" "$REPO_API/branches/$DEFAULT_BRANCH" -o /tmp/branch.json
python3 -c "import json; d=json.load(open('/tmp/branch.json')); assert 'commit' in d" 2>/dev/null || { echo "Error: invalid branch API response"; cat /tmp/branch.json; exit 1; }
HEAD_SHA=$(python3 -c "import json; print(json.load(open('/tmp/branch.json'))['commit']['sha'])")
echo "head_sha=$HEAD_SHA" >> $GITHUB_OUTPUT
echo "default_branch=$DEFAULT_BRANCH" >> $GITHUB_OUTPUT
echo "HEAD distant: ${HEAD_SHA:0:8}"
LAST_CHECKED=""
[ -f "$STATE_FILE" ] && LAST_CHECKED=$(cat "$STATE_FILE")
# Fallback Supabase si cache manquant
if [ -z "$LAST_CHECKED" ] && [ -n "$SUPABASE_DATABASE_URL" ]; then
LAST_CHECKED=$(python3 "$PERSIST" read)
[ -n "$LAST_CHECKED" ] && echo "Restored from Supabase: ${LAST_CHECKED:0:8}"
fi
if [ -z "$LAST_CHECKED" ]; then
persist_supabase "$HEAD_SHA"
echo "has_changes=false" >> $GITHUB_OUTPUT
echo "Premier check HEAD ${HEAD_SHA:0:8} enregistre"
exit 0
fi
if [ "$LAST_CHECKED" = "$HEAD_SHA" ]; then
echo "has_changes=false" >> $GITHUB_OUTPUT
echo "Aucun changement (toujours ${HEAD_SHA:0:8})"
persist_supabase "$HEAD_SHA"
exit 0
fi
echo "Nouveaux commits: ${LAST_CHECKED:0:8} -> ${HEAD_SHA:0:8}"
echo "last_checked=$LAST_CHECKED" >> $GITHUB_OUTPUT
echo "has_changes=true" >> $GITHUB_OUTPUT
curl -sL -H "$GH_HEADER" "$REPO_API/compare/$LAST_CHECKED...$HEAD_SHA" -o /tmp/compare.json
python3 -c "import json; d=json.load(open('/tmp/compare.json')); assert 'files' in d" 2>/dev/null || { echo "Error: invalid compare API response"; cat /tmp/compare.json; exit 1; }
python3 "$ANALYZER" /tmp/compare.json "$LAST_CHECKED" "$HEAD_SHA"
persist_supabase "$HEAD_SHA"
- name: Check Cloudflare for new extensions
if: steps.monitor.outputs.has_changes == 'true'
env:
HEAD_SHA: ${{ steps.monitor.outputs.head_sha }}
run: |
python3 "$CLOUDFLARE_CHECK"
- name: Create or update issue
if: steps.monitor.outputs.has_changes == 'true'
env:
GH_TOKEN: ${{ github.token }}
run: |
LAST_CHECKED="${{ steps.monitor.outputs.last_checked }}"
HEAD_SHA="${{ steps.monitor.outputs.head_sha }}"
RUN_ID="${{ github.run_id }}"
if [ "${CRITICAL_CHANGED:-0}" -gt 0 ] || [ "${NEW_CHANGED:-0}" -gt 0 ]; then
EXISTING=$(gh issue list \
--label "keiyoushi-upstream" \
--state open \
--json number,title \
--jq '.[] | select(.title | startswith("[Keiyoushi]")) | .number' \
| head -1)
if [ -n "$EXISTING" ]; then
gh issue comment "$EXISTING" \
--body-file /tmp/keiyoushi_issue_body.md
else
gh issue create \
--title "[Keiyoushi] Changements upstream ($(date +'%d/%m/%Y'))" \
--label "keiyoushi-upstream" \
--body-file /tmp/keiyoushi_issue_body.md
fi
else
echo "Aucun changement critique ou nouveau. Pas d'issue creee."
fi