name: on-pull-request
on: pull_request


env:
  NX_BRANCH: ${{ github.event.number }}
  NX_RUN_GROUP: ${{ github.run_id }}
  NX_CLOUD_AUTH_TOKEN: ${{ secrets.NX_CLOUD_AUTH_TOKEN }}
  MOZ_HEALESS: 1
  SAUCE_USERNAME_PR: valorkinpr
  SAUCE_ACCESS_KEY_PR: e0a97bd3-4b74-4408-89bf-cce1b44a8bf1
  CYPRESS_CACHE_FOLDER: 'node_modules/.cypress'
  CYPRESS_RECORD_KEY: 4aa7a1c0-3a4f-444e-b324-6fc305a543a8

jobs:
  # one run
  one_run:
    runs-on: ubuntu-latest
    steps:
      - name: Cancel Previous Runs
        uses: styfle/cancel-workflow-action@0.9.0
        with:
          access_token: ${{ secrets.GITHUB_TOKEN }}

  # install dependencies
  install:
    runs-on: ubuntu-latest
    needs: one_run
    steps:
      - uses: actions/checkout@v2.3.4
      - uses: actions/cache@v2.1.4
        id: cache
        with:
          path: |
            ~/.npm
            node_modules
            !node_modules/ngx-bootstrap
          key: node_modules-${{ hashFiles('**/package-lock.json') }}
      - run: npm ci
        if: steps.cache.outputs.cache-hit != 'true'

  # build ngx-bootstrap
  build:
    needs: install
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2.3.4
      - uses: actions/cache@v2.1.4
        with:
          path: |
            ~/.npm
            node_modules
            !node_modules/ngx-bootstrap
          key: node_modules-${{ hashFiles('**/package-lock.json') }}
      - uses: actions/cache@v2.1.4
        with:
          path: |
            dist
            node_modules/ngx-bootstrap
          key: dist-${{ github.run_id }}
      - run: npx ng build --runner cloud --prod --with-deps

  # run unit tests
  unit_tests_with_coverage:
    runs-on: ubuntu-latest
    needs: install
    steps:
      - uses: actions/checkout@v2.3.4
      - uses: actions/cache@v2.1.4
        with:
          path: |
            ~/.npm
            node_modules
            !node_modules/ngx-bootstrap
          key: node_modules-${{ hashFiles('**/package-lock.json') }}
      - run: npm test -- --runner cloud --codeCoverage
      - run: npx codecov ./coverage/
        continue-on-error: true

  # run linting
  linting:
    runs-on: ubuntu-latest
    needs: install
    steps:
      - uses: actions/checkout@v2.3.4
      - uses: actions/cache@v2.1.4
        with:
          path: |
            ~/.npm
            node_modules
            !node_modules/ngx-bootstrap
          key: node_modules-${{ hashFiles('**/package-lock.json') }}
      - run: npm run lint -- --runner cloud

  # firebase deploy preview
  firebase_preview:
    runs-on: ubuntu-latest
    needs: build
    outputs:
      output_url: ${{ steps.firebase_hosting_preview.outputs.details_url }}
    steps:
      - uses: actions/checkout@v2.3.4
      - uses: actions/cache@v2.1.4
        with:
          path: |
            dist
            node_modules/ngx-bootstrap
          key: dist-${{ github.run_id }}
      - uses: FirebaseExtended/action-hosting-deploy@v0
        continue-on-error: true
        id: firebase_hosting_preview
        with:
          repoToken: '${{ secrets.GITHUB_TOKEN }}'
          firebaseServiceAccount: '${{ secrets.FIREBASE_SERVICE_ACCOUNT_NGX_BOOTSTRAP_DEMO }}'
          projectId: ngx-bootstrap-demo
          expires: 7d
        env:
          FIREBASE_CLI_PREVIEWS: hostingchannels

  # run cypress
  e2e_smoke:
    name: Cypress run
    runs-on: ubuntu-latest
    needs: [build, firebase_preview]

    strategy:
      # when one test fails, DO NOT cancel the other
      # containers, because this will kill Cypress processes
      # leaving the Dashboard hanging ...
      # https://github.com/cypress-io/github-action/issues/48
      fail-fast: false
      matrix:
        # run 3 copies of the current job in parallel
        containers: [1, 2, 3, 4, 5]
    steps:
      - uses: actions/checkout@v2.3.4
      - uses: actions/cache@v2.1.4
        with:
          path: |
            ~/.npm
            node_modules
            !node_modules/ngx-bootstrap
          key: node_modules-${{ hashFiles('**/package-lock.json') }}

      # because of "record" and "parallel" parameters
      # these containers will load balance all found tests among themselves
      - name: smoke e2e on firebase
        if: ${{ needs.firebase_preview.outputs.output_url }}
        run: npx nx run ngx-bootstrap-docs-e2e:e2e -c firebase --runner cloud --record --group smoke --baseUrl=${{ needs.firebase_preview.outputs.output_url }}/ngx-bootstrap/ -- --parallel

      - name: smoke e2e local
        if: ${{ !needs.firebase_preview.outputs.output_url }}
        run: npx nx run ngx-bootstrap-docs-e2e:e2e --prod --runner cloud --record --group smoke -- --parallel

################

  # run migrate to latest
  latest_install:
    runs-on: ubuntu-latest
    needs: one_run
    steps:
      - uses: actions/checkout@v2.3.4
      - uses: actions/cache@v2.1.4
        id: cache
        with:
          path: |
            ~/.npm
            node_modules
            !node_modules/ngx-bootstrap
          key: latest-node_modules-${{ hashFiles('**/package-lock.json') }}
      - run: npm ci
        if: steps.cache.outputs.cache-hit != 'true'

  # build with latest
  latest_build:
    runs-on: ubuntu-latest
    needs: latest_install
    steps:
      - uses: actions/checkout@v2.3.4
      - uses: actions/cache@v2.1.4
        with:
          path: |
            ~/.npm
            node_modules
            !node_modules/ngx-bootstrap
          key: latest-node_modules-${{ hashFiles('**/package-lock.json') }}
      - run: npx nx migrate --run-migrations
      - run: npx nx build --prod --with-deps

  # run unit tests on latest
  latest_unit_tests_with_coverage:
    runs-on: ubuntu-latest
    needs: latest_install
    steps:
      - uses: actions/checkout@v2.3.4
      - uses: actions/cache@v2.1.4
        with:
          path: |
            ~/.npm
            node_modules
            !node_modules/ngx-bootstrap
          key: latest-node_modules-${{ hashFiles('**/package-lock.json') }}
      - run: npx nx migrate --run-migrations
      - run: npm test

  # run linting on latest
  latest_linting:
    runs-on: ubuntu-latest
    needs: latest_install
    steps:
      - uses: actions/checkout@v2.3.4
      - uses: actions/cache@v2.1.4
        with:
          path: |
            ~/.npm
            node_modules
            !node_modules/ngx-bootstrap
          key: latest-node_modules-${{ hashFiles('**/package-lock.json') }}
      - run: npx nx migrate --run-migrations
      - run: npm run lint
