name: Run backend specs
on:
  pull_request:
    branches:
      - develop
      - master
    paths:
      - 'serverless/**'

jobs:
  setup:
    name: Install dependencies
    runs-on: ubuntu-latest
    timeout-minutes: 10
    defaults:
      run:
        shell: bash
        working-directory: ./serverless/users
    if: "!contains(toJSON(github.event.commits.*.message), '[skip-ci]') && !contains(toJSON(github.event.commits.*.message), '[skip ci]')"
    steps:
      - uses: actions/checkout@v3
      - name: Use Node.js 
        uses: actions/setup-node@v3
        with:
          node-version: 14.x
          cache: 'yarn'
      - name: Cache
        uses: actions/cache@v3
        id: pr-cache-node_modules
        with:
          path: ./serverless/users/node_modules
          key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
      - name: Install dependencies
        if: steps.pr-cache-node_modules.outputs.cache-hit != 'true'
        run: pwd && yarn install --frozen-lockfile --non-interactive
  specs:
    name: Run all specs and lint
    runs-on: ubuntu-latest
    timeout-minutes: 10
    needs: setup
    defaults:
      run:
        shell: bash
        working-directory: ./serverless/users
    steps:
      - uses: actions/checkout@v3
      - name: Use Node.js 
        uses: actions/setup-node@v3
        with:
          node-version: 14.x
      - name: Cache
        uses: actions/cache@v3
        id: pr-cache-node_modules
        with:
          path: ./serverless/users/node_modules
          key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
      - name: Run specs
        run: yarn run test:cov:silent --ci
        env:
          CI: true
          NODE_OPTIONS: "--max-old-space-size=7340"
      - uses: codecov/codecov-action@v2
        with:
          name: CodeCov Check
          token: ${{ secrets.CODECOV_TOKEN }}
          fail_ci_if_error: true 
          flags: backend
          verbose: true
      - name: Run lint
        continue-on-error: true
        run: yarn lint:fix
