image: briar/ci-image-android:latest

stages:
  - test
  - optional_tests
  - check_reproducibility

workflow:
  # when to create a CI pipeline
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
    - if: '$CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS'
      when: never # avoids duplicate jobs for branch and MR
    - if: '$CI_COMMIT_BRANCH'
    - if: '$CI_COMMIT_TAG'

.base-test:
  before_script:
    - set -e
    - export GRADLE_USER_HOME=$PWD/.gradle
  cache:
    key: "$CI_COMMIT_REF_SLUG"
    paths:
      - .gradle/wrapper
      - .gradle/caches
  after_script:
    # these file change every time and should not be cached
    - rm -f $GRADLE_USER_HOME/caches/modules-2/modules-2.lock
    - rm -fr $GRADLE_USER_HOME/caches/*/plugin-resolution/

test:
  extends: .base-test
  stage: test
  script:
    - ./gradlew -Djava.security.egd=file:/dev/urandom animalSnifferMain animalSnifferTest
    - ./gradlew -Djava.security.egd=file:/dev/urandom assembleOfficialDebug :briar-headless:linuxJars
    - ./gradlew -Djava.security.egd=file:/dev/urandom compileOfficialDebugAndroidTestSources compileScreenshotDebugAndroidTestSources check
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
      when: always
    - when: always

android test:
  extends: .base-test
  stage: optional_tests
  image: briar/ci-image-android-emulator:latest
  script:
    # start emulator first, so it can fail early
    - start-emulator.sh
    # run normal and screenshot tests together (exclude Large tests)
    - ./gradlew -Djava.security.egd=file:/dev/urandom connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.package=org.briarproject.briar.android -Pandroid.testInstrumentationRunnerArguments.notAnnotation=androidx.test.filters.LargeTest
  after_script:
    - adb pull /sdcard/Pictures/screenshots
  artifacts:
    name: "${CI_PROJECT_PATH}_${CI_JOB_STAGE}_${CI_COMMIT_REF_NAME}_${CI_COMMIT_SHA}"
    paths:
      - kernel.log
      - logcat.txt
      - briar-android/build/reports/androidTests/connected/flavors/*
      - screenshots
    expire_in: 3 days
    when: on_failure
  rules:
    - if: '$CI_PIPELINE_SOURCE == "schedule"'
      when: manual
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
      changes:
        - briar-android/**/*
      when: manual
      allow_failure: true
    - if: '$CI_COMMIT_TAG == null'
      when: manual
      allow_failure: true
  retry:
    max: 1
  tags:
    - kvm

test_reproducible:
  stage: check_reproducibility
  script:
    - "curl -X POST -F token=${RELEASE_CHECK_TOKEN} -F ref=master -F variables[RELEASE_TAG]=${CI_COMMIT_REF_NAME} https://code.briarproject.org/api/v4/projects/61/trigger/pipeline"
  only:
    - tags

.optional_tests:
  stage: optional_tests
  extends: .base-test

bridge test:
  extends: .optional_tests
  rules:
    - if: '$CI_PIPELINE_SOURCE == "schedule"'
      when: on_success
      allow_failure: false
    - if: '$CI_COMMIT_TAG == null'
      when: manual
      allow_failure: true
  script:
    - OPTIONAL_TESTS=org.briarproject.bramble.plugin.tor.BridgeTest ./gradlew --info bramble-java:test --tests BridgeTest
  timeout: 3h

mailbox integration test:
  extends: .optional_tests
  rules:
    - if: '$CI_PIPELINE_SOURCE == "schedule"'
      when: on_success
    - if: '$CI_COMMIT_TAG == null'
      when: manual
      allow_failure: true # TODO figure out how not to allow failure while leaving this optional
  script:
    # start mailbox
    - cd /opt && git clone --depth 1 https://code.briarproject.org/briar/briar-mailbox.git briar-mailbox
    - cd briar-mailbox
    - mkdir -p /root/.local/share # create directory that mailbox (currently) expects to exist
    - ./gradlew run --args="--debug --setup-token 54686973206973206120736574757020746f6b656e20666f722042726961722e" &
    # run mailbox integration test once mailbox has started
    - cd "$CI_PROJECT_DIR"
    - bramble-core/src/test/bash/wait-for-mailbox.sh
    - OPTIONAL_TESTS=org.briarproject.bramble.mailbox.MailboxIntegrationTest ./gradlew --info bramble-core:test --tests MailboxIntegrationTest
