# Java Gradle CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-java/ for more details
#

defaults: &defaults
  working_directory: /tmp

version: 2.1

# https://discuss.circleci.com/t/convenience-images-update-add-our-convenience-image-tooling-to-your-own-docker-images-with-orbs/30604
# https://circleci.com/orbs/registry/?filterBy=all
orbs:
  build-tools: circleci/build-tools@3.0.0
  jq: circleci/jq@3.0.0
  docker: circleci/docker@2.6.0 # needs jq above
  node: circleci/node@5.3.0

jobs:
  download_from_code_climate:
    <<: *defaults
    machine: true
    working_directory: ~/repo
    steps:
      - run:
          name: Download cc-test-reporter
          command: |
            mkdir -p tmp/
            curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./tmp/cc-test-reporter
            chmod +x ./tmp/cc-test-reporter
            #./tmp/cc-test-reporter before-build
      - persist_to_workspace:
          root: tmp
          paths:
            - cc-test-reporter

  build_test_java:
    <<: *defaults
    docker:
      # specify the version you desire here
      # https://circleci.com/docs/2.0/docker-image-tags.json
      # circleci/openjdk:11-jdk-node-browsers
      # https://circleci.com/docs/2.0/circleci-images/#openjdk
      # https://hub.docker.com/r/circleci/openjdk/
      # adoptopenjdk/openjdk15
      # https://circleci.com/developer/images/image/cimg/openjdk
      - image: cimg/openjdk:21.0.2
        environment:
          CC_TEST_REPORTER_ID: $CC_TEST_REPORTER_ID
          COVERALLS_REPO_TOKEN: $COVERALLS_REPO_TOKEN
          CODACY_PROJECT_TOKEN: $CODACY_PROJECT_TOKEN
          FOSSA_API_KEY: $FOSSA_API_KEY

    working_directory: ~/repo

    environment:
      # Customize the JVM maximum heap limit
      JVM_OPTS: -Xmx3200m
      TERM: dumb

    steps:
      - checkout
      - run: java --version
      - build-tools/install-ci-tools
      - jq/install
      #- docker/install-docker-tools
      - docker/install-docker-compose:
          version: '1.29.2'
      - node/install

      - attach_workspace:
          at: ~/repo/tmp

      # Download and cache dependencies
      - restore_cache:
          keys:
            - v1-gradle-dependencies-{{ checksum "build.gradle" }}-{{ arch }}
            # fallback to using the latest cache if no exact match is found
            - v1-gradle-dependencies-{{ arch }}

      - run:
          name: Upgrade Git
          command: |
            make git-upgrade-ubuntu

      - run:
          name: Get Git Submodules
          command: |
            make git

      - run: bash ./gradlew dependencies

      - save_cache:
          paths:
            - ~/.gradle
          key: v1-gradle-dependencies-{{ checksum "build.gradle" }}-{{ arch }}

      # run tests!
      - run: bash ./gradlew test jacocoJupTestReport

      # upload test coverage to codecov
      - run: bash <(curl -s https://codecov.io/bash) #-cF java

      # run test coverage to code climate // https://github.com/codeclimate/test-reporter/issues/243
      - run:
          name: Run CodeClimate
          command: |
            cd ./src/
            ../tmp/cc-test-reporter format-coverage --add-prefix src/ --prefix /home/circleci/repo/src -t jacoco ../build/reports/jacoco/report.xml -o ../tmp/codeclimate.java.json
      - persist_to_workspace:
          root: tmp
          paths:
            - codeclimate.java.json

      # run test coverage to coveralls
      # - run:
      #     name: Upload to coveralls
      #     command: |
      #       bash ./gradlew test jacocoJupTestReport coverallsJacoco

      # Run fossa
      - run:
          name: Run fossa
          command: |
            cd ./src/
            curl -H 'Cache-Control: no-cache' https://raw.githubusercontent.com/fossas/fossa-cli/master/install.sh | bash
            fossa init
            fossa analyze

      # upload test coverage to codacy
      # - run:
      #     command: |
      #       wget -O codacy-coverage-reporter-assembly.jar https://oss.sonatype.org/service/local/repositories/releases/content/com/codacy/codacy-coverage-reporter/2.0.2/codacy-coverage-reporter-2.0.2-assembly.jar
      #       java -cp codacy-coverage-reporter-assembly.jar com.codacy.CodacyCoverageReporter -l Java -r build/reports/jacoco/report.xml
      #       export CODACY_PROJECT_TOKEN=$CODACY_PROJECT_TOKEN
      #       codacy-coverage-reporter -l Java -r build/reports/jacoco/report.xml

  build_test_python:
    <<: *defaults
    docker:
      # https://circleci.com/developer/images/image/cimg/python
      - image: cimg/python:3.12.6
        environment:
          CC_TEST_REPORTER_ID: $CC_TEST_REPORTER_ID
          COVERALLS_REPO_TOKEN: $COVERALLS_REPO_TOKEN
          CODACY_PROJECT_TOKEN: $CODACY_PROJECT_TOKEN

    working_directory: ~/repo

    steps:
      - checkout

      - attach_workspace:
          at: ~/repo/tmp

      - run:
          name: Upgrade Git
          command: |
            make git-upgrade-ubuntu

      - run:
          name: Get Git Submodules
          command: |
            make git

      - run:
          command: |
            python3 -m venv venv
            . venv/bin/activate
            python3 --version
            python3 -m pip install -r requirements.txt
            coverage-3.11 run scripts/run_unit_tests.py
            coverage-3.11 xml
            python3 scripts/get_problems_count.py

      # upload test coverage to codecov
      - run: bash <(curl -s https://codecov.io/bash) #-cF python

      # run test coverage to code climate
      - run:
          name: Run CodeClimate
          command: |
            tmp/cc-test-reporter format-coverage --prefix /home/circleci/repo -t coverage.py coverage.xml -o tmp/codeclimate.python.json
      - persist_to_workspace:
          root: tmp
          paths:
            - codeclimate.python.json

      # run test coverage to coveralls
      # - run:
      #     name: Upload to coveralls
      #     command: |
      #       python3 -m venv venv
      #       . venv/bin/activate
      #       coveralls

      # upload test coverage to codacy
      # - run:
      #     command: |
      #       export CODACY_PROJECT_TOKEN=$CODACY_PROJECT_TOKEN
      #       python3 -m codacy-coverage -r coverage.xml

  upload_to_code_climate:
    <<: *defaults
    docker:
      - image: cimg/python:3.12.6
        environment:
          CC_TEST_REPORTER_ID: $CC_TEST_REPORTER_ID
    working_directory: ~/repo
    steps:
      - attach_workspace:
          at: ~/repo/tmp
      - run:
          name: Upload coverage results to Code Climate
          command: |
            ./tmp/cc-test-reporter sum-coverage ./tmp/codeclimate.*.json -p 2 -o ./tmp/codeclimate.total.json
            ./tmp/cc-test-reporter upload-coverage -i ./tmp/codeclimate.total.json

workflows:
  build-test:
    jobs:
      - download_from_code_climate:
          filters:
            branches:
              only:
                - main
      - build_test_java:
          filters:
            branches:
              only:
                - main
          requires:
            - download_from_code_climate
      - build_test_python:
          filters:
            branches:
              only:
                - main
          requires:
            - download_from_code_climate
      - upload_to_code_climate:
          filters:
            branches:
              only:
                - main
          requires:
            - build_test_java
            - build_test_python
