name: Update file index

on:
  push:
    branches:
      - '**'

permissions:
  contents: write

concurrency:
  group: file-index-${{ github.ref }}
  cancel-in-progress: false

jobs:
  update-index:
    runs-on: ubuntu-latest
    timeout-minutes: 5
    steps:
      - name: Checkout branch
        uses: actions/checkout@v7
        with:
          ref: ${{ github.ref }}

      - name: Test index generator
        run: python3 -m unittest discover -s tests -v

      - name: Generate index.html
        run: python3 scripts/generate_index.py

      - name: Commit changed index
        env:
          INDEX_BRANCH: ${{ github.ref_name }}
        run: |
          git add -- index.html
          if git diff --cached --quiet; then
            echo "index.html is already up to date."
            exit 0
          fi
          git config user.name "github-actions[bot]"
          git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git commit -m "docs: update file index"
          git push origin "HEAD:$INDEX_BRANCH"
