name: Check

on:
  pull_request:
  push:
    branches:
      - main

permissions:
  contents: read

jobs:
  check:
    name: npm run check
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
        with:
          fetch-depth: 0
          persist-credentials: false

      - name: Detect compatibility changes
        id: compatibility
        env:
          BASE_SHA: ${{ github.event_name == 'pull_request' && github.event.pull_request.base.sha || github.event.before }}
          HEAD_SHA: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }}
        run: |
          if git diff --quiet "$BASE_SHA" "$HEAD_SHA" -- \
            .github/workflows/check.yml \
            README.md \
            manifest.json \
            package.json \
            package-lock.json \
            scripts/api-baseline.mjs \
            scripts/app-server-compatibility.mjs \
            scripts/generate-app-server-types.mjs \
            src/app-server/connection/compatibility.json \
            src/generated/app-server \
            versions.json; then
            echo "required=false" >> "$GITHUB_OUTPUT"
          else
            echo "required=true" >> "$GITHUB_OUTPUT"
          fi

      - name: Setup Node.js
        uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6
        with:
          node-version: 26
          cache: npm

      - name: Install dependencies
        run: npm ci --ignore-scripts

      - name: Install recorded Codex CLI
        if: steps.compatibility.outputs.required == 'true'
        run: npm install --global "@openai/codex@$(node scripts/app-server-compatibility.mjs --tested-cli-version)"

      - name: Check API baselines
        if: steps.compatibility.outputs.required == 'true'
        run: npm run api:baseline

      - name: Check generated app-server bindings
        if: steps.compatibility.outputs.required == 'true'
        run: npm run generate:app-server-types:check

      - name: Check pull request commit messages
        if: github.event_name == 'pull_request'
        env:
          BASE_SHA: ${{ github.event.pull_request.base.sha }}
          HEAD_SHA: ${{ github.event.pull_request.head.sha }}
        run: npm run commitlint -- --from "$BASE_SHA" --to "$HEAD_SHA" --verbose

      - name: Check pushed commit messages
        if: github.event_name == 'push'
        env:
          BASE_SHA: ${{ github.event.before }}
          HEAD_SHA: ${{ github.sha }}
        run: npm run commitlint -- --from "$BASE_SHA" --to "$HEAD_SHA" --verbose

      - name: Check release metadata
        run: npm run release:check

      - name: Check
        run: npm run check
