Skip to content

Commit 8cc935f

Browse files
authored
Replace PyGithub with ghapi (has better license) (#375)
Signed-off-by: Ziv Nevo <[email protected]>
1 parent 190671c commit 8cc935f

File tree

4 files changed

+12
-38
lines changed

4 files changed

+12
-38
lines changed

nca/FileScanners/GenericTreeScanner.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,15 @@ def is_yaml_file(file_name):
4343
extension = os.path.splitext(file_name)[1]
4444
return extension in {'.yaml', '.yml', '.json'}
4545

46-
def _yield_yaml_file(self, path, stream, from_repo=False):
46+
def _yield_yaml_file(self, path, stream):
4747
"""
4848
yields the yaml file for its data
4949
:param str path: the path of the file
5050
:param stream: an IO-Text stream or Union of the file contents, depends on the scanner's type
51-
:param bool from_repo: indicates if the given path is from a repository
5251
"""
53-
decoded_stream = stream
54-
if from_repo:
55-
decoded_stream = stream.decoded_content
5652
yaml = YAML(typ="rt") if self.rt_load else YAML(typ="safe")
5753
try:
58-
yield YamlFile(yaml.load_all(decoded_stream), path)
54+
yield YamlFile(yaml.load_all(stream), path)
5955
except error.MarkedYAMLError as parse_error:
6056
print(f'{parse_error.problem_mark.name}:{parse_error.problem_mark.line}:{parse_error.problem_mark.column}:',
6157
'Parse Error:', parse_error.problem, file=stderr)

nca/FileScanners/GitScanner.py

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import os
77
from urllib.parse import urlparse
88
from urllib.request import urlopen
9-
from github import Github, GithubException
9+
from ghapi.all import GhApi
1010
from .GenericTreeScanner import GenericTreeScanner
1111

1212

@@ -33,37 +33,15 @@ def __init__(self, url, rt_load=False):
3333
self.url_path = parsed_url.path.split('/', maxsplit=5)
3434
if len(self.url_path) < 3:
3535
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')
6239

6340
def _scan_dir_in_repo(self, path, recursive):
6441
if path and not path.endswith('/'):
6542
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')
6745
for element in git_tree.tree:
6846
if element.type != 'blob':
6947
continue
@@ -74,7 +52,7 @@ def _scan_dir_in_repo(self, path, recursive):
7452
if not recursive and element.path.count('/') != path.count('/'):
7553
continue
7654

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))
7856

7957
def get_yamls(self):
8058
"""
@@ -94,7 +72,7 @@ def get_yamls(self):
9472
path_in_repo = '' if len(self.url_path) == 5 else self.url_path[5]
9573

9674
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))
9876
if path_in_repo.endswith('**'):
9977
return self._scan_dir_in_repo(path_in_repo[:-2], True) # path_in_repo without **
10078
return self._scan_dir_in_repo(path_in_repo, False)

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
PyGithub==1.57
1+
ghapi==1.0.3
22
ruamel.yaml==0.17.21
33
Flask==2.2.2
44
PyYAML==6.0

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ classifiers =
1818
[options]
1919
packages = find:
2020
install_requires =
21-
PyGithub==1.57
21+
ghapi==1.0.3
2222
ruamel.yaml==0.17.21
2323
Flask==2.2.2
2424
PyYAML==6.0

0 commit comments

Comments
 (0)