diff --git a/build_pr_changes.py b/build_pr_changes.py index 2f78434..022c533 100755 --- a/build_pr_changes.py +++ b/build_pr_changes.py @@ -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) @@ -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." ) @@ -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")