name: Hugo regression

on: [pull_request]

permissions:
  contents: read

env:
  PRODUCTION_SITEMAP: https://example.com/sitemap.xml
  SITE_PREFIX: /
  HUGO_VERSION: '0.131.0'

jobs:
  check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
      - name: Build the website
        run: |
          curl -fsSL "https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_${HUGO_VERSION}_linux-amd64.tar.gz" \
            | tar -xz -C "$RUNNER_TEMP" hugo
          "$RUNNER_TEMP/hugo" --destination public --baseURL "http://127.0.0.1:8000${SITE_PREFIX%/}/"
      - name: Serve the build and check routes
        env:
          BUILD_DIR: public
          PR_HEAD_SHA: ${{ github.event.pull_request.head.sha }}
        run: |
          SITE_ORIGIN=http://127.0.0.1:8000
          serve_dir="$BUILD_DIR"
          if [[ "$SITE_PREFIX" != "/" ]]; then
            serve_dir="$(mktemp -d "$RUNNER_TEMP/sireg-site.XXXXXX")"
            mkdir -p "$(dirname "$serve_dir$SITE_PREFIX")"
            ln -s "$PWD/$BUILD_DIR" "$serve_dir$SITE_PREFIX"
          fi

          python3 -m http.server 8000 --bind 127.0.0.1 --directory "$serve_dir" > "$RUNNER_TEMP/site.log" 2>&1 &
          server=$!
          trap 'kill "$server" 2>/dev/null || true' EXIT
          curl --fail --silent --show-error --retry 10 --retry-delay 1 --retry-connrefused \
            "$SITE_ORIGIN${SITE_PREFIX%/}/" > /dev/null

          npx --yes sireg@1.1.0 check \
            --sitemap "$PRODUCTION_SITEMAP" \
            --origin "$SITE_ORIGIN" \
            --strategy random --limit 25 --seed "$PR_HEAD_SHA" \
            --expect-status 200 --max-redirects 0 \
            --report console --html reports/sireg.html
      - uses: actions/upload-artifact@v4
        if: always()
        with:
          name: sireg-report
          path: reports/sireg.html
          if-no-files-found: ignore
