Skip to content

Commit 8d638ca

Browse files
shenxianpengclaude
andcommitted
fix: skip the PR comment on push events without a warning
pr-comments: true on a push event annotated every run with "::warning::Unable to post PR comment: Unable to determine PR number", because only PR_COMMENTS_ENABLED short-circuited add_pr_comments() and get_pr_number() raised for a ref that is not refs/pull/N/merge. Return early with a plain log line when the event is not a pull request: there is nothing to comment on, and that is not a problem worth an annotation. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018xYa7m3qup5wyN5MaXFgf6
1 parent 9703fab commit 8d638ca

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

‎main.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,12 @@ def add_pr_comments(results: list[ScopeResult]) -> int:
11021102
if not PR_COMMENTS_ENABLED:
11031103
return 0
11041104

1105+
# A push has no pull request to comment on. This used to fall through to
1106+
# get_pr_number(), which raised, and every push run carried a warning.
1107+
if not is_pr_event():
1108+
print("Skipping PR comment: not a pull request event.")
1109+
return 0
1110+
11051111
# Fork PRs triggered by the pull_request event receive a read-only token;
11061112
# the GitHub API will always reject comment writes with 403.
11071113
# pull_request_target events always have the configured token permissions.

‎main_test.py‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,9 +1153,47 @@ def test_disabled_returns_zero(self):
11531153
rc = main.add_pr_comments([pass_scope()])
11541154
self.assertEqual(rc, 0)
11551155

1156+
def test_push_event_skips_comment_without_warning(self):
1157+
"""A push has no PR to comment on, and that is not a problem.
1158+
1159+
It used to reach get_pr_number(), which raised, so every push run
1160+
with pr-comments enabled carried a "Unable to post PR comment"
1161+
warning annotation.
1162+
"""
1163+
event_path = os.path.join(tempfile.mkdtemp(), "event.json")
1164+
with open(event_path, "w", encoding="utf-8") as f:
1165+
json.dump({"ref": "refs/heads/main", "pusher": {"name": "octocat"}}, f)
1166+
with (
1167+
patch("main.PR_COMMENTS_ENABLED", True),
1168+
patch.dict(
1169+
os.environ,
1170+
{
1171+
"GITHUB_EVENT_NAME": "push",
1172+
"GITHUB_REF": "refs/heads/main",
1173+
"GITHUB_EVENT_PATH": event_path,
1174+
"GITHUB_TOKEN": "token",
1175+
"GITHUB_REPOSITORY": "owner/repo",
1176+
},
1177+
),
1178+
patch("main.get_pr_number") as mock_number,
1179+
patch("builtins.print") as mock_print,
1180+
):
1181+
rc = main.add_pr_comments([fail_scope()])
1182+
self.assertEqual(rc, 0)
1183+
mock_number.assert_not_called()
1184+
printed = [
1185+
call[0][0]
1186+
for call in mock_print.call_args_list
1187+
if call[0] and isinstance(call[0][0], str)
1188+
]
1189+
self.assertFalse(
1190+
[line for line in printed if line.startswith("::warning")], printed
1191+
)
1192+
11561193
def test_fork_pr_skips_comment_and_warns(self):
11571194
with (
11581195
patch("main.PR_COMMENTS_ENABLED", True),
1196+
patch.dict(os.environ, {"GITHUB_EVENT_NAME": "pull_request"}),
11591197
patch("main.is_fork_pr", return_value=True),
11601198
patch("main.JOB_SUMMARY_ENABLED", False),
11611199
patch("builtins.print") as mock_print,
@@ -1170,6 +1208,7 @@ def test_fork_pr_writes_job_summary_hint(self):
11701208
summary_path = os.path.join(tempfile.mkdtemp(), "summary.txt")
11711209
with (
11721210
patch("main.PR_COMMENTS_ENABLED", True),
1211+
patch.dict(os.environ, {"GITHUB_EVENT_NAME": "pull_request"}),
11731212
patch("main.is_fork_pr", return_value=True),
11741213
patch("main.JOB_SUMMARY_ENABLED", True),
11751214
patch("main.GITHUB_STEP_SUMMARY", summary_path),
@@ -1200,6 +1239,7 @@ def test_creates_comment_with_rendered_body(self):
12001239
{
12011240
"GITHUB_TOKEN": "token",
12021241
"GITHUB_REPOSITORY": "owner/repo",
1242+
"GITHUB_EVENT_NAME": "pull_request",
12031243
"GITHUB_REF": "refs/pull/12/merge",
12041244
},
12051245
),
@@ -1233,6 +1273,7 @@ def test_updates_existing_comment_when_changed(self):
12331273
{
12341274
"GITHUB_TOKEN": "token",
12351275
"GITHUB_REPOSITORY": "owner/repo",
1276+
"GITHUB_EVENT_NAME": "pull_request",
12361277
"GITHUB_REF": "refs/pull/12/merge",
12371278
},
12381279
),
@@ -1263,6 +1304,7 @@ def test_skips_when_comment_is_up_to_date(self):
12631304
{
12641305
"GITHUB_TOKEN": "token",
12651306
"GITHUB_REPOSITORY": "owner/repo",
1307+
"GITHUB_EVENT_NAME": "pull_request",
12661308
"GITHUB_REF": "refs/pull/12/merge",
12671309
},
12681310
),
@@ -1317,6 +1359,7 @@ def _run(self, side_effect):
13171359
{
13181360
"GITHUB_TOKEN": "token",
13191361
"GITHUB_REPOSITORY": "owner/repo",
1362+
"GITHUB_EVENT_NAME": "pull_request",
13201363
"GITHUB_REF": "refs/pull/12/merge",
13211364
},
13221365
),

0 commit comments

Comments
 (0)