name: changelog-filename-gate

on:
  workflow_dispatch:
  pull_request:
    branches:
      - main
    types:
      - opened
      - reopened
      - synchronize
      - edited
  push:
    branches:
      - main

jobs:
  validate:
    name: validate
    runs-on: ubuntu-latest
    steps:
      # Keep the gate self-contained: the test environment cannot reliably clone GitHub Actions repositories.
      - name: Checkout full history
        run: |
          git init -q .
          git remote add origin "${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git"
          git fetch --no-tags --prune origin \
            "+refs/heads/*:refs/remotes/origin/*" \
            "+refs/pull/*/head:refs/remotes/pull/*/head" \
            "+refs/pull/*/merge:refs/remotes/pull/*/merge"
          git checkout --detach "$GITHUB_SHA"

      - name: Verify Node.js runtime
        run: |
          node --version
          npm --version
          node -e "if (Number(process.versions.node.split('.')[0]) < 20) process.exit(1)"

      - name: Run regression tests
        run: npm test

      - name: Validate new changelog filenames
        run: npm run check:filenames -- --event "$GITHUB_EVENT_PATH"

      - name: Validate changelog frontmatter
        run: npm run check:frontmatter -- --event "$GITHUB_EVENT_PATH"

      - name: Validate changelog path aliases
        run: npm run check:path-aliases
