name: CI

on:
  push:
    branches: [main]
    tags: ['v*']
  pull_request:

env:
  JEST_JUNIT_OUTPUT_DIR: ./coverage/junit/

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5
      - name: Enable Corepack
        run: corepack enable
      - uses: actions/setup-node@v5
        with:
          node-version: 24.9.0
          cache: 'yarn'
      - name: Install dependencies
        run: yarn install --immutable
      - name: Build
        run: yarn run build
      - name: Upload workspace
        uses: actions/upload-artifact@v4
        with:
          name: workspace
          path: .
          retention-days: 1

  validate:
    runs-on: ubuntu-latest
    needs: build
    steps:
      - uses: actions/checkout@v5
      - name: Enable Corepack
        run: corepack enable
      - name: Download workspace
        uses: actions/download-artifact@v5
        with:
          name: workspace
          path: .
      - name: Install dependencies
        run: yarn install --immutable
      - name: Locale verify
        run: yarn run locale:verify
      - name: Lint
        run: yarn run lint
      - name: Run unit tests and publish to codeclimate
        env:
          JEST_JUNIT_OUTPUT_DIR: ./coverage/junit/
        run: yarn test:unit --coverage
      - uses: qltysh/qlty-action/coverage@v2
        with:
          token: ${{ secrets.QLTY_COVERAGE_TOKEN }}
          files: coverage/lcov.info
      - name: Store junit results
        uses: actions/upload-artifact@v4
        with:
          name: junit
          path: coverage/junit

  test-integration:
    runs-on: ubuntu-latest
    needs: build
    steps:
      - uses: actions/checkout@v5
      - name: Enable Corepack
        run: corepack enable
      - name: Download workspace
        uses: actions/download-artifact@v5
        with:
          name: workspace
          path: .
      - name: Install dependencies
        run: yarn install --immutable
      - name: Install Playwright Browsers
        run: npx playwright install --with-deps
      - name: Test component
        run: yarn run test:component
      - name: Test mashup
        run: yarn run test:mashup
      - name: Store mashup artifacts
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: mashup
          path: ./test/mashup
      - name: Test integration
        run: yarn run test:integration
      - name: Test rendering
        run: yarn run test:rendering
      - name: Store rendering artifacts
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: rendering
          path: ./test/rendering

  test-create:
    runs-on: ubuntu-latest
    needs: build
    steps:
      - uses: actions/checkout@v5
      - name: Enable Corepack
        run: corepack enable
      - name: Download workspace
        uses: actions/download-artifact@v5
        with:
          name: workspace
          path: .
      - name: Install dependencies
        run: yarn install --immutable
      - name: Install Playwright Chromium
        run: npx playwright install --with-deps chromium
      - run: chmod +x .github/scripts/nebula_create.sh
      - name: Create Nebula visualization project (Picasso)
        run: .github/scripts/nebula_create.sh generated/hello none false false true true
      - name: Create Nebula visualization project (Barchart)
        run: .github/scripts/nebula_create.sh generated/barchart barchart false false true true
      - name: Create Nebula mashup project
        run: .github/scripts/nebula_create.sh generated/hello-mashup none true true true false
      - name: Store barchart screenshots
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: barchart-screenshots
          path: generated/barchart/screenshots
      - name: Store mashup artifacts
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: mashup-artifacts
          path: test/mashup/__artifacts__

  api-governance:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: .
    steps:
      - name: Checkout repository
        uses: actions/checkout@v5
        with:
          fetch-depth: 0
      - name: Enable Corepack
        run: corepack enable
      - name: Set up Node.js
        uses: actions/setup-node@v5
        with:
          node-version: 22.20.0
          cache: 'yarn'

      - name: Install dependencies
        run: yarn install --immutable

      - name: Create version.txt
        id: version
        run: |
          set -x
          if [ "${GITHUB_REF_TYPE}" = "tag" ]; then
            version=${GITHUB_REF_NAME#v}
          else
            yarn workspace "@nebula.js/stardust" version prepatch
            version=$(node -p "require('./apis/stardust/api-spec/spec.json').info.version")
          fi
          echo "$version" > ./version.txt
          echo "version=$version" >> $GITHUB_OUTPUT
          echo "Building $version"

      - name: Prepare API Compliance
        run: |
          docker pull ghcr.io/qlik-download/api-compliance
          docker create -v /specs --name specs alpine:3.4 /bin/true
          docker cp ./apis/stardust/api-spec/spec.json specs:/specs
          docker cp ./apis/stardust/api-spec/listbox-spec.json specs:/specs

      - name: Run API Compliance
        env:
          CREDENTIALS_S3_SECRETKEY: ${{ secrets.APICULTURIST_S3 }}
          CREDENTIALS_GITHUB: ${{ secrets.APICULTURIST_GITHUB }}
          CREDENTIALS_COLONY: ${{ secrets.APICULTURIST_TOKEN }}
          COMMIT_SHA: ${{ github.sha }}
          VER: v${{ steps.version.outputs.version }}
        run: |
          docker run --volumes-from specs \
            -e SPEC_PATHS="abdc47c2-921a-45d3-ba5c-80d448574c5b@/specs/spec.json,ba0bcf91-d7a6-4048-a36a-ad9a2dc8c03e@/specs/listbox-spec.json" \
            -e COMMIT_SHA="$COMMIT_SHA" \
            -e RELEASE_TAG="$VER" \
            -e PRE_RELEASE="true" \
            -e CREDENTIALS_S3_SECRETKEY="$CREDENTIALS_S3_SECRETKEY" \
            -e CREDENTIALS_GITHUB="$CREDENTIALS_GITHUB" \
            -e CREDENTIALS_COLONY="$CREDENTIALS_COLONY" \
            ghcr.io/qlik-download/api-compliance
