name: CI

on:
  push:
    branches:
      # - main
      - master
    paths:
      - '**'
      - '!**.md'
      - '!.github/**'
      - '.github/scripts/**'
      - '.github/workflows/build_push.yml'
  # Manual trigger
  workflow_dispatch:

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

env:
  CI_CHUNK_SIZE: 65
  IS_PR_CHECK: false

jobs:
  prepare:
    name: Prepare job
    runs-on: 'ubuntu-24.04'
    outputs:
      latestCommitMessage: ${{ steps.set-env.outputs.LATEST_COMMIT_MESSAGE }}
      matrix: ${{ steps.generate-matrices.outputs.matrix }}
      delete: ${{ steps.generate-matrices.outputs.delete }}
    steps:
      - name: Checkout ${{ github.ref_name }} branch
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          fetch-depth: 0

      - name: Set env
        id: set-env
        run: |
          echo "LATEST_COMMIT_MESSAGE<<{delimiter}
          $(git log -1 --pretty=%B)
          {delimiter}" >> $GITHUB_OUTPUT

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

      - name: Set up Gradle
        uses: gradle/actions/setup-gradle@06832c7b30a0129d7fb559bcc6e43d26f6374244 # v4.3.1
        with:
          cache-read-only: true

      - name: Get last successful CI commit
        id: last_successful_ci_commit
        uses: nrwl/nx-set-shas@dbe0650947e5f2c81f59190a38512cf49126fe6b # v4.3.0
        with:
          main-branch-name: ${{ github.ref_name }}

      - id: generate-matrices
        name: Create output matrices
        run: |
          python ./.github/scripts/generate-build-matrices.py ${{ steps.last_successful_ci_commit.outputs.base }} Release

  build:
    name: Build extensions (${{ matrix.chunk.number }})
    needs: prepare
    runs-on: 'ubuntu-24.04'
    if: ${{ toJson(fromJson(needs.prepare.outputs.matrix).chunk) != '[]' }}
    strategy:
      matrix: ${{ fromJSON(needs.prepare.outputs.matrix) }}
    steps:
      - name: Checkout ${{ github.ref_name }} branch
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

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

      - name: Set up Gradle
        uses: gradle/actions/setup-gradle@06832c7b30a0129d7fb559bcc6e43d26f6374244 # v4.3.1
        with:
          cache-read-only: ${{ matrix.chunk.number > 1 }}

      - name: Prepare signing key
        run: |
          echo "${{ secrets.SIGNING_KEY }}" | base64 -di > signingkey.jks
          # Verify key is properly created
          ls -la signingkey.jks
          keytool -list -v -keystore signingkey.jks -storepass "${{ secrets.KEY_STORE_PASSWORD }}" -alias "${{ secrets.ALIAS }}" || exit 1

      - name: Build extensions (${{ matrix.chunk.number }})
        env:
          ALIAS: ${{ secrets.ALIAS }}
          KEY_STORE_PASSWORD: ${{ secrets.KEY_STORE_PASSWORD }}
          KEY_PASSWORD: ${{ secrets.KEY_PASSWORD }}
        run: |
          ./gradlew $(echo '${{ toJson(matrix.chunk.modules) }}' | jq -r 'join(" ")')

      - name: Upload APKs (${{ matrix.chunk.number }})
        uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
        if: github.repository == 'Kareadita/tach-extension'
        with:
          name: "individual-apks-${{ matrix.chunk.number }}"
          path: "**/*.apk"
          retention-days: 1

      - name: Clean up CI files
        run: rm signingkey.jks

  publish:
    name: Publish extension repo
    needs: [prepare, build]
    if: github.repository == 'Kareadita/tach-extension'
    runs-on: 'ubuntu-24.04'
    steps:
      - name: Download APK artifacts
        uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0
        with:
          path: ~/apk-artifacts

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

      - name: Checkout ${{ github.ref_name }} branch
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          ref: ${{ github.ref_name }}
          path: ${{ github.ref_name }}

      - name: Create repo artifacts
        run: |
          cd ${{ github.ref_name }}
          python ./.github/scripts/move-built-apks.py
          INSPECTOR_LINK="$(curl -s "https://api.github.com/repos/keiyoushi/extensions-inspector/releases/latest" | jq -r '.assets[0].browser_download_url')"
          curl -L "$INSPECTOR_LINK" -o ./Inspector.jar
          java -jar ./Inspector.jar "repo/apk" "output.json" "tmp"
          python ./.github/scripts/create-repo.py

      - name: Checkout repo branch
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          repository: Kareadita/tach-extension
          token: ${{ secrets.GITHUB_TOKEN }}
          ref: repo
          path: repo

      - name: Merge repo
        run: |
          cd repo
          python ../${{ github.ref_name }}/.github/scripts/merge-repo.py '${{ needs.prepare.outputs.delete }}' '${{ github.ref_name }}/repo'

      - name: Deploy repo
        uses: EndBug/add-and-commit@a94899bca583c204427a224a7af87c02f9b325d5 # v9
        with:
          message: "${{ needs.prepare.outputs.latestCommitMessage }}"
          cwd: "./repo"
          committer_name: github-actions[bot]
          committer_email: github-actions[bot]@users.noreply.github.com

      - name: Purge cached index on jsDelivr
        run: |
          curl https://purge.jsdelivr.net/gh/Kareadita/tach-extension@repo/index.min.json
