name: Pull Request

on:
  pull_request:
    types: [opened, synchronize, reopened]

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

permissions: {}

defaults:
  run:
    shell: bash

jobs:
  analyze:
    runs-on: ubuntu-latest
    outputs:
      snapshots: ${{ steps.filter.outputs.snapshots }}
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false
      - uses: dorny/paths-filter@d1c1ffe0248fe513906c8e24db8ea791d46f8590 # v3.0.3
        id: filter
        with:
          filters: |
            snapshots:
              - 'tests/e2e/ng-snapshot/package.json'

  lint:
    runs-on: ubuntu-latest
    steps:
      - name: Initialize environment
        uses: angular/dev-infra/github-actions/npm/checkout-and-setup-node@55af1f65dbeb0fe6a3de1d1134bd0dd8bc5e60cc
      - name: Setup Bazel
        uses: angular/dev-infra/github-actions/bazel/setup@55af1f65dbeb0fe6a3de1d1134bd0dd8bc5e60cc
      - name: Setup ESLint Caching
        uses: actions/cache@668228422ae6a00e4ad889ee87cd7109ec5666a7 # v5.0.4
        with:
          path: .eslintcache
          key: ${{ runner.os }}-${{ hashFiles('.eslintrc.json') }}
      - name: Install node modules
        run: pnpm install --frozen-lockfile
      - name: Generate JSON schema types
        # Schema types are required to correctly lint the TypeScript code
        run: pnpm run build-schema
      - name: Run ESLint
        run: pnpm lint --cache-strategy content
      - name: Validate NgBot Configuration
        run: pnpm ng-dev ngbot verify
      - name: Validate Circular Dependencies
        run: pnpm ts-circular-deps check
      - name: Run Validation
        run: pnpm admin validate
      - name: Check tooling setup
        run: pnpm check-tooling-setup
      - name: Check commit message
        # Commit message validation is only done on pull requests as its too late to validate once
        # it has been merged.
        run: pnpm ng-dev commit-message validate-range ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }}
      - name: Check code format
        # Code formatting checks are only done on pull requests as its too late to validate once
        # it has been merged.
        run: pnpm ng-dev format changed --check ${{ github.event.pull_request.base.sha }}
      - name: Check Package Licenses
        uses: angular/dev-infra/github-actions/linting/licenses@55af1f65dbeb0fe6a3de1d1134bd0dd8bc5e60cc

  build:
    runs-on: ubuntu-latest
    steps:
      - name: Initialize environment
        uses: angular/dev-infra/github-actions/npm/checkout-and-setup-node@55af1f65dbeb0fe6a3de1d1134bd0dd8bc5e60cc
      - name: Setup Bazel
        uses: angular/dev-infra/github-actions/bazel/setup@55af1f65dbeb0fe6a3de1d1134bd0dd8bc5e60cc
      - name: Setup Bazel RBE
        uses: angular/dev-infra/github-actions/bazel/configure-remote@55af1f65dbeb0fe6a3de1d1134bd0dd8bc5e60cc
      - name: Install node modules
        run: pnpm install --frozen-lockfile
      - name: Build release targets
        run: pnpm ng-dev release build
      - name: Store PR release packages
        uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
        with:
          name: packages
          path: dist/releases/*.tgz
          retention-days: 14

  test:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Initialize environment
        uses: angular/dev-infra/github-actions/npm/checkout-and-setup-node@55af1f65dbeb0fe6a3de1d1134bd0dd8bc5e60cc
      - name: Setup Bazel
        uses: angular/dev-infra/github-actions/bazel/setup@55af1f65dbeb0fe6a3de1d1134bd0dd8bc5e60cc
      - name: Setup Bazel RBE
        uses: angular/dev-infra/github-actions/bazel/configure-remote@55af1f65dbeb0fe6a3de1d1134bd0dd8bc5e60cc
      - name: Install node modules
        run: pnpm install --frozen-lockfile
      - name: Run module and package tests
        run: pnpm bazel test -- //... -//tests/...

  e2e:
    needs: build
    strategy:
      fail-fast: false
      matrix:
        node: [22]
        subset: [esbuild, webpack]
        shard: [0, 1, 2, 3, 4, 5]
    runs-on: ubuntu-latest
    steps:
      - name: Initialize environment
        uses: angular/dev-infra/github-actions/npm/checkout-and-setup-node@55af1f65dbeb0fe6a3de1d1134bd0dd8bc5e60cc
      - name: Install node modules
        run: pnpm install --frozen-lockfile
      - name: Setup Bazel
        uses: angular/dev-infra/github-actions/bazel/setup@55af1f65dbeb0fe6a3de1d1134bd0dd8bc5e60cc
      - name: Setup Bazel RBE
        uses: angular/dev-infra/github-actions/bazel/configure-remote@55af1f65dbeb0fe6a3de1d1134bd0dd8bc5e60cc
      - name: Run CLI E2E tests
        run: pnpm bazel test --test_env=E2E_SHARD_TOTAL=6 --test_env=E2E_SHARD_INDEX=${{ matrix.shard }} --config=e2e //tests:e2e.${{ matrix.subset }}_node${{ matrix.node }}

  build-e2e-windows-subset:
    runs-on: ubuntu-latest
    steps:
      - name: Initialize environment
        uses: angular/dev-infra/github-actions/npm/checkout-and-setup-node@55af1f65dbeb0fe6a3de1d1134bd0dd8bc5e60cc
      - name: Setup Bazel
        uses: angular/dev-infra/github-actions/bazel/setup@55af1f65dbeb0fe6a3de1d1134bd0dd8bc5e60cc
      - name: Setup Bazel RBE
        uses: angular/dev-infra/github-actions/bazel/configure-remote@55af1f65dbeb0fe6a3de1d1134bd0dd8bc5e60cc
      - name: Install node modules
        run: pnpm install --frozen-lockfile
      - name: Build E2E tests for Windows on Linux
        run: |
          pnpm bazel build \
            --config=e2e \
            //tests:e2e.esbuild_node24 \
            --platforms=tools:windows_x64
      - name: Store built Windows E2E tests
        uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
        with:
          name: win-e2e-build-artifacts
          path: |
            dist/bin/tests/**
            !**/node_modules/**
          retention-days: 1
          if-no-files-found: 'error'

  e2e-windows-subset:
    needs: build-e2e-windows-subset
    runs-on: windows-2025
    steps:
      - name: Initialize environment
        uses: angular/dev-infra/github-actions/npm/checkout-and-setup-node@55af1f65dbeb0fe6a3de1d1134bd0dd8bc5e60cc
      - name: Install node modules
        run: pnpm install --frozen-lockfile
      - name: Download built Windows E2E tests
        uses: actions/download-artifact@37930b1c2abaa49bbe596cd826c3c89aef350131 # v7.0.0
        with:
          name: win-e2e-build-artifacts
          path: dist/bin/tests/
      - name: Run CLI E2E tests
        uses: ./.github/shared-actions/windows-bazel-test
        with:
          test_target_name: e2e.esbuild_node24
        env:
          E2E_SHARD_TOTAL: 1
          TESTBRIDGE_TEST_ONLY: tests/basic/{build,rebuild,serve}.ts

  e2e-package-managers:
    needs: build
    strategy:
      fail-fast: false
      matrix:
        node: [22]
        subset: [yarn, pnpm, bun]
        shard: [0, 1, 2]
    runs-on: ubuntu-latest
    steps:
      - name: Initialize environment
        uses: angular/dev-infra/github-actions/npm/checkout-and-setup-node@55af1f65dbeb0fe6a3de1d1134bd0dd8bc5e60cc
      - name: Install node modules
        run: pnpm install --frozen-lockfile
      - name: Setup Bazel
        uses: angular/dev-infra/github-actions/bazel/setup@55af1f65dbeb0fe6a3de1d1134bd0dd8bc5e60cc
      - name: Setup Bazel RBE
        uses: angular/dev-infra/github-actions/bazel/configure-remote@55af1f65dbeb0fe6a3de1d1134bd0dd8bc5e60cc
      - name: Run CLI E2E tests
        run: pnpm bazel test --test_env=E2E_SHARD_TOTAL=3 --test_env=E2E_SHARD_INDEX=${{ matrix.shard }} --config=e2e //tests:e2e.${{ matrix.subset }}_node${{ matrix.node }}

  e2e-snapshots:
    needs: [analyze, build]
    if: needs.analyze.outputs.snapshots == 'true'
    strategy:
      fail-fast: false
      matrix:
        node: [22]
        subset: [esbuild, webpack]
        shard: [0, 1, 2, 3, 4, 5]
    runs-on: ubuntu-latest
    steps:
      - name: Initialize environment
        uses: angular/dev-infra/github-actions/npm/checkout-and-setup-node@55af1f65dbeb0fe6a3de1d1134bd0dd8bc5e60cc
      - name: Install node modules
        run: pnpm install --frozen-lockfile
      - name: Setup Bazel
        uses: angular/dev-infra/github-actions/bazel/setup@55af1f65dbeb0fe6a3de1d1134bd0dd8bc5e60cc
      - name: Setup Bazel RBE
        uses: angular/dev-infra/github-actions/bazel/configure-remote@55af1f65dbeb0fe6a3de1d1134bd0dd8bc5e60cc
      - name: Run CLI E2E tests
        run: pnpm bazel test --test_env=E2E_SHARD_TOTAL=6 --test_env=E2E_SHARD_INDEX=${{ matrix.shard }} --config=e2e //tests:e2e.snapshots.${{ matrix.subset }}_node${{ matrix.node }}
