name: Sync wiki

on:
  push:
    branches: [main]
    paths:
      - docs/**

concurrency:
  group: sync-wiki
  cancel-in-progress: true

permissions:
  contents: write

jobs:
  sync-wiki:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Clone wiki
        run: |
          git clone --depth 1 \
            https://x-access-token:${{ github.token }}@github.com/${{ github.repository }}.wiki.git \
            wiki-temp

      - name: Sync docs to wiki
        run: |
          find wiki-temp -not -path 'wiki-temp/.git*' -type f -delete
          python3 .github/scripts/sync-wiki.py

      - name: Commit and push
        working-directory: wiki-temp
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add -A
          git diff --staged --quiet || git commit -m "sync from ${{ github.sha }}"
          git push
