name: CI
on:
  push:
    branches:
      - main
    tags:
      - v*

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  build:
    name: Build app
    runs-on: 'ubuntu-24.04'

    steps:
      - name: Clone repo
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

      - name: Set up JDK
        uses: actions/setup-java@c5195efecf7bdfc987ee8bae7a71cb8b11521c00 # v4.7.1
        with:
          java-version: 17
          distribution: temurin

      - name: Set up gradle
        uses: gradle/actions/setup-gradle@ac638b010cf58a27ee6c972d7336334ccaf61c96 # v4.4.1

      - name: Check code format
        run: ./gradlew spotlessCheck

      - name: Build app
        run: ./gradlew assembleRelease -Pinclude-telemetry -Penable-updater

      - name: Run unit tests
        run: ./gradlew testReleaseUnitTest

      - name: Upload APK
        uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
        with:
          name: arm64-v8a-${{ github.sha }}
          path: app/build/outputs/apk/release/app-arm64-v8a-release-unsigned.apk

      - name: Upload mapping
        uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
        with:
          name: mapping-${{ github.sha }}
          path: app/build/outputs/mapping/release

      # Sign APK and create release for tags

      - name: Get tag name
        if: startsWith(github.ref, 'refs/tags/') && github.repository == 'mihonapp/mihon'
        run: |
          set -x
          echo "VERSION_TAG=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_ENV

      - name: Sign APK
        if: startsWith(github.ref, 'refs/tags/') && github.repository == 'mihonapp/mihon'
        uses: r0adkll/sign-android-release@f30bdd30588842ac76044ecdbd4b6d0e3e813478
        with:
          releaseDirectory: app/build/outputs/apk/release
          signingKeyBase64: ${{ secrets.SIGNING_KEY }}
          alias: ${{ secrets.ALIAS }}
          keyStorePassword: ${{ secrets.KEY_STORE_PASSWORD }}
          keyPassword: ${{ secrets.KEY_PASSWORD }}
        env:
          BUILD_TOOLS_VERSION: '35.0.1'

      - name: Clean up build artifacts
        if: startsWith(github.ref, 'refs/tags/') && github.repository == 'mihonapp/mihon'
        run: |
          set -e

          mv app/build/outputs/apk/release/app-universal-release-unsigned-signed.apk mihon-${{ env.VERSION_TAG }}.apk
          mv app/build/outputs/apk/release/app-arm64-v8a-release-unsigned-signed.apk mihon-arm64-v8a-${{ env.VERSION_TAG }}.apk
          mv app/build/outputs/apk/release/app-armeabi-v7a-release-unsigned-signed.apk mihon-armeabi-v7a-${{ env.VERSION_TAG }}.apk
          mv app/build/outputs/apk/release/app-x86-release-unsigned-signed.apk mihon-x86-${{ env.VERSION_TAG }}.apk
          mv app/build/outputs/apk/release/app-x86_64-release-unsigned-signed.apk mihon-x86_64-${{ env.VERSION_TAG }}.apk

      - name: Create Release
        if: startsWith(github.ref, 'refs/tags/') && github.repository == 'mihonapp/mihon'
        uses: softprops/action-gh-release@72f2c25fcb47643c292f7107632f7a47c1df5cd8 # v2.3.2
        with:
          tag_name: ${{ env.VERSION_TAG }}
          name: Mihon ${{ env.VERSION_TAG }}
          body: |
            <!-->
            > [!TIP]
            >
            > ### If you are unsure which version to download then go with `mihon-${{ env.VERSION_TAG }}.apk`
          files: |
            mihon-${{ env.VERSION_TAG }}.apk
            mihon-arm64-v8a-${{ env.VERSION_TAG }}.apk
            mihon-armeabi-v7a-${{ env.VERSION_TAG }}.apk
            mihon-x86-${{ env.VERSION_TAG }}.apk
            mihon-x86_64-${{ env.VERSION_TAG }}.apk
          draft: true
          prerelease: false
          token: ${{ secrets.MIHON_BOT_TOKEN }}
