version: 2.1
orbs:
  # defined at https://github.com/snowdrop/circleci-orbs
  # needs to have been published before being used here
  openshift: snowdrop/openshift@0.4.0
jobs:
  openshift311:
    machine: true

    steps:
    - checkout

    - run:
        name: Check of commit is from Maven Release Plugin
        command: |
          commit_message=$(git log -1 --pretty=%B)
          if [[ ${commit_message} =~ ^\[maven-release-plugin\].* ]]; then
            echo "Commits from the maven-release-plugin don't trigger a regular CI build"
            circleci step halt
          fi


    # based on this: https://discuss.circleci.com/t/circle-2-0-caching-is-too-limited-to-be-very-useful/11694/10
    - restore_cache:
        keys:
          - spring-boot-bom-{{ .Branch }}-{{ checksum "pom.xml" }}
          - spring-boot-bom-{{ .Branch }}
          - spring-boot-bom

      # Will download all deps needed into .m2
    - run:
        name: Check all dependencies are available and store them locally
        command: |
          sed -i.bak -e "s/<dependencyManagement>//g" pom.xml
          sed -i -e "s/<\/dependencyManagement>//g" pom.xml
          mvn dependency:tree
          mv pom.xml.bak pom.xml

    - run:
        name: Download testsuite
        command: |
          pushd /tmp
          git clone --single-branch -b 2.1.x https://github.com/snowdrop/testsuite.git
          cd testsuite

          # Done in order to cache maven artifacts
          # Can't use 'mvn dependency:go-offline'. See https://stackoverflow.com/q/14694139/2504224
          mvn -s ~/project/.circleci/settings.xml install -DskipTests

          popd

    - run:
        name: Download http-booster
        command: |
          pushd /tmp
          git clone --single-branch -b sb-2.1.x https://github.com/snowdrop/spring-boot-http-booster.git
          cd spring-boot-http-booster

          # Done in order to cache maven artifacts
          ./mvnw -s ~/project/.circleci/settings.xml dependency:go-offline

          popd

    - save_cache:
        paths:
        - ~/.m2
        key: spring-boot-bom-{{ .Branch }}-{{ checksum "pom.xml" }}

    - run:
        name: Install BOM locally
        command: mvn install

    - openshift/launch_openshift:
        version: "3.11"

    - run:
        name: Test http booster
        command: |
          bom_version=$(mvn -s .circleci/settings.xml -q -Dexec.executable="echo" -Dexec.args='${project.version}' --non-recursive exec:exec)
          versionRE='^([1-9]\.[0-9]\.[0-9]+).*'
          if [[ "${bom_version}" =~ ${versionRE} ]]; then
            version_numbers_only=${BASH_REMATCH[1]}
            spring_boot_release="${version_numbers_only}.RELEASE"

            echo "Testing http-booster using"
            echo "spring-boot.bom.version=${bom_version}"
            echo "spring-boot.version=${spring_boot_release}"

            pushd /tmp/spring-boot-http-booster
            ./mvnw -s ~/project/.circleci/settings.xml clean verify -Popenshift,openshift-it -Dspring-boot.bom.version=${bom_version} -Dspring-boot.version=${spring_boot_release}
            popd

          else
            echo "The BOM version is invalid!"
            exit 1
          fi

    - run:
        name: Launch testsuite
        command: |
          bom_version=$(mvn -s .circleci/settings.xml -q -Dexec.executable="echo" -Dexec.args='${project.version}' --non-recursive exec:exec)
          versionRE='^([1-9]\.[0-9]\.[0-9]+).*'
          if [[ "${bom_version}" =~ ${versionRE} ]]; then
            version_numbers_only=${BASH_REMATCH[1]}
            spring_boot_release="${version_numbers_only}.RELEASE"

            echo "Launching testsuite using"
            echo "spring-boot.bom.version=${bom_version}"
            echo "spring-boot.version=${spring_boot_release}"

            pushd /tmp/testsuite
            oc policy add-role-to-user view -z default
            mvn -s ~/project/.circleci/settings.xml clean verify -Popenshift -Dspring-boot.bom.version=${bom_version} -Dspring-boot.version=${spring_boot_release}
            popd

          else
            echo "The BOM version is invalid!"
            exit 1
          fi

workflows:
  version: 2
  build_and_test:
    jobs:
      - openshift311:
        filters:
          branches:
            ignore:
            - master
