Skip to content

Commit

Permalink
refactor: a helper for paged results
Browse files Browse the repository at this point in the history
"When using a paged API endpoint, I want an iterable of pages" -- No one ever.

This helper lets us focus on the things we care about: the items we are
asking for.
  • Loading branch information
Ned Batchelder authored and nedbat committed Nov 21, 2023
1 parent d727047 commit fee7a73
Showing 1 changed file with 25 additions and 30 deletions.
55 changes: 25 additions & 30 deletions edx_repo_tools/repo_checks/repo_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@
cache = lru_cache(maxsize=None)


def all_paged_items(func, *args, **kwargs):
"""
Get all items from a GhApi function returning paged results.
"""
return chain.from_iterable(paged(func, *args, per_page=100, **kwargs))


def is_security_private_fork(api, org, repo):
"""
Check to see if a specific repo is a private security fork.
Expand Down Expand Up @@ -373,14 +380,11 @@ def fix(self, dry_run=False):
)

# Check to see if a PR exists
prs = chain.from_iterable(
paged(
self.api.pulls.list,
owner=self.org_name,
repo=self.repo_name,
head=self.branch_name,
per_page=100,
)
prs = all_paged_items(
self.api.pulls.list,
owner=self.org_name,
repo=self.repo_name,
head=self.branch_name,
)

prs = [pr for pr in prs if pr.head.ref == self.branch_name]
Expand Down Expand Up @@ -438,13 +442,10 @@ def check(self):
"""
See if our labels exist.
"""
existing_labels_from_api = chain.from_iterable(
paged(
self.api.issues.list_labels_for_repo,
self.org_name,
self.repo_name,
per_page=100,
)
existing_labels_from_api = all_paged_items(
self.api.issues.list_labels_for_repo,
self.org_name,
self.repo_name,
)
existing_labels = {
self._simplify_label(label.name): {
Expand Down Expand Up @@ -555,13 +556,10 @@ def is_relevant(self):
raise NotImplementedError

def check(self):
teams = chain.from_iterable(
paged(
self.api.repos.list_teams,
self.org_name,
self.repo_name,
per_page=100,
)
teams = all_paged_items(
self.api.repos.list_teams,
self.org_name,
self.repo_name,
)

team_permissions = {team.slug: team.permission for team in teams}
Expand Down Expand Up @@ -943,14 +941,11 @@ def main(org, dry_run, _github_token, check_names, repos, start_at):
if not repos:
repos = [
repo.name
for repo in chain.from_iterable(
paged(
api.repos.list_for_org,
org,
sort="created",
direction="desc",
per_page=100,
)
for repo in all_paged_items(
api.repos.list_for_org,
org,
sort="created",
direction="desc",
)
]

Expand Down

0 comments on commit fee7a73

Please sign in to comment.