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@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
        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@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
        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
