on:
  workflow_call:

jobs:
  # Release alpha version on NPM for Hive libraries
  npm:
    uses: the-guild-org/shared-config/.github/workflows/release-snapshot.yml@main
    permissions:
      pull-requests: write
      actions: write
      contents: write
    with:
      npmTag: alpha
      buildScript: build:libraries
      packageManager: pnpm
      nodeVersion: '24.13'
    secrets:
      githubToken: ${{ secrets.GITHUB_TOKEN }}
      npmToken: ${{ secrets.NPM_TOKEN }}

  # Upload zipped tarballs to GitHub
  cli-artifacts:
    runs-on: ubuntu-22.04

    steps:
      - name: checkout
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
        with:
          fetch-depth: 2

      - name: setup environment
        uses: ./.github/actions/setup
        with:
          codegen: false # no need to run because release script will run it anyway
          actor: release-alpha

      - name: build libraries
        run: pnpm build:libraries

      - name: pack tarballs
        working-directory: packages/libraries/cli
        run: pnpm oclif:pack

      - name: upload darwin arm64
        uses: actions/upload-artifact@4cec3d8aa04e39d1a68397de0c4cd6fb9dce8ec1 # v4
        with:
          name: hive-cli-darwin-arm64
          path: ./packages/libraries/cli/dist/*-darwin-arm64.tar.gz

      - name: upload darwin x64
        uses: actions/upload-artifact@4cec3d8aa04e39d1a68397de0c4cd6fb9dce8ec1 # v4
        with:
          name: hive-cli-darwin-x64
          path: ./packages/libraries/cli/dist/*-darwin-x64.tar.gz

      - name: upload win x64
        uses: actions/upload-artifact@4cec3d8aa04e39d1a68397de0c4cd6fb9dce8ec1 # v4
        with:
          name: hive-cli-win-x64
          path: ./packages/libraries/cli/dist/*-win32-x64.tar.gz

      - name: upload linux arm64
        uses: actions/upload-artifact@4cec3d8aa04e39d1a68397de0c4cd6fb9dce8ec1 # v4
        with:
          name: hive-cli-linux-arm64
          path: ./packages/libraries/cli/dist/*-linux-arm64.tar.gz

      - name: upload linux x64
        uses: actions/upload-artifact@4cec3d8aa04e39d1a68397de0c4cd6fb9dce8ec1 # v4
        with:
          name: hive-cli-linux-x64
          path: ./packages/libraries/cli/dist/*-linux-x64.tar.gz

  cli-test:
    strategy:
      matrix:
        os: [macos-latest, ubuntu-latest]
    runs-on: ${{ matrix.os }}
    needs: [cli-artifacts]
    steps:
      - name: Pull artifact for macos
        if: matrix.os == 'macos-latest'
        uses: actions/download-artifact@cc203385981b70ca67e1cc392babf9cc229d5806 # v4
        with:
          name: hive-cli-darwin-x64

      - name: Pull artifact for ubuntu
        if: matrix.os == 'ubuntu-latest'
        uses: actions/download-artifact@cc203385981b70ca67e1cc392babf9cc229d5806 # v4
        with:
          name: hive-cli-linux-x64

      - name: Unzip and run
        run: |
          rm -rf /usr/local/bin/node
          rm -rf /usr/local/lib/node
          rm -rf /opt/homebrew/bin/node

          if command -v node &> /dev/null
          then
              echo "Expected node to not be installed"
              command -v node
              exit 1
          fi
          tar -xzf *.tar.gz
          # Expect the command to succeed
          ./hive/bin/hive --version || exit 1

  extract-cli-version:
    needs: [npm]
    runs-on: ubuntu-22.04
    name: Extract CLI version
    outputs:
      published: ${{ steps.cli.outputs.published }}
      version: ${{ steps.cli.outputs.version }}
    steps:
      - name: Extract published version
        if:
          needs.npm.outputs.published && contains(needs.npm.outputs.publishedPackages,
          '"@graphql-hive/cli"')
        id: cli
        run: |
          echo '${{needs.npm.outputs.publishedPackages}}' > cli-ver.json
          VERSION=`echo $(jq -r '.[] | select(.name | endswith("@graphql-hive/cli")).version' cli-ver.json)`
          echo "version=$VERSION" >> "$GITHUB_OUTPUT"
          echo "published=true" >> "$GITHUB_OUTPUT"

  publish_docker:
    needs: [extract-cli-version]
    uses: ./.github/workflows/dockerize-cli.yaml
    if: ${{ needs.extract-cli-version.outputs.published == 'true' }}
    with:
      cliVersion: ${{ needs.extract-cli-version.outputs.version }}
      publishLatest: false
    secrets: inherit
