|
| 1 | +"""Draft release notes for a Strands Agents package via Bedrock. |
| 2 | +
|
| 3 | +The maintainer types the explicit version at workflow_dispatch time. This |
| 4 | +script's `bump` output is purely advisory — surfaced in the run summary so |
| 5 | +reviewers can sanity-check the typed version against what the commits suggest. |
| 6 | +The version is NOT derived from this script. |
| 7 | +
|
| 8 | +Inputs (env): |
| 9 | + PACKAGE "python" or "typescript" |
| 10 | + PREV_TAG previous release tag (e.g. "python/v1.2.3") |
| 11 | + NEW_REF git ref or SHA being released (typically a pinned SHA) |
| 12 | + BEDROCK_MODEL inference profile / model id (defaults to a Claude Sonnet on Bedrock) |
| 13 | + AWS_REGION defaults to us-west-2 (matches strands-command.yml) |
| 14 | +
|
| 15 | +Outputs (files in $GITHUB_WORKSPACE): |
| 16 | + release-notes.md markdown release notes |
| 17 | + bump.txt advisory bump: one of "major", "minor", "patch" |
| 18 | +
|
| 19 | +Exit codes: |
| 20 | + 0 notes drafted successfully |
| 21 | + 1 no commits between PREV_TAG and NEW_REF |
| 22 | + 2 Bedrock call failed or returned unparseable output |
| 23 | + (the caller workflow falls back to `git shortlog` on non-zero exit) |
| 24 | +""" |
| 25 | + |
| 26 | +from __future__ import annotations |
| 27 | + |
| 28 | +import json |
| 29 | +import os |
| 30 | +import subprocess |
| 31 | +import sys |
| 32 | +from pathlib import Path |
| 33 | + |
| 34 | +import boto3 |
| 35 | +from botocore.exceptions import BotoCoreError, ClientError |
| 36 | + |
| 37 | + |
| 38 | +WORKSPACE = Path(os.environ.get("GITHUB_WORKSPACE", ".")) |
| 39 | +NOTES_PATH = WORKSPACE / "release-notes.md" |
| 40 | +BUMP_PATH = WORKSPACE / "bump.txt" |
| 41 | + |
| 42 | + |
| 43 | +def run(cmd: list[str]) -> str: |
| 44 | + result = subprocess.run(cmd, capture_output=True, text=True, check=True) |
| 45 | + return result.stdout |
| 46 | + |
| 47 | + |
| 48 | +def collect_commits(prev_tag: str, new_ref: str) -> str: |
| 49 | + """Return the git log between prev_tag and new_ref, one commit per block.""" |
| 50 | + return run( |
| 51 | + [ |
| 52 | + "git", |
| 53 | + "log", |
| 54 | + f"{prev_tag}..{new_ref}", |
| 55 | + "--pretty=format:--- COMMIT ---%n%H%n%an%n%s%n%b", |
| 56 | + "--no-merges", |
| 57 | + ] |
| 58 | + ) |
| 59 | + |
| 60 | + |
| 61 | +def collect_diff_stats(prev_tag: str, new_ref: str) -> str: |
| 62 | + """Return per-file change stats so the model can reason about scope.""" |
| 63 | + return run(["git", "diff", "--stat", f"{prev_tag}...{new_ref}"]) |
| 64 | + |
| 65 | + |
| 66 | +def build_prompt( |
| 67 | + package: str, |
| 68 | + prev_tag: str, |
| 69 | + new_ref: str, |
| 70 | + commits: str, |
| 71 | + diff_stats: str, |
| 72 | +) -> str: |
| 73 | + return f"""You are drafting release notes for the {package} package of the Strands Agents SDK. |
| 74 | +
|
| 75 | +Previous tag: `{prev_tag}` |
| 76 | +New ref: `{new_ref}` |
| 77 | +
|
| 78 | +## Your task |
| 79 | +
|
| 80 | +1. Suggest a semver bump (major / minor / patch) from the commits using |
| 81 | + Conventional Commits as a guide: `feat:` -> minor, `fix:` / `refactor:` / |
| 82 | + `perf:` / `docs:` -> patch, anything with `BREAKING CHANGE:` in the body or |
| 83 | + `!:` in the subject -> major. If the commits show API changes that aren't |
| 84 | + flagged with `!`, still call it major and say why. This suggestion is |
| 85 | + ADVISORY — the maintainer types the actual version separately. |
| 86 | +
|
| 87 | +2. Draft user-facing release notes in markdown, grouped under these headings |
| 88 | + (omit headings with no entries): |
| 89 | + - **Breaking changes** |
| 90 | + - **Features** |
| 91 | + - **Bug fixes** |
| 92 | + - **Other changes** (docs, refactors, internal) |
| 93 | +
|
| 94 | +3. Each bullet should be one line, written for users, referencing the commit |
| 95 | + hash in parens. |
| 96 | +
|
| 97 | +## Output format |
| 98 | +
|
| 99 | +Return a single JSON object with exactly two keys: |
| 100 | +- `bump`: one of "major", "minor", "patch" |
| 101 | +- `notes`: the markdown release notes as a single string |
| 102 | +
|
| 103 | +Do not wrap the JSON in markdown fences or any prose. The first character of |
| 104 | +your response must be `{{` and the last must be `}}`. |
| 105 | +
|
| 106 | +## Commits since last release |
| 107 | +
|
| 108 | +``` |
| 109 | +{commits} |
| 110 | +``` |
| 111 | +
|
| 112 | +## Diff stats |
| 113 | +
|
| 114 | +``` |
| 115 | +{diff_stats} |
| 116 | +``` |
| 117 | +""" |
| 118 | + |
| 119 | + |
| 120 | +def call_bedrock(prompt: str, model_id: str, region: str) -> dict: |
| 121 | + client = boto3.client("bedrock-runtime", region_name=region) |
| 122 | + response = client.converse( |
| 123 | + modelId=model_id, |
| 124 | + messages=[{"role": "user", "content": [{"text": prompt}]}], |
| 125 | + inferenceConfig={"maxTokens": 4096, "temperature": 0.2}, |
| 126 | + ) |
| 127 | + text = response["output"]["message"]["content"][0]["text"].strip() |
| 128 | + return json.loads(text) |
| 129 | + |
| 130 | + |
| 131 | +def main() -> int: |
| 132 | + package = os.environ["PACKAGE"] |
| 133 | + prev_tag = os.environ["PREV_TAG"] |
| 134 | + new_ref = os.environ["NEW_REF"] |
| 135 | + model_id = os.environ.get( |
| 136 | + "BEDROCK_MODEL", "us.anthropic.claude-sonnet-4-5-20250929-v1:0" |
| 137 | + ) |
| 138 | + region = os.environ.get("AWS_REGION", "us-west-2") |
| 139 | + |
| 140 | + commits = collect_commits(prev_tag, new_ref) |
| 141 | + if not commits.strip(): |
| 142 | + print(f"No commits between {prev_tag} and {new_ref} — nothing to release.") |
| 143 | + return 1 |
| 144 | + |
| 145 | + diff_stats = collect_diff_stats(prev_tag, new_ref) |
| 146 | + prompt = build_prompt(package, prev_tag, new_ref, commits, diff_stats) |
| 147 | + |
| 148 | + try: |
| 149 | + result = call_bedrock(prompt, model_id, region) |
| 150 | + except (BotoCoreError, ClientError) as exc: |
| 151 | + # Network / IAM / throttling. Caller workflow handles fallback. |
| 152 | + print(f"Bedrock call failed: {exc}", file=sys.stderr) |
| 153 | + return 2 |
| 154 | + except json.JSONDecodeError as exc: |
| 155 | + # Model returned non-JSON. Surface it for the reviewer. |
| 156 | + print(f"Could not parse Bedrock response as JSON: {exc}", file=sys.stderr) |
| 157 | + return 2 |
| 158 | + |
| 159 | + bump = result.get("bump", "unknown") |
| 160 | + notes = result.get("notes", "").strip() |
| 161 | + if not notes: |
| 162 | + print("Bedrock response had no `notes` content.", file=sys.stderr) |
| 163 | + return 2 |
| 164 | + |
| 165 | + NOTES_PATH.write_text(notes) |
| 166 | + BUMP_PATH.write_text(bump) |
| 167 | + print(f"Wrote {NOTES_PATH} ({len(notes)} chars)") |
| 168 | + print(f"Wrote {BUMP_PATH} -> {bump} (advisory)") |
| 169 | + return 0 |
| 170 | + |
| 171 | + |
| 172 | +if __name__ == "__main__": |
| 173 | + sys.exit(main()) |
0 commit comments