name: Check code before submitting

permissions: read-all

on:
  merge_group:
  push:
    branches:
      - main
  pull_request:

jobs:
  check-format:
    name: '[Required] Check correct format'
    runs-on: ubuntu-latest

    steps:
      - name: Check out repository
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          fetch-depth: 2

      - name: Set up Node.js
        uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
        with:
          cache: npm
          node-version-file: '.nvmrc'

      - name: Install dependencies
        run: npm ci

      - name: Run format check
        run: npm run check-format

  check-docs:
    name: '[Required] Check docs updated'
    runs-on: ubuntu-latest

    steps:
      - name: Check out repository
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          fetch-depth: 2

      - name: Set up Node.js
        uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
        with:
          cache: npm
          node-version-file: '.nvmrc'

      - name: Install dependencies
        run: npm ci

      - name: Generate
        run: npm run gen

      - name: Check if autogenerated code and docs are out of date
        run: |
          diff_file=$(mktemp doc_diff_XXXXXX)
          git diff --color > $diff_file
          if [[ -s $diff_file ]]; then
            echo "Please update the documentation by running 'npm run gen'. The following was the diff"
            cat $diff_file
            rm $diff_file
            exit 1
          fi
          rm $diff_file
