name: Release

on:
  workflow_dispatch:
    inputs:
      bump:
        description: "Version bump type"
        required: true
        type: choice
        options:
          - patch
          - minor
          - major

permissions:
  contents: write
  id-token: write

jobs:
  release:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7
        with:
          fetch-depth: 0

      - uses: pnpm/action-setup@0ebf47130e4866e96fce0953f49152a61190b271 # v6

      - uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6
        with:
          node-version: "24"
          cache: pnpm

      - run: pnpm install --frozen-lockfile

      - run: npm install -g npm@latest

      - name: Bump version
        id: version
        env:
          BUMP: ${{ inputs.bump }}
        run: |
          pnpm version --no-git-tag-version "$BUMP"
          version="v$(node -p "require('./package.json').version")"
          echo "version=$version" >> "$GITHUB_OUTPUT"

      - name: Generate version file & build
        run: |
          pnpm run gv
          pnpm run build

      - name: Update changelog
        run: pnpm exec git-cliff -o --tag ${{ steps.version.outputs.version }}

      - name: Generate release notes
        run: pnpm exec git-cliff --unreleased --tag ${{ steps.version.outputs.version }} --strip header -o RELEASE_NOTES.md

      - name: Publish to npm
        run: npm publish --provenance --access public

      - name: Commit, tag & push
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add CHANGELOG.md package.json src/version.ts pnpm-lock.yaml
          git commit -m "chore: release ${{ steps.version.outputs.version }}"
          git tag ${{ steps.version.outputs.version }}
          git push
          git push --tags

      - name: Create GitHub release
        run: gh release create ${{ steps.version.outputs.version }} --notes-file RELEASE_NOTES.md
        env:
          GH_TOKEN: ${{ github.token }}
