name: "Windows Build"

# any branch is useful for testing before a PR is submitted
on:
  push:
    paths-ignore:
      - "doc/**"
  pull_request:
    paths-ignore:
      - "doc/**"

permissions:
  contents: read

jobs:
  build-zip:
    permissions: write-all
    # run on pushes to any branch
    # run on PRs from external forks
    if: |
      (github.event_name != 'pull_request')
      || (github.event.pull_request.head.repo.id != github.event.pull_request.base.repo.id)
    name: Build .zip
    runs-on: windows-latest
    steps:
      -
        uses: msys2/setup-msys2@v2
        with:
          path-type: inherit
          install: >-
            zip
      -
        name: Checkout repository
        uses: actions/checkout@v4
      -
        uses: actions/setup-node@v4
        with:
          node-version: 22
      - uses: pnpm/action-setup@v4
        name: Install pnpm
        with:
          version: 9.0.4
          run_install: false
      - name: Get pnpm store directory
        shell: bash
        run: |
          echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
      - uses: actions/cache@v4
        name: Setup pnpm cache
        with:
          path: ${{ env.STORE_PATH }}
          key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-store-
      - name: Only install direct dependencies
        run: pnpm config set auto-install-peers false
      -
        name: Install all dependencies and symlink for ep_etherpad-lite
        shell: msys2 {0}
        run: bin/installDeps.sh
      -
        name: Run the backend tests
        shell: msys2 {0}
        working-directory: src
        run: pnpm test
      -
        name: Run Etherpad
        working-directory: src
        run: |
          pnpm i
          pnpm exec playwright install --with-deps
          pnpm run prod &
          curl --connect-timeout 10 --max-time 20 --retry 5 --retry-delay 10 --retry-max-time 60 --retry-connrefused http://127.0.0.1:9001/p/test
          pnpm exec playwright install chromium  --with-deps
          pnpm run test-ui --project=chromium
      # On release, create release
      - name: Generate Changelog
        if: ${{startsWith(github.ref, 'refs/tags/v') }}
        working-directory: bin
        run: pnpm run generateChangelog ${{ github.ref }} > ${{ github.workspace }}-CHANGELOG.txt
      - name: Release
        uses: softprops/action-gh-release@v2
        if: ${{startsWith(github.ref, 'refs/tags/v') }}
        with:
          body_path: ${{ github.workspace }}-CHANGELOG.txt
          make_latest: true
