# This image lives in https://dev.funkwhale.audio/funkwhale/ci
image: $CI_REGISTRY/funkwhale/ci/android:latest

variables:
  COBERTURA_REPORT: '$CI_PROJECT_DIR/app/build/reports/cobertura.xml'
  JACOCO_CSV_LOCATION: '$CI_PROJECT_DIR/app/build/reports/jacoco/jacocoTestReport/jacocoTestReport.csv'
  JACOCO_XML_LOCATION: '$CI_PROJECT_DIR/app/build/reports/jacoco/jacocoTestReport/jacocoTestReport.xml'

stages:
  - build_ci_env
  - test
  - visualize
  - build
  - test-after-build
  - deploy

cache: &global_cache
  key: ${CI_PIPELINE_ID}
  paths:
    - .gradle/wrapper
    - .gradle/caches
  policy: pull

.gradle-default:
  before_script:
    - export GRADLE_USER_HOME=$(pwd)/.gradle
    - chmod +x ./gradlew
    - mkdir -p .android && touch .android/repositories.cfg
  script:
    - echo "Overwrite me"

.build:
  stage: build
  variables:
    apk_file: 'app/build/outputs/apk/debug/app-debug.apk'
    metadata_file: 'metadata/audio.funkwhale.android.dev.yml'
    metadata_template: 'metadata/preview.template.yml'
    output_metadata: 'app/build/outputs/apk/debug/output-metadata.json'
  before_script: 
    - git fetch --unshallow --tags
  after_script:
    - export versionCode=`$ANDROID_HOME/build-tools/30.0.2/aapt dump badging $apk_file | grep versionCode | awk '{print $3}' | sed s/versionCode=//g | sed s/\'//g`
    - apt update && apt install gettext-base
    - cat $metadata_template | envsubst > $metadata_file
  extends: .gradle-default
  artifacts:
    paths:
      - $apk_file
      - $metadata_file
      - $output_metadata
  cache:
    # inherit all global cache settings
    <<: *global_cache

test:
  extends: .gradle-default
  stage: test
  except:
    - tags
  script:
    - ./gradlew --no-daemon --stacktrace test jacocoTestReport
    - awk -F"," '{ instructions += $4 + $5; covered += $5 } END { print covered, "/", instructions, " instructions covered"; print 100*covered/instructions, "% covered" }' $JACOCO_CSV_LOCATION
  artifacts:
    reports:
      junit: app/build/test-results/test**/TEST-*.xml
    paths:
      - $JACOCO_XML_LOCATION
  cache:
    # inherit all global cache settings
    <<: *global_cache
    # override the policy
    policy: pull-push
  
test_nonfree_code:
  stage: test-after-build
  image: registry.funkwhale.audio/funkwhale/ci/android-fdroidserver
  script:
    - fdroid scanner -v app/build/outputs/apk/debug/app-debug.apk | tee output.txt
    - ! grep "CRITICAL" output.txt

coverage:
  stage: visualize
  image: haynes/jacoco2cobertura:1.0.9
  script:
    # convert report from jacoco to cobertura, use relative project path
    - 'python /opt/cover2cover.py $JACOCO_XML_LOCATION $CI_PROJECT_DIR/app/src/main/java > app/build/reports/cobertura.xml'
  needs: [ "test" ]
  dependencies:
    - test
  except:
    - tags
  artifacts:
    reports:
      coverage_report:
        coverage_format: cobertura
        path: $COBERTURA_REPORT

build-develop:
  extends: .build
  script:
    - echo -n $PREVIEW_SIGNING_KEY_STORE | base64 -d > app/android.keystore
    - ./gradlew --stacktrace --no-daemon assembleDebug -x check -Psigning.store=android.keystore -Psigning.store_passphrase=$PREVIEW_SIGNING_KEY_PASS -Psigning.key_passphrase=$PREVIEW_SIGNING_KEY_PASS
  only:
    - develop

build-release:
  variables:
    apk_file: 'app/build/outputs/apk/release/app-release.apk'
    output_metadata: 'app/build/outputs/apk/release/output-metadata.json'
    metadata_template: 'metadata/release.template.yml'
    metadata_file: 'metadata/audio.funkwhale.android.yml'
  extends: .build
  script:
    - echo -n $SIGNING_KEY_STORE | base64 -d > app/android.keystore
    - ./gradlew --stacktrace --no-daemon assembleRelease -Psigning.store=android.keystore -Psigning.store_passphrase=$SIGNING_KEY_PASS -Psigning.key_passphrase=$SIGNING_KEY_PASS
  only:
    - tags

build-bleeding-edge:
  extends: .build
  script:
    - ./gradlew --stacktrace --no-daemon -x check assembleDebug
  except:
    - develop
    - tags

.deploy:
  image: curlimages/curl:latest
  script:
    - 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file $FILE "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/$PACKAGE/$CI_COMMIT_SHORT_SHA/$PACKAGE-$CI_COMMIT_SHORT_SHA.apk"'

deploy-develop:
  extends: .deploy
  stage: deploy
  only:
    - develop
  variables:
    FILE: app/build/outputs/apk/debug/app-debug.apk
    PACKAGE: audio.funkwhale.ffa.dev

deploy-release:
  extends: .deploy
  stage: deploy
  only:
    - tags
  variables:
    FILE: app/build/outputs/apk/release/app-release.apk
    PACKAGE: audio.funkwhale.ffa

trigger-fdroid-update-develop:
  stage: .post
  only:
    - develop
  image: curlimages/curl:7.88.1
  script: curl "https://fdroid.funkwhale.audio/hooks/update-index?name=audio.funkwhale.ffa.dev&version=$CI_COMMIT_SHORT_SHA"

trigger-fdroid-update-release:
  stage: .post
  only:
    - tags
  image: curlimages/curl:7.88.1
  script: curl "https://fdroid.funkwhale.audio/hooks/update-index?name=audio.funkwhale.ffa&version=$CI_COMMIT_SHORT_SHA"
