Skip to content

Commit 972ebb6

Browse files
nashifhenrikbrixandersen
authored andcommitted
ci: set_assignee: add release team on PRs without review
In cases where we are not able to assign a PR due to lack of matches in the maintainer file, add the release team to the reviewers so someone can triage it. Also add a comment to make it clear that the release team was added for triage purposes only. Signed-off-by: Anas Nashif <[email protected]>
1 parent 804508f commit 972ebb6

File tree

1 file changed

+76
-2
lines changed

1 file changed

+76
-2
lines changed

scripts/set_assignees.py

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
import argparse
77
import datetime
8+
import hashlib
89
import os
10+
import re
911
import sys
1012
import time
1113
from collections import defaultdict
@@ -26,6 +28,49 @@ def log(s):
2628
print(s, file=sys.stdout)
2729

2830

31+
SIGN_PREFIX = "<!--bot-signature:"
32+
SIGN_SUFFIX = "-->"
33+
34+
def normalize(text: str) -> str:
35+
text = text.replace('\r\n', '\n').strip()
36+
text = re.sub(r'[ \t]+', ' ', text)
37+
text = re.sub(r'\n{3,}', '\n\n', text)
38+
return text
39+
40+
def make_signature(body: str, tag: str = "default") -> str:
41+
h = hashlib.sha256(normalize(body).encode("utf-8")).hexdigest()[:16]
42+
return f"{SIGN_PREFIX}{tag}:{h}{SIGN_SUFFIX}"
43+
44+
def post_or_update_signed_comment(pr, body: str, tag: str = "default", update_if_found=True):
45+
issue = pr.as_issue()
46+
sig = make_signature(body, tag)
47+
signed_body = f"{body}\n\n{sig}"
48+
49+
# Try to detect the bot/user login for “same author” filtering
50+
try:
51+
me = pr.base.repo._requester._Requester__authorizationProvider.user.login
52+
except Exception:
53+
me = None
54+
55+
existing = None
56+
for c in issue.get_comments():
57+
if me and c.user.login != me:
58+
continue
59+
if sig in c.body:
60+
existing = c
61+
break
62+
63+
if existing:
64+
if update_if_found and normalize(existing.body) != normalize(signed_body):
65+
existing.edit(signed_body)
66+
print("Updated existing signed comment.")
67+
return existing
68+
print("Signed comment already present, skipping.")
69+
return existing
70+
71+
# No signed comment found → create new
72+
return issue.create_comment(signed_body)
73+
2974
def parse_args():
3075
global args
3176
parser = argparse.ArgumentParser(
@@ -250,6 +295,7 @@ def process_pr(gh, maintainer_file, number):
250295
else:
251296
log("Too many labels to be applied")
252297

298+
reviewers_added = False
253299
if collab:
254300
reviewers = []
255301
existing_reviewers = set()
@@ -297,15 +343,19 @@ def process_pr(gh, maintainer_file, number):
297343
reviewers = list(_all_maintainers.keys())
298344

299345
if reviewers:
346+
reviewers_added = True
300347
try:
301348
log(f"adding reviewers {reviewers}...")
302349
if not args.dry_run:
303350
pr.create_review_request(reviewers=reviewers)
304351
except GithubException:
305-
log("can't add reviewer")
352+
log("can't add reviewers")
353+
else:
354+
log("no reviewers to add")
306355

307356
ms = []
308357
# assignees
358+
assignees_added = False
309359
if assignees and (not pr.assignee or args.dry_run):
310360
try:
311361
for assignee in assignees:
@@ -315,12 +365,36 @@ def process_pr(gh, maintainer_file, number):
315365
log("Error: Unknown user")
316366

317367
for mm in ms:
368+
assignees_added = True
318369
log(f"Adding assignee {mm}...")
319370
if not args.dry_run:
320-
pr.add_to_assignees(mm)
371+
try:
372+
pr.add_to_assignees(mm)
373+
except GithubException:
374+
log("can't add assignees.")
321375
else:
322376
log("not setting assignee")
323377

378+
379+
teams = [
380+
"release",
381+
]
382+
if not (assignees_added or reviewers_added):
383+
log("No assignees or reviewers could be found, adding triage team")
384+
# if we could not find any assignees or reviewers, add a comment to the PR
385+
# and add release as reviewer to make sure someone looks at it.
386+
if not args.dry_run:
387+
team = teams[0]
388+
# Use a comment with a signature so that we can update it if needed
389+
comment = (
390+
f"Could not find any assignees or reviewers for this PR, adding "
391+
f"@zephyrproject-rtos/{team} as reviewer to make sure someone looks at it. "
392+
f"@zephyrproject-rtos/{team}, please triage and assign this PR "
393+
f"appropriately. Thanks!"
394+
)
395+
post_or_update_signed_comment(pr, comment, tag="triage")
396+
pr.create_review_request(team_reviewers=teams)
397+
324398
time.sleep(1)
325399

326400

0 commit comments

Comments
 (0)