name: GitHub release

on:
  workflow_dispatch:
    inputs:
      ezno-version:
        description: "Semver for Ezno (CLI) to release on"
        required: false
        default: "latest"
      
env:
  CACHE_PATHS: |
    ~/.cargo/bin/
    ~/.cargo/registry/index/
    ~/.cargo/registry/cache/
    ~/.cargo/git/db/
    target/

jobs:
  get-build-info:
    runs-on: ubuntu-latest

    outputs:
      new-ezno-version: ${{ steps.get-version.outputs.new-ezno-version }}
      SPONSORS: ${{ steps.get-sponsors-and-contributors.outputs.SPONSORS }}
      CONTRIBUTORS: ${{ steps.get-sponsors-and-contributors.outputs.CONTRIBUTORS }}

    steps:
      - uses: actions/checkout@v4
        with:
          fetch-tags: true

      - name: Get version
        id: get-version
        run: |
          if [ ${{ inputs.ezno-version }} = 'latest' ]; then
            echo "::group::Ezno tags"
            git for-each-ref --sort=creatordate --format '%(refname:short)' 'refs/tags/release/ezno-[0-9]*'
            echo "::endgroup::"

            TAG=$(git for-each-ref --sort=creatordate --format '%(refname:short)' 'refs/tags/release/ezno-[0-9]*' | tail -n 1)
            echo "Building GH release for ${TAG:13}"
            echo "new-ezno-version=${TAG:13}" >> $GITHUB_OUTPUT

            # Replace '.' with '-'
            NAME_VERSION=$(echo $VERSION | sed -e "s/\./-/g")
          else
            VERSION="${{ inputs.ezno-version }}"
            echo "Building GH release for ${VERSION}"
            echo "new-ezno-version=${VERSION}" >> $GITHUB_OUTPUT

            # Replace '.' with '-'
            NAME_VERSION=$(echo $VERSION | sed -e "s/\./-/g")
          fi

      - id: get-sponsors-and-contributors
        run: |
          SPONSORS=$(gh api graphql -f query='{
            user(login: "kaleidawave") {
                sponsorshipsAsMaintainer(first: 100, activeOnly: false) {
                edges {
                    node {
                    sponsor {
                        name, login
                    }
                    }
                }
                }
            }
          }' -q '.data.user.sponsorshipsAsMaintainer.edges | map(.node.sponsor.name // .node.sponsor.login) | join(",")')

          CONTRIBUTORS=$(
            gh pr list --state merged --json author | jq 'map(.author.name // .author.login) | unique | join(",")' --raw-output
          )
            
          echo "SPONSORS=$SPONSORS" # >> $GITHUB_OUTPUT
          echo "CONTRIBUTORS=$CONTRIBUTORS"

        shell: bash
        env:
          GH_TOKEN: ${{ github.token }}

  build:
    needs: [get-build-info]

    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest]
        include:
        - os: windows-latest
          executable-extension: .exe
          platform_name: x86_64-pc-windows
        - os: ubuntu-latest
          platform_name: x86_64-unknown-linux

    runs-on: ${{ matrix.os }}

    # Important that everything here works in all the above OSes!
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: actions/cache@v4
        with:
          path: ${{ env.CACHE_PATHS }}
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

      - name: Build binary
        run: cargo build --release
        env:
          SPONSORS: ${{ needs.get-build-info.outputs.SPONSORS }}
          CONTRIBUTORS: ${{ needs.get-build-info.outputs.CONTRIBUTORS }}

      - name: Rename and move release assets
        run: |
          mkdir artifacts
          mv target/release/ezno${{ matrix.executable-extension }} "artifacts/ezno-${{ needs.get-build-info.outputs.new-ezno-version }}-${{ matrix.platform_name }}${{ matrix.executable-extension }}"

      - uses: actions/upload-artifact@v4
        with:
          name: build-artifacts-${{ matrix.os }}
          path: artifacts/*
          if-no-files-found: error
          retention-days: 1

  github-release:
    needs: [build, get-build-info]
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v4
    
    - uses: actions/download-artifact@v4
      with:
        path: build-artifacts
        pattern: build-artifacts-*
        merge-multiple: true

    - name: Print artifacts
      run: |
        echo "::group::Build artifacts"
        ls -R build-artifacts
        echo "::endgroup::"
        
    - name: GitHub release
      uses: softprops/action-gh-release@v1
      with:
        name: "Ezno ${{ needs.get-build-info.outputs.new-ezno-version }}"
        tag_name: "release/ezno-${{ needs.get-build-info.outputs.new-ezno-version }}"
        body: "For @kaleidawave to update"
        files: |
          README.md
          build-artifacts/*
