name: Lint Test Deploy

on: [push]

jobs:
  lint:
    name: Lint

    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: Use Node.js 16
        uses: actions/setup-node@v1
        with:
          node-version: 16

      - 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

  tests:
    name: Tests

    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: Use Node.js 16
        uses: actions/setup-node@v1
        with:
          node-version: 16

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

      - name: Install WordPress
        run: |
          chmod -R 767 ./ # TODO: Possibly integrate in wp-env
          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

  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@v2

      - 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@v2

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