# This workflow will build PRs submitted to the master branch.

name: PR Builder

on:
  pull_request:
    types: [ labeled ]
    branches: [ master ]
    paths-ignore:
      - "**.md"
      - "LICENSE"
  workflow_dispatch:

env:
  IDENTITY_APPS_SOURCE: ./target/identity-apps
  GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

jobs:
  lint:
    name: ⬣ ESLint (STATIC ANALYSIS)
    if: ${{github.event.label.name == 'trigger-pr-builder' || github.event_name == 'workflow_dispatch'}}
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [ lts/* ]
        maven-version: [ 3.6.3 ]
        java-version: [ 1.8 ]
    steps:
      - name: ⬇️ Checkout
        id: checkout
        uses: actions/checkout@v2.3.3

      - name: 🟢 Setup node
        id: setup-node
        uses: actions/setup-node@v2
        with:
          node-version: ${{ matrix.node-version }}

      - name: ☕ Set up JDK 1.8
        id: jdk-setup
        uses: actions/setup-java@v1
        with:
          java-version: ${{ matrix.java-version }}

      - name: 🦩 Set up Maven
        uses: stCarolas/setup-maven@v4
        id: mvn-setup
        with:
          maven-version: ${{ matrix.maven-version }}

      - name: 🐜 Ant Job
        id: run-ant-job
        run: |
          mvn antrun:run@build-pack
          mvn antrun:run@init-extensions

      - name: 🥡 Setup pnpm
        id: setup-pnpm
        uses: pnpm/action-setup@v2.1.0
        with:
          version: latest
          run_install: false

      - name: 🎈 Get pnpm store directory
        id: get-pnpm-cache-dir
        run: |
          echo "::set-output name=pnpm_cache_dir::$(pnpm store path)"

      - name: 🔆 Cache pnpm modules
        uses: actions/cache@v3
        id: pnpm-cache
        with:
          path: ${{ steps.get-pnpm-cache-dir.outputs.pnpm_cache_dir }}
          key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-store-

      - name: 🧩 Install Dependencies
        id: install-dependencies
        working-directory: ${{ env.IDENTITY_APPS_SOURCE }}
        run:  |
          pnpm install --no-frozen-lockfile
          pnpm --filter @wso2is/console --filter @wso2is/myaccount exec node scripts/pre-install.js

      - name: 🦄 Lint Changed Files
        id: lint-changed-with-eslint
        working-directory: ${{ env.IDENTITY_APPS_SOURCE }}/.github/workflows
        run: |
            bash ./scripts/lint-changed.sh ${{ github.event.pull_request.number }}

      - name: ⏳ Lint All Files
        id: lint-with-eslint
        working-directory: ${{ env.IDENTITY_APPS_SOURCE }}
        run: "pnpm nx run-many --target=lint:targeted --all --parallel --rule 'header/header: 0' ."

  typecheck:
    name: ʦ Typecheck (STATIC ANALYSIS)
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [ lts/* ]
        maven-version: [ 3.6.3 ]
        java-version: [ 1.8 ]
    steps:
      - name: ⬇️ Checkout
        id: checkout
        uses: actions/checkout@v2.3.3

      - name: 🟢 Setup node
        id: setup-node
        uses: actions/setup-node@v2
        with:
          node-version: ${{ matrix.node-version }}

      - name: ☕ Set up JDK 1.8
        id: jdk-setup
        uses: actions/setup-java@v1
        with:
          java-version: ${{ matrix.java-version }}

      - name: 🦩 Set up Maven
        uses: stCarolas/setup-maven@v4
        id: mvn-setup
        with:
          maven-version: ${{ matrix.maven-version }}

      - name: 🐜 Ant Job
        id: run-ant-job
        run: |
          mvn antrun:run@build-pack
          mvn antrun:run@init-extensions

      - name: 🥡 Setup pnpm
        id: setup-pnpm
        uses: pnpm/action-setup@v2.1.0
        with:
          version: latest
          run_install: false

      - name: 🎈 Get pnpm store directory
        id: get-pnpm-cache-dir
        run: |
          echo "::set-output name=pnpm_cache_dir::$(pnpm store path)"

      - name: 🔆 Cache pnpm modules
        uses: actions/cache@v3
        id: pnpm-cache
        with:
          path: ${{ steps.get-pnpm-cache-dir.outputs.pnpm_cache_dir }}
          key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-store-

      - name: 🧩 Install Dependencies
        id: install-dependencies
        working-directory: ${{ env.IDENTITY_APPS_SOURCE }}
        run:  |
          pnpm install --no-frozen-lockfile
          pnpm --filter @wso2is/console --filter @wso2is/myaccount exec node scripts/pre-install.js

      - name: 👷 Build Re-usable Modules
        id: build-reusable-modules
        working-directory: ${{ env.IDENTITY_APPS_SOURCE }}
        run:  pnpm build:modules

      - name: ☄️ Check Type Errors
        working-directory: ${{ env.IDENTITY_APPS_SOURCE }}
        run: pnpm typecheck

  test:
    name: 👾 Unit Test (TESTING)
    #needs: [ lint, typecheck ]
    if: ${{ false }} # disable temporarily
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [ lts/* ]
        maven-version: [ 3.6.3 ]
        java-version: [ 1.8 ]
    steps:
      - name: ⬇️ Checkout
        id: checkout
        uses: actions/checkout@v2.3.3

      - name: 🟢 Setup node
        id: setup-node
        uses: actions/setup-node@v2
        with:
          node-version: ${{ matrix.node-version }}

      - name: ☕ Set up JDK 1.8
        id: jdk-setup
        uses: actions/setup-java@v1
        with:
          java-version: ${{ matrix.java-version }}

      - name: 🦩 Set up Maven
        uses: stCarolas/setup-maven@v4
        id: mvn-setup
        with:
          maven-version: ${{ matrix.maven-version }}

      - name: 🐜 Ant Job
        id: run-ant-job
        run: |
          mvn antrun:run@build-pack
          mvn antrun:run@init-extensions

      - name: 🥡 Setup pnpm
        id: setup-pnpm
        uses: pnpm/action-setup@v2.1.0
        with:
          version: latest
          run_install: false

      - name: 🎈 Get pnpm store directory
        id: get-pnpm-cache-dir
        run: |
          echo "::set-output name=pnpm_cache_dir::$(pnpm store path)"

      - name: 🔆 Cache pnpm modules
        uses: actions/cache@v3
        id: pnpm-cache
        with:
          path: ${{ steps.get-pnpm-cache-dir.outputs.pnpm_cache_dir }}
          key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-store-

      - name: 🧩 Install Dependencies
        id: install-dependencies
        working-directory: ${{ env.IDENTITY_APPS_SOURCE }}
        run:  |
          pnpm install --no-frozen-lockfile
          pnpm --filter @wso2is/console --filter @wso2is/myaccount exec node scripts/pre-install.js

      - name: 👷 Build Re-usable Modules
        id: build-reusable-modules
        working-directory: ${{ env.IDENTITY_APPS_SOURCE }}
        run:  pnpm build:modules

      - name: 🃏 Run Jest & Collect Coverage
        id: run-jest-test-and-coverage
        working-directory: ${{ env.IDENTITY_APPS_SOURCE }}
        run: pnpm test:unit:coverage

      - name: 🤖 Aggregate Test Coverage
        id: aggregate-coverage-reports
        working-directory: ${{ env.IDENTITY_APPS_SOURCE }}
        run: |
          pnpm test:unit:coverage:aggregate
          pnpm nyc:text-summary-report
          pnpm nyc:text-report

  build:
    name: 🚧 Build
    #needs: [ lint, typecheck, test ]
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [ lts/* ]
        maven-version: [ 3.6.3 ]
        java-version: [ 1.8 ]
    steps:
      - name: ⬇️ Checkout
        id: checkout
        uses: actions/checkout@v2.3.3

      - name: 🟢 Setup node
        id: setup-node
        uses: actions/setup-node@v2
        with:
          node-version: ${{ matrix.node-version }}

      - name: ☕ Set up JDK 1.8
        id: jdk-setup
        uses: actions/setup-java@v1
        with:
          java-version: ${{ matrix.java-version }}

      - name: 🦩 Set up Maven
        uses: stCarolas/setup-maven@v4
        id: mvn-setup
        with:
          maven-version: ${{ matrix.maven-version }}

      - name: 🥡 Setup pnpm
        uses: pnpm/action-setup@v2.1.0
        with:
          version: latest
          run_install: false

      - name: 🎈 Get pnpm store directory
        id: get-pnpm-cache-dir
        run: |
          echo "::set-output name=pnpm_cache_dir::$(pnpm store path)"

      - name: 🔆 Cache pnpm modules
        uses: actions/cache@v3
        id: pnpm-cache
        with:
          path: ${{ steps.get-pnpm-cache-dir.outputs.pnpm_cache_dir }}
          key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-store-

      - name: 🏗️ Maven Build
        id: build-with-maven
        run: mvn clean install -U -Dlint.exec.skip=true

      - name: 💾 Cache local Maven repository
        id: cache-maven-m2
        uses: actions/cache@v2
        with:
          path: ~/.m2/repository
          key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
          restore-keys: |
            ${{ runner.os }}-maven-
