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@v4
        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: 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@v7
        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
