Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4026138

Browse files
Refine generated commands in print_pr_check_run_errors.py
Make the generated commands for print_workflow_run_errors.py cleaner by only including --owner, --repo, and --token if they were explicitly specified by the user when calling print_pr_check_run_errors.py. This relies on print_workflow_run_errors.py being able to pick up these values from its own environment or defaults when they are not explicitly passed in the sub-command.
1 parent e6b533a commit 4026138

File tree

1 file changed

+47
-9
lines changed

1 file changed

+47
-9
lines changed

scripts/print_pr_check_run_errors.py

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -398,15 +398,53 @@ def parse_repo_url_arg(url_string):
398398
check_run_id = run.get('id')
399399
check_run_name = run.get('name')
400400

401-
# Construct the command
402-
# We use final_owner and final_repo which were resolved from args or git remote
403-
command = [
404-
"scripts/print_workflow_run_errors.py",
405-
"--owner", final_owner,
406-
"--repo", final_repo,
407-
"--token", "\"<YOUR_GITHUB_TOKEN_OR_USE_ENV_VAR>\"", # Using a placeholder
408-
"--run-id", str(check_run_id)
409-
]
401+
# Construct the command for print_workflow_run_errors.py
402+
command = ["scripts/print_workflow_run_errors.py"]
403+
404+
# Conditionally add --owner and --repo
405+
owner_repo_was_cmd_line_arg = False
406+
# A simple check: if --url is in sys.argv, or --owner or --repo.
407+
# This doesn't perfectly check if argparse *used* these from sys.argv vs default,
408+
# but it's a good heuristic for "user attempted to specify".
409+
# More robust would be to compare args.url to its default, etc.
410+
# For now, this heuristic is acceptable.
411+
if any(arg.startswith("--url") for arg in sys.argv):
412+
owner_repo_was_cmd_line_arg = True
413+
if not owner_repo_was_cmd_line_arg: # only check --owner/--repo if --url wasn't specified
414+
if any(arg.startswith("--owner") for arg in sys.argv) or \
415+
any(arg.startswith("--repo") for arg in sys.argv):
416+
owner_repo_was_cmd_line_arg = True
417+
418+
if owner_repo_was_cmd_line_arg:
419+
command.extend(["--owner", final_owner, "--repo", final_repo])
420+
# No 'else' needed: if not explicit, print_workflow_run_errors.py should auto-detect
421+
422+
# Conditionally add --token
423+
# Add only if the token was provided via the --token argument to *this* script.
424+
# We need to know if args.token initially came from the command line vs. env/file.
425+
# The current `args.token` is always populated if a token is found.
426+
# We need a way to distinguish. Let's check if sys.argv contained --token.
427+
token_was_cmd_line_arg = False
428+
for i, arg_val in enumerate(sys.argv):
429+
if arg_val == "--token":
430+
if i + 1 < len(sys.argv): # Ensure there's a value after --token
431+
# Check if the value matches the one we are using.
432+
# This isn't foolproof if token is passed as --token=$GITHUB_TOKEN,
433+
# but it's a reasonable heuristic for direct --token <value>
434+
if sys.argv[i+1] == args.token:
435+
token_was_cmd_line_arg = True
436+
break # Found --token, stop checking
437+
elif arg_val.startswith("--token="):
438+
if arg_val.split('=', 1)[1] == args.token:
439+
token_was_cmd_line_arg = True
440+
break
441+
442+
443+
if token_was_cmd_line_arg:
444+
command.extend(["--token", "\"<YOUR_EXPLICIT_TOKEN_HERE>\""]) # Placeholder for explicit token
445+
# No 'else': if not explicit cmd line, print_workflow_run_errors.py should use env/file
446+
447+
command.extend(["--run-id", str(check_run_id)])
410448

411449
# Add some optional parameters if they are set in the current script's args,
412450
# assuming print_workflow_run_errors.py supports them or similar ones.

0 commit comments

Comments
 (0)