|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Suggest next semver and changelog by sending the git diff to Claude. |
| 3 | +# Does not rely on commit messages. Changelog is suitable for GoReleaser --release-notes. |
| 4 | +# |
| 5 | +# Auth (first non-empty wins): |
| 6 | +# - ANTHROPIC_API_KEY: call Claude directly. |
| 7 | +# - OP_ANTHROPIC_API_KEY_REF: 1Password reference (default below; override if your item path differs). |
| 8 | +# |
| 9 | +# Optional: CLAUDE_MODEL (default: claude-sonnet-4-6) — e.g. claude-opus-4-6. |
| 10 | +# |
| 11 | +# Requires: curl, jq; for 1Password: op CLI |
| 12 | +# Usage: bin/suggest-version-ai.sh [base_ref] [-o release_notes.md] |
| 13 | +# base_ref defaults to the latest git tag. |
| 14 | +# -o FILE write changelog markdown to FILE (default: dist/release_notes.md) |
| 15 | +# |
| 16 | +# Output: bump (major|minor|patch), next_version (e.g. v1.3.0), and changelog file. |
| 17 | + |
| 18 | +set -euo pipefail |
| 19 | + |
| 20 | +BASE_REF="" |
| 21 | +RELEASE_NOTES_FILE="dist/release_notes.md" |
| 22 | +while [[ $# -gt 0 ]]; do |
| 23 | + case "$1" in |
| 24 | + -o) RELEASE_NOTES_FILE="$2"; shift 2 ;; |
| 25 | + *) BASE_REF="$1"; shift ;; |
| 26 | + esac |
| 27 | +done |
| 28 | +BASE_REF="${BASE_REF:-$(git describe --tags --abbrev=0 2>/dev/null)}" |
| 29 | +SUGGESTED_VERSION_FILE="$(dirname "$RELEASE_NOTES_FILE")/suggested_version" |
| 30 | + |
| 31 | +if [ -z "$BASE_REF" ]; then |
| 32 | + echo "ERROR: No base ref. Pass a tag or branch, or create a tag first." >&2 |
| 33 | + exit 1 |
| 34 | +fi |
| 35 | + |
| 36 | +# Cap diff size to stay within context |
| 37 | +MAX_DIFF_CHARS=50000 |
| 38 | +DIFF="$(git diff "$BASE_REF"..HEAD 2>/dev/null | head -c "$MAX_DIFF_CHARS")" |
| 39 | + |
| 40 | +# Get API key from 1Password if not set (default ref; override with OP_ANTHROPIC_API_KEY_REF) |
| 41 | +OP_ANTHROPIC_API_KEY_REF="${OP_ANTHROPIC_API_KEY_REF:-op://Shared/Anthropic API Key/credential}" |
| 42 | +if [ -z "${ANTHROPIC_API_KEY:-}" ]; then |
| 43 | + if command -v op >/dev/null 2>&1; then |
| 44 | + ANTHROPIC_API_KEY=$(op read "$OP_ANTHROPIC_API_KEY_REF" 2>/dev/null) || true |
| 45 | + fi |
| 46 | +fi |
| 47 | +if [ -z "${ANTHROPIC_API_KEY:-}" ]; then |
| 48 | + echo "ERROR: Set ANTHROPIC_API_KEY or OP_ANTHROPIC_API_KEY_REF (1Password)." >&2 |
| 49 | + exit 1 |
| 50 | +fi |
| 51 | + |
| 52 | +# Remove stale outputs from a previous run so a failure partway through doesn't mislead the next invocation. |
| 53 | +trap 'rm -f "$RELEASE_NOTES_FILE" "$SUGGESTED_VERSION_FILE"' EXIT |
| 54 | + |
| 55 | +if [ -z "$DIFF" ]; then |
| 56 | + echo "No changes since $BASE_REF. Bump: patch (no change)." >&2 |
| 57 | + CHANGELOG="No code changes since $BASE_REF." |
| 58 | + mkdir -p "$(dirname "$RELEASE_NOTES_FILE")" |
| 59 | + echo "$CHANGELOG" > "$RELEASE_NOTES_FILE" |
| 60 | + echo "patch" |
| 61 | + CURRENT="${BASE_REF#v}" |
| 62 | + if [[ "$CURRENT" =~ ^[0-9]+\.[0-9]+\.[0-9]+ ]]; then |
| 63 | + MAJOR="${CURRENT%%.*}"; REST="${CURRENT#*.}"; MINOR="${REST%%.*}"; PATCH="${REST#*.}"; PATCH="${PATCH%%[-+]*}" |
| 64 | + NEXT="v${MAJOR}.${MINOR}.$((PATCH+1))" |
| 65 | + if [ -n "$(git tag -l "$NEXT")" ]; then |
| 66 | + echo "ERROR: tag $NEXT already exists. Push it or use: make release tag=$NEXT" >&2 |
| 67 | + exit 1 |
| 68 | + fi |
| 69 | + echo "$NEXT" > "$SUGGESTED_VERSION_FILE" |
| 70 | + echo "$NEXT" |
| 71 | + fi |
| 72 | + trap - EXIT |
| 73 | + exit 0 |
| 74 | +fi |
| 75 | + |
| 76 | +PROMPT="You are a release engineer. Given the following git diff for a CLI application (Kosli CLI), do two things. |
| 77 | +
|
| 78 | +Scope: Consider ONLY changes to the CLI itself—i.e. code under cmd/ and internal/ that affects user-facing commands, flags, and behavior. IGNORE all other changes when deciding the version and when writing the changelog: |
| 79 | +- Ignore: documentation (docs*, *.md), Helm charts (charts/), CI/workflows (.github/), scripts (bin/, scripts/), tests (*_test.go, testdata/), Makefile, config files, and any other non-CLI code. |
| 80 | +- If the diff contains only ignored changes, recommend a patch bump and write a single short line for the changelog (e.g. \"No user-facing CLI changes.\"). |
| 81 | +
|
| 82 | +1) Suggest the semantic version bump (based only on CLI changes): |
| 83 | + - major: Breaking changes (removed/renamed commands or flags, changed default behavior). |
| 84 | + - minor: New commands, flags, subcommands, or features. |
| 85 | + - patch: Bug fixes, refactors, internal or dependency updates; or no user-facing CLI changes. |
| 86 | +
|
| 87 | +2) Write a short changelog in markdown for the GitHub release body. Include only user-facing CLI changes. Use bullet points; be concise; no preamble. |
| 88 | + - Structure the changelog with section headers (e.g. \"# Breaking changes\", \"# New features\", \"# Bug fixes\" or \"# Improvements\") and list items under each header. Use only headers that have at least one change—omit any section that would be empty. |
| 89 | + - Do not write placeholder lines under any header (no \"No other changes\", \"No user-facing CLI changes in this release\", or similar). If there are no CLI changes at all, output a single short line only (no headers). |
| 90 | +
|
| 91 | +Reply in this exact format (no other text before or after): |
| 92 | +BUMP: major|minor|patch |
| 93 | +---CHANGELOG--- |
| 94 | +<markdown changelog here>" |
| 95 | + |
| 96 | +CLAUDE_MODEL="${CLAUDE_MODEL:-claude-sonnet-4-6}" |
| 97 | +DIFF_FILE=$(mktemp) |
| 98 | +trap 'rm -f "$DIFF_FILE" "$RELEASE_NOTES_FILE" "$SUGGESTED_VERSION_FILE"' EXIT |
| 99 | +printf '%s' "$DIFF" > "$DIFF_FILE" |
| 100 | +BODY=$(jq -n \ |
| 101 | + --arg model "$CLAUDE_MODEL" \ |
| 102 | + --arg prompt "$PROMPT" \ |
| 103 | + --rawfile diff "$DIFF_FILE" \ |
| 104 | + '{model: $model, max_tokens: 1024, messages: [{role: "user", content: ($prompt + "\n\n--- diff ---\n\n" + $diff)}]}') |
| 105 | + |
| 106 | +RESPONSE=$(curl -s -S -X POST "https://api.anthropic.com/v1/messages" \ |
| 107 | + -H "x-api-key: $ANTHROPIC_API_KEY" \ |
| 108 | + -H "anthropic-version: 2023-06-01" \ |
| 109 | + -H "Content-Type: application/json" \ |
| 110 | + -d "$BODY") |
| 111 | + |
| 112 | +CONTENT=$(echo "$RESPONSE" | jq -r '.content[0].text // empty') |
| 113 | +if [ -z "$CONTENT" ]; then |
| 114 | + echo "ERROR: Anthropic API failed or returned no content. Response:" >&2 |
| 115 | + echo "$RESPONSE" | jq . >&2 |
| 116 | + exit 1 |
| 117 | +fi |
| 118 | + |
| 119 | +BUMP=$(echo "$CONTENT" | tr '[:upper:]' '[:lower:]' | grep -oE 'major|minor|patch' | head -1) |
| 120 | +case "$BUMP" in |
| 121 | + major|minor|patch) ;; |
| 122 | + *) |
| 123 | + echo "WARN: Could not parse bump (got: $CONTENT). Defaulting to patch." >&2 |
| 124 | + BUMP=patch |
| 125 | + ;; |
| 126 | +esac |
| 127 | + |
| 128 | +CHANGELOG_MARKER='---CHANGELOG---' |
| 129 | +if echo "$CONTENT" | grep -qF -- "$CHANGELOG_MARKER"; then |
| 130 | + CHANGELOG=$(echo "$CONTENT" | sed -n "/${CHANGELOG_MARKER}/,\$ p" | tail -n +2) |
| 131 | +else |
| 132 | + CHANGELOG=$(echo "$CONTENT" | sed -n '2,$ p') |
| 133 | +fi |
| 134 | +mkdir -p "$(dirname "$RELEASE_NOTES_FILE")" |
| 135 | +echo "$CHANGELOG" > "$RELEASE_NOTES_FILE" |
| 136 | + |
| 137 | +# Compute next version from current tag |
| 138 | +CURRENT="${BASE_REF#v}" |
| 139 | +if [[ "$CURRENT" =~ ^[0-9]+\.[0-9]+\.[0-9]+ ]]; then |
| 140 | + MAJOR="${CURRENT%%.*}"; REST="${CURRENT#*.}"; MINOR="${REST%%.*}"; PATCH="${REST#*.}"; PATCH="${PATCH%%[-+]*}" |
| 141 | + case "$BUMP" in |
| 142 | + major) NEXT="v$((MAJOR+1)).0.0" ;; |
| 143 | + minor) NEXT="v${MAJOR}.$((MINOR+1)).0" ;; |
| 144 | + patch) NEXT="v${MAJOR}.${MINOR}.$((PATCH+1))" ;; |
| 145 | + esac |
| 146 | +else |
| 147 | + NEXT="" |
| 148 | +fi |
| 149 | + |
| 150 | +if [ -n "$NEXT" ]; then |
| 151 | + if [ -n "$(git tag -l "$NEXT")" ]; then |
| 152 | + echo "ERROR: tag $NEXT already exists. Push it or use: make release tag=$NEXT" >&2 |
| 153 | + exit 1 |
| 154 | + fi |
| 155 | + echo "$NEXT" > "$SUGGESTED_VERSION_FILE" |
| 156 | +fi |
| 157 | +echo "Suggested bump: $BUMP (from diff since $BASE_REF)" >&2 |
| 158 | +echo "Next version: $NEXT" >&2 |
| 159 | +echo "Changelog: $RELEASE_NOTES_FILE" >&2 |
| 160 | +trap - EXIT |
| 161 | +echo "$BUMP" |
| 162 | +echo "$NEXT" |
0 commit comments