Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

ci(pr): check pull request titles and template headings (#10158)

Alexander Eichhorneac371249d32
diff --git a/.github/workflows/pr-metadata-checks.yml b/.github/workflows/pr-metadata-checks.yml
new file mode 100644
index 0000000000..f8cbada220
--- /dev/null
+++ b/.github/workflows/pr-metadata-checks.yml
@@ -0,0 +1,46 @@
+# Checks the pull request title and description against the repository conventions.
+#
+# The title must be a Conventional Commit (it becomes the squash-merge subject) and must not name the
+# frontend package. The description must keep the headings of .github/pull_request_template.md in order.
+# Rules live in scripts/check_pr_metadata.py.
+#
+# The job only runs for pull_request events, so a merge group reports the workflow as skipped and is not blocked.
+
+name: 'pr metadata checks'
+
+on:
+ pull_request:
+ types:
+ - 'opened'
+ - 'edited'
+ - 'reopened'
+ - 'synchronize'
+ - 'ready_for_review'
+ merge_group:
+
+permissions:
+ contents: read
+
+# One run per pull request: a new edit or push cancels the superseded run. Merge groups fall back to the run id.
+concurrency:
+ group: pr-metadata-checks-${{ github.event.pull_request.number || github.run_id }}
+ cancel-in-progress: true
+
+jobs:
+ pr-metadata-checks:
+ # Bots (dependabot, release automation) do not follow the PR template.
+ if: ${{ github.event_name == 'pull_request' && github.event.pull_request.user.type != 'Bot' }}
+ runs-on: blacksmith-4vcpu-ubuntu-2404
+ timeout-minutes: 3 # expected run time: <30 s
+ steps:
+ - name: checkout
+ uses: useblacksmith/checkout@25227e61ff9dafe400e22fa487b673eac4e4409a # v1.8.1
+ with:
+ persist-credentials: false
+
+ - name: check pull request title and description
+ # Title and body are untrusted input: pass them through env, never interpolate them into the script.
+ env:
+ PR_TITLE: ${{ github.event.pull_request.title }}
+ PR_BODY: ${{ github.event.pull_request.body }}
+ run: python3 scripts/check_pr_metadata.py
diff --git a/.github/workflows/python-tests.yml b/.github/workflows/python-tests.yml
index f4e6b26677..76fb54e409 100644
--- a/.github/workflows/python-tests.yml
+++ b/.github/workflows/python-tests.yml
@@ -183,6 +183,8 @@ jobs:
# tests/ covers a few of these (e.g. test_check_pins.py, test_docs_json_export.py)
- 'scripts/**'
- 'pins.json'
+ # test_check_pr_metadata.py parses the template as a body.
+ - '.github/pull_request_template.md'
- name: setup uv
if: ${{ steps.changed-files.outputs.python_any_changed == 'true' || inputs.always_run == true }}
diff --git a/scripts/check_pr_metadata.py b/scripts/check_pr_metadata.py
new file mode 100644
index 0000000000..4816cc6af9
--- /dev/null
+++ b/scripts/check_pr_metadata.py
@@ -0,0 +1,116 @@
+"""Check a pull request title and body against the repository's contribution rules.
+
+Reads PR_TITLE and PR_BODY from the environment, prints every failure and exits 1 if there is any.
+
+- The title is a Conventional Commit subject: `type(scope)!: subject`. Squash merges use it as the commit subject.
+- The title does not name the frontend package.
+- The body keeps the headings of .github/pull_request_template.md in template order. Summary, QA Instructions,
+ Review and Checklist are required; the other sections are optional.
+"""
+
+import os
+import re
+import sys
+
+TYPES = ("feat", "fix", "docs", "chore", "ci", "test", "refactor", "perf", "build", "style", "revert")
+
+# The template's `## ` headings in order. The second element says whether the section is required.
+SECTIONS = (
+ ("Summary", True),
+ ("Related Issues / Discussions", False),
+ ("QA Instructions", True),
+ ("Review", True),
+ ("Compatibility / Rollout", False),
+ ("Checklist", True),
+)
+
+# GitHub's Revert button titles a PR `Revert "<original title>"`.
+REVERT_RE = re.compile(r'^Revert ".+"$')
+TITLE_RE = re.compile(r"^(?P<type>[a-z]+)(\([^()\s][^()]*\))?(?P<breaking>!)?:(?: (?P<subject>.*))?$")
+FENCE_RE = re.compile(r"^ {0,3}(`{3,}|~{3,})")
+COMMENT_START_RE = re.compile(r"^ {0,3}<!--")
+FORBIDDEN_TITLE_WORD = "webv2"
+
+
+def check_title(title: str) -> list[str]:
+ failures = []
+ match = TITLE_RE.match(title.strip())
+ if REVERT_RE.match(title.strip()):
+ pass
+ elif match is None or match["type"] not in TYPES:
+ failures.append(
+ f"Title {title!r} is not a Conventional Commit title. Use `type(scope): subject` or `type: subject` "
+ f"with type one of {', '.join(TYPES)}, e.g. `fix(queue): keep paused jobs on restart`."
+ )
+ elif not (match["subject"] or "").strip():
+ failures.append("Title has an empty subject. Put a short description after the colon.")
+ if FORBIDDEN_TITLE_WORD in title.lower():
+ failures.append(
+ f"Title mentions `{FORBIDDEN_TITLE_WORD}`. Name the feature area instead (e.g. `fix(canvas): ...`), "
+ "not the frontend package."
+ )
+ return failures
+
+
+def body_headings(body: str) -> list[str]:
+ """Return the text of every `## ` heading that renders as a heading, in order.
+
+ Line by line, as CommonMark: fenced code blocks and HTML comment blocks hide their lines. A comment block
+ starts at a line beginning with `<!--` (up to three spaces of indent) and ends with the first line containing
+ `-->`, which belongs to the block; an unclosed one runs to the end. A fence is literal inside a comment
+ and `<!--` is literal inside a fence.
+ """
+ headings = []
+ fence = None
+ in_comment = False
+ for line in body.replace("\r\n", "\n").replace("\r", "\n").split("\n"):
+ if in_comment:
+ in_comment = "-->" not in line
+ continue
+ if fence is None and COMMENT_START_RE.match(line):
+ in_comment = "-->" not in line
+ continue
+ fence_match = FENCE_RE.match(line)
+ if fence_match:
+ marker = fence_match[1]
+ if fence is None:
+ fence = marker
+ elif marker[0] == fence[0] and len(marker) >= len(fence) and line.strip() == marker:
+ fence = None
+ continue
+ if fence is None and line.startswith("## "):
+ headings.append(line[3:].strip())
+ return headings
+
+
+def check_body(body: str) -> list[str]:
+ failures = []
+ headings = body_headings(body)
+ names = [name for name, _ in SECTIONS]
+ for name, required in SECTIONS:
+ if required and name not in headings:
+ failures.append(f"Body is missing the `## {name}` section. Copy it from .github/pull_request_template.md.")
+ present = [h for h in headings if h in names]
+ for name in names:
+ if present.count(name) > 1:
+ failures.append(f"Body has more than one `## {name}` section. Keep exactly one.")
+ ranks = [names.index(h) for h in dict.fromkeys(present)]
+ if ranks != sorted(ranks):
+ expected = " > ".join(n for n in names if n in present)
+ failures.append(
+ f"Body sections are out of order. Order them as in .github/pull_request_template.md: {expected}."
+ )
+ return failures
+
+
+def main() -> int:
+ failures = check_title(os.environ.get("PR_TITLE", "")) + check_body(os.environ.get("PR_BODY", ""))
+ for failure in failures:
+ print(f"FAIL: {failure}")
+ if not failures:
+ print("PR title and description follow the repository conventions.")
+ return 1 if failures else 0
+
+
+if __name__ == "__main__":
+ sys.exit(main())
diff --git a/tests/test_check_pr_metadata.py b/tests/test_check_pr_metadata.py
new file mode 100644
index 0000000000..98872cf6f5
--- /dev/null
+++ b/tests/test_check_pr_metadata.py
@@ -0,0 +1,248 @@
+"""scripts/check_pr_metadata.py must enforce the PR title and template rules, driven through its environment entry."""
+
+import importlib.util
+import sys
+from pathlib import Path
+
+import pytest
+
+REPO_ROOT = Path(__file__).resolve().parent.parent
+SCRIPT = REPO_ROOT / "scripts" / "check_pr_metadata.py"
+TEMPLATE = REPO_ROOT / ".github" / "pull_request_template.md"
+
+spec = importlib.util.spec_from_file_location("check_pr_metadata", SCRIPT)
+assert spec is not None and spec.loader is not None
+check_pr_metadata = importlib.util.module_from_spec(spec)
+sys.modules[spec.name] = check_pr_metadata
+spec.loader.exec_module(check_pr_metadata)
+
+GOOD_TITLE = "fix(queue): keep paused jobs on restart"
+REQUIRED = ["Summary", "QA Instructions", "Review", "Checklist"]
+ALL_SECTIONS = [
+ "Summary",
+ "Related Issues / Discussions",
+ "QA Instructions",
+ "Review",
+ "Compatibility / Rollout",
+ "Checklist",
+]
+
+
+def body(*headings: str, sep: str = "\n") -> str:
+ return sep.join(f"## {h}{sep}{sep}text{sep}" for h in headings)
+
+
+def run(monkeypatch, capsys, title: str | None, body_text: str | None) -> tuple[int, str]:
+ for key, value in (("PR_TITLE", title), ("PR_BODY", body_text)):
+ if value is None:
+ monkeypatch.delenv(key, raising=False)
+ else:
+ monkeypatch.setenv(key, value)
+ code = check_pr_metadata.main()
+ return code, capsys.readouterr().out
+
+
+ "title",
+ [GOOD_TITLE, "feat: add thing", "feat(ui)!: drop old route", "revert(api): undo change", "ci: pin action"],
+)
+def test_valid_titles_pass(monkeypatch, capsys, title):
+ assert run(monkeypatch, capsys, title, body(*REQUIRED))[0] == 0
+
+
+ "title",
+ [
+ "",
+ "Fix the queue",
+ "fix(queue) keep jobs",
+ "fix:no space",
+ "feature: add thing",
+ "Fix: add thing",
+ "fix(): x",
+ 'Revert ""',
+ "Revert fix(x): y",
+ ],
+)
+def test_malformed_titles_fail_as_not_conventional(monkeypatch, capsys, title):
+ code, out = run(monkeypatch, capsys, title, body(*REQUIRED))
+ assert code == 1
+ assert "not a Conventional Commit title" in out
+ assert "empty subject" not in out
+
+
[email protected]("title", ["fix: ", "fix: "])
+def test_empty_subject_fails_as_empty_subject(monkeypatch, capsys, title):
+ code, out = run(monkeypatch, capsys, title, body(*REQUIRED))
+ assert code == 1
+ assert "empty subject" in out
+ assert "not a Conventional Commit title" not in out
+
+
+def test_github_revert_button_title_passes(monkeypatch, capsys):
+ assert run(monkeypatch, capsys, 'Revert "fix(queue): keep paused jobs"', body(*REQUIRED))[0] == 0
+
+
+def test_revert_title_naming_frontend_package_still_fails(monkeypatch, capsys):
+ code, out = run(monkeypatch, capsys, 'Revert "fix(webv2): thing"', body(*REQUIRED))
+ assert code == 1
+ assert "webv2" in out
+
+
[email protected]("title", ["fix(webv2): thing", "feat: WebV2 thing"])
+def test_frontend_package_name_in_title_fails(monkeypatch, capsys, title):
+ code, out = run(monkeypatch, capsys, title, body(*REQUIRED))
+ assert code == 1
+ assert "webv2" in out
+
+
+def test_webv2_in_body_is_allowed(monkeypatch, capsys):
+ text = body(*REQUIRED) + "\nTouches webv2 canvas.\n"
+ assert run(monkeypatch, capsys, GOOD_TITLE, text)[0] == 0
+
+
[email protected]("missing", REQUIRED)
+def test_missing_required_section_fails(monkeypatch, capsys, missing):
+ code, out = run(monkeypatch, capsys, GOOD_TITLE, body(*[h for h in REQUIRED if h != missing]))
+ assert code == 1
+ assert f"## {missing}" in out
+
+
+def test_conditional_sections_are_accepted_in_template_order(monkeypatch, capsys):
+ assert run(monkeypatch, capsys, GOOD_TITLE, body(*ALL_SECTIONS))[0] == 0
+
+
+def test_conditional_section_out_of_order_fails(monkeypatch, capsys):
+ text = body("Summary", "QA Instructions", "Related Issues / Discussions", "Review", "Checklist")
+ code, out = run(monkeypatch, capsys, GOOD_TITLE, text)
+ assert code == 1
+ assert "out of order" in out
+
+
+def test_required_sections_out_of_order_fail(monkeypatch, capsys):
+ code, out = run(monkeypatch, capsys, GOOD_TITLE, body("Summary", "Review", "QA Instructions", "Checklist"))
+ assert code == 1
+ assert "out of order" in out
+
+
+def test_duplicate_section_fails(monkeypatch, capsys):
+ code, out = run(monkeypatch, capsys, GOOD_TITLE, body("Summary", *REQUIRED))
+ assert code == 1
+ assert "more than one" in out
+
+
+def test_all_failures_reported_together(monkeypatch, capsys):
+ code, out = run(monkeypatch, capsys, "Fix webv2 thing", body("Summary", "Checklist"))
+ assert code == 1
+ assert "Conventional Commit" in out
+ assert "webv2" in out
+ assert "## QA Instructions" in out
+ assert "## Review" in out
+
+
+def test_crlf_body_passes(monkeypatch, capsys):
+ assert run(monkeypatch, capsys, GOOD_TITLE, body(*REQUIRED, sep="\r\n"))[0] == 0
+
+
[email protected]("fence", ["```", "~~~", "````"])
+def test_heading_inside_code_fence_is_ignored(monkeypatch, capsys, fence):
+ text = body("Summary", "QA Instructions", "Checklist") + f"\n{fence}\n## Review\n{fence}\n"
+ code, out = run(monkeypatch, capsys, GOOD_TITLE, text)
+ assert code == 1
+ assert "## Review" in out
+
+
+def missing(name: str) -> str:
+ return f"missing the `## {name}` section"
+
+
+def test_all_required_headings_commented_out_fail_multiline(monkeypatch, capsys):
+ text = "<!--\n" + body(*REQUIRED) + "-->\n"
+ code, out = run(monkeypatch, capsys, GOOD_TITLE, text)
+ assert code == 1
+ assert all(missing(name) in out for name in REQUIRED)
+
+
+def test_all_required_headings_commented_out_fail_single_line(monkeypatch, capsys):
+ text = "\n".join(f"<!-- ## {name} -->" for name in REQUIRED)
+ code, out = run(monkeypatch, capsys, GOOD_TITLE, text)
+ assert code == 1
+ assert all(missing(name) in out for name in REQUIRED)
+
+
+def test_fence_marker_inside_comment_does_not_hide_later_headings(monkeypatch, capsys):
+ text = "<!--\n```\n-->\n" + body(*REQUIRED)
+ assert run(monkeypatch, capsys, GOOD_TITLE, text)[0] == 0
+
+
+def test_comment_opener_inside_fence_does_not_hide_later_headings(monkeypatch, capsys):
+ text = "```\n<!--\n```\n" + body(*REQUIRED)
+ assert run(monkeypatch, capsys, GOOD_TITLE, text)[0] == 0
+
+
+def test_unclosed_comment_hides_later_headings(monkeypatch, capsys):
+ text = body("Summary", "QA Instructions") + "<!-- never closed\n" + body("Review", "Checklist")
+ code, out = run(monkeypatch, capsys, GOOD_TITLE, text)
+ assert code == 1
+ assert missing("Review") in out
+ assert missing("Checklist") in out
+ assert missing("Summary") not in out
+
+
+def test_text_after_comment_end_on_closing_line_is_not_a_heading(monkeypatch, capsys):
+ text = body("Summary", "QA Instructions", "Checklist") + "<!--\nnote\n--> ## Review\n"
+ code, out = run(monkeypatch, capsys, GOOD_TITLE, text)
+ assert code == 1
+ assert missing("Review") in out
+
+
+def test_mid_line_comment_opener_does_not_hide_next_heading(monkeypatch, capsys):
+ text = "## Summary\ntext <!-- start\n" + body("QA Instructions", "Review", "Checklist")
+ assert run(monkeypatch, capsys, GOOD_TITLE, text)[0] == 0
+
+
+def test_shorter_fence_line_does_not_close_longer_fence(monkeypatch, capsys):
+ text = body("Summary", "QA Instructions", "Checklist") + "\n````\n```\n## Review\n````\n"
+ code, out = run(monkeypatch, capsys, GOOD_TITLE, text)
+ assert code == 1
+ assert "## Review" in out
+
+
+def test_tilde_line_does_not_close_backtick_fence(monkeypatch, capsys):
+ text = body("Summary", "QA Instructions", "Checklist") + "\n```\n~~~\n## Review\n```\n"
+ code, out = run(monkeypatch, capsys, GOOD_TITLE, text)
+ assert code == 1
+ assert "## Review" in out
+
+
+def test_unclosed_fence_swallows_later_headings(monkeypatch, capsys):
+ text = body("Summary", "QA Instructions") + "\n```\n" + body("Review", "Checklist")
+ code, out = run(monkeypatch, capsys, GOOD_TITLE, text)
+ assert code == 1
+ assert "## Review" in out
+ assert "## Checklist" in out
+
+
+def test_third_level_heading_is_not_the_section(monkeypatch, capsys):
+ text = body("Summary", "QA Instructions", "Checklist") + "\n### Review\n"
+ code, out = run(monkeypatch, capsys, GOOD_TITLE, text)
+ assert code == 1
+ assert "## Review" in out
+
+
+def test_heading_after_closed_fence_counts(monkeypatch, capsys):
+ text = "## Summary\n```\n## Review\n```\n" + body("QA Instructions", "Review", "Checklist")
+ assert run(monkeypatch, capsys, GOOD_TITLE, text)[0] == 0
+
+
[email protected]("empty", ["", None])
+def test_empty_or_missing_body_fails(monkeypatch, capsys, empty):
+ code, out = run(monkeypatch, capsys, GOOD_TITLE, empty)
+ assert code == 1
+ for name in REQUIRED:
+ assert f"## {name}" in out
+
+
+def test_pull_request_template_passes(monkeypatch, capsys):
+ template = TEMPLATE.read_text(encoding="utf-8")
+ assert run(monkeypatch, capsys, GOOD_TITLE, template)[0] == 0