Repository navigation
REVIEW FIRST! Collect changelog entries from per-PR fragments #985
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,36 @@ | ||
| # Maintenance tasks for rope; a thin wrapper around | ||
| # bin/changelog.py and the Sphinx documentation. Kept portable | ||
| # between GNU make and BSD make. | ||
|
|
||
| PYTHON ?= python3 | ||
| CHANGELOG = $(PYTHON) bin/changelog.py | ||
| ENTRIES != ls changelog.d/[0-9]*.md 2>/dev/null || true | ||
|
|
||
| .PHONY: help changelog-add changelog-preview changelog docs release FORCE | ||
|
|
||
| help: | ||
| @echo "make changelog-add N=<issue/PR> [TEXT=...] add changelog.d/N.md" | ||
| @echo "make changelog-preview show the next release section" | ||
| @echo "make changelog fold entries into CHANGELOG.md" | ||
| @echo "make docs build the HTML documentation" | ||
| @echo "make release changelog + docs" | ||
|
|
||
| changelog-add: | ||
| @$(CHANGELOG) add "$$N" "$$TEXT" | ||
|
|
||
| changelog-preview: | ||
| @$(CHANGELOG) preview | ||
|
|
||
| changelog: CHANGELOG.md | ||
|
|
||
| # Collecting removes the entries, so any entry left means | ||
| # CHANGELOG.md is out of date, whatever the timestamps say. | ||
| CHANGELOG.md: $(ENTRIES) $(ENTRIES:%=FORCE) | ||
| @$(CHANGELOG) collect | ||
|
|
||
| FORCE: | ||
|
|
||
| docs: | ||
| cd docs && $(MAKE) html | ||
|
|
||
| release: changelog docs |
|
mcepl marked this conversation as resolved.
Resolved
|
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,214 @@ | ||
| #!/usr/bin/env python3 | ||
| """Manage changelog entries in changelog.d/ (see changelog.d/README.md).""" | ||
|
|
||
| import argparse | ||
| import os | ||
| import re | ||
| import shlex | ||
| import shutil | ||
| import subprocess | ||
| import sys | ||
| import tempfile | ||
| from pathlib import Path | ||
|
|
||
| ROOT = Path(__file__).resolve().parent.parent | ||
| ENTRIES_DIR = ROOT / "changelog.d" | ||
| TEMPLATE = ENTRIES_DIR / "TEMPLATE.md" | ||
| CHANGELOG = ROOT / "CHANGELOG.md" | ||
| PYPROJECT = ROOT / "pyproject.toml" | ||
|
|
||
| # Entries live in changelog.d/<N>.md (or <N>-<suffix>.md), one or | ||
| # more per pull request, and are only folded into CHANGELOG.md at | ||
| # release time, so that pull requests do not all conflict on the | ||
| # top of CHANGELOG.md. | ||
|
|
||
|
|
||
| class ChangelogError(Exception): | ||
| pass | ||
|
|
||
|
|
||
| def read_text(path): | ||
| return path.read_text(encoding="utf-8") | ||
|
|
||
|
|
||
| def write_text(path, text): | ||
| path.write_text(text, encoding="utf-8", newline="\n") | ||
|
|
||
|
|
||
| def version(): | ||
| match = re.search( | ||
| r"""^version\s*=\s*['"]([^'"]+)['"]""", read_text(PYPROJECT), re.M | ||
| ) | ||
| if match is None: | ||
| raise ChangelogError(f"Cannot find version in {PYPROJECT.name}") | ||
| return match.group(1) | ||
|
|
||
|
|
||
| def entry_key(path): | ||
| """Sort by number, then 1234.md, 1234-2.md, …, 1234-10.md""" | ||
| number = re.match(r"\d+", path.stem).group() | ||
| suffix = path.stem[len(number) :] | ||
| return int(number), len(suffix), suffix | ||
|
|
||
|
|
||
| def entry_files(): | ||
| """Return the entry files, sorted by issue/PR number""" | ||
| return sorted( | ||
| ( | ||
| path | ||
| for path in ENTRIES_DIR.glob("*.md") | ||
| if path.name[0].isdigit() and path.is_file() | ||
| ), | ||
| key=entry_key, | ||
| ) | ||
|
|
||
|
|
||
| def entry_path(n): | ||
| path = ENTRIES_DIR / f"{n}.md" | ||
| suffix = 2 | ||
| while path.exists(): | ||
| path = ENTRIES_DIR / f"{n}-{suffix}.md" | ||
| suffix += 1 | ||
| return path | ||
|
|
||
|
|
||
| def editor_command(): | ||
| """Return the user's editor as an argument list | ||
|
|
||
| Like git, prefer $VISUAL and $EDITOR, then git's own editor | ||
| (core.editor), and fall back to notepad or vi. | ||
| """ | ||
| editor = os.environ.get("VISUAL") or os.environ.get("EDITOR") | ||
| if not editor and shutil.which("git"): | ||
| try: | ||
| editor = subprocess.run( | ||
| ["git", "var", "GIT_EDITOR"], | ||
| cwd=ROOT, | ||
| capture_output=True, | ||
| text=True, | ||
| check=True, | ||
| ).stdout.strip() | ||
| except subprocess.CalledProcessError: | ||
| editor = None | ||
| if not editor: | ||
| editor = "notepad" if os.name == "nt" else "vi" | ||
| if os.name == "nt": | ||
| # Keep backslashes in Windows paths, just strip the quotes. | ||
| return [arg.strip('"') for arg in shlex.split(editor, posix=False)] | ||
| return shlex.split(editor) | ||
|
|
||
|
|
||
| def clean_entry(text): | ||
| """Drop HTML comments, trailing whitespace and blank lines around""" | ||
| text = re.sub(r"<!--.*?-->", "", text, flags=re.S) | ||
| lines = [line.rstrip() for line in text.splitlines()] | ||
| return "\n".join(lines).strip("\n") | ||
|
|
||
|
|
||
| def cmd_add(n, text=None): | ||
| if not re.fullmatch(r"\d+", n): | ||
| raise ChangelogError(f"Not an issue/PR number: {n!r}") | ||
|
|
||
| if text: | ||
| entry = f"- #{n} {text}" | ||
| else: | ||
| template = read_text(TEMPLATE).replace("NNNN", n) | ||
| # Edit in a private directory, so that an aborted edit | ||
| # leaves nothing behind and the editor still sees a *.md | ||
| # file name. | ||
| with tempfile.TemporaryDirectory(prefix="changelog.") as tmpdir: | ||
| tmp = Path(tmpdir) / f"{n}.md" | ||
| write_text(tmp, template) | ||
| try: | ||
| subprocess.run([*editor_command(), str(tmp)], check=True) | ||
| except (OSError, subprocess.CalledProcessError) as err: | ||
| raise ChangelogError(f"Running the editor failed: {err}") from err | ||
| entry = read_text(tmp) | ||
| if clean_entry(entry) == clean_entry(template): | ||
| raise ChangelogError("Entry left unchanged, nothing written.") | ||
| entry = clean_entry(entry) | ||
|
|
||
| if not re.sub(rf"^- #{n}", "", entry).strip(): | ||
| raise ChangelogError("Empty entry, nothing written.") | ||
|
|
||
| path = entry_path(n) | ||
| write_text(path, entry + "\n") | ||
| print(f"Created {path.relative_to(ROOT).as_posix()}:") | ||
| print(entry) | ||
|
|
||
|
|
||
| def release_section(): | ||
| files = entry_files() | ||
| if not files: | ||
| raise ChangelogError(f"No entries in {ENTRIES_DIR.name}/") | ||
| entries = "".join(clean_entry(read_text(path)) + "\n" for path in files) | ||
| return f"# Release {version()}\n\n{entries}", files | ||
|
|
||
|
|
||
| def cmd_preview(): | ||
| section, _ = release_section() | ||
| sys.stdout.write(section) | ||
|
|
||
|
|
||
| def cmd_collect(): | ||
| section, files = release_section() | ||
| heading = section.splitlines()[0] | ||
| changelog = read_text(CHANGELOG) | ||
| if heading in changelog.splitlines(): | ||
| raise ChangelogError( | ||
| f"{CHANGELOG.name} already has {heading!r}; " | ||
| f"bump version in {PYPROJECT.name} first" | ||
| ) | ||
|
|
||
| # Keep whatever precedes the first release (the header). | ||
| match = re.search(r"^# Release ", changelog, re.M) | ||
| pos = match.start() if match else len(changelog) | ||
| write_text(CHANGELOG, f"{changelog[:pos]}{section}\n{changelog[pos:]}") | ||
| for path in files: | ||
| path.unlink() | ||
| try: | ||
| subprocess.run( | ||
| ["git", "add", "-A", ENTRIES_DIR.name, CHANGELOG.name], | ||
| cwd=ROOT, | ||
| check=True, | ||
| ) | ||
| except (OSError, subprocess.CalledProcessError) as err: | ||
| print(f"Warning: git add failed: {err}", file=sys.stderr) | ||
| print(f"{CHANGELOG.name} updated for {version()}; review and commit.") | ||
|
|
||
|
|
||
| def parse_args(argv): | ||
| parser = argparse.ArgumentParser(prog="changelog.py", description=__doc__) | ||
| commands = parser.add_subparsers(dest="command", required=True) | ||
| add = commands.add_parser( | ||
| "add", | ||
| help="add changelog.d/N.md (the default command)", | ||
| description="Add changelog.d/N.md, in an editor unless TEXT is given.", | ||
| ) | ||
| add.add_argument("n", metavar="N", help="issue or pull request number") | ||
| add.add_argument("text", metavar="TEXT", nargs="?", help="the entry text") | ||
| commands.add_parser("preview", help="show the next release section") | ||
| commands.add_parser("collect", help="fold the entries into CHANGELOG.md") | ||
| # `changelog.py 1234 ...` is short for `changelog.py add 1234 ...` | ||
| if argv and argv[0].isdigit(): | ||
| argv = ["add", *argv] | ||
| return parser.parse_args(argv) | ||
|
|
||
|
|
||
| def main(argv): | ||
| args = parse_args(argv) | ||
| try: | ||
| if args.command == "add": | ||
| cmd_add(args.n, args.text) | ||
| elif args.command == "preview": | ||
| cmd_preview() | ||
| else: | ||
| cmd_collect() | ||
| except ChangelogError as err: | ||
| print(err, file=sys.stderr) | ||
| return 1 | ||
| return 0 | ||
|
mcepl marked this conversation as resolved.
Resolved
|
||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| sys.exit(main(sys.argv[1:])) | ||
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 @@ | ||
| - #223 Add regression tests for restructuring and patchedast with `elif(` written without a space (@mcepl) |
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,5 @@ | ||
| - #985 Collect changelog entries from `changelog.d/` instead of | ||
| editing `CHANGELOG.md` directly, so that pull requests no longer | ||
| conflict with each other on the top of the changelog. Add yours | ||
| with `python bin/changelog.py <N>`, see `changelog.d/README.md`. | ||
| (@mcepl) |
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,31 @@ | ||
| # Changelog entries | ||
|
|
||
| Every pull request adds its changelog entry as a file in this | ||
| directory, named after the issue or pull request number, e.g. | ||
| `changelog.d/1234.md`. The entries are collected into | ||
| `CHANGELOG.md` when a new version is released. | ||
|
|
||
| ## How to add an entry | ||
|
|
||
| Run | ||
|
|
||
| ```sh | ||
| python bin/changelog.py 1234 | ||
| ``` | ||
|
|
||
| It opens a text editor with `TEMPLATE.md` and saves the result | ||
| as `changelog.d/1234.md`. To use a different editor than the one | ||
| it picks, set the `VISUAL` or `EDITOR` environment variable. Or | ||
| give the text on the command line and skip the editor: | ||
|
|
||
| ```sh | ||
| python bin/changelog.py 1234 'Fix renaming of keyword arguments (@yourname)' | ||
| ``` | ||
|
|
||
| Alternatively, copy `TEMPLATE.md` to `changelog.d/1234.md` and | ||
| edit it by hand. `TEMPLATE.md` also describes how to write the | ||
| entry. | ||
|
|
||
| `python bin/changelog.py preview` shows how the entries will look | ||
| in the next release; `python bin/changelog.py --help` lists all | ||
| commands. |
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,15 @@ | ||
| - #NNNN Describe what changed, why, and what users of rope may | ||
| notice. (@your-github-handle) | ||
|
|
||
| <!-- | ||
| Replace NNNN with the issue or pull request number and save this | ||
| file as changelog.d/NNNN.md. | ||
|
|
||
| The entry is a Markdown list item starting with the issue/PR | ||
| number; several related ones may be listed together, e.g. | ||
| "- #826, #940 ...". Wrap long lines and indent continuation lines | ||
| by two spaces. Do not hesitate to write more than a one-liner. | ||
| Credit yourself at the end. | ||
|
|
||
| This comment is removed when the entries are collected. | ||
| --> |
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
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.