Skip to content

Commit d2daa87

Browse files
committed
fix: label on GitHub Enterprise and when another run creates the label first
1 parent f074938 commit d2daa87

3 files changed

Lines changed: 41 additions & 6 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ runners need a few tools — see [Good to know](#good-to-know).
8080
| `author-email` | `false` | Check each commit's author email |
8181
| `pr-title` | `false` | Check the pull request title against Conventional Commits — the one that matters for squash merges. Pull request events only |
8282
| `pr-body` | `false` | Check the pull request description against the AI rules of `cchk.toml` (`ai_attribution`) only. Pull request events only |
83-
| `ai-label` | `false` | Label the pull request when an AI tool wrote any of it (a commit, the description, or an agent's app that opened it): `true` for `ai-assisted`, or the label's name. Labels are only added, never removed. Needs `pull-requests: write`; skipped on fork pull requests |
83+
| `ai-label` | `false` | Label the pull request when an AI tool wrote any of it (a commit, the description, or an agent's app that opened it): `true` for `ai-assisted`, or the label's name. Labels are only added, never removed. Needs `pull-requests: write`; skipped on fork pull requests under `pull_request` |
8484
| `pr-comments` | `false` | Post the report as a pull request comment, edited in place on later runs. Needs `pull-requests: write`; skipped on fork pull requests |
8585
| `job-summary` | `true` | Write the report to the job summary |
8686
| `dry-run` | `false` | Report failures as warnings and always exit 0 |

‎main.py‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,7 +1911,8 @@ def add_ai_label(results: list[ScopeResult]) -> None:
19111911
if not repo_name:
19121912
raise ValueError("GITHUB_REPOSITORY is not set")
19131913
pr_number = get_pr_number()
1914-
repo = Github(auth=Auth.Token(token)).get_repo(repo_name)
1914+
api_url = os.getenv("GITHUB_API_URL", "https://api.github.com")
1915+
repo = Github(auth=Auth.Token(token), base_url=api_url).get_repo(repo_name)
19151916
issue = repo.get_issue(pr_number)
19161917
if any(label.name == name for label in issue.labels):
19171918
print(f"PR #{pr_number} already has the label '{name}' ({found}).")
@@ -1921,9 +1922,14 @@ def add_ai_label(results: list[ScopeResult]) -> None:
19211922
except GithubException as e:
19221923
if e.status != 404:
19231924
raise
1924-
repo.create_label(
1925-
name, AI_LABEL_COLOR, "Written with the help of an AI tool"
1926-
)
1925+
try:
1926+
repo.create_label(
1927+
name, AI_LABEL_COLOR, "Written with the help of an AI tool"
1928+
)
1929+
except GithubException as created:
1930+
# 422: another run created it in the meantime; use that one.
1931+
if created.status != 422:
1932+
raise
19271933
issue.add_to_labels(name)
19281934
print(f"Added the label '{name}' to PR #{pr_number}: {found}.")
19291935
except GithubException as e:

‎main_test.py‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3784,7 +3784,9 @@ class TestAddAiLabel(unittest.TestCase):
37843784
def setUp(self):
37853785
main.PR_BODY_AI_TOOLS.clear()
37863786

3787-
def _run(self, results, label="true", labels=(), get_label=None, add=None):
3787+
def _run(
3788+
self, results, label="true", labels=(), get_label=None, add=None, create=None
3789+
):
37883790
issue = MagicMock()
37893791
issue.labels = [MagicMock() for _ in labels]
37903792
for mock_label, name in zip(issue.labels, labels):
@@ -3795,6 +3797,8 @@ def _run(self, results, label="true", labels=(), get_label=None, add=None):
37953797
repo.get_issue.return_value = issue
37963798
if get_label is not None:
37973799
repo.get_label.side_effect = get_label
3800+
if create is not None:
3801+
repo.create_label.side_effect = create
37983802
github_module = MagicMock()
37993803
github_module.GithubException = _StubGithubException
38003804
github_module.Github.return_value.get_repo.return_value = repo
@@ -3807,6 +3811,7 @@ def _run(self, results, label="true", labels=(), get_label=None, add=None):
38073811
"GITHUB_REPOSITORY": "owner/repo",
38083812
"GITHUB_EVENT_NAME": "pull_request",
38093813
"GITHUB_REF": "refs/pull/12/merge",
3814+
"GITHUB_API_URL": "https://ghe.example.com/api/v3",
38103815
},
38113816
),
38123817
patch("main.is_fork_pr_with_readonly_token", return_value=False),
@@ -3841,6 +3846,30 @@ def test_creates_a_missing_label(self):
38413846
)
38423847
issue.add_to_labels.assert_called_once_with("ai-assisted")
38433848

3849+
def test_uses_the_api_url_of_the_server(self):
3850+
_repo, _issue, _printed, github_module = self._run([self.AI_SCOPE])
3851+
self.assertEqual(
3852+
github_module.Github.call_args.kwargs["base_url"],
3853+
"https://ghe.example.com/api/v3",
3854+
)
3855+
3856+
def test_a_label_created_meanwhile_is_still_added(self):
3857+
_repo, issue, printed, _ = self._run(
3858+
[self.AI_SCOPE],
3859+
get_label=_StubGithubException(404),
3860+
create=_StubGithubException(422),
3861+
)
3862+
issue.add_to_labels.assert_called_once_with("ai-assisted")
3863+
self.assertIn("Added the label 'ai-assisted' to PR #12: Claude Code.", printed)
3864+
3865+
def test_a_label_that_cannot_be_created_is_not_added(self):
3866+
_repo, issue, _printed, _ = self._run(
3867+
[self.AI_SCOPE],
3868+
get_label=_StubGithubException(404),
3869+
create=_StubGithubException(403),
3870+
)
3871+
issue.add_to_labels.assert_not_called()
3872+
38443873
def test_already_labelled(self):
38453874
_repo, issue, printed, _ = self._run([self.AI_SCOPE], labels=("ai-assisted",))
38463875
issue.add_to_labels.assert_not_called()

0 commit comments

Comments
 (0)