Skip to content

Commit 3d187db

Browse files
shenxianpengclaude
andcommitted
fix(pr-comments): skip the comment for Dependabot pull requests too
is_fork_pr_with_readonly_token() compared head and base repositories, so a Dependabot pull request, whose branch lives in the repository itself, went on to the API with the read-only token GitHub hands Dependabot-triggered pull_request runs, took a 403, and logged "Ensure your workflow grants 'pull-requests: write' permission" — advice the workflow had already followed, on every Dependabot PR. Treat GITHUB_ACTOR == dependabot[bot] as read-only under pull_request (pull_request_target keeps its configured permissions, as for forks), so Dependabot takes the same graceful skip as fork PRs, and word the warning and the job-summary notice for both cases. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018xYa7m3qup5wyN5MaXFgf6
1 parent 073d579 commit 3d187db

3 files changed

Lines changed: 89 additions & 16 deletions

File tree

‎docs/fork-pr-comments.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
When a pull request is opened from a **forked repository**, the `GITHUB_TOKEN` used by the
44
`pull_request` event has **read-only** permissions by design (GitHub security policy).
5-
This means `pr-comments: true` cannot write a comment back to the PR.
5+
This means `pr-comments: true` cannot write a comment back to the PR. Pull requests
6+
opened by **Dependabot** are in the same position: GitHub runs Dependabot-triggered
7+
`pull_request` workflows with a read-only token even though the branch lives in your own
8+
repository, so everything below applies to them too.
69

710
By default, commit-check-action handles this gracefully:
811

‎main.py‎

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,14 +1395,26 @@ def is_fork_pr() -> bool:
13951395
return False
13961396

13971397

1398+
#: The actor GitHub reports for pull requests Dependabot opens.
1399+
DEPENDABOT_ACTOR = "dependabot[bot]"
1400+
1401+
13981402
def is_fork_pr_with_readonly_token() -> bool:
1399-
"""Returns True when the PR is from a fork AND the event has a read-only token.
1403+
"""Returns True when this run's GITHUB_TOKEN cannot write to the pull request.
14001404
14011405
Under the pull_request event, GITHUB_TOKEN is read-only for fork PRs.
1406+
GitHub runs Dependabot-triggered pull_request workflows with a read-only
1407+
token as well, although a Dependabot branch lives in the repository
1408+
itself, so those runs take the same path: judged by ``is_fork_pr()``
1409+
alone the action tried to comment, got a 403, and advised granting
1410+
``pull-requests: write`` — which the workflow had already done.
1411+
14021412
Under pull_request_target, GITHUB_TOKEN has the workflow's configured
1403-
permissions regardless of whether the PR is from a fork.
1413+
permissions regardless of where, or from whom, the PR came.
14041414
"""
1405-
return is_fork_pr() and os.getenv("GITHUB_EVENT_NAME", "") != "pull_request_target"
1415+
if os.getenv("GITHUB_EVENT_NAME", "") == "pull_request_target":
1416+
return False
1417+
return is_fork_pr() or os.getenv("GITHUB_ACTOR", "") == DEPENDABOT_ACTOR
14061418

14071419

14081420
def get_pr_number() -> int:
@@ -1471,27 +1483,28 @@ def add_pr_comments(results: list[ScopeResult]) -> int:
14711483
print("Skipping PR comment: not a pull request event.")
14721484
return 0
14731485

1474-
# Fork PRs triggered by the pull_request event receive a read-only token;
1475-
# the GitHub API will always reject comment writes with 403.
1476-
# pull_request_target events always have the configured token permissions.
1486+
# Fork PRs and Dependabot PRs triggered by the pull_request event receive
1487+
# a read-only token; the GitHub API will always reject comment writes with
1488+
# 403. pull_request_target events always have the configured permissions.
14771489
if is_fork_pr_with_readonly_token():
14781490
msg = (
1479-
"Skipping PR comment: pull requests from forked repositories "
1480-
"cannot write comments via the pull_request event (GITHUB_TOKEN is "
1481-
"read-only for forks). "
1491+
"Skipping PR comment: GITHUB_TOKEN is read-only for this run. "
1492+
"Pull requests from forked repositories, and pull requests opened "
1493+
"by Dependabot, cannot write comments via the pull_request event. "
14821494
"See https://github.com/commit-check/commit-check-action/blob/main/docs/fork-pr-comments.md "
1483-
"for how to enable PR comments on fork PRs."
1495+
"for how to enable PR comments on them."
14841496
)
14851497
print(f"::warning::{msg}")
14861498
if JOB_SUMMARY_ENABLED and GITHUB_STEP_SUMMARY:
14871499
with open(GITHUB_STEP_SUMMARY, "a", encoding="utf-8") as f:
14881500
f.write(
14891501
"\n---\n"
14901502
"### \u2139\ufe0f PR Comment Skipped\n\n"
1491-
"Pull requests from forked repositories cannot write comments "
1492-
"using the `pull_request` event because `GITHUB_TOKEN` has "
1493-
"read-only permissions.\n\n"
1494-
"> **\U0001f4a1 Tip:** To enable PR comments on fork PRs, see "
1503+
"Pull requests from forked repositories, and pull requests "
1504+
"opened by Dependabot, cannot write comments using the "
1505+
"`pull_request` event because `GITHUB_TOKEN` has read-only "
1506+
"permissions.\n\n"
1507+
"> **\U0001f4a1 Tip:** To enable PR comments on them, see "
14951508
"[Enabling PR Comments on Fork Pull Requests]"
14961509
"(https://github.com/commit-check/commit-check-action/blob/main/docs/fork-pr-comments.md).\n"
14971510
)

‎main_test.py‎

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1843,6 +1843,30 @@ def test_push_event_skips_comment_without_warning(self):
18431843
[line for line in printed if line.startswith("::warning")], printed
18441844
)
18451845

1846+
def test_dependabot_pr_takes_the_skip_path_and_names_dependabot(self):
1847+
with (
1848+
patch("main.PR_COMMENTS_ENABLED", True),
1849+
patch.dict(
1850+
os.environ,
1851+
{
1852+
"GITHUB_EVENT_NAME": "pull_request",
1853+
"GITHUB_ACTOR": "dependabot[bot]",
1854+
"GITHUB_REF": "refs/pull/12/merge",
1855+
},
1856+
),
1857+
patch("main.is_fork_pr", return_value=False),
1858+
patch("main.JOB_SUMMARY_ENABLED", False),
1859+
patch("main.get_pr_number") as mock_number,
1860+
patch("builtins.print") as mock_print,
1861+
):
1862+
rc = main.add_pr_comments([fail_scope()])
1863+
self.assertEqual(rc, 0)
1864+
mock_number.assert_not_called() # never reached the API
1865+
warning = mock_print.call_args_list[0][0][0]
1866+
self.assertTrue(warning.startswith("::warning::Skipping PR comment"), warning)
1867+
self.assertIn("Dependabot", warning)
1868+
self.assertNotIn("403", warning)
1869+
18461870
def test_fork_pr_skips_comment_and_warns(self):
18471871
with (
18481872
patch("main.PR_COMMENTS_ENABLED", True),
@@ -2089,7 +2113,40 @@ def test_fork_pr_with_pull_request_target_event(self):
20892113
def test_same_repo_not_fork(self):
20902114
with (
20912115
patch("main.is_fork_pr", return_value=False),
2092-
patch.dict(os.environ, {"GITHUB_EVENT_NAME": "pull_request"}),
2116+
patch.dict(
2117+
os.environ,
2118+
{"GITHUB_EVENT_NAME": "pull_request", "GITHUB_ACTOR": "octocat"},
2119+
),
2120+
):
2121+
self.assertFalse(main.is_fork_pr_with_readonly_token())
2122+
2123+
def test_dependabot_pull_request_has_a_readonly_token(self):
2124+
"""A Dependabot branch is in the same repository, so is_fork_pr() is
2125+
False, yet GitHub hands its pull_request runs a read-only token. It
2126+
used to fall through to the API, take a 403, and tell the user to
2127+
grant a permission the workflow already had."""
2128+
with (
2129+
patch("main.is_fork_pr", return_value=False),
2130+
patch.dict(
2131+
os.environ,
2132+
{
2133+
"GITHUB_EVENT_NAME": "pull_request",
2134+
"GITHUB_ACTOR": "dependabot[bot]",
2135+
},
2136+
),
2137+
):
2138+
self.assertTrue(main.is_fork_pr_with_readonly_token())
2139+
2140+
def test_dependabot_pull_request_target_has_a_write_token(self):
2141+
with (
2142+
patch("main.is_fork_pr", return_value=False),
2143+
patch.dict(
2144+
os.environ,
2145+
{
2146+
"GITHUB_EVENT_NAME": "pull_request_target",
2147+
"GITHUB_ACTOR": "dependabot[bot]",
2148+
},
2149+
),
20932150
):
20942151
self.assertFalse(main.is_fork_pr_with_readonly_token())
20952152

0 commit comments

Comments
 (0)