name: Hub Release

on:
  push:
    tags:
      - "hub/v*"
  workflow_dispatch:
    inputs:
      version:
        description: "Version to release (e.g., v1.0.0)"
        required: true
        type: string

permissions:
  contents: write

jobs:
  release:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: mcps/hub
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: "1.24"
          cache-dependency-path: mcps/hub/go.sum

      - name: Determine version
        id: version
        run: |
          if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
            VERSION="${{ inputs.version }}"
          else
            # Extract version from tag (hub/v1.0.0 -> v1.0.0)
            VERSION="${GITHUB_REF_NAME#hub/}"
          fi
          echo "version=${VERSION}" >> "$GITHUB_OUTPUT"
          echo "Using version: ${VERSION}"

      - name: Create tag for goreleaser
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          # Create a local tag for GoReleaser (it expects v1.2.0, not hub/v1.2.0)
          git tag -f "${{ steps.version.outputs.version }}" -m "Release ${{ steps.version.outputs.version }}"

      - name: Run GoReleaser
        uses: goreleaser/goreleaser-action@v6
        with:
          distribution: goreleaser
          version: "~> v2"
          args: release --clean
          workdir: mcps/hub
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          GORELEASER_CURRENT_TAG: ${{ steps.version.outputs.version }}
