@@ -23,43 +23,44 @@ def extract_github_mentions(text: str | None) -> list[str]:
2323
2424
2525def extract_controlled_mentions (text : str | None ) -> list [str ]:
26- """Extract mentions only from controlled collaboration sections.
27-
28- Supported formats:
29- - `相关人员: @alice @bob`
30- - `协作请求:` followed by bullet lines like `- @alice`
31- """
26+ """Extract mentions only from the trailing controlled collaboration section."""
3227 if not text :
3328 return []
3429
30+ lines = text .splitlines ()
31+ if not lines :
32+ return []
33+
34+ end = len (lines ) - 1
35+ while end >= 0 and not lines [end ].strip ():
36+ end -= 1
37+ if end < 0 :
38+ return []
39+
3540 result : list [str ] = []
3641 seen : set [str ] = set ()
37- in_list_section = False
3842
39- for raw_line in text .splitlines ():
40- line = raw_line .strip ()
43+ # Case 1: 文末为单行 "相关人员: @a @b"
44+ tail = lines [end ].strip ()
45+ if "相关人员:" in tail :
46+ suffix = tail .split ("相关人员:" , 1 )[1 ]
47+ for username in extract_github_mentions (suffix ):
48+ if username not in seen :
49+ seen .add (username )
50+ result .append (username )
51+ return result
4152
42- if "相关人员:" in line :
43- suffix = line .split ("相关人员:" , 1 )[1 ]
44- for username in extract_github_mentions (suffix ):
53+ # Case 2: 文末为列表形式
54+ list_lines : list [str ] = []
55+ idx = end
56+ while idx >= 0 and re .match (r"^\s*-\s+@" , lines [idx ]):
57+ list_lines .append (lines [idx ])
58+ idx -= 1
59+
60+ if list_lines and idx >= 0 and lines [idx ].strip ().startswith ("协作请求:" ):
61+ for line in reversed (list_lines ):
62+ for username in extract_github_mentions (line ):
4563 if username not in seen :
4664 seen .add (username )
4765 result .append (username )
48- in_list_section = False
49- continue
50-
51- if line .startswith ("协作请求:" ):
52- in_list_section = True
53- continue
54-
55- if in_list_section :
56- if re .match (r"^\s*-\s+@" , raw_line ):
57- for username in extract_github_mentions (raw_line ):
58- if username not in seen :
59- seen .add (username )
60- result .append (username )
61- continue
62- if line and not line .startswith ("-" ):
63- in_list_section = False
64-
6566 return result
0 commit comments