name: CI

on:
  pull_request:
    paths-ignore:
      - 'microsite/**'
jobs:
  verify:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [12.x, 14.x]

    env:
      CI: true
      NODE_OPTIONS: --max-old-space-size=4096

    steps:
      - uses: actions/checkout@v2
      - name: fetch branch master
        run: git fetch origin master

      # Beginning of yarn setup, keep in sync between all workflows.
      # TODO(Rugvip): move this to composite action once all features we use are supported
      - name: use node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node-version }}
          registry-url: https://registry.npmjs.org/ # Needed for auth

      # Cache every node_modules folder inside the monorepo
      - name: cache all node_modules
        id: cache-modules
        uses: actions/cache@v2
        with:
          path: '**/node_modules'
          # We use both yarn.lock and package.json as cache keys to ensure that
          # changes to local monorepo packages bust the cache.
          key: ${{ runner.os }}-v${{ matrix.node-version }}-node_modules-${{ hashFiles('yarn.lock', '**/package.json') }}

      # If we get a cache hit for node_modules, there's no need to bring in the global
      # yarn cache or run yarn install, as all dependencies will be installed already.

      - name: find location of global yarn cache
        id: yarn-cache
        if: steps.cache-modules.outputs.cache-hit != 'true'
        run: echo "::set-output name=dir::$(yarn cache dir)"

      - name: cache global yarn cache
        uses: actions/cache@v2
        if: steps.cache-modules.outputs.cache-hit != 'true'
        with:
          path: ${{ steps.yarn-cache.outputs.dir }}
          key: ${{ runner.os }}-yarn-${{ hashFiles('yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-yarn-

      - name: yarn install
        if: steps.cache-modules.outputs.cache-hit != 'true'
        run: yarn install --frozen-lockfile
      # End of yarn setup

      - name: check for yarn.lock changes
        id: yarn-lock
        run: git diff --quiet origin/master HEAD -- yarn.lock
        continue-on-error: true

      - name: verify doc links
        run: node docs/verify-links.js

      - name: prettier
        run: yarn prettier:check

      - name: lint
        run: yarn lerna -- run lint --since origin/master

      - name: type checking and declarations
        run: yarn tsc:full

      - name: build changed packages
        if: ${{ steps.yarn-lock.outcome == 'success' }}
        run: yarn lerna -- run build --since origin/master

      - name: build all packages
        if: ${{ steps.yarn-lock.outcome == 'failure' }}
        run: yarn lerna -- run build

      - name: verify type dependencies
        run: yarn lint:type-deps

      - name: test changed packages
        if: ${{ steps.yarn-lock.outcome == 'success' }}
        run: yarn lerna -- run test --since origin/master -- --coverage

      - name: test all packages (and upload coverage)
        if: ${{ steps.yarn-lock.outcome == 'failure' }}
        run: |
          yarn lerna -- run test -- --coverage
          bash <(curl -s https://codecov.io/bash)

      - name: verify plugin template
        run: yarn lerna -- run diff -- --check

      - name: ensure clean working directory
        run: |
          if files=$(git ls-files --exclude-standard --others --modified) && [[ -z "$files" ]]; then
            exit 0
          else
            echo ""
            echo "Working directory has been modified:"
            echo ""
            git status --short
            echo ""
            exit 1
          fi
