name: Release

on:
  release:
    types:
      - published

permissions: {}

jobs:
  release:
    name: Release
    runs-on: ubuntu-latest
    if: github.repository == 'stac-utils/pystac'
    environment:
      name: pypi
    permissions:
      id-token: write
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
      - uses: astral-sh/setup-uv@c771a70e6277c0a99b617c7a806ffedaca235ff9 # v9.0.0
      - name: Determine package path from tag
        id: package
        run: |
          tag="${{ github.event.release.tag_name }}"
          if [[ "$tag" =~ ^v[0-9] ]]; then
            path="."
          else
            component="${tag%-v*}"
            path=$(jq -r --arg c "$component" \
              '.packages | to_entries[] | select(.value.component == $c) | .key' \
              release-please-config.json)
          fi
          if [[ -z "$path" || "$path" == "null" ]]; then
            echo "::error::Could not determine package path for tag $tag"
            exit 1
          fi
          echo "path=$path" >> "$GITHUB_OUTPUT"
      - name: Build
        working-directory: ${{ steps.package.outputs.path }}
        run: uv build -o dist
      - uses: pypa/gh-action-pypi-publish@ba38be9e461d3875417946c167d0b5f3d385a247 # v1.14.1
        with:
          packages-dir: ${{ steps.package.outputs.path }}/dist
      - name: Build pystac-core
        if: steps.package.outputs.path == '.'
        working-directory: core
        run: uv build -o dist
      - uses: pypa/gh-action-pypi-publish@ba38be9e461d3875417946c167d0b5f3d385a247 # v1.14.1
        if: steps.package.outputs.path == '.'
        with:
          packages-dir: core/dist
