@@ -30,9 +30,23 @@ def rebuild_cache(self):
3030 for host in ctx .options .hosts .split (',' ):
3131 self .add_to_maps (host .strip ())
3232
33+ def is_public_repo (self , repo ):
34+ if repo in self .repo_map :
35+ return self .repo_map [repo ]
36+
37+ repo_path = 'repos' if '/' in repo else 'repositories'
38+ url = f'https://api.github.com/{ repo_path } /{ repo } '
39+ response = requests .get (url , headers = {'Authorization' : 'Bearer %s' % ctx .options .token })
40+ if response .status_code == 200 :
41+ self .repo_map [repo ] = response .json ()['private' ] == False
42+ return self .repo_map [repo ]
43+ else :
44+ return False
45+
3346 def __init__ (self ):
3447 self .ip_map = {}
3548 self .dns_map = {}
49+ self .repo_map = {}
3650
3751 self .methods_map = {
3852 'GET' : HTTP .GET ,
@@ -44,9 +58,6 @@ def __init__(self):
4458
4559 # a map of tricky permissions, that do not fall into a pattern of (GET|POST|etc) /repos/{owner}/{repo}/{what}/{id} -> {what, permission}
4660 map = {
47- ('GET' , '/repos/{owner}/{repo}/environments' , 'actions' , 'read' ),
48- ('GET' , '/repositories/{id}/environments' , 'actions' , 'read' ),
49-
5061 ('GET' , '/repos/{owner}/{repo}/codeowners/errors' , 'contents' , 'read' ),
5162 ('GET' , '/repositories/{id}/codeowners/errors' , 'contents' , 'read' ),
5263 ('PUT' , '/repos/{owner}/{repo}/pulls/{pull_number}/merge' , 'contents' , 'write' ),
@@ -303,6 +314,12 @@ def get_permission(self, path, method, query):
303314 # Get the permission by the pattern of (GET|POST|etc) /repos/{owner}/{repo}/{what}/{id} -> {what, permission}
304315 if len (path_segments ) >= 5 :
305316 if path_segments [1 ] == 'repos' and path_segments [4 ] == 'actions' :
317+ if method == 'GET' and self .is_public_repo (f'{ path_segments [2 ]} /{ path_segments [3 ]} ' ):
318+ return []
319+ return [('actions' , 'read' if method == 'GET' else 'write' )]
320+ elif path_segments [1 ] == 'repos' and path_segments [4 ] == 'environments' :
321+ if method == 'GET' and self .is_public_repo (f'{ path_segments [2 ]} /{ path_segments [3 ]} ' ):
322+ return []
306323 return [('actions' , 'read' if method == 'GET' else 'write' )]
307324 elif path_segments [1 ] == 'repos' and (path_segments [4 ] == 'check-runs' or path_segments [4 ] == 'check-suites' ):
308325 return [('checks' , 'read' if method == 'GET' else 'write' )]
@@ -332,6 +349,12 @@ def get_permission(self, path, method, query):
332349
333350 if len (path_segments ) >= 4 :
334351 if path_segments [1 ] == 'repositories' and path_segments [3 ] == 'actions' :
352+ if method == 'GET' and self .is_public_repo (path_segments [2 ]):
353+ return []
354+ return [('actions' , 'read' if method == 'GET' else 'write' )]
355+ elif path_segments [1 ] == 'repositories' and path_segments [3 ] == 'environments' :
356+ if method == 'GET' and self .is_public_repo (path_segments [2 ]):
357+ return []
335358 return [('actions' , 'read' if method == 'GET' else 'write' )]
336359 elif path_segments [1 ] == 'repositories' and (path_segments [3 ] == 'check-runs' or path_segments [3 ] == 'check-suites' ):
337360 return [('checks' , 'read' if method == 'GET' else 'write' )]
0 commit comments