diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index 73f482297..f1c5e43fe 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -7,6 +7,6 @@ Fixes #(issue) # Checklist (delete if not relevant): - [ ] I have added tests that prove my fix is effective or that my feature works -- [ ] I have updated CHANGELOG.md +- [ ] I have added a changelog entry to `changelog.d/` (see `changelog.d/README.md`) - [ ] I have made corresponding changes to user documentation for new features - [ ] I have made corresponding changes to library documentation for API changes diff --git a/CHANGELOG.md b/CHANGELOG.md index 4bb036f7f..8230eb963 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ -# **Upcoming release** - -- #223 Add regression tests for restructuring and patchedast with `elif(` written without a space (@mcepl) - + # Release 1.16.0 diff --git a/Makefile b/Makefile new file mode 100644 index 000000000..6b558716b --- /dev/null +++ b/Makefile @@ -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= [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 diff --git a/bin/changelog.py b/bin/changelog.py new file mode 100755 index 000000000..843822b6c --- /dev/null +++ b/bin/changelog.py @@ -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/.md (or -.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 + + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:])) diff --git a/changelog.d/223.md b/changelog.d/223.md new file mode 100644 index 000000000..0d98c1bac --- /dev/null +++ b/changelog.d/223.md @@ -0,0 +1 @@ +- #223 Add regression tests for restructuring and patchedast with `elif(` written without a space (@mcepl) diff --git a/changelog.d/985.md b/changelog.d/985.md new file mode 100644 index 000000000..7bce3abf7 --- /dev/null +++ b/changelog.d/985.md @@ -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 `, see `changelog.d/README.md`. + (@mcepl) diff --git a/changelog.d/README.md b/changelog.d/README.md new file mode 100644 index 000000000..55866b847 --- /dev/null +++ b/changelog.d/README.md @@ -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. diff --git a/changelog.d/TEMPLATE.md b/changelog.d/TEMPLATE.md new file mode 100644 index 000000000..8191c969b --- /dev/null +++ b/changelog.d/TEMPLATE.md @@ -0,0 +1,15 @@ +- #NNNN Describe what changed, why, and what users of rope may + notice. (@your-github-handle) + + diff --git a/docs/Makefile b/docs/Makefile index d4bb2cbb9..b44be6e48 100644 --- a/docs/Makefile +++ b/docs/Makefile @@ -12,7 +12,12 @@ BUILDDIR = _build help: @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) -.PHONY: help Makefile +.PHONY: help html Makefile + +# Explicit rule, so that `make html` (used by the top-level +# Makefile) works with BSD make too, which lacks pattern rules. +html: + @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) # Catch-all target: route all unknown targets to Sphinx using the new # "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). diff --git a/docs/contributing.rst b/docs/contributing.rst index 796699986..9c402df37 100644 --- a/docs/contributing.rst +++ b/docs/contributing.rst @@ -112,6 +112,15 @@ Follow the instructions on GitHub_ on how to setup Git and fork the `python-rope/rope`_ repository. Once your changes are ready, send a `pull request`_ for review. +Every pull request should add a changelog entry, a file in +``changelog.d/`` named after the issue or pull request number. To +create it, run:: + + python bin/changelog.py 1234 + +See ``changelog.d/README.md`` for details. Please do not edit +``CHANGELOG.md`` directly. + Programming Style ----------------- diff --git a/docs/release-process.rst b/docs/release-process.rst index 87c356864..b3f386129 100644 --- a/docs/release-process.rst +++ b/docs/release-process.rst @@ -10,15 +10,16 @@ Pre-Release Release ------- -1. Ensure tickets assigned to Milestones are up to date -2. Update ``CHANGELOG.md`` +1. Increment version number in ``pyproject.toml`` and collect the + changelog entries: ``python bin/changelog.py collect`` +2. Ensure tickets assigned to Milestones are up to date and match + ``CHANGELOG.md`` 3. Close milestone -4. Increment version number in ``pyproject.toml`` -5. `git commit && git push` -6. Tag the release with the tag annotation containing the release information, +4. ``git commit -a && git push`` +5. Tag the release with the tag annotation containing the release information, ``python bin/tag-release.py`` -7. Create Github Release -8. Publish release announcements to GitHub Discussions +6. Create Github Release +7. Publish release announcements to GitHub Discussions Release Schedule