---
name: "Lint and Test"
on:
  push:
    branches:
      - "main"
  pull_request:
    branches:
      - "*"
jobs:
  paths-filter:
    runs-on: "depot-ubuntu-24.04-small"
    outputs:
      codechange: "${{ steps.code-filter.outputs.codechange }}"
    steps:
      - uses: "actions/checkout@v5"
      - uses: "dorny/paths-filter@v3"
        id: "code-filter"
        with:
          filters: |
            codechange:
              - src/**
              - .github/workflows/authzed-node.yaml
              - package.json
              - js-dist/package.json
              - buf.gen.yaml
  test:
    name: Lint and Test
    runs-on: "depot-ubuntu-24.04-small"
    strategy:
      matrix:
        node-version: [18, 20, 22]
    needs: "paths-filter"
    if: |
      needs.paths-filter.outputs.codechange == 'true'
    steps:
      - uses: actions/checkout@v5
      - uses: "authzed/action-spicedb@v1"
        with:
          version: "latest"
      - uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
          cache-dependency-path: ./package.json
          cache: "yarn"
      - uses: bahmutov/npm-install@v1
        with:
          useLockFile: false
      - name: Run lint
        run: "CI=true yarn lint"
      - name: Run prettier
        run: "CI=true yarn run prettier src -c"
      - name: Run Yarn tests
        run: "CI=true yarn only-run-tests"
  build-js-client:
    name: Build and Test JS client
    runs-on: "depot-ubuntu-24.04-small"
    strategy:
      matrix:
        node-version: [18, 20, 22]
    needs: "paths-filter"
    if: |
      needs.paths-filter.outputs.codechange == 'true'
    steps:
      - uses: actions/checkout@v5
      - uses: "authzed/action-spicedb@v1"
        with:
          version: "latest"
      - uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
          cache-dependency-path: ./package.json
          cache: "yarn"
      - uses: bahmutov/npm-install@v1
        with:
          useLockFile: false
      - name: Run build
        run: yarn build-js-client
        working-directory: ./
      - uses: bahmutov/npm-install@v1
        with:
          useLockFile: false
          working-directory: ./js-dist
      - name: Run tests
        run: CI=true yarn only-run-tests
        working-directory: ./js-dist
      - uses: actions/upload-artifact@v4
        with:
          name: js-client-${{ matrix.node-version }}
          path: |
            js-dist/**
            !js-dist/node_modules/**
