Skip to content

Commit fa674e2

Browse files
committed
add_pr_links: append the PR reference at the end of the entry
Move the appended reference to the very end of an entry (after any trailing period) instead of before the period, and only skip an entry that already *ends* with a PR reference. A prior PR or issue cited in the body (e.g. "Fixes #6030: …") no longer blocks appending the PR link at the end. Co-authored-by: Isaac
1 parent 29e883e commit fa674e2

1 file changed

Lines changed: 29 additions & 24 deletions

File tree

‎tools/add_pr_links.py‎

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
A fragment holds one entry per `* `/`- ` line, or a single entry when it has no
1414
such marker (see `render_nextchanges` in `internal/genkit/release_tagging.py`,
1515
which bullets the first line and passes continuation lines through). The
16-
reference goes at the end of an entry, so an entry wrapped over several lines
17-
gets it on the last line rather than mid-sentence.
16+
reference is appended at the very end of an entry, so an entry wrapped over
17+
several lines gets it on the last line.
1818
"""
1919

2020
import argparse
@@ -24,11 +24,15 @@
2424
# A `* `/`- ` line starts a new entry; see the module docstring.
2525
ENTRY_MARKER_RE = re.compile(r"^\s*[*-] ")
2626

27-
# Any `#1234` counts as a reference, raw or already expanded into a link: the
28-
# author pointed at a PR (or a related issue) themselves, so leave the entry
29-
# alone. This is also what makes a re-run a no-op, so the workflow's own push
30-
# cannot retrigger itself into a loop.
31-
EXISTING_REF_RE = re.compile(r"#\d+")
27+
# A reference already at the *end* of an entry, either the raw `(#1234)` this
28+
# tool writes or the `([#1234](…/pull/1234))` link update_github_links.py
29+
# expands it to. Only a trailing reference makes us leave the entry alone; a
30+
# `#1234` earlier in the body (e.g. "Fixes #6030: …") does not, so the PR is
31+
# still appended at the end. Matching both forms is also what makes a re-run a
32+
# no-op, so the workflow's own push cannot retrigger itself into a loop. A pull
33+
# *link* is required rather than any `([#…](…))`, so an issue link at the end
34+
# doesn't block the PR reference; a trailing period is tolerated.
35+
END_REF_RE = re.compile(r"(?:\(#\d+\)|\(\[#\d+\]\([^)]*/pull/\d+\)\))\s*\.?\s*$")
3236

3337

3438
def entry_ranges(lines):
@@ -48,48 +52,49 @@ def entry_ranges(lines):
4852

4953

5054
def append_reference(line, pr):
51-
"""Append ``(#pr)`` to one line, before its trailing period.
52-
53-
Existing entries in CHANGELOG.md put the link inside the sentence rather
54-
than after it, e.g. ``… on every run ([#6060](…)).``
55+
"""Append ``(#pr)`` to the very end of one line.
5556
5657
>>> append_reference("Added the `databricks quickstart` command.", 1234)
57-
'Added the `databricks quickstart` command (#1234).'
58+
'Added the `databricks quickstart` command. (#1234)'
5859
>>> append_reference("* No trailing period", 1234)
5960
'* No trailing period (#1234)'
6061
"""
61-
body = line.rstrip()
62-
if body.endswith("."):
63-
return f"{body[:-1]} (#{pr})."
64-
return f"{body} (#{pr})"
62+
return f"{line.rstrip()} (#{pr})"
6563

6664

6765
def annotate_text(text, pr):
6866
r"""Append ``(#pr)`` to every entry in a fragment that has no reference.
6967
7068
>>> annotate_text("Added the `databricks quickstart` command.\n", 1234)
71-
'Added the `databricks quickstart` command (#1234).\n'
69+
'Added the `databricks quickstart` command. (#1234)\n'
7270
73-
Entries are annotated independently, and one that already references a PR
74-
is left untouched:
71+
Entries are annotated independently, and one that already ends with a PR
72+
reference is left untouched:
7573
7674
>>> annotate_text("* one\n* two ([#99](https://github.com/databricks/cli/pull/99))\n", 1234)
7775
'* one (#1234)\n* two ([#99](https://github.com/databricks/cli/pull/99))\n'
7876
77+
A reference to a prior PR or issue in the body does not count — the PR is
78+
still appended at the end:
79+
80+
>>> annotate_text("Fixes [#6030](https://github.com/databricks/cli/issues/6030): a bug.\n", 1234)
81+
'Fixes [#6030](https://github.com/databricks/cli/issues/6030): a bug. (#1234)\n'
82+
7983
A wrapped entry gets the reference at its end, not mid-sentence:
8084
8185
>>> annotate_text("A long entry that wraps\nover two lines.\n", 1234)
82-
'A long entry that wraps\nover two lines (#1234).\n'
86+
'A long entry that wraps\nover two lines. (#1234)\n'
8387
"""
8488
lines = text.split("\n")
8589
for start, stop in entry_ranges(lines):
86-
entry = lines[start:stop]
87-
if EXISTING_REF_RE.search("\n".join(entry)):
88-
continue
8990
content = [i for i in range(start, stop) if lines[i].strip()]
9091
if not content:
9192
continue
92-
lines[content[-1]] = append_reference(lines[content[-1]], pr)
93+
last = lines[content[-1]]
94+
# Only a reference at the end of the entry blocks appending one.
95+
if END_REF_RE.search(last):
96+
continue
97+
lines[content[-1]] = append_reference(last, pr)
9398
return "\n".join(lines)
9499

95100

0 commit comments

Comments
 (0)