From 818090c9d93ec6ee3b6cd988d2a6bb826a8873c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Sch=C3=B6chlin?= Date: Wed, 2 Oct 2024 15:03:31 +0200 Subject: [PATCH] fix paging for repo collaborators and improve output (#44) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- plugins/module_utils/github.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/plugins/module_utils/github.py b/plugins/module_utils/github.py index 5c47b00..1178751 100644 --- a/plugins/module_utils/github.py +++ b/plugins/module_utils/github.py @@ -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: @@ -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 ) @@ -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): @@ -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" @@ -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' @@ -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