on:
  workflow_call:
    outputs:
      hivePublish:
        description: 'A boolean indicating whether hive pkg was published'
        value: ${{ jobs.publish.outputs.hivePublish }}
      hiveVersion:
        description: 'A semver version of the hive pkg, in case it was published'
        value: ${{ jobs.publish.outputs.hiveVersion }}
      cliPublish:
        description: 'A boolean indicating whether cli pkg was published'
        value: ${{ jobs.publish.outputs.cliPublish }}
      cliVersion:
        description: 'A semver version of the cli pkg, in case it was published'
        value: ${{ jobs.publish.outputs.cliVersion }}

jobs:
  publish:
    runs-on: ubuntu-22.04
    permissions:
      pull-requests: write
      actions: write
      contents: write
    env:
      HIVE_TOKEN: ${{ secrets.HIVE_TOKEN }}
      # AWS_* are used by Oclif CLI
      AWS_ACCESS_KEY_ID: ${{ secrets.R2_ACCESS_KEY_ID }}
      AWS_SECRET_ACCESS_KEY: ${{ secrets.R2_SECRET_ACCESS_KEY }}
      AWS_S3_ENDPOINT: https://6d5bc18cd8d13babe7ed321adba3d8ae.r2.cloudflarestorage.com

    outputs:
      cliPublish: ${{ steps.cli.outputs.publish }}
      cliVersion: ${{ steps.cli.outputs.version }}
      hivePublish: ${{ steps.hive.outputs.publish }}
      hiveVersion: ${{ steps.hive.outputs.version }}

    steps:
      - name: checkout
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
        with:
          fetch-depth: 2
          token: ${{ secrets.BOT_GITHUB_TOKEN }}

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

      - name: prepare npm credentials
        run: |
          echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> $HOME/.npmrc
        env:
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

      - uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: stable

      # see https://github.com/changesets/action/issues/523
      - name: fix gh-api issues with changesets
        run: |
          echo "Cargo.lock" >> .gitignore
          git ls-files | while read -r file; do [ -x "$file" ] && chmod -x "$file" || true; done

      - name: publish stable
        id: changesets
        uses: changesets/action@e0145edc7d9d8679003495b11f87bd8ef63c0cba # v1.5.3
        with:
          publish: pnpm release
          version: pnpm release:version
          commit: 'chore(release): update monorepo packages versions'
          title: 'Upcoming Release Changes'
          commitMode: github-api
        env:
          GITHUB_TOKEN: ${{ secrets.BOT_GITHUB_TOKEN }}

      - name: extract published cli version
        if:
          steps.changesets.outputs.published && contains(steps.changesets.outputs.publishedPackages,
          '"@graphql-hive/cli"')
        id: cli
        run: |
          echo '${{steps.changesets.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 "publish=true" >> $GITHUB_OUTPUT

      - name: extract published hive version
        if:
          steps.changesets.outputs.published && contains(steps.changesets.outputs.publishedPackages,
          '"hive"')
        id: hive
        run: |
          echo '${{steps.changesets.outputs.publishedPackages}}' > hive-ver.json
          VERSION=`echo $(jq -r '.[] | select(.name | endswith("hive")).version' hive-ver.json)`
          echo "hiveVersion=$VERSION" >> $GITHUB_OUTPUT
          echo "hivePublish=true" >> $GITHUB_OUTPUT

      # Needed for `oclif pack win`
      - name: Install NSIS
        run: |
          sudo apt-get -y install nsis

      - name: pack tarballs
        if: steps.cli.outputs.publish == 'true'
        working-directory: packages/libraries/cli
        run: pnpm oclif:pack

      - name: upload tarballs
        if: steps.cli.outputs.publish == 'true'
        working-directory: packages/libraries/cli
        run: pnpm oclif:upload

      - name: promote tarballs
        if: steps.cli.outputs.publish == 'true'
        working-directory: packages/libraries/cli
        env:
          VERSION: ${{ steps.cli.outputs.version }}
        run: pnpm oclif promote --no-xz --sha ${GITHUB_SHA:0:7} --version $VERSION
