From 7c214a4deb143b5fb10df9c792df8dbc8df15127 Mon Sep 17 00:00:00 2001 From: Alec Thomas Date: Thu, 25 Jun 2026 10:41:49 +1000 Subject: [PATCH] feat: label bump PRs with formula or cask name Co-authored-by: Claude Code --- scripts/bump-and-pr.py | 32 +++++++++++++++++++++++++++- scripts/test_bump_cask_and_pr.py | 33 +++++++++++++++++++++++++++++ scripts/test_bump_formula_and_pr.py | 33 +++++++++++++++++++++++++++++ scripts/test_helpers.py | 7 ++++++ 4 files changed, 104 insertions(+), 1 deletion(-) diff --git a/scripts/bump-and-pr.py b/scripts/bump-and-pr.py index 5af8c8b..562ff5e 100755 --- a/scripts/bump-and-pr.py +++ b/scripts/bump-and-pr.py @@ -149,6 +149,28 @@ def run_command(args: list[str], *, capture_output: bool = False) -> subprocess. return result +def ensure_label_exists(label: str, pr_repo: str) -> None: + existing = run_command( + [ + "gh", + "label", + "list", + "--repo", + pr_repo, + "--search", + label, + "--json", + "name", + "--jq", + ".[].name", + ], + capture_output=True, + ).stdout.splitlines() + if label in existing: + return + run_command(["gh", "label", "create", label, "--repo", pr_repo], capture_output=True) + + def create_or_update_pr( *, branch: str, @@ -158,6 +180,7 @@ def create_or_update_pr( pr_base: str, pr_repo: str, change_kind: str, + label: str, ) -> None: change_kind_capitalized = change_kind.capitalize() @@ -200,8 +223,12 @@ def create_or_update_pr( capture_output=True, ).stdout.strip() + ensure_label_exists(label, pr_repo) + if existing_pr and existing_pr != "null": - run_command(["gh", "pr", "edit", existing_pr, "--title", pr_title, "--body-file", body_file_path]) + run_command( + ["gh", "pr", "edit", existing_pr, "--title", pr_title, "--body-file", body_file_path, "--add-label", label] + ) print(f"Pull request updated: {existing_pr}") return @@ -220,6 +247,8 @@ def create_or_update_pr( pr_title, "--body-file", body_file_path, + "--label", + label, ], capture_output=True, ).stdout.strip() @@ -345,6 +374,7 @@ def main() -> None: pr_base=values["PR_BASE"], pr_repo=values["PR_REPO"], change_kind=bump_type, + label=bump_name, ) diff --git a/scripts/test_bump_cask_and_pr.py b/scripts/test_bump_cask_and_pr.py index 420f180..e832116 100644 --- a/scripts/test_bump_cask_and_pr.py +++ b/scripts/test_bump_cask_and_pr.py @@ -126,6 +126,39 @@ def test_existing_pr_is_updated(self) -> None: self.assertTrue(any(command.startswith("gh pr edit ") for command in commands)) self.assertFalse(any(command.startswith("gh pr create ") for command in commands)) + def test_label_created_and_added_on_pr_create(self) -> None: + self.write_cask("demo", "b" * 64) + + result = self.run_script() + + self.assertEqual(result.returncode, 0, msg=result.stderr) + + commands = self.read_commands() + self.assertIn("gh label list --repo block/homebrew-tap --search demo --json name --jq .[].name", commands) + self.assertIn("gh label create demo --repo block/homebrew-tap", commands) + self.assertTrue(any(c.startswith("gh pr create ") and c.endswith(" --label demo") for c in commands)) + + def test_existing_label_is_not_recreated(self) -> None: + self.write_cask("demo", "b" * 64) + + result = self.run_script(FAKE_EXISTING_LABELS="demo") + + self.assertEqual(result.returncode, 0, msg=result.stderr) + + commands = self.read_commands() + self.assertFalse(any(c.startswith("gh label create ") for c in commands)) + self.assertTrue(any(c.startswith("gh pr create ") and c.endswith(" --label demo") for c in commands)) + + def test_label_added_on_pr_edit(self) -> None: + self.write_cask("demo", "b" * 64) + + result = self.run_script(FAKE_EXISTING_PR_URL="https://github.com/block/homebrew-tap/pull/901") + + self.assertEqual(result.returncode, 0, msg=result.stderr) + + commands = self.read_commands() + self.assertTrue(any(c.startswith("gh pr edit ") and c.endswith(" --add-label demo") for c in commands)) + def test_no_changes_skips_pr(self) -> None: self.write_cask("demo", "b" * 64) diff --git a/scripts/test_bump_formula_and_pr.py b/scripts/test_bump_formula_and_pr.py index 5d8c8f6..10bf3ff 100644 --- a/scripts/test_bump_formula_and_pr.py +++ b/scripts/test_bump_formula_and_pr.py @@ -177,6 +177,39 @@ def test_existing_pr_is_updated(self) -> None: self.assertTrue(any(command.startswith("gh pr edit ") for command in commands)) self.assertFalse(any(command.startswith("gh pr create ") for command in commands)) + def test_label_created_and_added_on_pr_create(self) -> None: + self.write_formula("demo", "b" * 64) + + result = self.run_script() + + self.assertEqual(result.returncode, 0, msg=result.stderr) + + commands = self.read_commands() + self.assertIn("gh label list --repo block/homebrew-tap --search demo --json name --jq .[].name", commands) + self.assertIn("gh label create demo --repo block/homebrew-tap", commands) + self.assertTrue(any(c.startswith("gh pr create ") and c.endswith(" --label demo") for c in commands)) + + def test_existing_label_is_not_recreated(self) -> None: + self.write_formula("demo", "b" * 64) + + result = self.run_script(FAKE_EXISTING_LABELS="demo") + + self.assertEqual(result.returncode, 0, msg=result.stderr) + + commands = self.read_commands() + self.assertFalse(any(c.startswith("gh label create ") for c in commands)) + self.assertTrue(any(c.startswith("gh pr create ") and c.endswith(" --label demo") for c in commands)) + + def test_label_added_on_pr_edit(self) -> None: + self.write_formula("demo", "b" * 64) + + result = self.run_script(FAKE_EXISTING_PR_URL="https://github.com/block/homebrew-tap/pull/901") + + self.assertEqual(result.returncode, 0, msg=result.stderr) + + commands = self.read_commands() + self.assertTrue(any(c.startswith("gh pr edit ") and c.endswith(" --add-label demo") for c in commands)) + def test_no_changes_skips_pr(self) -> None: self.write_formula("demo", "b" * 64) diff --git a/scripts/test_helpers.py b/scripts/test_helpers.py index f347c6a..4e02a26 100644 --- a/scripts/test_helpers.py +++ b/scripts/test_helpers.py @@ -54,6 +54,13 @@ def install_fake_gh(fake_bin: pathlib.Path) -> None: " } >> \"$PR_BODY_LOG\"\n" " fi\n" "}\n" + "if [[ \"${1:-}\" == \"label\" && \"${2:-}\" == \"list\" ]]; then\n" + " printf '%s\\n' ${FAKE_EXISTING_LABELS:-}\n" + " exit 0\n" + "fi\n" + "if [[ \"${1:-}\" == \"label\" && \"${2:-}\" == \"create\" ]]; then\n" + " exit 0\n" + "fi\n" "if [[ \"${1:-}\" == \"pr\" && \"${2:-}\" == \"list\" ]]; then\n" " printf '%s\\n' \"${FAKE_EXISTING_PR_URL:-null}\"\n" " exit 0\n"