Skip to content

Commit

Permalink
fixup: use requests link property
Browse files Browse the repository at this point in the history
  • Loading branch information
mag3me committed Nov 16, 2024
1 parent 6a1d3a2 commit e06a5b0
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions capycli/bom/findsources.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,27 +190,19 @@ def _get_github_repo(self, github_ref):
url = 'https://' + url.replace('//', '/')
repo = {}
while 'tags_url' not in repo and 'github.com' in url:
print('DEBUG running github_request')
print(f'DEBUG self.github_request({url}, {self.github_name}, {self.github_token})')
repo = self.github_request(url, self.github_name, self.github_token)
print(f'DEBUG repo {repo}')
url = url.rsplit('/', 1)[0] # remove last path segment
if 'tags_url' not in repo:
raise ValueError(f"Unable to make @github_ref {github_ref} work!")
return repo

def _get_link(self, link, which='next'):
"""Helper to read link-Header from GitHub API responses.
Each URL in the Link-header is labelled with a relation,
usually, first, last, prev or next. We use this method to
retrieve the URL by its relation's name.
"""
for match in self.github_header_link_regex.findall(link):
if which in match[2:5]:
return match[0]
raise KeyError(f'No link to {which}-page in response!')

def _get_link_page(self, link, which='next'):
def _get_link_page(self, res, which='next'):
"""Helper to only get the referenced page number a link-header-URL."""
try:
url = urlparse(self._get_link(link, which))
url = urlparse(res.links[which]['url'])
return parse_qs(url.query)['page'][0]
except KeyError: # GitHub gave us only one results page
return 1
Expand Down Expand Up @@ -283,7 +275,7 @@ def version_to_github_tag(self, version, github_ref, version_prefix=None):
url = repo['tags_url'] +'?per_page=100'
res = self.github_request(url, self.github_name,
self.github_token, return_response=True)
pages = self._get_link_page(res.headers.get('link', ''), 'last')
pages = self._get_link_page(res)
prefix, suffix = None, None # tag parts not relevant to versioning
for _ in range(pages): # we prefer this over "while True"
# note: in res.json() we already have the first results page
Expand Down Expand Up @@ -348,7 +340,7 @@ def version_to_github_tag(self, version, github_ref, version_prefix=None):
return self._render_github_source_url(repo, guess)
print(':-(')
try:
url = self._get_link(res.headers['link'], 'next')
url = res.links['next']['url']
res = self.github_request(url, self.github_name,
self.github_token, return_response=True)
print('version_to_github_tag: next page!')
Expand Down

0 comments on commit e06a5b0

Please sign in to comment.