on:
  pull_request:
    types: [ closed, merged ]

jobs:
  cleanup:
    runs-on: x86_64
    container:
      image: alpine:latest
    steps:
      - name: Environment setup
        run: |
          apk add git nodejs jq coreutils curl tree gawk grep
      - name: Pages repo pull
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          ref: pages
      - name: Render website
        run: |
          export GITHUB_PR_NAME=$(grep -l ${{ github.sha }} previews/*/git_sha.txt | awk -F '/' '{print $2}')
          echo $GITHUB_PR_NAME >> $GITHUB_ENV
          rm -rf previews/$GITHUB_PR_NAME || true
          cd previews
          tree -d -H '.' -L 1 --noreport --charset utf-8 -T "Versions" -o index.html
      - name: Website upload
        run: |
          git config user.name "forgejo-actions[bot]"
          git config user.email "dev@ayakael.net"
          git add .
          git commit -m "Clean-up $GITHUB_PR_NAME"
          git push
