version: 2.1
orbs:
  android: circleci/android@0.2.1
  sonarcloud: sonarsource/sonarcloud@1.0.1
commands:
  install-ndk: android/install-ndk
  restore-build-cache: android/restore-build-cache
  save-build-cache: android/save-build-cache
  scan-sonar: sonarcloud/scan
jobs:
  quick_build_release_no_tests:
    executor: android/android
    resource_class: large
    steps:
      - checkout
      - run:
          name: Check if assemble required
          command: |
            last_commit=$(git log -1 --pretty=%B)
            if [[ $last_commit != *"Version bump"* ]]; then
              circleci-agent step halt
              echo "Skipping job"
            fi
      - install-ndk:
          ndk-sha: "c81a5bcb4672a18d3647bf6898cd4dbcb978d0e8"
          ndk-version: "android-ndk-r21c"
      - restore-build-cache
      - restore_cache:
          key: jars-{{ checksum "build.gradle" }}-{{ checksum  "Corona-Warn-App/build.gradle" }}-{{ checksum  "Server-Protocol-Buffer/build.gradle" }}
      - run:
          name: Quick Build
          command: ./gradlew assembleDeviceRelease
          environment:
            JVM_OPTS: -Xmx2048m
            GRADLE_OPTS: -Xmx1536m -XX:+HeapDumpOnOutOfMemoryError -Dorg.gradle.caching=true -Dorg.gradle.configureondemand=true -Dkotlin.compiler.execution.strategy=in-process -Dkotlin.incremental=false
      - save-build-cache
      - save_cache:
          paths:
            - ~/.gradle
          key: jars-{{ checksum "build.gradle" }}-{{ checksum  "Corona-Warn-App/build.gradle" }}-{{ checksum  "Server-Protocol-Buffer/build.gradle" }}
      - store_artifacts:
          path: Corona-Warn-App/build/reports
          destination: reports
  quick_build_for_testers_no_tests:
    executor: android/android
    resource_class: large
    steps:
      - checkout
      - run:
          name: Check if assemble required
          command: |
            last_commit=$(git log -1 --pretty=%B)
            if [[ $last_commit != *"Version bump"* ]]; then
              circleci-agent step halt
              echo "Skipping job"
            fi
      - install-ndk:
          ndk-sha: "c81a5bcb4672a18d3647bf6898cd4dbcb978d0e8"
          ndk-version: "android-ndk-r21c"
      - restore-build-cache
      - restore_cache:
          key: jars-{{ checksum "build.gradle" }}-{{ checksum  "Corona-Warn-App/build.gradle" }}-{{ checksum  "Server-Protocol-Buffer/build.gradle" }}
      - run:
          name: Quick Build
          command: ./gradlew :Corona-Warn-App:assembleDeviceForTestersRelease
          environment:
            JVM_OPTS: -Xmx2048m
            GRADLE_OPTS: -Xmx1536m -XX:+HeapDumpOnOutOfMemoryError -Dorg.gradle.caching=true -Dorg.gradle.configureondemand=true -Dkotlin.compiler.execution.strategy=in-process -Dkotlin.incremental=false
      - save-build-cache
      - save_cache:
          paths:
            - ~/.gradle
          key: jars-{{ checksum "build.gradle" }}-{{ checksum  "Corona-Warn-App/build.gradle" }}-{{ checksum  "Server-Protocol-Buffer/build.gradle" }}
      - store_artifacts:
          path: Corona-Warn-App/build/reports
          destination: reports
  device_release_unit_tests:
    executor: android/android
    resource_class: large
    parallelism: 4
    steps:
      - checkout
      - restore-build-cache
      - restore_cache:
          key: jars-{{ checksum "build.gradle" }}-{{ checksum  "Corona-Warn-App/build.gradle" }}-{{ checksum  "Server-Protocol-Buffer/build.gradle" }}
      - run:
          name: test splitting output
          command: circleci tests glob "**/test*/**/*.kt" | circleci tests split | xargs -n 1 echo
      - run:
          name: Unit tests
          command: ./gradlew :Corona-Warn-App:testDeviceReleaseUnitTest -i -PtestFilter="`circleci tests glob "**/test*/**/*.kt" | circleci tests split`"
          environment:
            JVM_OPTS: -Xmx2048m
            GRADLE_OPTS: -Xmx1536m -XX:+HeapDumpOnOutOfMemoryError -Dorg.gradle.caching=true -Dorg.gradle.configureondemand=true -Dkotlin.compiler.execution.strategy=in-process -Dkotlin.incremental=false
      - save-build-cache
      - save_cache:
          paths:
            - ~/.gradle
          key: jars-{{ checksum "build.gradle" }}-{{ checksum  "Corona-Warn-App/build.gradle" }}-{{ checksum  "Server-Protocol-Buffer/build.gradle" }}
      - run:
          name: JaCoCo report
          command: ./gradlew :Corona-Warn-App:jacocoTestReportDeviceRelease
          environment:
            JVM_OPTS: -Xmx2048m
            GRADLE_OPTS: -Xmx1536m -XX:+HeapDumpOnOutOfMemoryError -Dorg.gradle.caching=true -Dorg.gradle.configureondemand=true -Dkotlin.compiler.execution.strategy=in-process -Dkotlin.incremental=false
      - store_artifacts:
          path: Corona-Warn-App/build/reports
          destination: reports
      - store_test_results:
          path: Corona-Warn-App/build/test-results
      - run:
          name: Skip SonarCloud for external Pull Requests
          command: '[[ -v CIRCLE_PR_REPONAME ]] && circleci-agent step halt || true'
      - scan-sonar
  device_for_testers_unit_tests:
    executor: android/android
    resource_class: large
    parallelism: 4
    steps:
      - checkout
      - restore-build-cache
      - restore_cache:
          key: jars-{{ checksum "build.gradle" }}-{{ checksum  "Corona-Warn-App/build.gradle" }}-{{ checksum  "Server-Protocol-Buffer/build.gradle" }}
      - run:
          name: test splitting output
          command: circleci tests glob "**/test*/**/*.kt" | circleci tests split | xargs -n 1 echo
      - run:
          name: Unit tests
          command: ./gradlew :Corona-Warn-App:testDeviceForTestersReleaseUnitTest -i -PtestFilter="`circleci tests glob "**/test*/**/*.kt" | circleci tests split`"
          environment:
            JVM_OPTS: -Xmx2048m
            GRADLE_OPTS: -Xmx1536m -XX:+HeapDumpOnOutOfMemoryError -Dorg.gradle.caching=true -Dorg.gradle.configureondemand=true -Dkotlin.compiler.execution.strategy=in-process -Dkotlin.incremental=false
      - save-build-cache
      - save_cache:
          paths:
            - ~/.gradle
          key: jars-{{ checksum "build.gradle" }}-{{ checksum  "Corona-Warn-App/build.gradle" }}-{{ checksum  "Server-Protocol-Buffer/build.gradle" }}
      - store_artifacts:
          path: Corona-Warn-App/build/reports
          destination: reports
      - store_test_results:
          path: Corona-Warn-App/build/test-results
  lint_device_release_check:
    executor: android/android
    resource_class: large
    steps:
      - checkout
      - run:
          name: Lint check device release
          command: ./gradlew --profile :Corona-Warn-App:lintDeviceRelease
          environment:
            JVM_OPTS: -Xmx2048m
            GRADLE_OPTS: -Xmx1536m -XX:+HeapDumpOnOutOfMemoryError -Dorg.gradle.caching=true -Dorg.gradle.configureondemand=true -Dkotlin.compiler.execution.strategy=in-process -Dkotlin.incremental=false
      - store_artifacts:
          path: Corona-Warn-App/build/reports
          destination: reports
  ktlint_device_release_check:
    executor: android/android
    resource_class: medium
    steps:
      - checkout
      - run:
          name: Ktlint check device release
          command: ./gradlew :Corona-Warn-App:ktlintDeviceReleaseCheck
          environment:
            JVM_OPTS: -Xmx2048m
            GRADLE_OPTS: -Xmx1536m -XX:+HeapDumpOnOutOfMemoryError -Dorg.gradle.caching=true -Dorg.gradle.configureondemand=true -Dkotlin.compiler.execution.strategy=in-process -Dkotlin.incremental=false
      - store_artifacts:
          path: Corona-Warn-App/build/reports
          destination: reports
  lint_device_for_testers_check:
    executor: android/android
    resource_class: large
    steps:
      - checkout
      - run:
          name: Lint check device for testers
          command: ./gradlew --profile :Corona-Warn-App:lintDeviceForTestersRelease
          environment:
            JVM_OPTS: -Xmx2048m
            GRADLE_OPTS: -Xmx1536m -XX:+HeapDumpOnOutOfMemoryError -Dorg.gradle.caching=true -Dorg.gradle.configureondemand=true -Dkotlin.compiler.execution.strategy=in-process -Dkotlin.incremental=false
      - store_artifacts:
          path: Corona-Warn-App/build/reports
          destination: reports
  ktlint_device_for_testers_check:
    executor: android/android
    resource_class: medium
    steps:
      - checkout
      - run:
          name: Ktlint check device for testers
          command: ./gradlew :Corona-Warn-App:ktlintDeviceForTestersReleaseCheck
          environment:
            JVM_OPTS: -Xmx2048m
            GRADLE_OPTS: -Xmx1536m -XX:+HeapDumpOnOutOfMemoryError -Dorg.gradle.caching=true -Dorg.gradle.configureondemand=true -Dkotlin.compiler.execution.strategy=in-process -Dkotlin.incremental=false
      - store_artifacts:
          path: Corona-Warn-App/build/reports
          destination: reports
  detekt:
    executor: android/android
    resource_class: medium
    steps:
      - checkout
      - run:
          name: Detekt check
          command: ./gradlew :Corona-Warn-App:detekt
          environment:
            JVM_OPTS: -Xmx2048m
            GRADLE_OPTS: -Xmx1536m -XX:+HeapDumpOnOutOfMemoryError -Dorg.gradle.caching=true -Dorg.gradle.configureondemand=true -Dkotlin.compiler.execution.strategy=in-process -Dkotlin.incremental=false
      - store_artifacts:
          path: Corona-Warn-App/build/reports
          destination: reports
workflows:
  version: 2
  quick_build:
    jobs:
      - quick_build_release_no_tests
      - quick_build_for_testers_no_tests
      - device_release_unit_tests
      - device_for_testers_unit_tests
      - lint_device_release_check
      - lint_device_for_testers_check
      - ktlint_device_release_check
      - ktlint_device_for_testers_check
      - detekt
