name: Integration Test Runner
on:
  workflow_dispatch:
    inputs:
      pr:
        description: "Enter PR link (ex: https://github.com/wso2-extensions/identity-inbound-auth-oauth/pull/1481)"
        default:
        required: true
      jdk:
        description: "Enter Java version (ex: 8,11)"
        default: 11
        required: true

env:
  MAVEN_OPTS: -Dhttp.keepAlive=false -Dmaven.wagon.http.pool=false -Dmaven.wagon.http.retryHandler.class=standard -Dmaven.wagon.http.retryHandler.count=3 -Djdk.util.zip.disableZip64ExtraFieldValidation=true -XX:+HeapDumpOnOutOfMemoryError

jobs:
  run-test-runners:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        include:
          - runner_id: 1
            tests: "is-tests-default-configuration,is-test-rest-api,is-test-webhooks,is-tests-scim2,is-test-adaptive-authentication"
          - runner_id: 2
            tests: "is-test-adaptive-authentication-nashorn,is-test-adaptive-authentication-nashorn-with-restart,is-tests-default-configuration-ldap,is-tests-uuid-user-store,is-tests-federation,is-tests-federation-restart"
          - runner_id: 3
            tests: "is-tests-jdbc-userstore,is-tests-read-only-userstore,is-tests-oauth-jwt-token-gen-enabled,is-tests-email-username,is-tests-saml-query-profile,is-tests-default-encryption"
          - runner_id: 4
            tests: "is-tests-with-individual-configuration-changes"
    env:
      JAVA_TOOL_OPTIONS: "-Djdk.util.zip.disableZip64ExtraFieldValidation=true -Djdk.nio.zipfs.allowDotZipEntry=true"
    steps:
      - name: Print Input
        run: echo "Running the Test runner ${{ matrix.runner_id }} for PR - ${{ github.event.inputs.pr }}"
      - name: Comment build info
        if: ${{ matrix.runner_id == 1 }}
        run: |
          owner=$(echo '${{github.event.inputs.pr}}' | cut -d "/" -f 4)
          repo=$(echo '${{github.event.inputs.pr}}' | cut -d "/" -f 5)
          pr_number=$(echo '${{github.event.inputs.pr}}' | cut -d "/" -f 7)
          curl -X POST https://api.github.com/repos/$owner/$repo/issues/$pr_number/comments -H 'Authorization: token ${{secrets.PR_BUILDER_COMMENT}}' -d '{"body":"PR builder started \nLink: https://github.com/wso2/product-is/actions/runs/${{github.run_id}}"}'
      - uses: actions/checkout@v4
      - name: Set up Adopt JDK 8
        uses: actions/setup-java@v4
        with:
          java-version: "8"
          distribution: "adopt"
      - name: Echo java 8 home
        run: |
          echo "J8HOME=$JAVA_HOME" >> ${GITHUB_ENV}
        id: java-8-setup
      - name: Set up Adopt JDK 11
        uses: actions/setup-java@v4
        with:
          java-version: "11"
          distribution: "adopt"
      - name: Echo java 11 home
        run: |
          echo "J11HOME=$JAVA_HOME" >> ${GITHUB_ENV}
      - name: Build init
        run: |
          echo "CURRENT_MONTH=$(date +%Y-%m)" >> ${GITHUB_ENV}
      - name: Cache maven packages
        id: cache-maven-m2
        uses: actions/cache@v4
        with:
          path: |
            ~/.m2
            !~/.m2/repository/org/wso2/is/wso2is
          key: ${{ runner.os }}-pr-builder-${{ env.CURRENT_MONTH }}-${{ matrix.runner_id }}
      - name: Setup pnpm
        id: setup-pnpm
        uses: pnpm/action-setup@v4
        with:
          version: latest
          run_install: false
      - name: Run PR builder ${{ matrix.runner_id }}
        id: builder_step
        env:
          PR_LINK: ${{github.event.inputs.pr}}
          JDK_VERSION: ${{github.event.inputs.jdk}}
          JAVA_8_HOME: ${{env.J8HOME}}
          JAVA_11_HOME: ${{env.J11HOME}}
        run: |
          bash .github/scripts/pr-builder.sh ${{ matrix.runner_id }} "${{ matrix.tests }}"
      - name: Archive PR diff file
        if: ${{ matrix.runner_id == 1 }}
        uses: actions/upload-artifact@v4
        with:
          name: repo-pr-diff
          path: |
            ${{steps.builder_step.outputs.REPO_NAME}}/diff.diff
          if-no-files-found: warn
      - name: Archive repo mvn build log
        if: ${{ matrix.runner_id == 1 && steps.builder_step.outputs.REPO_NAME != 'product-is' }}
        uses: actions/upload-artifact@v4
        with:
          name: repo-mvn-build-log
          path: |
            ${{steps.builder_step.outputs.REPO_NAME}}/mvn-build.log
          if-no-files-found: warn
      - name: Archive product-is mvn build log
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: product-is-mvn-build-log-${{ matrix.runner_id }}
          path: |
            product-is-${{ matrix.runner_id }}/mvn-build.log
          if-no-files-found: warn
      - name: Archive product-is surefire reports
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: product-is-surefire-report-${{ matrix.runner_id }}
          path: |
            product-is-${{ matrix.runner_id }}/**/surefire-reports
          if-no-files-found: warn
      - name: Archive heap dump
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: heap-dump
          path: |
            **/**.hprof
          if-no-files-found: ignore
  comment-and-approve:
    name: Comment the status and Approve PR
    needs: run-test-runners
    runs-on: ubuntu-latest
    if: always()
    steps:
      - name: Comment build status
        run: |
          owner=$(echo '${{github.event.inputs.pr}}' | cut -d "/" -f 4)
          repo=$(echo '${{github.event.inputs.pr}}' | cut -d "/" -f 5)
          pr_number=$(echo '${{github.event.inputs.pr}}' | cut -d "/" -f 7)
          curl -X POST https://api.github.com/repos/$owner/$repo/issues/$pr_number/comments -H 'Authorization: token ${{secrets.PR_BUILDER_COMMENT}}' -d '{"body":"PR builder completed \nLink: https://github.com/wso2/product-is/actions/runs/${{github.run_id}} \nStatus: ${{ needs.run-test-runners.result }}"}'
      - name: Approve PR
        if: ${{ needs.run-test-runners.result == 'success' }}
        run: |
          owner=$(echo '${{github.event.inputs.pr}}' | cut -d "/" -f 4)
          repo=$(echo '${{github.event.inputs.pr}}' | cut -d "/" -f 5)
          pr_number=$(echo '${{github.event.inputs.pr}}' | cut -d "/" -f 7)
          curl -X POST https://api.github.com/repos/$owner/$repo/pulls/$pr_number/reviews -H 'Authorization:token ${{secrets.PR_BUILDER_COMMENT}}' -d '{"body":"Approving the pull request based on the successful pr build https://github.com/wso2/product-is/actions/runs/${{github.run_id}}","event":"APPROVE"}'
