name: Build and test

on:
  push:
    branches:
      - master

jobs:
  build-and-test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 1
      - uses: actions/setup-node@v2
        with:
          node-version: 18
      - name: Build and run all tests
        run: |
          npm ci
          npm run build
          npm run bundlesize
          npm run test:unit:coverage
          npm run test:e2e
        env:
          CI: true
          CI_REPO_NAME: ${{ github.event.repository.name }}
          CI_REPO_OWNER: ${{ github.event.organization.login }}
          CI_COMMIT_SHA: ${{ github.sha }}
          GIT_COMMIT: ${{ github.sha }}
          CI_BRANCH: ${{ github.head_ref }}
          BUNDLESIZE_GITHUB_TOKEN: ${{secrets.BUNDLESIZE_GITHUB_TOKEN}}
          FORCE_COLOR: 2
          HUSKY_SKIP_INSTALL: true
      ##
      ## Disabling for now. There does not appear to be a secure way to do this
      ## with protected branches. See discussion:
      ## https://github.community/t/how-to-push-to-protected-branches-in-a-github-action/16101
      ##
      # - name: Commit built files
      #   run: |
      #     git config --local user.email "action@github.com"
      #     git config --local user.name "GitHub Action"
      #     git commit -m "Update build files 🏗" -a || echo "No changes to commit" && exit 0
      # - name: Push changes
      #   uses: ad-m/github-push-action@master
      #   with:
      #     github_token: ${{ secrets.GITHUB_TOKEN }}

      - name: Upload coverage to Codecov
        run: bash <(curl -s https://codecov.io/bash)
          -f ./coverage/lcov.info
          -B ${{ github.head_ref }}
          -C ${{ github.sha }}
          -Z || echo 'Codecov upload failed'
        env:
          CI: true
          GITLAB_CI: true # pretend we are GitLab CI, while Codecov adding support for Github Actions
          CODECOV_ENV: github-action
          CODECOV_TOKEN: ${{secrets.CODECOV_TOKEN}}
