name: Library CI

on:
  # Uncomment the schedule block to enable scheduled runs (e.g., weekly)
  #schedule:
   #- cron: '0 2 * * 0'

  # Automatically triggered when pushing to these branches.
  # THIS FLOW HAS ONLY BEEN TESTED FOR PUSH EVENTS TO THESE BRANCHES, OTHERS MAY BEHAVE INCORRECTLY
  push:
    branches: [ release/v3-infinity, master, main, 'patch/**', 'minor/**' ]
  # Automatically triggered when creating, re-opening, or synchronizing PRS
  # that target these branches.
  # THIS FLOW HAS ONLY BEEN TESTED FOR PR EVENTS THAT TARGET THESE BRANCHES, OTHERS MAY BEHAVE INCORRECTLY
  pull_request:
    types: [ opened, reopened, synchronize ]
    branches: [ release/v3-infinity, master, main, 'patch/**', 'minor/**' ]
  # Manually triggered via the GitHub UI (can be used against any branch of your repository)
  workflow_dispatch:
    inputs:
      gradle_log_level:
        description: 'Gradle log level'
        required: false
        default: lifecycle
        type: choice
        options:
          - lifecycle
          - info
          - debug
          - warn
          - error

# Required permissions to execute the reusable workflow
permissions:
  contents: write
  issues: read
  checks: write
  pull-requests: write
  id-token: write

jobs:
  # !!!                          IMPORTANT NOTE                                           !!!
  # THIS JOB'S NAME MUST BE "ci" OTHERWISE THE ORG LEVEL RULESETS WILL NOT
  # PROPERLY APPLY STATUS CHECK TO PULL REQUESTS
  ci:
    runs-on: 'runner-1-core-3gi'

    env:
      DATE_STRING: ${{ github.run_id }}${{ github.run_number }}
      ARTIFACTORY_USR: ${{ secrets.ARTIFACTORY_USR }}
      ARTIFACTORY_PSW: ${{ secrets.ARTIFACTORY_PWD }}
      BITBUCKET_USR: ${{ secrets.BITBUCKET_USR }}
      BITBUCKET_PSW: ${{ secrets.BITBUCKET_PSW }}

    steps:

      # Checkout
      - name: Checkout Repo
        uses: actions/checkout@v4

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: npm

      # ---------- Init Stage ----------
      - name: Install Dependencies
        run: |
          apt-get update
          apt-get install -y jq lsof libasound2t64 libatk1.0-0 libc6 libcairo2 \
            libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgdk-pixbuf2.0-0 \
            libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 \
            libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 \
            libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator3-1 \
            libappindicator3-1 libnss3 lsb-release libgbm1 xdg-utils wget curl gnupg

      - name: Configure Artifactory npm
        run: |
          curl -u$ARTIFACTORY_USR:$ARTIFACTORY_PSW \
            https://bin.pega.io/artifactory/api/npm/auth \
            | sed -E "s/_auth = /\/\/bin.pega.io\/artifactory\/api\/npm\/npmjs-dev\/:_auth=/g" \
            > ~/.npmrc

          npm config set registry https://bin.pega.io/artifactory/api/npm/npmjs-dev/

      - name: Install Project Dependencies
        run: npm install

      # ---------- Unit Test + Coverage ----------
      - name: Run Unit Tests with Coverage
        run: npm run coverage

      # ---------- Publish Test Report ----------
      - name: Upload Test Results
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: unit-test-results
          path: test-results/test/TEST-results-unit.xml