Skip to content

Commit

Permalink
fix paging for repo collaborators and improve output (#44)
Browse files Browse the repository at this point in the history
fix paging for repo collaborators and improve output

The current implementation fails when the maximum page size of a api request is exceeded. See SovereignCloudStack/github-manager#184

Reviewed-by: Artem Goncharov
Reviewed-by: Marc Schöchlin <[email protected]>
  • Loading branch information
scoopex authored Oct 2, 2024
1 parent befc593 commit 818090c
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions plugins/module_utils/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,9 @@ def request(
if key in info:
error_data[key] = info[key]

self.save_error(f"{error_msg}: {error_data}")
error_data["response"] = response or "no response"
error_data["body"] = body or "no body"
self.save_error(f"request failed {error_msg}: {error_data}")
elif status == 404 and ignore_missing:
return None
if status == 204:
Expand Down Expand Up @@ -251,16 +253,15 @@ def paginated_request(self, url, headers=None, timeout=15, **kwargs):

def get_owner_teams(self, owner):
"""Get Team information"""
rsp = self.paginated_request(
method='GET',
return self.paginated_request(
url=f'orgs/{owner}/teams',
error_msg=f"Cannot fetch teams for {owner}"
)
return rsp

def get_team(self, owner, name, ignore_missing=False):
return self.request(
url=f"orgs/{owner}/teams/{name}",
error_msg="Error fetching {owner}/{team} team",
error_msg=f"Error fetching {owner}/{name} team",
ignore_missing=ignore_missing
)

Expand Down Expand Up @@ -319,7 +320,7 @@ def get_team_members(self, owner, team, role='maintainer'):
method='GET',
url=(f"orgs/{owner}/"
f"teams/{team}/members?role={role}"),
error_msg=f"Cannot fetch team {team}@{owner} {role}s"
error_msg=f"Cannot fetch team {team}@{owner} with role {role}"
)

def get_team_repo_permissions(self, owner, team, repo):
Expand Down Expand Up @@ -570,7 +571,7 @@ def get_repo_teams(self, owner, repo):

def get_repo_collaborators(self, owner, repo, affiliation='direct'):
"""Get repo collaborators"""
return self.request(
return self.paginated_request(
method='GET',
url=(f"repos/{owner}/{repo}/collaborators?affiliation={affiliation}"),
error_msg=f"Cannot fetch repo {owner}/{repo} collaborators"
Expand Down Expand Up @@ -782,6 +783,7 @@ def _manage_org_team(
is_existing = True
team_status = 'unchanged'
if not current:

# Create new team
changed = True
team_status = 'created'
Expand All @@ -794,6 +796,10 @@ def _manage_org_team(
parent=target.get('parent'),
maintainers=target.get('maintainer', [])
)
if current is None:
self.save_error(f"Unable to create team: {slug} / {target} with "
f"maintainers : {', '.join(target.get('maintainer', []))} "
"(all maintainers must be completely onboarded)")
slug = current['slug']
else:
is_existing = False
Expand Down

0 comments on commit 818090c

Please sign in to comment.