5
5
6
6
import argparse
7
7
import datetime
8
+ import hashlib
8
9
import os
10
+ import re
9
11
import sys
10
12
import time
11
13
from collections import defaultdict
@@ -26,6 +28,49 @@ def log(s):
26
28
print (s , file = sys .stdout )
27
29
28
30
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
+
29
74
def parse_args ():
30
75
global args
31
76
parser = argparse .ArgumentParser (
@@ -250,6 +295,7 @@ def process_pr(gh, maintainer_file, number):
250
295
else :
251
296
log ("Too many labels to be applied" )
252
297
298
+ reviewers_added = False
253
299
if collab :
254
300
reviewers = []
255
301
existing_reviewers = set ()
@@ -297,15 +343,19 @@ def process_pr(gh, maintainer_file, number):
297
343
reviewers = list (_all_maintainers .keys ())
298
344
299
345
if reviewers :
346
+ reviewers_added = True
300
347
try :
301
348
log (f"adding reviewers { reviewers } ..." )
302
349
if not args .dry_run :
303
350
pr .create_review_request (reviewers = reviewers )
304
351
except GithubException :
305
- log ("can't add reviewer" )
352
+ log ("can't add reviewers" )
353
+ else :
354
+ log ("no reviewers to add" )
306
355
307
356
ms = []
308
357
# assignees
358
+ assignees_added = False
309
359
if assignees and (not pr .assignee or args .dry_run ):
310
360
try :
311
361
for assignee in assignees :
@@ -315,12 +365,36 @@ def process_pr(gh, maintainer_file, number):
315
365
log ("Error: Unknown user" )
316
366
317
367
for mm in ms :
368
+ assignees_added = True
318
369
log (f"Adding assignee { mm } ..." )
319
370
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." )
321
375
else :
322
376
log ("not setting assignee" )
323
377
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
+
324
398
time .sleep (1 )
325
399
326
400
0 commit comments