name: Pull Request Check

on: pull_request

jobs:
  check:
    runs-on: ubuntu-latest

    permissions:
      contents: read
      pull-requests: write
      checks: write

    env:
      BROWSERSLIST_IGNORE_OLD_DATA: true

    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Use Node.js 18
        uses: actions/setup-node@v3
        with:
          node-version: 18.x

      - name: pnpm cache
        uses: actions/cache@v3
        with:
          path: ./common/temp/pnpm-store
          key: ${{ runner.os }}-pnpm-${{ hashFiles('common/config/rush/pnpm-lock.yaml') }}
          restore-keys: ${{ runner.os }}-pnpm-

      - name: Install
        run: node common/scripts/install-run-rush.js install

      - name: Check dependencies
        run: node common/scripts/install-run-rush.js check

      - name: Verify change files
        run: node common/scripts/install-run-rush.js change --verify

      - name: Rush Build
        run: node common/scripts/install-run-rush.js rebuild --verbose

      - name: Install API Extractor
        run: npm install -g @microsoft/api-extractor@7.18.4

      - name: Check for API changes to @snowplow/browser-tracker
        working-directory: ./trackers/browser-tracker
        run: api-extractor run

      - name: Check for API changes to @snowplow/node-tracker
        working-directory: ./trackers/node-tracker
        run: api-extractor run

      - name: Check for API changes to @snowplow/react-native-tracker
        working-directory: ./trackers/react-native-tracker
        run: api-extractor run

      - name: Check bundle size using bundlemon
        if: github.event.pull_request.head.repo.full_name == github.repository
        run: npx bundlemon@3
        env:
          BUNDLEMON_PROJECT_ID: 69e5e44cf67d5035af9fd1c0
          BUNDLEMON_PROJECT_APIKEY: ${{ secrets.BUNDLEMON_PROJECT_APIKEY }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          CI_COMMIT_SHA: ${{ github.event.pull_request.head.sha || github.sha }}
