Skip to content

Commit 6cdf883

Browse files
committed
ci: post reviewers via REST instead of gh pr edit
`gh pr edit --add-reviewer` runs a GraphQL query that touches team login/name/slug fields, all of which require read:org. Our tokens have repo+workflow but not read:org, so the step fails with "Your token has not been granted the required scopes." Switch to POST /repos/{owner}/{repo}/pulls/{n}/requested_reviewers, which only writes and works with the existing pull-requests:write scope. Send team_reviewers (bare slug, not org/slug) and reviewers in separate arrays, each chunked to 15. Signed-off-by: Matt Liberty <mliberty@precisioninno.com>
1 parent 2baff41 commit 6cdf883

1 file changed

Lines changed: 21 additions & 11 deletions

File tree

.github/workflows/github-actions-on-label-create.yml

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -143,21 +143,31 @@ jobs:
143143
if matched:
144144
owners.update(o.lstrip("@") for o in matched)
145145
146-
teams = sorted(o for o in owners if "/" in o)
146+
# CODEOWNERS lists teams as "org/slug"; the REST endpoint wants the
147+
# bare slug in team_reviewers.
148+
team_slugs = sorted(t.split("/", 1)[1] for t in owners if "/" in t)
147149
users = sorted(o for o in owners
148150
if "/" not in o and o.lower() != author.lower())
149-
reviewers = teams + users
150151
151-
if not reviewers:
152+
if not (team_slugs or users):
152153
print("No CODEOWNERS-matched reviewers.")
153154
sys.exit(0)
154155
155-
# GitHub's request-reviewers endpoint caps each array at 15; chunk so
156-
# PRs that match many owner teams don't get 422'd.
157-
for i in range(0, len(reviewers), 15):
158-
batch = reviewers[i:i + 15]
159-
print("Requesting:", batch)
160-
subprocess.check_call(
161-
["gh", "pr", "edit", pr, "--repo", upstream,
162-
"--add-reviewer", ",".join(batch)])
156+
# Use the REST POST endpoint directly: `gh pr edit --add-reviewer`
157+
# runs a GraphQL query that needs read:org, which our tokens don't
158+
# have. POST /pulls/{n}/requested_reviewers only writes, so the
159+
# existing repo / pull-requests:write scope is enough. Each array
160+
# is capped at 15 per call.
161+
def request(body):
162+
print("Requesting:", body)
163+
subprocess.run(
164+
["gh", "api", "--method", "POST",
165+
f"repos/{upstream}/pulls/{pr}/requested_reviewers",
166+
"--input", "-"],
167+
input=json.dumps(body), text=True, check=True)
168+
169+
for i in range(0, len(team_slugs), 15):
170+
request({"team_reviewers": team_slugs[i:i + 15]})
171+
for i in range(0, len(users), 15):
172+
request({"reviewers": users[i:i + 15]})
163173
PY

0 commit comments

Comments
 (0)