11import github
22import click
3- import urllib . request
3+
44from pathlib import Path
55from typing import List , Tuple
66import re
2323]
2424
2525
26- def xor_strings (a , b ):
27- result = int (a , 16 ) ^ int (b , 16 )
28- return "{:x}" .format (result )
29-
30-
3126def get_github_client ():
32- bot_token = "1353d990cdb8b8ceb1b73d301dce83cc0da3db29"
33- bot_token_key = "a1b2c3d47311f8e29e204f85a81b4df4a44e252c"
34-
35- return github .Github (xor_strings (bot_token , bot_token_key ))
27+ return github .Github (os .environ ["BOT_TOKEN" ])
3628
3729
3830CLIENT = get_github_client ()
3931
32+ # faster checks
33+ valid_users_cache = set ()
34+
4035
4136def check_user (user : str , line_idx : int ):
37+ print (f"Checking that { user } actually exists" )
4238 if user [0 ] != "@" :
4339 raise ValueError (
4440 f"User '{ user } ' at line { line_idx } of CODEOWNERS "
@@ -49,7 +45,10 @@ def check_user(user: str, line_idx: int):
4945 if user in WRITE_ACCESS_LIST :
5046 return None
5147 try :
48+ if user in valid_users_cache :
49+ return user
5250 CLIENT .get_user (user )
51+ valid_users_cache .add (user )
5352 except github .UnknownObjectException :
5453 raise KeyError (
5554 f"User '{ user } ' line { line_idx } does not exist. Did you make a typo?"
@@ -158,12 +157,8 @@ def get_pull_request_id_from_gh_actions():
158157@click .command ()
159158@click .option ("--pull-request-id" )
160159@click .option ("--no-dry-run" , is_flag = True )
161- @click .argument ("file" )
162- def notify_codeowners (pull_request_id , no_dry_run , file ):
163- if file .startswith ("http" ):
164- text = urllib .request .urlopen (file ).read ().decode ("utf-8" )
165- else :
166- text = Path (file ).read_text ()
160+ def notify_codeowners (pull_request_id , no_dry_run ):
161+ text = (Path (__file__ ).parents [1 ] / "CODEOWNERS" ).read_text ()
167162 codeowners = parse_codeowners (text )
168163
169164 if pull_request_id is not None :
0 commit comments