|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Draft a changelog entry from merged pull requests on GitHub. |
| 3 | +
|
| 4 | + python build_tools/changelog.py |
| 5 | + python build_tools/changelog.py --since-pr 20 |
| 6 | + python build_tools/changelog.py --since-date 2026-06-01 |
| 7 | +
|
| 8 | +Needs the GitHub CLI (``gh``), logged in (``gh auth login``). Queries merged |
| 9 | +PRs against the repository's ``main`` branch on GitHub itself, not local git |
| 10 | +state, so the result does not depend on which branch or commit you happen to |
| 11 | +have checked out. |
| 12 | +
|
| 13 | +A PR's ``bug`` / ``enhancement`` / ``documentation`` / ``maintenance`` label |
| 14 | +decides its section. Many PRs in this repo carry no label, so an unlabeled PR |
| 15 | +falls back to a keyword match on its title. A PR that matches no label and no |
| 16 | +keyword lands in "Needs a label" rather than a guessed section, so labeling |
| 17 | +it and re-running is the fix, not moving it by hand. |
| 18 | +
|
| 19 | +Nothing is written to CHANGELOG.md automatically. Paste the output in |
| 20 | +yourself, replacing the whole "## Unreleased" section each time -- it is |
| 21 | +meant to be regenerated in full, not appended to. Until you rename that |
| 22 | +heading to a real version and start a fresh "## Unreleased" above it, |
| 23 | +nothing counts as released, so a plain re-run always shows every merged PR |
| 24 | +again. Renaming it to a version heading is what makes "--since-pr" (or the |
| 25 | +auto-detected default) start narrowing later runs. |
| 26 | +""" |
| 27 | + |
| 28 | +import argparse |
| 29 | +import json |
| 30 | +from pathlib import Path |
| 31 | +import re |
| 32 | +import shutil |
| 33 | +import subprocess |
| 34 | +import sys |
| 35 | + |
| 36 | +SECTIONS = { |
| 37 | + "bug": "Bug fixes", |
| 38 | + "enhancement": "Enhancements", |
| 39 | + "documentation": "Documentation", |
| 40 | + "maintenance": "Maintenance", |
| 41 | +} |
| 42 | + |
| 43 | +# Not a real GitHub label; where a PR lands when no label and no keyword |
| 44 | +# rule below matched it. Kept separate from SECTIONS so it renders last and |
| 45 | +# is never treated as a labelable category to search for on a PR. |
| 46 | +UNCLASSIFIED = "unclassified" |
| 47 | +UNCLASSIFIED_HEADING = "Needs a label" |
| 48 | + |
| 49 | +# Tried in order against the title, for PRs with no matching label. |
| 50 | +KEYWORD_RULES = [ |
| 51 | + ("bug", re.compile(r"\bfix|bug|hotfix|\bpatch", re.I)), |
| 52 | + ( |
| 53 | + "documentation", |
| 54 | + re.compile(r"\bdocs?\b|readme|tutorial|notebook|changelog|contributing", re.I), |
| 55 | + ), |
| 56 | + ( |
| 57 | + "maintenance", |
| 58 | + re.compile( |
| 59 | + r"\bchore|refactor|cleanup|bump|deps?\b|dependabot|\bci\b|revert" |
| 60 | + r"|merge branch", |
| 61 | + re.I, |
| 62 | + ), |
| 63 | + ), |
| 64 | +] |
| 65 | + |
| 66 | +CHANGELOG_PATH = Path(__file__).resolve().parent.parent / "CHANGELOG.md" |
| 67 | + |
| 68 | + |
| 69 | +def _run(cmd: list[str]) -> str: |
| 70 | + result = subprocess.run(cmd, capture_output=True, text=True) |
| 71 | + if result.returncode != 0: |
| 72 | + sys.exit(f"$ {' '.join(cmd)}\n{result.stderr.strip()}") |
| 73 | + return result.stdout |
| 74 | + |
| 75 | + |
| 76 | +def _require_gh() -> None: |
| 77 | + if shutil.which("gh") is None: |
| 78 | + sys.exit("Needs the GitHub CLI ('gh'). Install it, then 'gh auth login'.") |
| 79 | + if subprocess.run(["gh", "auth", "status"], capture_output=True).returncode != 0: |
| 80 | + sys.exit("'gh' is not logged in. Run 'gh auth login' first.") |
| 81 | + |
| 82 | + |
| 83 | +def _default_repo() -> str: |
| 84 | + """OWNER/REPO, read from the 'origin' remote rather than hardcoded.""" |
| 85 | + url = _run(["git", "remote", "get-url", "origin"]).strip() |
| 86 | + match = re.search(r"github\.com[:/](?P<repo>[^/]+/[^/]+?)(\.git)?$", url) |
| 87 | + if not match: |
| 88 | + sys.exit(f"Could not read a GitHub repo from the 'origin' remote: {url!r}") |
| 89 | + return match.group("repo") |
| 90 | + |
| 91 | + |
| 92 | +def _last_released_pr() -> int | None: |
| 93 | + """Highest PR number under the most recent *released* heading. |
| 94 | +
|
| 95 | + "## Unreleased" is not a release: it gets fully regenerated on every run |
| 96 | + until you rename it to a real version and start a fresh, empty |
| 97 | + "Unreleased" above it. Counting PRs already listed there as "handled" |
| 98 | + would mean a plain re-run stops reporting anything the moment you first |
| 99 | + paste a draft in, which is the wrong direction -- there is nothing to |
| 100 | + protect against re-showing until something has actually shipped. |
| 101 | + """ |
| 102 | + if not CHANGELOG_PATH.exists(): |
| 103 | + return None |
| 104 | + sections = re.split(r"^## (.+)$", CHANGELOG_PATH.read_text(), flags=re.M) |
| 105 | + for heading, body in zip(sections[1::2], sections[2::2]): |
| 106 | + if heading.strip().lower() == "unreleased": |
| 107 | + continue |
| 108 | + numbers = [int(n) for n in re.findall(r"\[#(\d+)\]", body)] |
| 109 | + return max(numbers) if numbers else None |
| 110 | + return None |
| 111 | + |
| 112 | + |
| 113 | +def fetch_merged_prs(repo: str, base: str, limit: int) -> list[dict]: |
| 114 | + out = _run( |
| 115 | + [ |
| 116 | + "gh", |
| 117 | + "pr", |
| 118 | + "list", |
| 119 | + "-R", |
| 120 | + repo, |
| 121 | + "--base", |
| 122 | + base, |
| 123 | + "--state", |
| 124 | + "merged", |
| 125 | + "--limit", |
| 126 | + str(limit), |
| 127 | + "--json", |
| 128 | + "number,title,author,labels,mergedAt,url", |
| 129 | + ] |
| 130 | + ) |
| 131 | + return json.loads(out) |
| 132 | + |
| 133 | + |
| 134 | +def classify(pr: dict) -> str: |
| 135 | + labels = {label["name"] for label in pr["labels"]} |
| 136 | + for key in SECTIONS: |
| 137 | + if key in labels: |
| 138 | + return key |
| 139 | + for key, pattern in KEYWORD_RULES: |
| 140 | + if pattern.search(pr["title"]): |
| 141 | + return key |
| 142 | + return UNCLASSIFIED |
| 143 | + |
| 144 | + |
| 145 | +def render(prs: list[dict], heading: str) -> str: |
| 146 | + all_sections = {**SECTIONS, UNCLASSIFIED: UNCLASSIFIED_HEADING} |
| 147 | + buckets: dict[str, list[dict]] = {key: [] for key in all_sections} |
| 148 | + for pr in prs: |
| 149 | + buckets[classify(pr)].append(pr) |
| 150 | + |
| 151 | + lines = [ |
| 152 | + "<!-- Draft, not a source of truth. A PR's label decides its " |
| 153 | + "section; an unlabeled PR is guessed from its title. Anything " |
| 154 | + "matching neither ends up in 'Needs a label' -- label it on " |
| 155 | + "GitHub and re-run rather than moving it here by hand. -->", |
| 156 | + "", |
| 157 | + f"## {heading}", |
| 158 | + "", |
| 159 | + ] |
| 160 | + for key, section_heading in all_sections.items(): |
| 161 | + entries = buckets[key] |
| 162 | + if not entries: |
| 163 | + continue |
| 164 | + lines.append(f"### {section_heading}") |
| 165 | + lines.append("") |
| 166 | + for pr in entries: |
| 167 | + login = pr["author"].get("login", "unknown") |
| 168 | + lines.append( |
| 169 | + f"- {pr['title']} ([#{pr['number']}]({pr['url']})) " |
| 170 | + f"by [@{login}](https://github.com/{login})" |
| 171 | + ) |
| 172 | + lines.append("") |
| 173 | + |
| 174 | + contributors = sorted( |
| 175 | + {pr["author"]["login"] for pr in prs if not pr["author"].get("is_bot", False)}, |
| 176 | + key=str.lower, |
| 177 | + ) |
| 178 | + if contributors: |
| 179 | + lines.append("### Contributors") |
| 180 | + lines.append("") |
| 181 | + lines.append("Thanks to the following people for this release:") |
| 182 | + lines.append("") |
| 183 | + lines.append( |
| 184 | + ", ".join( |
| 185 | + f"[@{login}](https://github.com/{login})" for login in contributors |
| 186 | + ) |
| 187 | + ) |
| 188 | + lines.append("") |
| 189 | + |
| 190 | + return "\n".join(lines) |
| 191 | + |
| 192 | + |
| 193 | +def main() -> None: |
| 194 | + parser = argparse.ArgumentParser( |
| 195 | + description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter |
| 196 | + ) |
| 197 | + parser.add_argument( |
| 198 | + "--repo", default=None, help="OWNER/REPO. Default: read from 'origin'." |
| 199 | + ) |
| 200 | + parser.add_argument( |
| 201 | + "--base", default="main", help="Base branch merged PRs target. Default: main." |
| 202 | + ) |
| 203 | + parser.add_argument( |
| 204 | + "--since-pr", type=int, default=None, help="Only PRs numbered above this." |
| 205 | + ) |
| 206 | + parser.add_argument( |
| 207 | + "--since-date", default=None, help="Only PRs merged after this (YYYY-MM-DD)." |
| 208 | + ) |
| 209 | + parser.add_argument( |
| 210 | + "--limit", type=int, default=300, help="Max merged PRs to fetch. Default 300." |
| 211 | + ) |
| 212 | + parser.add_argument( |
| 213 | + "--title", default="Unreleased", help="Section heading. Default: Unreleased." |
| 214 | + ) |
| 215 | + parser.add_argument("--output", default=None, help="Also write the markdown here.") |
| 216 | + args = parser.parse_args() |
| 217 | + |
| 218 | + _require_gh() |
| 219 | + repo = args.repo or _default_repo() |
| 220 | + |
| 221 | + since_pr = args.since_pr |
| 222 | + if since_pr is None and args.since_date is None: |
| 223 | + since_pr = _last_released_pr() |
| 224 | + |
| 225 | + prs = fetch_merged_prs(repo, args.base, args.limit) |
| 226 | + |
| 227 | + if since_pr is not None: |
| 228 | + prs = [pr for pr in prs if pr["number"] > since_pr] |
| 229 | + if args.since_date is not None: |
| 230 | + prs = [pr for pr in prs if pr["mergedAt"][:10] > args.since_date] |
| 231 | + |
| 232 | + if not prs: |
| 233 | + sys.exit("No merged PRs in range. Nothing to report.") |
| 234 | + |
| 235 | + prs.sort(key=lambda pr: pr["number"], reverse=True) |
| 236 | + |
| 237 | + markdown = render(prs, args.title) |
| 238 | + print(markdown) |
| 239 | + |
| 240 | + if args.output: |
| 241 | + Path(args.output).write_text(markdown) |
| 242 | + |
| 243 | + |
| 244 | +if __name__ == "__main__": |
| 245 | + main() |
0 commit comments