Skip to content

Commit 1b6856d

Browse files
committed
Refactor footer production
1 parent c291494 commit 1b6856d

38 files changed

+136
-67
lines changed

repomatic/broken_links.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -319,13 +319,10 @@ def manage_combined_broken_links_issue(
319319
sphinx_section = f"## Sphinx linkcheck\n\n{sphinx_content}"
320320

321321
has_broken_links = lychee_has_broken or sphinx_has_broken
322-
body = (
323-
render_template(
324-
"broken-links-issue",
325-
lychee_section=lychee_section,
326-
sphinx_section=sphinx_section,
327-
)
328-
+ "\n"
322+
body = render_template(
323+
"broken-links-issue",
324+
lychee_section=lychee_section,
325+
sphinx_section=sphinx_section,
329326
)
330327

331328
# Write combined body to a temporary file.

repomatic/github/pr_body.py

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,26 @@ def _substitute(text: str, kwargs: dict[str, str]) -> str:
101101
return text
102102

103103

104-
def render_template(name: str, **kwargs: str) -> str:
105-
"""Load and render a PR body template with variable substitution.
104+
def _render_single(name: str, kwargs: dict[str, str]) -> tuple[str, bool]:
105+
"""Render a single template and return its body with footer preference.
106+
107+
:param name: Template name without ``.md`` extension.
108+
:param kwargs: Variables to substitute into the template.
109+
:return: A tuple of (rendered body, wants_footer).
110+
"""
111+
meta, body = load_template(name)
112+
result = _substitute(body, kwargs).strip()
113+
wants_footer = meta.get("footer") != "false" and name != "generated-footer"
114+
return result, wants_footer
115+
116+
117+
def render_template(*names: str, **kwargs: str) -> str:
118+
"""Load and render one or more templates with variable substitution.
119+
120+
When multiple template names are given, each is rendered and joined with
121+
a blank line. The ``generated-footer`` attribution is appended
122+
once at the end if **any** of the templates wants it (i.e. does not have
123+
``footer: false`` in its frontmatter).
106124
107125
Static templates (no ``$variable`` placeholders) are returned as-is.
108126
Dynamic templates use ``string.Template`` (``$variable`` syntax) to avoid
@@ -111,15 +129,24 @@ def render_template(name: str, **kwargs: str) -> str:
111129
Consecutive blank lines left by empty variables are collapsed to a single
112130
blank line.
113131
114-
:param name: Template name without ``.md`` extension.
115-
:param kwargs: Variables to substitute into the template.
132+
:param names: One or more template names without ``.md`` extension.
133+
:param kwargs: Variables to substitute into all templates.
116134
:return: The rendered markdown string.
117135
"""
118-
_meta, body = load_template(name)
119-
result = _substitute(body, kwargs)
120-
# Collapse runs of 3+ newlines (from empty variable substitutions) into
121-
# a single blank line, and strip leading/trailing whitespace.
122-
return re.sub(r"\n{3,}", "\n\n", result).strip()
136+
parts = []
137+
append_footer = False
138+
for name in names:
139+
body, wants_footer = _render_single(name, kwargs)
140+
parts.append(body)
141+
if wants_footer:
142+
append_footer = True
143+
result = "\n\n".join(parts)
144+
if append_footer:
145+
result += "\n\n" + generated_footer()
146+
result = re.sub(r"\n{3,}", "\n\n", result)
147+
if append_footer:
148+
result += "\n"
149+
return result
123150

124151

125152
def render_title(name: str, **kwargs: str) -> str:
@@ -243,7 +270,7 @@ def generate_pr_metadata_block() -> str:
243270
table_format=TableFormat.GITHUB,
244271
)
245272

246-
return render_template("generated-footer", table=table)
273+
return render_template("pr-metadata", table=table)
247274

248275

249276
def _repo_url() -> str:
@@ -274,13 +301,23 @@ def generate_refresh_tip() -> str:
274301
return render_template("refresh-tip", workflow_dispatch_url=workflow_dispatch_url)
275302

276303

304+
def generated_footer() -> str:
305+
"""Render the attribution footer from the ``generated-footer`` template.
306+
307+
Single source of truth for the attribution line appended to all PR and
308+
issue bodies produced by repomatic.
309+
310+
:return: A markdown string with a horizontal rule and the attribution line.
311+
"""
312+
return "---\n\n" + render_template("generated-footer")
313+
314+
277315
def build_pr_body(prefix: str, metadata_block: str) -> str:
278-
"""Concatenate prefix, refresh tip, and metadata footer into a PR body.
316+
"""Concatenate prefix, refresh tip, metadata block, and footer into a PR body.
279317
280318
:param prefix: Content to prepend before the metadata block. Can be empty.
281-
:param metadata_block: The generated footer from
282-
:func:`generate_pr_metadata_block`, which includes the collapsible
283-
metadata table and the attribution line.
319+
:param metadata_block: The collapsible metadata block from
320+
:func:`generate_pr_metadata_block`.
284321
:return: The complete PR body string.
285322
"""
286323
parts: list[str] = []
@@ -290,4 +327,5 @@ def build_pr_body(prefix: str, metadata_block: str) -> str:
290327
if tip:
291328
parts.append(tip)
292329
parts.append(metadata_block)
330+
parts.append(generated_footer())
293331
return "\n\n\n".join(parts)

repomatic/github/unsubscribe.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -530,16 +530,6 @@ def render_report(result: UnsubscribeResult) -> str:
530530
p1_detail_table = _render_detail_table(p1.rows)
531531
details_section = f"---\n\n{p1_detail_table}" if p1_detail_table else ""
532532

533-
phase1 = render_template(
534-
"unsubscribe-phase1",
535-
mode=mode,
536-
summary_line=summary_line,
537-
batch_details_rows=batch_details_rows,
538-
state_breakdown_rows=state_breakdown_rows,
539-
backlog_warning=backlog_warning,
540-
details_section=details_section,
541-
)
542-
543533
# Phase 2 content.
544534
if p2.skipped:
545535
phase2_content = f"> [!WARNING]\n> {p2.skip_reason}"
@@ -580,14 +570,18 @@ def render_report(result: UnsubscribeResult) -> str:
580570
p2_parts.extend(["", p2_detail_table])
581571
phase2_content = "\n".join(p2_parts)
582572

583-
phase2 = render_template(
573+
return render_template(
574+
"unsubscribe-phase1",
584575
"unsubscribe-phase2",
585576
mode=mode,
577+
summary_line=summary_line,
578+
batch_details_rows=batch_details_rows,
579+
state_breakdown_rows=state_breakdown_rows,
580+
backlog_warning=backlog_warning,
581+
details_section=details_section,
586582
phase2_content=phase2_content,
587583
)
588584

589-
return phase1 + "\n\n---\n\n" + phase2 + "\n"
590-
591585

592586
def unsubscribe_threads(
593587
months: int,

repomatic/templates/available-admonition.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
args: [version, verb, platforms]
3+
footer: false
34
---
45

56
> [!NOTE]

repomatic/templates/broken-links-issue.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,3 @@ args: [lychee_section, sphinx_section]
77
\$lychee_section
88

99
\$sphinx_section
10-
11-
---
12-
13-
🏭 Generated with [repomatic](https://github.com/kdeldycke/repomatic)

repomatic/templates/bump-version.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
args: [version, part]
33
title: Bump $part version to `v$version`
4+
footer: false
45
---
56

67
### Description

repomatic/templates/detect-squash-merge.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
args: [pr_ref]
33
title: 🚨 Squash merge detected — release skipped
4+
footer: false
45
---
56

67
### Summary
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1+
---
2+
footer: false
3+
---
4+
15
> [!WARNING]
26
> This version is **not released yet** and is under active development.

repomatic/templates/fix-changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
title: Fix changelog
3+
footer: false
34
---
45

56
### Description

repomatic/templates/fix-typos.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
title: Typo
3+
footer: false
34
---
45

56
### Description

0 commit comments

Comments
 (0)