Skip to content

Commit e8f2965

Browse files
Copilotcclauss
andauthored
Improve scripts/pr_file_map.py observability and output metadata (#15317)
* Use pathlib and add PR/file status reporting in pr_file_map Co-authored-by: cclauss <3709715+cclauss@users.noreply.github.com> * Ensure pr_file_map outputs summary with zero open PRs Co-authored-by: cclauss <3709715+cclauss@users.noreply.github.com> * Retain no-open-PR message in generated pr_file_map report Co-authored-by: cclauss <3709715+cclauss@users.noreply.github.com> * Return after no-open-PR summary in pr_file_map Co-authored-by: cclauss <3709715+cclauss@users.noreply.github.com> * Apply batched suggestions from code review Co-authored-by: Christian Clauss <cclauss@me.com> * Apply suggestion from @cclauss --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: cclauss <3709715+cclauss@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent 00e10ff commit e8f2965

1 file changed

Lines changed: 33 additions & 9 deletions

File tree

scripts/pr_file_map.py

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,19 @@
55
Lists all open pull requests in the current directory's git repo (via `gh`)
66
and, for each file touched by any open PR, which PR number(s) touch it.
77
8-
Output is GitHub-flavored Markdown: a sorted list of files that currently
8+
Output is GitHub-flavored Markdown that includes this script's path, the
9+
current UTC datetime, and summary counts for open PRs, file entries, and
10+
existing/missing files. It then renders a sorted list of files that currently
911
exist in the working directory, each with its modifying PR numbers, followed
1012
by a separate section for files referenced by open PRs but that do not exist
1113
in the working directory (e.g. deleted, renamed, or on a branch not checked
1214
out locally).
1315
16+
Run status is also written to stderr with:
17+
- Number of PRs from `get_open_prs()`
18+
- Number of files from `get_pr_files()`
19+
- Number of existing and missing files
20+
1421
Requirements: gh (GitHub CLI), authenticated (`gh auth login`)
1522
1623
Usage:
@@ -19,11 +26,12 @@
1926
"""
2027

2128
import json
22-
import os
2329
import shutil
2430
import subprocess
2531
import sys
2632
from collections import defaultdict
33+
from datetime import UTC, datetime
34+
from pathlib import Path
2735

2836

2937
def run_gh(args: list[str]) -> str:
@@ -73,36 +81,52 @@ def main() -> None:
7381
check_gh_auth()
7482

7583
prs = get_open_prs()
76-
if not prs:
77-
print("No open pull requests found.")
78-
return
84+
pr_count = len(prs)
85+
print(f"PR count from get_open_prs(): {pr_count}", file=sys.stderr)
7986

8087
file_to_prs: dict[str, list[int]] = defaultdict(list)
88+
file_count = 0
8189

8290
for pr in prs:
8391
pr_number = pr["number"]
84-
for path in get_pr_files(pr_number):
92+
pr_files = get_pr_files(pr_number)
93+
file_count += len(pr_files)
94+
for path in pr_files:
8595
file_to_prs[path].append(pr_number)
96+
print(f"File count from get_pr_files(): {file_count}", file=sys.stderr)
8697

8798
existing: dict[str, list[int]] = {}
8899
missing: dict[str, list[int]] = {}
89100

90101
for path, pr_numbers in file_to_prs.items():
91-
target = existing if os.path.exists(path) else missing
102+
target = existing if Path(path).exists() else missing
92103
target[path] = sorted(set(pr_numbers))
104+
existing_count = len(existing)
105+
missing_count = len(missing)
106+
print(
107+
f"Existing files: {existing_count}, Missing files: {missing_count}",
108+
file=sys.stderr,
109+
)
93110

94111
# --- Render GitHub-flavored Markdown ---
95112
print("# Open Pull Request File Map\n")
113+
print(f"- Script: `{Path(__file__).resolve()}`")
114+
print(f"- Generated (UTC): `{datetime.now(UTC).isoformat()}`")
115+
print(f"- Number of PRs: `{pr_count}`")
116+
print(f"- Number of files: `{file_count}`")
117+
if pr_count == 0:
118+
print("No open pull requests found.")
119+
return
96120

97-
print("## Existing files\n")
121+
print(f"## `{existing_count}` existing files\n")
98122
if existing:
99123
for path in sorted(existing):
100124
pr_list = " ".join(f"#{n}" for n in existing[path])
101125
print(f"- `{path}`: {pr_list}")
102126
else:
103127
print("_None._")
104128

105-
print("\n## Files not present in the working directory\n")
129+
print(f"\n## `{missing_count}` files not present in the working directory\n")
106130
if missing:
107131
for path in sorted(missing):
108132
pr_list = " ".join(f"#{n}" for n in missing[path])

0 commit comments

Comments
 (0)