Skip to content

Commit

Permalink
Add -k <query> to checkout PR by search query
Browse files Browse the repository at this point in the history
  • Loading branch information
bernhardkaindl committed Oct 10, 2024
1 parent 2787866 commit 68be931
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions build_pr_changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,33 @@ def add_compiler_to_specs(specs_to_check, args) -> List[str]:
return specs_to_check


def checkout_pr_by_search_query(args: argparse.Namespace) -> ExitCode:
"""Checkout the PR branch by searching for the PR number."""
if not args.checkout:
return Success

# Find the PR number from the PR query:
query = f"gh pr list --limit 1 -S '{args.checkout}' --json number -q.[].number"
print("Querying for the PR to check out, please wait a second or so:")
exitcode, number = subprocess.getstatusoutput(query)
if exitcode != 0 or not number:
print(f"Failed to find the PR by querying for '{args.checkout}'\n" + number)
return exitcode or 1
# View the information about the PR:
exitcode, output = subprocess.getstatusoutput(f"gh pr view {number}")
if exitcode != 0:
print("Failed to view the PR:", output)
return exitcode
print(f'Found PR for "{args.checkout}":')
print(output)
# Checkout the PR branch:
exitcode, output = subprocess.getstatusoutput(f"gh pr checkout {number}")
if exitcode != 0:
print("Failed to checkout the PR branch:", output)
return exitcode
return Success


def parse_args() -> argparse.Namespace:
"""Run spack install on recipes changed in the current branch from develop."""
basicConfig(format="%(message)s", level=INFO)
Expand Down Expand Up @@ -605,6 +632,12 @@ def parse_args() -> argparse.Namespace:
"--dependencies",
help="Additional dependency specs for the packages.",
)
argparser.add_argument(
"-k",
"--checkout",
help="Checkout the PR branch (find it by PR query) to check the changes.",
type=str,
)
argparser.add_argument(
"-l", "--label-success", action="store_true", help="Label the PR on success."
)
Expand Down Expand Up @@ -634,6 +667,10 @@ def main(args) -> int:
# - Add support for installing the packages in a container, sandbox, or remote host.
# Use pxssh module of pexpect: https://pexpect.readthedocs.io/en/stable/api/pxssh.html

exitcode = checkout_pr_by_search_query(args)
if exitcode != Success:
return exitcode

# Get the number of the current PR:
# In case we shall approve/merge, we need the PR number so we don't approve/merge the wrong PR.
exitcode, output = subprocess.getstatusoutput("gh pr view --json number -q .number")
Expand Down

0 comments on commit 68be931

Please sign in to comment.