Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/github_actions/production-depende…
Browse files Browse the repository at this point in the history
…ncies-5da8da3d55
  • Loading branch information
GeekMasher authored Nov 18, 2024
2 parents 8ce669f + 4872138 commit dd376c1
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/ghastoolkit/octokit/octokit.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Octokit"""

import os
import inspect
import logging
Expand Down Expand Up @@ -217,6 +219,9 @@ def get(
"GitHub Token required for this request"
)

cursor = None
page = 1 # Page starts at 1

result = []
params = {}
# if the parameter is in the path, ignore it
Expand All @@ -226,10 +231,11 @@ def get(

params["per_page"] = RestRequest.PER_PAGE

page = 1 # index starts at 1

while True:
params["page"] = page
if cursor:
params["after"] = cursor.replace("%3D", "=")
else:
params["page"] = page

response = self.session.get(url, params=params)
# Every response should be a JSON (including errors)
Expand Down Expand Up @@ -271,6 +277,17 @@ def get(
if len(response_json) < RestRequest.PER_PAGE:
break

# Use a cursor for pagination
if link := response.headers.get("Link"):
if next := [x for x in link.split(", ") if x.endswith('rel="next"')]:
next = next[0].split(">;")[0].replace("<", "")
# If `after` parameter is not in the URL
if after := next.split("&after="):
# We don't want to paginate if the cursor is a URL
if not after[0].startswith("http"):
cursor = after[0]
logger.debug(f"Cursor :: {cursor}")

page += 1

return result
Expand Down

0 comments on commit dd376c1

Please sign in to comment.