name: Update Release Notes

on:
  workflow_call:
    inputs:
      tag:
        required: true
        type: string

permissions:
  contents: write

jobs:
  update-notes:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          ref: ${{ inputs.tag }}
          fetch-depth: 0
          persist-credentials: false

      - name: Generate release notes
        id: cliff
        uses: orhun/git-cliff-action@f50e11560dce63f7c33227798f90b924471a88b5 # v4.8.0
        with:
          config: github-keepachangelog
          args: --latest --strip all
        env:
          OUTPUT: ${{ runner.temp }}/release-notes.md
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          GITHUB_REPO: ${{ github.repository }}

      - name: Update release body
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          RELEASE_TAG: ${{ inputs.tag }}
        run: |
          gh release edit "$RELEASE_TAG" \
            --notes-file "${{ runner.temp }}/release-notes.md" \
            --repo "${{ github.repository }}"
