Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion scripts/bump-and-pr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()

Expand Down Expand Up @@ -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

Expand All @@ -220,6 +247,8 @@ def create_or_update_pr(
pr_title,
"--body-file",
body_file_path,
"--label",
label,
],
capture_output=True,
).stdout.strip()
Expand Down Expand Up @@ -345,6 +374,7 @@ def main() -> None:
pr_base=values["PR_BASE"],
pr_repo=values["PR_REPO"],
change_kind=bump_type,
label=bump_name,
)


Expand Down
33 changes: 33 additions & 0 deletions scripts/test_bump_cask_and_pr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
33 changes: 33 additions & 0 deletions scripts/test_bump_formula_and_pr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
7 changes: 7 additions & 0 deletions scripts/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down