# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# Finally, it triggers the e2e tests workflow.
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: CI

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

jobs:
  build:
    runs-on: ubuntu-latest

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

    steps:
      - uses: actions/checkout@v2

      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v2
        with:
          node-version: ${{ matrix.node-version }}

      - name: Cache Yarn
        uses: actions/cache@v2
        with:
          path: ${{ github.workspace }}/.yarn
          key: ci-v1-${{ matrix.node-version }}-${{ hashFiles('yarn.lock') }}
          restore-keys: |
            ci-v1-${{ matrix.node-version }}-

      - name: Setup yarn
        run: yarn install --immutable --inline-builds

      - name: Run yarn build
        env:
          NODE_OPTIONS: "--max-old-space-size=4096"
        run: yarn build

      - name: Upload handler sizes to S3
        if: ${{ github.ref == 'refs/heads/master' && matrix.node-version == '14.x' }}
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_AT }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_ST }}
          AWS_DEFAULT_REGION: us-east-1
          GITHUB_SHA: ${{ github.sha }}
        run: yarn handlers:upload-handler-sizes
      #
      #      - run: yarn test
      #
      #      - run: yarn integration
      #
      #      - name: Upload code coverage
      #        if: ${{ matrix.node-version == '14.x' }}
      #        env:
      #          CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
      #        run: npx codecov

      - name: Calculate and post handler sizes
        if: (matrix.node-version == '14.x' && github.event_name == 'pull_request')
        env:
          GITHUB_NEW_SHA: ${{ github.event.pull_request.head.sha }}
          GITHUB_BASE_SHA: ${{ github.event.pull_request.base.sha }}
          PULL_REQUEST_ID: ${{ github.event.number }}
        run: yarn handlers:comment-handler-sizes
  start-e2e-tests:
    # Note: we only run e2e tests automatically if this is a push on master or if PR is not from a fork or renovate, as forks won't have access to secrets
    if: ${{ github.event_name == 'push' || (github.event_name == 'pull_request' && startsWith(github.event.pull_request.head.ref, 'renovate/') == false && github.event.pull_request.head.repo.full_name == github.repository) }}
    runs-on: ubuntu-latest

    steps:
      - name: Mark end-to-end tests as pending
        uses: Sibz/github-status-action@v1
        with:
          authToken: ${{ secrets.GITHUB_TOKEN }}
          context: "End-to-end Tests"
          description: "Waiting for end-to-end tests to pass"
          state: "pending"
          sha: ${{ github.event.pull_request.head.sha || github.sha }}

      - name: Trigger end-to-end tests workflow
        uses: benc-uk/workflow-dispatch@v1
        with:
          workflow: End-to-end Tests
          token: ${{ secrets.BOT_GITHUB_TOKEN }}
          ref: ${{ github.event.pull_request.head.ref || github.ref }}
          # FIXME: Passing sha doesn't seem to work, created GitHub ticket here: https://github.community/t/workflow-dispatch-cant-set-ref-to-commit-sha/138132
          # So for now we pass in SHA as an input
          # ref: ${{ github.event.pull_request.head.sha || github.sha }}
          inputs: '{"sha": "${{ github.event.pull_request.head.sha || github.sha }}"}'
