-
Notifications
You must be signed in to change notification settings - Fork 289
Add script to draft release notes from GitHub API #8934
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tautschnig
wants to merge
1
commit into
diffblue:develop
Choose a base branch
from
tautschnig:fix-7907-create-changelog
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| #!/usr/bin/env python3 | ||
| """ | ||
| Generate draft release notes for a CBMC release. | ||
|
|
||
| Calls the GitHub release-notes generation endpoint (the same one behind | ||
| the "Generate release notes" button in the GitHub UI) and prepends a | ||
| draft summary paragraph derived from the PR titles. | ||
|
|
||
| The tag need not exist yet; when auto-detecting the previous tag the | ||
| script falls back to the latest existing tag. | ||
|
|
||
| Usage: | ||
| scripts/draft_release_notes.py cbmc-6.8.0 | ||
| scripts/draft_release_notes.py cbmc-6.8.0 --previous cbmc-6.7.1 | ||
| """ | ||
|
|
||
| import json | ||
| import re | ||
| import subprocess | ||
| import sys | ||
| import textwrap | ||
|
|
||
|
|
||
| def gh_generate_notes(tag: str, previous: str, repo: str) -> str: | ||
| """Call the GitHub generate-notes API via `gh`.""" | ||
| cmd = [ | ||
| "gh", "api", f"repos/{repo}/releases/generate-notes", | ||
| "-f", f"tag_name={tag}", | ||
| "-f", f"previous_tag_name={previous}", | ||
| ] | ||
| result = subprocess.run(cmd, capture_output=True, text=True, check=True) | ||
| return json.loads(result.stdout)["body"] | ||
|
|
||
|
|
||
| def previous_tag(tag: str, repo: str) -> str: | ||
| """Find the tag immediately before *tag* using `gh`.""" | ||
| cmd = [ | ||
| "gh", "api", f"repos/{repo}/tags", | ||
| "--paginate", "-q", ".[].name", | ||
| ] | ||
| result = subprocess.run(cmd, capture_output=True, text=True, check=True) | ||
| tags = [t for t in result.stdout.splitlines() if t.startswith("cbmc-")] | ||
|
|
||
| def version_key(t): | ||
| parts = t.split("-", 1)[1].split(".") | ||
| nums = [] | ||
| for p in parts: | ||
| m = re.match(r"(\d+)", p) | ||
| nums.append(int(m.group(1)) if m else 0) | ||
| return nums | ||
|
|
||
| tags.sort(key=version_key, reverse=True) | ||
| for i, t in enumerate(tags): | ||
| if t == tag and i + 1 < len(tags): | ||
| return tags[i + 1] | ||
| # The new tag may not exist yet; fall back to the latest existing tag. | ||
| if tags: | ||
| return tags[0] | ||
| sys.exit(f"Cannot find a tag before {tag}") | ||
|
|
||
|
|
||
| def version_from_tag(tag: str) -> str: | ||
| return tag.split("-", 1)[1] | ||
|
|
||
|
|
||
| # Patterns for changes that are NOT user-facing | ||
| _SKIP = re.compile( | ||
| r"(?i)" | ||
| r"bump |dependabot|" | ||
| r"\bCI\b|ci:|ci job|GitHub Action|runner|" | ||
| r"Compile Java regression|" | ||
| r"CODEOWNERS|" | ||
| r"clang-format|" | ||
| r"Release CBMC|" | ||
| r"Merge pull request" | ||
| ) | ||
|
|
||
| # Patterns that suggest a user-visible feature (not just a fix/refactor) | ||
| _FEATURE = re.compile( | ||
| r"(?i)" | ||
| r"\badd\b|" | ||
| r"\bimplement\b|" | ||
| r"\bintroduce\b|" | ||
| r"\bsupport\b|" | ||
| r"\bnew\b|" | ||
| r"\benable\b" | ||
| ) | ||
|
|
||
|
|
||
| def draft_summary(notes: str, version: str) -> str: | ||
| """Build a one-paragraph draft summary from the generated notes. | ||
|
|
||
| Strategy: pick the top user-visible feature PRs and mention them. | ||
| This is a *draft* — the release manager should edit it. | ||
| """ | ||
| # Extract PR lines: "* <title> by @author in <url>" | ||
| pr_lines = [ | ||
| line.strip() | ||
| for line in notes.splitlines() | ||
| if line.strip().startswith("* ") | ||
| ] | ||
|
|
||
| # Filter to user-facing changes | ||
| visible = [l for l in pr_lines if not _SKIP.search(l)] | ||
| features = [l for l in visible if _FEATURE.search(l)] | ||
| fixes = [l for l in visible if l not in features] | ||
|
|
||
| # Extract short title + PR number for highlights | ||
| def extract(line: str): | ||
| m = re.match(r"\*\s+(.+?)\s+by\s+@", line) | ||
| title = m.group(1) if m else line.lstrip("* ") | ||
| m2 = re.search(r"/pull/(\d+)", line) | ||
| pr = m2.group(1) if m2 else None | ||
| return title, pr | ||
|
|
||
| highlights = [] | ||
| for line in (features or visible)[:3]: | ||
| title, pr = extract(line) | ||
| ref = f" (via #{pr})" if pr else "" | ||
| highlights.append(f"{title}{ref}") | ||
|
|
||
| n_fixes = len(fixes) | ||
| fix_note = "" | ||
| if n_fixes: | ||
| fix_note = ( | ||
| f" The release also includes {n_fixes} other change" | ||
| f"{'s' if n_fixes != 1 else ''}." | ||
| ) | ||
|
tautschnig marked this conversation as resolved.
|
||
|
|
||
| if not highlights: | ||
| return f"<!-- TODO: write a summary for CBMC {version} -->\n" | ||
|
|
||
| if len(highlights) == 1: | ||
| body = highlights[0] | ||
| elif len(highlights) == 2: | ||
| body = f"{highlights[0]} and {highlights[1]}" | ||
| else: | ||
| body = f"{highlights[0]}, {highlights[1]}, and {highlights[2]}" | ||
|
|
||
| return ( | ||
| f"<!-- DRAFT — please review and edit this summary -->\n" | ||
| f"This release includes {body}.{fix_note}\n" | ||
| ) | ||
|
|
||
|
|
||
| def format_release_notes(notes: str, version: str) -> str: | ||
| """Combine a header, draft summary, and the GitHub-generated body.""" | ||
| raw = draft_summary(notes, version) | ||
| # Keep the HTML comment on its own line; only wrap the prose. | ||
| lines = raw.split("\n", 1) | ||
| if len(lines) == 2 and lines[0].startswith("<!--"): | ||
| summary = lines[0] + "\n" + textwrap.fill(lines[1], width=80) | ||
| else: | ||
| summary = textwrap.fill(raw, width=80) | ||
| return f"# CBMC {version}\n\n{summary}\n\n{notes}\n" | ||
|
|
||
|
|
||
| def main(): | ||
| import argparse | ||
| p = argparse.ArgumentParser( | ||
| description="Generate draft CHANGELOG entry for a CBMC release" | ||
| ) | ||
| p.add_argument("tag", help="Release tag, e.g. cbmc-6.8.0") | ||
| p.add_argument("--previous", help="Previous release tag (auto-detected)") | ||
| p.add_argument("--repo", default="diffblue/cbmc") | ||
| p.add_argument( | ||
| "-o", "--output", | ||
| help="Write to file instead of stdout", | ||
| ) | ||
| args = p.parse_args() | ||
|
|
||
| prev = args.previous or previous_tag(args.tag, args.repo) | ||
| ver = version_from_tag(args.tag) | ||
|
|
||
| print(f"Generating notes for {args.tag} (since {prev})...", | ||
| file=sys.stderr) | ||
|
|
||
| notes = gh_generate_notes(args.tag, prev, args.repo) | ||
| output = format_release_notes(notes, ver) | ||
|
|
||
| if args.output: | ||
| with open(args.output, "w") as f: | ||
| f.write(output) | ||
| print(f"Written to {args.output}", file=sys.stderr) | ||
| else: | ||
| print(output) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.