name: Release Management

on:
  push:
    branches:
      - "main"

jobs:
  release-please:
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pull-requests: write
    outputs:
      release_created: ${{ steps.release.outputs.releases_created }}
      pr_created: ${{ steps.release.outputs.prs_created }}
    steps:
      - uses: googleapis/release-please-action@v4
        id: release
        with:
          token: ${{ secrets.RELEASE_PLEASE_TOKEN }}
          config-file: .github/release-please.json
          manifest-file: .github/release-manifest.json

  # Because uv isn't natively supported by release-please, we just need to bump the uv lockfile
  update-uv-lockfile:
    name: Update uv lockfile
    runs-on: ubuntu-latest
    needs: release-please
    permissions:
      contents: write
      pull-requests: write
    if: ${{ needs.release-please.outputs.pr_created == 'true' }}
    steps:
      - uses: actions/checkout@v4
        with:
          ref: release-please--branches--main
          fetch-depth: 2
      - name: Install uv
        uses: astral-sh/setup-uv@v3
        with:
          enable-cache: true
      - name: Update uv.lock
        run: uv lock
      - name: Merging changes with last commit
        run: |
          git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git config user.name "github-actions[bot]"

          if [[ -n "$(git status --porcelain)" ]]; then
            git add uv.lock
            git commit --amend --no-edit
            git push --force
          fi
