Repository navigation
fix(release): skip comments and fences in changelog sections #1335
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ### Fixed | ||
|
|
||
| - **Release check respects comments and fences in `CHANGELOG.md`**: `scripts/check_release.py` no longer counts a bullet inside an HTML comment or a fenced code block as a release entry, and `--notes` no longer stops at a `## ` heading inside a fenced example. This affects maintainers cutting a release (#1335, #1290). |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,15 +26,128 @@ | |
| _ENTRY = re.compile(r"^\s*[-*] \S", re.MULTILINE) | ||
|
|
||
|
|
||
| # A backtick fence's info string has no backtick: ```x``` is an inline span. | ||
| _FENCE = re.compile(r"^ {0,3}(`{3,}(?=[^`]*$)|~{3,})") | ||
| _TICKS = re.compile(r"`+") | ||
| _HEADING = re.compile(r"^ {0,3}#{1,6}(?:\s|$)") | ||
| # A heading, a list item or an HTML comment at the start of a line ends the paragraph a code span lives in. | ||
| _BLOCK = re.compile(r"^(?: {0,3}(?:#{1,6}(?:\s|$)|<!--)|\s*[-*+] \S)") | ||
|
|
||
|
|
||
| def _closes(lines: list[str], row: int, col: int, size: int) -> bool: | ||
| """Whether a backtick run of exactly ``size`` follows, within the paragraph.""" | ||
| for number in range(row, len(lines)): | ||
| line = lines[number][col:] if number == row else lines[number] | ||
| if number > row and (not line.strip() or _FENCE.match(line) or _BLOCK.match(line)): | ||
| return False | ||
| if any(len(run[0]) == size for run in _TICKS.finditer(line)): | ||
| return True | ||
| if _HEADING.match(lines[row]): # a heading is one line: no span runs past it | ||
| return False | ||
| return False | ||
|
|
||
|
|
||
| def _inline(lines: list[str], row: int, in_comment: bool, span: int) -> tuple[str, bool, int]: | ||
| """The visible text of ``lines[row]`` and the comment and code span state at its end.""" | ||
| line, text, col = lines[row], [], 0 | ||
| if span: | ||
| text.append("x") | ||
| while col < len(line): | ||
| if in_comment: | ||
| end = line.find("-->", col) | ||
| if end < 0: | ||
| break | ||
| col, in_comment = end + 3, False | ||
| elif span: | ||
| match = next((m for m in _TICKS.finditer(line, col) if len(m[0]) == span), None) | ||
| if match is None: | ||
| break | ||
| col, span = match.end(), 0 | ||
| elif line.startswith("<!--", col): | ||
| col, in_comment = col + 4, True | ||
| elif line[col] == "\\": | ||
| text.append(line[col:col + 2]) | ||
| col += 2 | ||
| elif line[col] == "`": | ||
| size = len(_TICKS.match(line, col)[0]) | ||
| span = size if _closes(lines, row, col + size, size) else 0 | ||
| text.append("x" if span else "`" * size) | ||
| col += size | ||
| else: | ||
| text.append(line[col]) | ||
| col += 1 | ||
| return "".join(text), in_comment, span | ||
|
|
||
|
|
||
| def _scan(lines: list[str]) -> list[tuple[bool, str]]: | ||
| """For each line, whether it starts outside a fence and a comment, and its visible text. | ||
|
|
||
| The text has HTML comments removed and each inline code span (which may | ||
| run over several lines) replaced by ``x``; a fence line, a line inside a | ||
| fence and the part of a line inside a comment have none. Fence markers | ||
| inside a comment, comment markers inside a fence or a code span, and an | ||
| escaped ``\\<`` mean nothing; backticks inside a comment are literal. | ||
| """ | ||
| scanned, opener, in_comment, span = [], "", False, 0 | ||
| for row, line in enumerate(lines): | ||
| if opener: | ||
| fence = _FENCE.match(line) | ||
| scanned.append((False, "")) | ||
| if (fence and fence[1][0] == opener[0] and len(fence[1]) >= len(opener) | ||
| and not line.strip().strip(opener[0])): | ||
| opener = "" | ||
| continue | ||
| fence = None if in_comment or span else _FENCE.match(line) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Should fix: A run shows it; it was not judged to be met in ordinary use. Shown by running: exit 1 (at the head) · exit 0 (at the merge base) · exit 0 (at the head with the fix)
What the proof does: Calls release_notes on a section with a fence opened on a list-item line and checks the notes do not include the next version. The problem shows as the output
import sys
sys.path.insert(0, 'scripts')
import check_release as c
text = "## [0.2.0]\n\n- ```markdown\n example\n ```\n\n- Real entry.\n\n## [0.1.0]\n- Old entry.\n"
notes = c.release_notes(text, '0.2.0')
if notes is None or 'Old entry' in notes:
print('LIST_FENCE_LEAK', repr(notes))
sys.exit(1) |
||
| if fence: | ||
| opener = fence[1] | ||
| scanned.append((False, "")) | ||
| continue | ||
| starts_visible = not in_comment | ||
| text, in_comment, span = _inline(lines, row, in_comment, span) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Trigger: Impact: Evidence: the four-space code line is not a fence, so this call passes it to Fix: Handle indented code before inline comment parsing and add acceptance/extraction regressions. Workaround: use a fenced example. Should fix: A run shows it; it was not judged to be met in ordinary use. Shown by running: exit 1 (at the head) · exit 1 (at the merge base, which cannot run it) · exit 0 (at the head with the fix)
What the proof does: Calls release_notes and _section_has_entry on a changelog whose section has an indented literal comment opener, and checks the notes stop at the next version and the entry is seen. The problem shows as the output
import sys
sys.path.insert(0, 'scripts')
import check_release as c
text = "## [0.2.0]\n\n <!- - literal example\n\n- Real entry.\n\n## [0.1.0]\n- Old entry.\n"
notes = c.release_notes(text, '0.2.0')
ok = notes is not None and 'Old entry' not in notes and c._section_has_entry(text, '0.2.0')
if not ok:
print('INDENTED_COMMENT_LEAK', repr(notes), c._section_has_entry(text, '0.2.0'))
sys.exit(1) |
||
| scanned.append((starts_visible, text)) | ||
| return scanned | ||
|
|
||
|
|
||
| def _section(changelog: str, version: str) -> tuple[list[str], int, int, list[tuple[bool, str]]] | None: | ||
| lines = changelog.split("\n") | ||
| scanned = _scan(lines) | ||
| start = re.compile(rf"^## \[{re.escape(version)}\]") | ||
| begin = next((i for i, line in enumerate(lines) if scanned[i][0] and start.match(line)), None) | ||
| if begin is None: | ||
| return None | ||
| end = next((i for i in range(begin + 1, len(lines)) | ||
| if scanned[i][0] and lines[i].startswith(("## ", "<a id="))), len(lines)) | ||
| return lines, begin, end, scanned | ||
|
|
||
|
|
||
| def release_notes(changelog: str, version: str) -> str | None: | ||
| """The body of the ``## [version]`` section, or None if there is none. | ||
|
|
||
| The section ends at the next heading or at the ``<a id=...>`` anchor scriv | ||
| writes above each version. | ||
| writes above each version. A heading or anchor inside a fenced code block | ||
| or an HTML comment does not end it. | ||
| """ | ||
| match = re.search(rf"^## \[{re.escape(version)}\][^\n]*\n(.*?)(?=^## |^<a id=|\Z)", | ||
| changelog, re.MULTILINE | re.DOTALL) | ||
| return match.group(1).strip() if match else None | ||
| section = _section(changelog, version) | ||
| if section is None: | ||
| return None | ||
| lines, begin, end, _ = section | ||
| body = "\n".join(lines[begin + 1:end]) | ||
|
vchamarthi marked this conversation as resolved.
Resolved
|
||
| return re.sub(r"\A(?:[ \t]*\n)+", "", body).rstrip() # keep the first line's indent | ||
|
|
||
|
|
||
| def _entries(scanned: list[tuple[bool, str]]) -> bool: | ||
| return any(_ENTRY.search(text) for visible, text in scanned if visible) | ||
|
|
||
|
|
||
| def _has_entry(notes: str) -> bool: | ||
| """Whether the notes hold a bullet outside HTML comments and fenced code blocks.""" | ||
| return _entries(_scan(notes.split("\n"))) | ||
|
|
||
|
|
||
| def _section_has_entry(changelog: str, version: str) -> bool: | ||
| """Like ``_has_entry`` on the section's notes, but a comment opened on its heading still counts.""" | ||
| section = _section(changelog, version) | ||
| return section is not None and _entries(section[3][section[1] + 1:section[2]]) | ||
|
|
||
|
|
||
| def problems(tag: str, project: Path = PROJECT) -> tuple[list[str], str | None]: | ||
|
|
@@ -43,8 +156,9 @@ def problems(tag: str, project: Path = PROJECT) -> tuple[list[str], str | None]: | |
| found = [] | ||
| if tag != f"v{version}": | ||
| found.append(f"tag {tag!r} does not match project.version {version!r} (want v{version})") | ||
| notes = release_notes((project / "CHANGELOG.md").read_text(encoding="utf-8"), version) | ||
| if not notes or not _ENTRY.search(notes): | ||
| changelog = (project / "CHANGELOG.md").read_text(encoding="utf-8") | ||
| notes = release_notes(changelog, version) | ||
| if not notes or not _section_has_entry(changelog, version): | ||
| found.append(f"CHANGELOG.md has no entries under '## [{version}]': " | ||
| f"run `scriv collect --version {version}`") | ||
| pending = sorted(p.name for p in (project / "changelog.d").glob("*.md") if p.name != "README.md") | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stop code-span matching at ordered-list boundaries too.
Trigger: a release body of
Exampleliteral\n1. <!-- draft\n - Hidden entry\n -->.Impact: a section with no visible release bullet passes validation.
Evidence:
_BLOCKomits ordered markers, so_closespairs the paragraph's backtick with the backtick inside the next list item's HTML comment._inlineconsequently skips the real comment opener, and_entriescounts- Hidden entry. In Markdown,1.interrupts the preceding paragraph and the list item contains an HTML comment block, not a cross-paragraph code span. Recognize ordered-list interruptions and add a regression throughproblems().Workaround: insert a blank line before
1..Should fix: A run shows it; it was not judged to be met in ordinary use.
Shown by running: exit 1 (at the head) · exit 1 (at the merge base, which cannot run it) · exit 0 (at the head with the fix)
python scripts/proof_c2.pyWhat the proof does: Checks that a bullet inside an HTML comment opened on an ordered list item is not counted as a release entry.
The problem shows as the output
ORDERED_LIST_SPAN_LEAK.dokimi/scripts/proof_c2.py