name: Release

on:
  push:
    tags:
      - v*
  workflow_call:
    inputs:
      tag:
        description: 'The tag to release (e.g. v0.70.2)'
        required: true
        type: string

jobs:
  pypi:
    name: Publish to PyPI
    runs-on: ubuntu-latest
    # Environment and permissions trusted publishing.
    environment:
      # Create this environment in the GitHub repository under Settings -> Environments
      name: pypi
    permissions:
      id-token: write
      contents: read
    steps:
      - name: Resolve tag
        id: resolve
        run: |
          if [ -n "${{ inputs.tag }}" ]; then
            TAG="${{ inputs.tag }}"
          else
            TAG="${GITHUB_REF#refs/tags/}"
          fi
          echo "tag=$TAG" >> $GITHUB_OUTPUT

      - name: Checkout
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          ref: ${{ steps.resolve.outputs.tag }}

      - name: Install uv
        uses: astral-sh/setup-uv@37802adc94f370d6bfd71619e3f0bf239e1f3b78 # v7.6.0
      - name: Install Python 3.11
        run: uv python install 3.11
      - name: Build
        run: uv build
      - name: Smoke test (wheel)
        run: uv run --isolated --no-project --with dist/*.whl nextcloud-mcp-server --help
      - name: Smoke test (source distribution)
        run: uv run --isolated --no-project --with dist/*.tar.gz nextcloud-mcp-server --help
      - name: Publish
        run: uv publish
