name: Lint Test Deploy

on: [push]

jobs:
  lint:
    name: Lint

    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3

      - name: Use Node.js 18
        uses: actions/setup-node@v3
        with:
          node-version: 18

      - name: Install dependencies
        run: |
          npm ci
          composer install

      - name: Run linter
        run: npm run lint

      - name: Run PHP linter
        run: |
          npm run wp-env start
          npm run lint:php:ci

  tests:
    name: Tests

    runs-on: ubuntu-latest

    strategy:
      matrix:
        wp: [ '6.2', '6.3', '6.4' ]

    steps:
      - uses: actions/checkout@v3

      - name: Use Node.js 18
        uses: actions/setup-node@v3
        with:
          node-version: 18

      - name: Install dependencies
        run: |
          npm ci
          composer install

      - name: Install Playwright dependencies
        run: |
          npx playwright install chromium firefox webkit --with-deps

      - name: "Configure environment with WP ${{ matrix.wp }}"
        run: |
          echo -e '{\n\t"core": "WordPress/WordPress#${{ matrix.wp }}"\n}' > ./.wp-env.override.json

      - name: Install WordPress
        run: |
          npm run wp-env start

      - name: Running JavaScript E2E tests
        run: npm run test:e2e

      - name: Running PHP unit tests
        run: npm run test:unit:php:ci

      - uses: actions/upload-artifact@v3
        if: always()
        with:
          name: playwright-report
          path: artifacts/
          retention-days: 7

  deploy-dry-run:
    name: '[DRY RUN] Deploy'

    # only run on master but not tags
    if: ${{ github.ref == 'refs/heads/master' && !startsWith(github.ref, 'refs/tags/') }}

    needs: [ lint, tests ]

    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3

      - shell: bash
        env:
          SVN_PASSWORD: ${{ secrets.SVN_PASSWORD }}
        run: |
          DRYRUN=true scripts/deploy-wp-plugin.sh

  deploy:
    name: Deploy

    # only run on tags
    if: startsWith(github.ref, 'refs/tags/')

    needs: [ lint, tests ]

    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3

      - shell: bash
        env:
          SVN_PASSWORD: ${{ secrets.SVN_PASSWORD }}
        run: |
          scripts/deploy-wp-plugin.sh
