name: Continuous Integration

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  check:
    name: Code Check & Linting
    runs-on: ubuntu-latest
    env:
      NG_CLI_ANALYTICS: ci
      GO111MODULE: on
      GOPROXY: "https://proxy.golang.org"
      NODE_OPTIONS: "--max-old-space-size=8192"
      TERM: xterm
    steps:
      - name: Setup Environment
        uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Setup Go
        uses: actions/setup-go@v2
        with:
          go-version: 1.14.4

      - name: Setup NodeJS
        uses: actions/setup-node@v1.4.2
        with:
          node-version: 12.6.0

      - name: Cache Node Modules
        uses: actions/cache@v2
        env:
          cache-name: cache-node-modules
        with:
          # npm cache files are stored in `~/.npm` on Linux/macOS
          path: node_modules
          key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-build-${{ env.cache-name }}-
            ${{ runner.os }}-build-
            ${{ runner.os }}-

      - name: Install Dependencies
        shell: bash
        run: |
          [ ! -d "node_modules" ] && npm ci || node aio/scripts/version.js && command -v golangci-lint >/dev/null 2>&1 || { curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin v1.23.6; } && go mod download

      - name: Install Codegen
        shell: bash
        run: |
          export GOPATH=$(go env GOPATH)/bin
          aio/scripts/install-codegen.sh

      - name: Check Code
        shell: bash
        run: |
          export GOPATH=$(go env GOPATH)/bin
          npm run check

  unit:
    name: Unit Tests
    runs-on: ubuntu-latest
    env:
      NG_CLI_ANALYTICS: ci
      GO111MODULE: on
      GOPROXY: "https://proxy.golang.org"
      NODE_OPTIONS: "--max-old-space-size=8192"
      TERM: xterm

    steps:
      - name: Setup Environment
        uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Setup Go
        uses: actions/setup-go@v2
        with:
          go-version: 1.14.4

      - name: Setup NodeJS
        uses: actions/setup-node@v1.4.2
        with:
          node-version: 12.6.0

      - name: Cache Node Modules
        uses: actions/cache@v2
        env:
          cache-name: cache-node-modules
        with:
          path: node_modules
          key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-build-${{ env.cache-name }}-
            ${{ runner.os }}-build-
            ${{ runner.os }}-

      - name: Install Dependencies
        shell: bash
        run: |
          [ ! -d "node_modules" ] && npm ci || node aio/scripts/version.js && command -v golangci-lint >/dev/null 2>&1 || { curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin v1.23.6; } && go mod download

      - name: Run Frontend Unit Tests
        uses: GabrielBB/xvfb-action@v1.2
        with:
          run: npm run test:frontend:coverage
          
      - name: Run Backend Unit Tests
        run: |
          export GOPATH=$(go env GOPATH)/bin
          npm run test:backend:coverage

      - name: Cleanup & Upload Coverage
        shell: bash
        run: |
          rm -rf .tmp
          bash <(curl -s https://codecov.io/bash)

  e2e:
    name: E2E Tests
    runs-on: ubuntu-latest
    env:
      NG_CLI_ANALYTICS: ci
      GO111MODULE: on
      GOPROXY: "https://proxy.golang.org"
      NODE_OPTIONS: "--max-old-space-size=8192"
      TERM: xterm

    steps:
      - name: Setup Environment
        uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Setup Go
        uses: actions/setup-go@v2
        with:
          go-version: 1.14.4

      - name: Setup NodeJS
        uses: actions/setup-node@v1.4.2
        with:
          node-version: 12.6.0

      - name: Cache Node Modules
        uses: actions/cache@v2
        env:
          cache-name: cache-node-modules
        with:
          path: node_modules
          key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-build-${{ env.cache-name }}-
            ${{ runner.os }}-build-
            ${{ runner.os }}-

      - name: Install Dependencies
        shell: bash
        run: |
          [ ! -d "node_modules" ] && npm ci || node aio/scripts/version.js && command -v golangci-lint >/dev/null 2>&1 || { curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin v1.23.6; } && go mod download && npm install cypress

      - name: Run E2E Tests
        run: npm run cluster:start && npm run e2e
