Skip to content

Commit 61bbd70

Browse files
endothermicdevcdecker
authored andcommitted
reckless: reduce github API calls
Github API calls are now ratelimited to 60 per hour. Searching through the subdirectory contents of lightningd/plugins will hit this limit after two searches or installs. The simple answer is to look for the directory rather than verifying a valid entrypoint in a suitably-named directory prior to cloning the repository. Also corrects a bug that was flagging submodules as files while populating directory contents.
1 parent 6fffba0 commit 61bbd70

File tree

1 file changed

+49
-11
lines changed

1 file changed

+49
-11
lines changed

tools/reckless

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,16 @@ class InstInfo:
147147
if self.srctype in [Source.DIRECTORY, Source.LOCAL_REPO]:
148148
depth = 5
149149
elif self.srctype == Source.GITHUB_REPO:
150-
depth = 2
150+
depth = 1
151151

152152
def search_dir(self, sub: SourceDir, subdir: bool,
153153
recursion: int) -> Union[SourceDir, None]:
154154
assert isinstance(recursion, int)
155+
# carveout for archived plugins in lightningd/plugins
156+
if recursion == 0 and 'archive' in sub.name.lower():
157+
pass
155158
# If unable to search deeper, resort to matching directory name
156-
if recursion < 1:
159+
elif recursion < 1:
157160
if sub.name.lower() == self.name.lower():
158161
# Partial success (can't check for entrypoint)
159162
self.name = sub.name
@@ -379,6 +382,42 @@ def populate_local_repo(path: str) -> list:
379382
return basedir.contents
380383

381384

385+
def source_element_from_repo_api(member: dict):
386+
# api accessed via <repo>/contents/
387+
if 'type' in member and 'name' in member and 'git_url' in member:
388+
if member['type'] == 'dir':
389+
return SourceDir(member['git_url'], srctype=Source.GITHUB_REPO,
390+
name=member['name'])
391+
elif member['type'] == 'file':
392+
# Likely a submodule
393+
if member['size'] == 0:
394+
return SourceDir(None, srctype=Source.GITHUB_REPO,
395+
name=member['name'])
396+
return SourceFile(member['name'])
397+
elif member['type'] == 'commit':
398+
# No path is given by the api here
399+
return SourceDir(None, srctype=Source.GITHUB_REPO,
400+
name=member['name'])
401+
# git_url with <repo>/tree/ presents results a little differently
402+
elif 'type' in member and 'path' in member and 'url' in member:
403+
if member['type'] not in ['tree', 'blob']:
404+
logging.debug(f' skipping {member["path"]} type={member["type"]}')
405+
if member['type'] == 'tree':
406+
return SourceDir(member['url'], srctype=Source.GITHUB_REPO,
407+
name=member['path'])
408+
elif member['type'] == 'blob':
409+
# This can be a submodule
410+
if member['size'] == 0:
411+
return SourceDir(member['git_url'], srctype=Source.GITHUB_REPO,
412+
name=member['name'])
413+
return SourceFile(member['path'])
414+
elif member['type'] == 'commit':
415+
# No path is given by the api here
416+
return SourceDir(None, srctype=Source.GITHUB_REPO,
417+
name=member['name'])
418+
return None
419+
420+
382421
def populate_github_repo(url: str) -> list:
383422
# FIXME: This probably contains leftover cruft.
384423
repo = url.split('/')
@@ -398,12 +437,17 @@ def populate_github_repo(url: str) -> list:
398437
repo_name = parsed_url.path.split('/')[start + 1]
399438

400439
# Get details from the github API.
401-
api_url = f'{API_GITHUB_COM}/repos/{repo_user}/{repo_name}/contents/'
440+
if API_GITHUB_COM in url:
441+
api_url = url
442+
else:
443+
api_url = f'{API_GITHUB_COM}/repos/{repo_user}/{repo_name}/contents/'
402444

403445
git_url = api_url
404446
if "api.github.com" in git_url:
405447
# This lets us redirect to handle blackbox testing
448+
logging.debug(f'fetching from gh API: {git_url}')
406449
git_url = (API_GITHUB_COM + git_url.split("api.github.com")[-1])
450+
# Ratelimiting occurs for non-authenticated GH API calls at 60 in 1 hour.
407451
r = urlopen(git_url, timeout=5)
408452
if r.status != 200:
409453
return False
@@ -413,14 +457,8 @@ def populate_github_repo(url: str) -> list:
413457
tree = json.loads(r.read().decode())
414458
contents = []
415459
for sub in tree:
416-
if 'type' in sub and 'name' in sub and 'git_url' in sub:
417-
if sub['type'] == 'dir':
418-
new_sub = SourceDir(sub['git_url'], srctype=Source.GITHUB_REPO,
419-
name=sub['name'])
420-
contents.append(new_sub)
421-
elif sub['type'] == 'file':
422-
new_file = SourceFile(sub['name'])
423-
contents.append(new_file)
460+
if source_element_from_repo_api(sub):
461+
contents.append(source_element_from_repo_api(sub))
424462
return contents
425463

426464

0 commit comments

Comments
 (0)