name: Registry RHBQ Build # job to build apicurio registry using Red Hat Build of Quarkus so the issues are detected earlier
on:
  workflow_dispatch:
    inputs:
      branch:
        description: 'Branch to checkout'
        required: true
        default: 'main'
      maven-profiles:
        description: 'Comma separated list of profiles to be activated'
        required: true
        default: 'prod,kafkasql,sql'
      skip-tests:
        description: 'Flag to run or skip tests'
        required: true
        default: 'false'
      script-url:
        description: 'Raw URL of the groovy manipulation script'
        required: true
        default: 'https://gist.githubusercontent.com/riprasad/a16a71a3062021739f025b763f95bab3/raw'
      extra-params:
        description: 'Extra Maven Build Parameters'
        required: false
        default: '-Dquarkus.package.type=legacy'

jobs:
  build:
    name: Build Project
    runs-on: ubuntu-22.04
    if: github.repository_owner == 'Apicurio'
    steps:

        - name: Set up JDK 17
          uses: actions/setup-java@v3
          with:
            java-version: '17'
            distribution: 'temurin'

        - name: Workaround jackson-coreutils
          run: |
            # upstream issue: https://github.com/java-json-tools/jackson-coreutils/issues/59
            rm -rf ~/.m2/repository/com/github/java-json-tools
            mkdir -p /tmp/coreutils-workaround
            ( cd /tmp/coreutils-workaround && mvn dependency:get -DremoteRepositories=https://repo1.maven.org/maven2 -Dartifact=com.github.java-json-tools:jackson-coreutils:2.0 )

        - name: Configure Red Hat Repository
          run: |
           pwd
           mkdir -p /home/runner/.m2
           chmod 755 /home/runner/.m2
           echo "<settings><profiles><profile><id>public-repositories</id><repositories><repository><id>central</id><name>Maven Central</name><url>https://repo1.maven.org/maven2</url><releases><enabled>true</enabled></releases><snapshots><enabled>false</enabled></snapshots></repository></repositories><pluginRepositories><pluginRepository><id>central</id><name>Maven Central</name><url>https://repo1.maven.org/maven2</url><releases><enabled>true</enabled></releases><snapshots><enabled>false</enabled></snapshots></pluginRepository></pluginRepositories></profile><profile><id>redhat-repository</id><repositories><repository><id>redhat-repository</id><name>Red Hat repository</name><url>http://maven.repository.redhat.com/ga/</url><layout>default</layout><releases><enabled>true</enabled><updatePolicy>never</updatePolicy></releases><snapshots><enabled>false</enabled><updatePolicy>never</updatePolicy></snapshots></repository></repositories><pluginRepositories><pluginRepository><id>redhat-repository</id><name>Red Hat repository</name><url>http://maven.repository.redhat.com/ga/</url><layout>default</layout><releases><enabled>true</enabled><updatePolicy>never</updatePolicy></releases><snapshots><enabled>false</enabled><updatePolicy>never</updatePolicy></snapshots></pluginRepository></pluginRepositories></profile></profiles><activeProfiles><activeProfile>public-repositories</activeProfile><activeProfile>redhat-repository</activeProfile></activeProfiles></settings>" > /home/runner/.m2/settings.xml

        - name: Apicurio Registry Checkout
          run: |
           mkdir registry
           cd registry
           git init
           git remote add origin "https://github.com/Apicurio/apicurio-registry.git"
           git fetch
           git checkout ${{ github.event.inputs.branch }}
           git branch --set-upstream-to=origin/${{ github.event.inputs.branch }}
           git pull

        - name: Download PME
          run: mvn dependency:get -Dartifact=org.commonjava.maven.ext:pom-manipulation-cli:LATEST:jar -Ddest=/home/runner/.m2/pom-manipulation-cli.jar

        - name: Run PME
          run: cd registry && java -jar /home/runner/.m2/pom-manipulation-cli.jar -DgroovyScripts=${{ github.event.inputs.script-url }}

        - name: Build Registry
          run: pwd && cd registry && ./mvnw clean install -P${{ github.event.inputs.maven-profiles }} -DskipTests=${{ github.event.inputs.skip-tests }} ${{ github.event.inputs.extra-params }}

        - name: Set test profile to all
          run: echo "test_profile=all" >> $GITHUB_ENV

        - name: Prepare Tenant Manager
          run: make build-integration-tests-multitenancy

        - name: Build integration-tests-common
          if: github.event.inputs.skip-tests == 'false'
          run: cd registry && ./mvnw install -Pintegration-tests -pl integration-tests/integration-tests-common

        - name: Run Integration Tests - sql
          if: github.event.inputs.skip-tests == 'false'
          run: cd registry && ./mvnw verify -Pintegration-tests -P${{ env.test_profile }} -Psql -pl integration-tests/testsuite -Dmaven.javadoc.skip=true --no-transfer-progress

        - name: Run Integration Tests - kafkasql
          if: github.event.inputs.skip-tests == 'false'
          run: cd registry && ./mvnw verify -Pintegration-tests -P${{ env.test_profile }} -Pkafkasql -pl integration-tests/testsuite -Dmaven.javadoc.skip=true --no-transfer-progress

        - name: Run Legacy Tests - sql
          if: github.event.inputs.skip-tests == 'false'
          run: cd registry && ./mvnw verify -Pintegration-tests -P${{ env.test_profile }} -Psql -pl integration-tests/legacy-tests -Dmaven.javadoc.skip=true --no-transfer-progress

        - name: Collect logs
          if: failure()
          run: ./.github/scripts/collect_logs.sh

        - name: Upload tests logs artifacts
          if: failure()
          uses: actions/upload-artifact@v4.0.0
          with:
            name: tests-logs
            path: artifacts
