name: changelog
permissions:
  contents: write
on:
  push:
    tags:
      - '[v]?[0-9]+.[0-9]+.[0-9]+'
  workflow_dispatch:
jobs:
  changelog:
    name: Generate changelog
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pull-requests: write
    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          fetch-depth: 0
      - name: Generate changelog
        uses: orhun/git-cliff-action@v4
        with:
          config: cliff.toml
          args: --verbose
        env:
          OUTPUT: CHANGELOG.md
          GITHUB_REPO: ${{ github.repository }}
      - name: Copy changelog to docs
        run: |
          # MDX (used by docusaurus) does not allow HTML comments, so rewrite
          # the cliff-template markers into MDX-style `{/* ... */}` comments.
          sed -e 's|<!--|{/*|g' -e 's|-->|*/}|g' \
              CHANGELOG.md > 'docs/developers/patch-notes.md'
      - name: Extract branch name
        shell: bash
        run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT
        id: extract_branch
      - name: Create PR
        uses: peter-evans/create-pull-request@v8
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          commit-message: 'chore(changelog): update changelog (auto)'
          title: 'chore(changelog): update changelog (auto)'
          body: 'This PR was created by a GitHub Action to update the changelog.'
          branch: bot/auto-changelog-update
          base: main
          add-paths: |
            CHANGELOG.md
            docs/developers/patch-notes.md
