name: AutoGPT Platform - Frontend CI

on:
  push:
    branches: [master, dev]
    paths:
      - ".github/workflows/platform-frontend-ci.yml"
      - "autogpt_platform/frontend/**"
      - "autogpt_platform/backend/Dockerfile"
      - "autogpt_platform/docker-compose.yml"
      - "autogpt_platform/docker-compose.platform.yml"
  pull_request:
    paths:
      - ".github/workflows/platform-frontend-ci.yml"
      - "autogpt_platform/frontend/**"
      - "autogpt_platform/backend/Dockerfile"
      - "autogpt_platform/docker-compose.yml"
      - "autogpt_platform/docker-compose.platform.yml"
  merge_group:
  workflow_dispatch:

concurrency:
  group: ${{ github.workflow }}-${{ github.event_name == 'merge_group' && format('merge-queue-{0}', github.ref) || format('{0}-{1}', github.ref, github.event.pull_request.number || github.sha) }}
  cancel-in-progress: ${{ github.event_name == 'pull_request' }}

defaults:
  run:
    shell: bash
    working-directory: autogpt_platform/frontend

jobs:
  setup:
    runs-on: ubuntu-latest
    outputs:
      components-changed: ${{ steps.filter.outputs.components }}

    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Check for component changes
        uses: dorny/paths-filter@v3
        id: filter
        with:
          filters: |
            components:
              - 'autogpt_platform/frontend/src/components/**'

      - name: Set up pnpm
        uses: pnpm/action-setup@v4
        with:
          package_json_file: autogpt_platform/frontend/package.json

      - name: Set up Node
        uses: actions/setup-node@v6
        with:
          node-version: "22.18.0"
          cache: "pnpm"
          cache-dependency-path: autogpt_platform/frontend/pnpm-lock.yaml

      - name: Install dependencies to populate cache
        run: pnpm install --frozen-lockfile

  lint:
    runs-on: ubuntu-latest
    needs: setup

    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Set up pnpm
        uses: pnpm/action-setup@v4
        with:
          package_json_file: autogpt_platform/frontend/package.json

      - name: Set up Node
        uses: actions/setup-node@v6
        with:
          node-version: "22.18.0"
          cache: "pnpm"
          cache-dependency-path: autogpt_platform/frontend/pnpm-lock.yaml

      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Run lint
        run: pnpm lint

  chromatic:
    runs-on: ubuntu-latest
    needs: setup
    # Disabled: to re-enable, remove 'false &&' from the condition below
    if: >-
      false
      && (github.ref == 'refs/heads/dev' || github.base_ref == 'dev')
      && needs.setup.outputs.components-changed == 'true'

    steps:
      - name: Checkout repository
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Set up pnpm
        uses: pnpm/action-setup@v4
        with:
          package_json_file: autogpt_platform/frontend/package.json

      - name: Set up Node
        uses: actions/setup-node@v6
        with:
          node-version: "22.18.0"
          cache: "pnpm"
          cache-dependency-path: autogpt_platform/frontend/pnpm-lock.yaml

      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Run Chromatic
        uses: chromaui/action@latest
        with:
          projectToken: chpt_9e7c1a76478c9c8
          onlyChanged: true
          workingDir: autogpt_platform/frontend
          token: ${{ secrets.GITHUB_TOKEN }}
          exitOnceUploaded: true

  integration_test:
    runs-on: ubuntu-latest
    needs: setup

    steps:
      - name: Checkout repository
        uses: actions/checkout@v6
        with:
          submodules: recursive

      - name: Set up pnpm
        uses: pnpm/action-setup@v4
        with:
          package_json_file: autogpt_platform/frontend/package.json

      - name: Set up Node
        uses: actions/setup-node@v6
        with:
          node-version: "22.18.0"
          cache: "pnpm"
          cache-dependency-path: autogpt_platform/frontend/pnpm-lock.yaml

      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Generate API client
        run: pnpm generate:api

      - name: Run Integration Tests
        run: pnpm test:unit

      - name: Upload coverage reports to Codecov
        if: ${{ !cancelled() }}
        uses: codecov/codecov-action@v5
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          flags: platform-frontend
          files: ./autogpt_platform/frontend/coverage/cobertura-coverage.xml
