|
24 | 24 | PROFILE = re.compile(r"\[@([a-z0-9-]+)\]\(https://github\.com/([a-z0-9-]+)/?\)", re.I) |
25 | 25 | ACCEPTANCE_ERROR = ( |
26 | 26 | "PR is not accepted work: the author needs repository write access " |
27 | | - "or a referenced state:accepted issue" |
| 27 | + "or state:accepted on the PR or a referenced issue" |
| 28 | +) |
| 29 | +DRAFT_COMMENT_MARKER = "<!-- openshell-review-readiness:unaccepted-work -->" |
| 30 | +DRAFT_COMMENT = ( |
| 31 | + "This PR has been automatically moved to draft because it's not associated " |
| 32 | + "with an issue that is accepted work. You're welcome to continue to work on " |
| 33 | + "it while it's in draft mode; it will be allowed to move to \"Ready for Review\" " |
| 34 | + "when the issue or PR have been marked accepted.\n\n" |
| 35 | + + DRAFT_COMMENT_MARKER |
28 | 36 | ) |
29 | 37 |
|
30 | 38 |
|
@@ -243,7 +251,9 @@ def _referenced_issues(repository: str, number: int, body: str): |
243 | 251 |
|
244 | 252 |
|
245 | 253 | def _accepted_work(repository: str, number: int, pr: dict) -> bool: |
246 | | - """Accept write access or a referenced accepted issue.""" |
| 254 | + """Accept write access or acceptance recorded on the PR or a referenced issue.""" |
| 255 | + if any(label["name"] == "state:accepted" for label in pr["labels"]): |
| 256 | + return True |
247 | 257 | owner, repo = repository.split("/") |
248 | 258 | author = pr["user"]["login"].lower() |
249 | 259 | cursor = None |
@@ -341,6 +351,13 @@ def check_review_readiness( |
341 | 351 | }""", |
342 | 352 | id=pr["node_id"], |
343 | 353 | ) |
| 354 | + comments = f"repos/{repository}/issues/{number}/comments" |
| 355 | + previous = _api(f"{comments}?per_page=100", pages=True) |
| 356 | + if not any( |
| 357 | + DRAFT_COMMENT_MARKER in (comment.get("body") or "") |
| 358 | + for comment in previous |
| 359 | + ): |
| 360 | + _api(comments, method="POST", data={"body": DRAFT_COMMENT}) |
344 | 361 | raise ValueError(ACCEPTANCE_ERROR) |
345 | 362 | categories = parse_reviewers(reviewers.read_text(encoding="utf-8")) |
346 | 363 | selected = select_reviewers(categories, {label["name"] for label in pr["labels"]}) |
@@ -395,14 +412,29 @@ def main(argv: list[str] | None = None) -> int: |
395 | 412 | pr = json.loads(args.event_path.read_text(encoding="utf-8"))["pull_request"] |
396 | 413 | else: |
397 | 414 | owner, repo = args.repo.split("/") |
398 | | - pr = _graphql( |
399 | | - """query($owner: String!, $repo: String!, $number: Int!) { |
400 | | - repository(owner: $owner, name: $repo) { |
401 | | - pullRequest(number: $number) { user: author { login } body } |
402 | | - } |
403 | | - }""", |
404 | | - owner=owner, repo=repo, number=args.pr, |
405 | | - )["repository"]["pullRequest"] |
| 415 | + cursor, labels = None, [] |
| 416 | + while True: |
| 417 | + pr = _graphql( |
| 418 | + """query($owner: String!, $repo: String!, $number: Int!, $cursor: String) { |
| 419 | + repository(owner: $owner, name: $repo) { |
| 420 | + pullRequest(number: $number) { |
| 421 | + user: author { login } |
| 422 | + body |
| 423 | + labels(first: 100, after: $cursor) { |
| 424 | + nodes { name } |
| 425 | + pageInfo { hasNextPage endCursor } |
| 426 | + } |
| 427 | + } |
| 428 | + } |
| 429 | + }""", |
| 430 | + owner=owner, repo=repo, number=args.pr, cursor=cursor, |
| 431 | + )["repository"]["pullRequest"] |
| 432 | + page = pr["labels"] |
| 433 | + labels.extend(page["nodes"]) |
| 434 | + if not page["pageInfo"]["hasNextPage"]: |
| 435 | + break |
| 436 | + cursor = page["pageInfo"]["endCursor"] |
| 437 | + pr["labels"] = labels |
406 | 438 | if not _accepted_work(args.repo, args.pr, pr): |
407 | 439 | raise ValueError(ACCEPTANCE_ERROR) |
408 | 440 | result = {"accepted": True} |
|
0 commit comments