Skip to content

Commit 86717bd

Browse files
Copilotshenxianpeng
andcommitted
fix: improve error handling in get_pr_commit_messages() with parent check and warning logs
Co-authored-by: shenxianpeng <3353385+shenxianpeng@users.noreply.github.com>
1 parent ca8b121 commit 86717bd

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

__pycache__/main.cpython-312.pyc

12.9 KB
Binary file not shown.

main.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,29 @@ def get_pr_commit_messages() -> list[str]:
4545
"""
4646
if os.getenv("GITHUB_EVENT_NAME", "") != "pull_request":
4747
return []
48+
49+
# Verify HEAD is a merge commit with at least 2 parents before using HEAD^1..HEAD^2.
50+
# A non-merge checkout (e.g. shallow fetch missing parents) would make the range query fail.
51+
try:
52+
parent_check = subprocess.run(
53+
["git", "rev-parse", "--verify", "HEAD^2"],
54+
stdout=subprocess.PIPE,
55+
stderr=subprocess.PIPE,
56+
encoding="utf-8",
57+
check=False,
58+
)
59+
if parent_check.returncode != 0:
60+
print(
61+
"::warning::HEAD does not have a second parent; skipping per-commit "
62+
"PR message validation. This is expected for non-merge checkouts or "
63+
f"shallow fetches. Details: {parent_check.stderr.strip()}",
64+
file=sys.stderr,
65+
)
66+
return []
67+
except Exception as e:
68+
print(f"::warning::Could not verify HEAD parents: {e}", file=sys.stderr)
69+
return []
70+
4871
try:
4972
result = subprocess.run(
5073
["git", "log", "--pretty=format:%B%x00", "HEAD^1..HEAD^2"],
@@ -53,10 +76,15 @@ def get_pr_commit_messages() -> list[str]:
5376
encoding="utf-8",
5477
check=False,
5578
)
56-
if result.returncode == 0 and result.stdout:
79+
if result.returncode == 0:
5780
return [m.strip() for m in result.stdout.split("\x00") if m.strip()]
58-
except Exception:
59-
pass
81+
print(
82+
f"::warning::git log HEAD^1..HEAD^2 failed (exit {result.returncode}): "
83+
f"{result.stderr.strip()}",
84+
file=sys.stderr,
85+
)
86+
except Exception as e:
87+
print(f"::warning::Failed to retrieve PR commit messages: {e}", file=sys.stderr)
6088
return []
6189

6290

0 commit comments

Comments
 (0)