name: Android CI

on:
  push:
    branches: [ master ]

jobs:
  build:
    runs-on: ubuntu-latest
    env:
      CCACHE_DIR: ${{ github.workspace }}/.ccache
      CCACHE_COMPILERCHECK: "%compiler% -dumpmachine; %compiler% -dumpversion"
      CCACHE_NOHASHDIR: true
      CCACHE_MAXSIZE: 1G
    steps:
      - uses: actions/checkout@v3
        with:
          submodules: 'recursive'
          fetch-depth: 0
      - name: Setup JDK 17
        uses: actions/setup-java@v3
        with:
          distribution: 'temurin'
          java-version: 17
          cache: 'gradle'
      - name: Retrieve version
        run: |
          echo VERSION=$(echo ${{ github.event.head_commit.id }} | head -c 10) >> $GITHUB_ENV
      - name: Set up ccache
        uses: hendrikmuhs/ccache-action@v1.2
        with:
          key: ${{ runner.os }}-${{ github.sha }}
          restore-keys: ${{ runner.os }}
      - name: Build with Gradle
        run: |
          echo 'org.gradle.caching=true' >> gradle.properties
          echo 'org.gradle.parallel=true' >> gradle.properties
          echo 'org.gradle.vfs.watch=true' >> gradle.properties
          echo 'org.gradle.jvmargs=-Xmx2048m' >> gradle.properties
          echo 'android.native.buildOutput=verbose' >> gradle.properties
          ./gradlew -PappVerName=${{ env.VERSION }} assembleRelease assembleDebug
      - name: Upload built apk
        if: success()
        uses: actions/upload-artifact@v3
        with:
          name: snapshot
          path: |
              app/build/outputs/apk
              app/build/outputs/mapping
              app/release
      - name: Post to channel
        if: github.ref == 'refs/heads/master'
        env:
          CHANNEL_ID: ${{ secrets.TELEGRAM_TO }}
          BOT_TOKEN: ${{ secrets.TELEGRAM_TOKEN }}
          FILE: app/release/BiliRoaming_${{ env.VERSION }}.apk
          COMMIT_MESSAGE: |+
            New push to github\!
            ```
            ${{ github.event.head_commit.message }}
            ```by `${{ github.event.head_commit.author.name }}`
            See commit detail [here](${{ github.event.head_commit.url }})
            Snapshot apk is attached \(unsupported by TAICHI\)
        run: |
          ESCAPED=`python3 -c 'import json,os,urllib.parse; print(urllib.parse.quote(json.dumps(os.environ["COMMIT_MESSAGE"])))'`
          curl -v "https://api.telegram.org/bot${BOT_TOKEN}/sendMediaGroup?chat_id=${CHANNEL_ID}&media=%5B%7B%22type%22:%22document%22,%20%22media%22:%22attach://release%22,%22parse_mode%22:%22MarkdownV2%22,%22caption%22:${ESCAPED}%7D%5D"  -F release="@$FILE"
