name: QEMU Build and test riscv64 crate

on:
  push:
    branches:
      - main
  release:
    types: [published]
  pull_request:
    branches:
      - "**"

concurrency:
  group: ${{ github.event_name == 'pull_request' && format('{0}-{1}', github.workflow_ref, github.event.pull_request.number) || github.run_id }}
  cancel-in-progress: true

permissions:
  id-token: write
  contents: read

jobs:
  build_crate:
    name: Build riscv64 crate and run tests
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest]

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 1

      - name: Set up QEMU on x86_64
        id: qemu
        uses: docker/setup-qemu-action@v3
        with:
          platforms: riscv64

      - name: Build and Test
        run: |
          docker run --rm --platform linux/riscv64 \
            -v ${{ github.workspace }}:/ws --workdir=/ws \
            chianetwork/ubuntu-22.04-risc-builder:latest \
            bash -exc '\
            cargo test --release -p chia-consensus
            '

  build_wheels:
    name: ${{ matrix.os }} 📦 Build Wheel ${{ matrix.python.major-dot-minor }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest]
        python:
          - major-dot-minor: "3.10"
            matrix: "3.10"
          - major-dot-minor: "3.11"
            matrix: "3.11"
          - major-dot-minor: "3.12"
            matrix: "3.12"
          - major-dot-minor: "3.13"
            matrix: "3.13"
          - major-dot-minor: "3.14"
            matrix: "3.14"

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 1

      - name: Set up QEMU on x86_64
        id: qemu
        uses: docker/setup-qemu-action@v3
        with:
          platforms: riscv64

      - name: Build and Test
        run: |
          docker run --rm --platform linux/riscv64 \
            -v ${{ github.workspace }}:/ws --workdir=/ws \
            chianetwork/ubuntu-22.04-risc-builder:latest \
            bash -exc '\
            pyenv global ${{ matrix.python.matrix }}
            python -m venv venv && \
            source ./venv/bin/activate && \
            pip install --upgrade pip && \
            pip install --extra-index-url https://pypi.chia.net/simple/ maturin==1.2.3 && \
            maturin build -i python --release -m wheel/Cargo.toml \
            '

      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: packages-${{ matrix.os }}-${{ matrix.python.major-dot-minor }}
          path: ./target/wheels
          if-no-files-found: error
  upload:
    name: Upload to Chia PyPI
    runs-on: ubuntu-latest
    needs:
      - build_wheels
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Set Env
        uses: Chia-Network/actions/setjobenv@main
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Download artifacts
        if: env.RELEASE == 'true'
        uses: actions/download-artifact@v4
        with:
          merge-multiple: true
          pattern: packages-*
          path: ./target/wheels

      - name: Configure AWS credentials
        if: env.RELEASE == 'true'
        uses: aws-actions/configure-aws-credentials@v4
        with:
          role-to-assume: arn:aws:iam::${{ secrets.CHIA_AWS_ACCOUNT_ID }}:role/installer-upload
          aws-region: us-west-2

      - name: List existing wheels
        if: env.RELEASE == 'true'
        shell: sh
        run: |
          aws s3 ls s3://download.chia.net/simple/chia-rs/ > existing_wheel_list_raw
          cat existing_wheel_list_raw
          # shellcheck disable=SC2002
          cat existing_wheel_list_raw | tr -s ' ' | cut -d ' ' -f 4 > existing_wheel_list

      - name: List new wheels
        if: env.RELEASE == 'true'
        shell: sh
        run: |
          (cd target/wheels/; ls chia_rs-*.whl) > new_wheel_list
          # shellcheck disable=SC2002
          cat new_wheel_list | xargs -I % sh -c 'ls -l target/wheels/%'

      - name: Choose wheels to upload
        if: env.RELEASE == 'true'
        shell: sh
        run: |
          grep -F -x -v -f existing_wheel_list new_wheel_list > upload_wheel_list
          cat upload_wheel_list

      - name: Upload wheels
        if: env.RELEASE == 'true'
        shell: sh
        run: |
          # shellcheck disable=SC2002
          cat upload_wheel_list | xargs -I % sh -c 'aws s3 cp target/wheels/% s3://download.chia.net/simple/chia-rs/'
