6
6
import os
7
7
from urllib .parse import urlparse
8
8
from urllib .request import urlopen
9
- from github import Github , GithubException
9
+ from ghapi . all import GhApi
10
10
from .GenericTreeScanner import GenericTreeScanner
11
11
12
12
@@ -33,37 +33,15 @@ def __init__(self, url, rt_load=False):
33
33
self .url_path = parsed_url .path .split ('/' , maxsplit = 5 )
34
34
if len (self .url_path ) < 3 :
35
35
raise Exception (f'Bad GitHub URL: { url } ' )
36
- self .ghe = Github (base_url = ghe_base_url , login_or_token = os .environ .get ('GHE_TOKEN' ))
37
- self .repo = self ._get_repo ()
38
- self .ref = self .url_path [4 ] if len (self .url_path ) >= 5 else 'master'
39
-
40
- def _get_repo (self ):
41
- """
42
- :return: An instance of the Repository API class matching the repository in the url
43
- :rtype: github.Repository.Repository
44
- """
45
- try :
46
- org = self .ghe .get_organization (str (self .url_path [1 ]))
47
- except GithubException :
48
- try :
49
- org = self .ghe .get_user (self .url_path [1 ])
50
- except GithubException :
51
- org = None
52
- if org is None :
53
- raise Exception (f'GitHub URL { self .url } does not point to a valid repository' )
54
-
55
- try :
56
- repo = org .get_repo (str (self .url_path [2 ]))
57
- except GithubException :
58
- repo = None
59
- if repo is None :
60
- raise Exception (f'GitHub URL { self .url } does not point to a valid repository' )
61
- return repo
36
+ self .ghe = GhApi (gh_host = ghe_base_url , owner = self .url_path [1 ], repo = self .url_path [2 ],
37
+ token = os .environ .get ('GHE_TOKEN' ))
38
+ self .ref = 'heads/' + (self .url_path [4 ] if len (self .url_path ) >= 5 else 'master' )
62
39
63
40
def _scan_dir_in_repo (self , path , recursive ):
64
41
if path and not path .endswith ('/' ):
65
42
path += '/'
66
- git_tree = self .repo .get_git_tree (self .ref , True )
43
+ ref = self .ghe .git .get_ref (ref = self .ref )
44
+ git_tree = self .ghe .git .get_tree (tree_sha = ref .object .sha , recursive = 'True' )
67
45
for element in git_tree .tree :
68
46
if element .type != 'blob' :
69
47
continue
@@ -74,7 +52,7 @@ def _scan_dir_in_repo(self, path, recursive):
74
52
if not recursive and element .path .count ('/' ) != path .count ('/' ):
75
53
continue
76
54
77
- yield from self ._yield_yaml_file (element .path , self .repo . get_contents ( element .path , self . ref ), True )
55
+ yield from self ._yield_yaml_file (element .path , self .ghe . get_content ( path = element .path ) )
78
56
79
57
def get_yamls (self ):
80
58
"""
@@ -94,7 +72,7 @@ def get_yamls(self):
94
72
path_in_repo = '' if len (self .url_path ) == 5 else self .url_path [5 ]
95
73
96
74
if is_file :
97
- return self ._yield_yaml_file (path_in_repo , self .repo . get_contents (path_in_repo , self . ref ), True )
75
+ return self ._yield_yaml_file (path_in_repo , self .ghe . get_content (path_in_repo ) )
98
76
if path_in_repo .endswith ('**' ):
99
77
return self ._scan_dir_in_repo (path_in_repo [:- 2 ], True ) # path_in_repo without **
100
78
return self ._scan_dir_in_repo (path_in_repo , False )
0 commit comments