1717import github
1818import json
1919from github import Github
20- from fnmatch import fnmatch
20+ import re
2121from collections import Counter
2222from pathlib import Path
2323
24+ def pattern_to_regex (pattern ):
25+ start_anchor = pattern .startswith ("/" )
26+ pattern = re .escape (pattern )
27+ # Replace `*` with "any number of non-slash characters"
28+ pattern = pattern .replace (r"\*" , "[^/]*" )
29+ if start_anchor :
30+ pattern = "^" + pattern
31+ return pattern
32+
2433def get_file_owners (file_path , codeowners_lines ):
2534 # Process lines in reverse (last matching pattern takes precedence)
2635 for line in reversed (codeowners_lines ):
@@ -36,18 +45,20 @@ def get_file_owners(file_path, codeowners_lines):
3645 owners = [owner .removeprefix ("@" ) for owner in parts [1 :]]
3746
3847 # Check if file matches pattern
39- if fnmatch (file_path , pattern ):
48+ file_regex = pattern_to_regex (pattern )
49+ if re .search (file_regex , file_path ) is not None :
4050 return owners # Remember, can still be empty!
4151 return [] # Should never happen, but just in case
4252
4353def main ():
54+ script_dir = Path (__file__ ).parent .absolute ()
55+ with open (script_dir / "codeowners_for_review_action" ) as f :
56+ codeowners_lines = f .readlines ()
57+
4458 g = Github (os .environ ['GITHUB_TOKEN' ])
4559 repo = g .get_repo ("huggingface/transformers" )
4660 with open (os .environ ['GITHUB_EVENT_PATH' ]) as f :
4761 event = json .load (f )
48- script_dir = Path (__file__ ).parent .absolute ()
49- with open (script_dir / "codeowners_for_review_action" ) as f :
50- codeowners_lines = f .readlines ()
5162
5263 # The PR number is available in the event payload
5364 pr_number = event ['pull_request' ]['number' ]
0 commit comments