Skip to content

Commit 6b069dd

Browse files
Refactor: Refine error reporting for auto-detection failures
- Removes `parser.print_help()` calls for specific auto-detection failures: - When the repository owner/name cannot be determined automatically and is not specified by the user. - When the current git branch cannot be determined (and --pull_number is not provided). - The script will still print a targeted error message and exit with an error code in these scenarios. - Other error conditions (e.g., invalid explicit arguments, or no PR found for a valid detected branch) will continue to print full usage information.
1 parent 80c927c commit 6b069dd

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

scripts/gha/get_pr_review_comments_standalone.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -320,30 +320,43 @@ def parse_repo_url(url_string):
320320

321321
if not final_owner or not final_repo:
322322
sys.stderr.write("Error: Could not determine repository. Please specify --url, OR both --owner and --repo, OR ensure git remote 'origin' is configured correctly.\n")
323-
parser.print_help()
324-
sys.exit(1)
323+
sys.exit(1) # No print_help() here, as per request
325324

326325
if not set_repo_url_standalone(final_owner, final_repo): # Sets global OWNER and REPO
327326
sys.stderr.write(f"Error: Could not set repository to {final_owner}/{final_repo}. Ensure owner/repo are correct.\n")
328327
sys.exit(1)
329328

330329
pull_request_number = args.pull_number
330+
current_branch_for_pr_check = None # Used to improve final error message
331331
if not pull_request_number:
332332
sys.stderr.write("Pull number not specified, attempting to find PR for current branch...\n")
333-
current_branch = get_current_branch_name()
334-
if current_branch:
335-
sys.stderr.write(f"Current git branch is: {current_branch}\n")
336-
# Pass global OWNER and REPO which are set by set_repo_url_standalone
337-
pull_request_number = get_latest_pr_for_branch(args.token, OWNER, REPO, current_branch)
333+
current_branch_for_pr_check = get_current_branch_name()
334+
if current_branch_for_pr_check:
335+
sys.stderr.write(f"Current git branch is: {current_branch_for_pr_check}\n")
336+
pull_request_number = get_latest_pr_for_branch(args.token, OWNER, REPO, current_branch_for_pr_check)
338337
if pull_request_number:
339-
sys.stderr.write(f"Found PR #{pull_request_number} for branch {current_branch}.\n")
338+
sys.stderr.write(f"Found PR #{pull_request_number} for branch {current_branch_for_pr_check}.\n")
340339
else:
341-
sys.stderr.write(f"No open PR found for branch {current_branch} in {OWNER}/{REPO}.\n")
340+
sys.stderr.write(f"No open PR found for branch {current_branch_for_pr_check} in {OWNER}/{REPO}.\n")
342341
else:
343-
sys.stderr.write("Could not determine current git branch. Cannot find PR automatically.\n")
342+
# Error: Could not determine current git branch (for PR detection).
343+
sys.stderr.write("Error: Could not determine current git branch. Cannot find PR automatically.\n")
344+
sys.exit(1) # No print_help() here, as per request
344345

345346
if not pull_request_number:
346-
sys.stderr.write("Error: Pull request number is required. Provide --pull_number or ensure an open PR exists for the current branch.\n")
347+
# This is reached if:
348+
# - --pull_number was not specified AND
349+
# - current branch was determined BUT no PR was found for it.
350+
# (The cases where branch itself couldn't be determined now exit above without print_help)
351+
error_message = "Error: Pull request number not specified and no open PR found for the current branch."
352+
if args.pull_number: # Should not happen if logic is correct, but as a safeguard
353+
error_message = f"Error: Pull request number {args.pull_number} is invalid or not found."
354+
elif not current_branch_for_pr_check: # Should be caught by exit above
355+
error_message = "Error: Pull request number not specified and could not determine current git branch."
356+
357+
sys.stderr.write(error_message + "\n")
358+
# Keeping print_help() for this more general failure case unless explicitly asked to remove.
359+
# This specific error (no PR found for a valid branch) is different from "can't find branch".
347360
parser.print_help()
348361
sys.exit(1)
349362

0 commit comments

Comments
 (0)