30 lines
1.6 KiB
Python
30 lines
1.6 KiB
Python
"""The changelog edit behind todo-item finish: adding a bullet to CHANGELOG.md."""
|
|
import re
|
|
|
|
from failure import Failure
|
|
from plan.release import UNRELEASED_HEADING
|
|
|
|
UNRELEASED = "## Unreleased"
|
|
# UNRELEASED_HEADING with the blank lines below it, after which a bullet goes first.
|
|
UNRELEASED_SECTION = re.compile(UNRELEASED_HEADING.pattern + r"\n+", UNRELEASED_HEADING.flags)
|
|
|
|
|
|
def with_bullet(changelog: str, bullet: str) -> str | Failure:
|
|
"""changelog with bullet first under its UNRELEASED_SECTION, adding ## Unreleased above the first release heading where it has none; a new changelog where changelog is empty. Failure where that section holds ### subsections, since the bullet belongs under one of them."""
|
|
text = bullet.strip().removeprefix("- ").strip()
|
|
if not text:
|
|
return Failure("the changelog bullet is empty")
|
|
if "\n" in text:
|
|
return Failure("the changelog bullet holds a line break; keep it on one line")
|
|
line = f"- {text}"
|
|
if not changelog.strip():
|
|
return f"# Changelog\n\n{UNRELEASED}\n\n{line}\n"
|
|
if section := UNRELEASED_SECTION.search(changelog):
|
|
rest = changelog[section.end():]
|
|
if re.search(r"^### ", re.split(r"^## ", rest, maxsplit=1, flags=re.M)[0], re.M):
|
|
return Failure(f"CHANGELOG.md's {section.group().strip()} holds ### subsections; add the bullet under the right one by hand, then finish with --no-changelog")
|
|
return changelog[:section.end()] + line + "\n" + ("" if rest.startswith("- ") or not rest else "\n") + rest
|
|
if release := re.search(r"^## ", changelog, re.M):
|
|
return changelog[:release.start()] + f"{UNRELEASED}\n\n{line}\n\n" + changelog[release.start():]
|
|
return changelog.rstrip("\n") + f"\n\n{UNRELEASED}\n\n{line}\n"
|