kind: pipeline
name: generic

steps:
  - name: generic
    image: nextcloudci/android:android-49
    environment:
      GIT_USERNAME:
        from_secret: GIT_USERNAME
      GIT_TOKEN:
        from_secret: GIT_TOKEN
    commands:
      - scripts/checkIfRunDrone.sh $GIT_USERNAME $GIT_TOKEN $DRONE_PULL_REQUEST || exit 0
      - ./gradlew assembleGeneric

  - name: notify
    image: drillster/drone-email
    settings:
      port: 587
      from: nextcloud-drone@kaminsky.me
      recipients_only: true
      username:
        from_secret: EMAIL_USERNAME
      password:
        from_secret: EMAIL_PASSWORD
      recipients:
        from_secret: EMAIL_RECIPIENTS
      host:
        from_secret: EMAIL_HOST
      GIT_USERNAME:
        from_secret: GIT_USERNAME
      GIT_TOKEN:
        from_secret: GIT_TOKEN
    when:
      event:
        - push
      status:
        - failure
      branch:
        - master

trigger:
  branch:
    - master
  event:
    - pull_request
    - push

---
kind: pipeline
name: gplay

steps:
  - name: gplay
    image: nextcloudci/android:android-49
    privileged: true
    environment:
      LOG_USERNAME:
        from_secret: LOG_USERNAME
      LOG_PASSWORD:
        from_secret: LOG_PASSWORD
      GIT_USERNAME:
        from_secret: GIT_USERNAME
      GIT_TOKEN:
        from_secret: GIT_TOKEN
      ORG_GRADLE_PROJECT_coverage: ''
    commands:
      - scripts/checkIfRunDrone.sh $GIT_USERNAME $GIT_TOKEN $DRONE_PULL_REQUEST || exit 0
      - emulator-headless -avd android-27 -no-snapshot -gpu swiftshader_indirect -no-window -no-audio &
      - ./gradlew assembleGplay
      - ./gradlew assembleGplayDebug
      - ./wait_for_emulator.sh
      - ./gradlew jacocoTestGplayDebugUnitTestReport || scripts/uploadReport.sh $LOG_USERNAME $LOG_PASSWORD $DRONE_BUILD_NUMBER "Unit" $DRONE_PULL_REQUEST $GIT_USERNAME $GIT_TOKEN
      - ./gradlew installGplayDebugAndroidTest
      - ./gradlew createGplayDebugCoverageReport || scripts/uploadReport.sh $LOG_USERNAME $LOG_PASSWORD $DRONE_BUILD_NUMBER "IT" $DRONE_PULL_REQUEST $GIT_USERNAME $GIT_TOKEN
      - ./gradlew combinedTestReport
      - curl -o codecov.sh https://codecov.io/bash
      - bash ./codecov.sh -t fc506ba4-33c3-43e4-a760-aada38c24fd5
  - name: notify
    image: drillster/drone-email
    settings:
      port: 587
      from: nextcloud-drone@kaminsky.me
      recipients_only: true
      username:
        from_secret: EMAIL_USERNAME
      password:
        from_secret: EMAIL_PASSWORD
      recipients:
        from_secret: EMAIL_RECIPIENTS
      host:
        from_secret: EMAIL_HOST
    when:
      event:
        - push
      status:
        - failure
      branch:
        - master

services:
  - name: server
    image: nextcloudci/server:server-3

trigger:
  branch:
    - master
  event:
    - push
    - pull_request
---
kind: pipeline
name: analysis

steps:
  - name: analysis
    image: nextcloudci/android:android-49
    environment:
      GIT_USERNAME:
        from_secret: GIT_USERNAME
      GIT_TOKEN:
        from_secret: GIT_TOKEN
      LOG_USERNAME:
        from_secret: LOG_USERNAME
      LOG_PASSWORD:
        from_secret: LOG_PASSWORD
    commands:
      - scripts/checkIfRunDrone.sh $GIT_USERNAME $GIT_TOKEN $DRONE_PULL_REQUEST || exit 0
      - export BRANCH=$(scripts/analysis/getBranchName.sh $GIT_USERNAME $GIT_TOKEN $DRONE_PULL_REQUEST)
      - scripts/analysis/analysis-wrapper.sh $GIT_USERNAME $GIT_TOKEN $BRANCH $LOG_USERNAME $LOG_PASSWORD $DRONE_BUILD_NUMBER $DRONE_PULL_REQUEST

  - name: notify
    image: drillster/drone-email
    settings:
      port: 587
      from: nextcloud-drone@kaminsky.me
      recipients_only: true
      username:
        from_secret: EMAIL_USERNAME
      password:
        from_secret: EMAIL_PASSWORD
      recipients:
        from_secret: EMAIL_RECIPIENTS
      host:
        from_secret: EMAIL_HOST
    when:
      event:
        - push
      status:
        - failure
      branch:
        - master

trigger:
  branch:
    - master
  event:
    - push
    - pull_request
---
kind: pipeline
name: qa

steps:
  - name: qa
    image: nextcloudci/android:android-49
    privileged: true
    environment:
      LOG_USERNAME:
        from_secret: LOG_USERNAME
      LOG_PASSWORD:
        from_secret: LOG_PASSWORD
      GIT_USERNAME:
        from_secret: GIT_USERNAME
      GIT_TOKEN:
        from_secret: GIT_TOKEN
      KS_PASS:
        from_secret: KS_PASS
      KEY_PASS:
        from_secret: KEY_PASS
    commands:
      - scripts/checkIfRunDrone.sh $GIT_USERNAME $GIT_TOKEN $DRONE_PULL_REQUEST || exit 0
      - sed -i "/qa/,/\}/ s/versionCode .*/versionCode $DRONE_BUILD_NUMBER/" build.gradle
      - sed -i "/qa/,/\}/ s/versionName .*/versionName \"$DRONE_BUILD_NUMBER\"/" build.gradle
      - ./gradlew assembleQaDebug
      - /opt/android-sdk-linux/build-tools/*/apksigner sign  --ks-pass pass:$KS_PASS --key-pass pass:$KEY_PASS --ks-key-alias key0 --ks scripts/QA_keystore.jks build/outputs/apk/qa/debug/qa-debug-*.apk
      - scripts/uploadArtifact.sh $LOG_USERNAME $LOG_PASSWORD $DRONE_BUILD_NUMBER $DRONE_PULL_REQUEST $GIT_USERNAME $GIT_TOKEN

trigger:
  event:
    - pull_request
