From bad1e57ebd907b41ab2e7f0a2b88171424f79cec Mon Sep 17 00:00:00 2001 From: Clark Boylan Date: Wed, 24 Jun 2020 16:04:46 -0700 Subject: [PATCH] Paginate all the gitea get requests According to gitea swagger definitions all of these GET requests for lists of items are paginated with a max limit of 50 items per request. Update our ansible machinery to properly page these items to avoid problems in the future. Note we should try and confirm that this is how it works for production gitea. Change-Id: I5df13288b497fb4fb716b4223b3dd61c698a7739 --- .../library/gitea_create_repos.py | 38 +++++++++---------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/playbooks/roles/gitea-git-repos/library/gitea_create_repos.py b/playbooks/roles/gitea-git-repos/library/gitea_create_repos.py index a0afec63fd..85acae89c0 100755 --- a/playbooks/roles/gitea-git-repos/library/gitea_create_repos.py +++ b/playbooks/roles/gitea-git-repos/library/gitea_create_repos.py @@ -71,6 +71,18 @@ class Gitea(object): def get(self, endpoint, *args, **kwargs): return self.request('GET', endpoint, *args, **kwargs) + def get_paginated(self, endpoint, *args, **kwargs): + params = { 'limit': 50, 'page': 1 } + results = [] + gitea_data = self.get(endpoint, *args, params=params, **kwargs).json() + while gitea_data: + results.extend(gitea_data) + # Gitea paginates and returns an empty list at the end of the + # listing. 50 items is the max limit. + params['page'] += 1 + gitea_data = self.get(endpoint, *args, params=params, **kwargs).json() + return results + def post(self, endpoint, *args, **kwargs): return self.request('POST', endpoint, *args, **kwargs) @@ -78,7 +90,7 @@ class Gitea(object): return self.request('PUT', endpoint, *args, **kwargs) def get_gitea_orgs(self): - orgs = self.get("/api/v1/user/orgs").json() + orgs = self.get_paginated("/api/v1/user/orgs") return [f['username'] for f in orgs] def make_gitea_org(self, org): @@ -88,33 +100,19 @@ class Gitea(object): self.log("Created org:", org) def ensure_gitea_teams(self, org): - team_list = self.get('/api/v1/orgs/{org}/teams'.format(org=org)).json() + team_list = self.get_paginated('/api/v1/orgs/{org}/teams'.format(org=org)) owner_id = [f['id'] for f in team_list if f['name'] == 'Owners'][0] - org_owners = self.get( + org_owners = self.get_paginated( '/api/v1/teams/{owner_id}/members'.format(owner_id=owner_id)) - if 'gerrit' not in [f['username'] for f in org_owners.json()]: + if 'gerrit' not in [f['username'] for f in org_owners]: self.put('/api/v1/teams/{owner_id}/members/gerrit'.format( owner_id=owner_id)) self.log("Added gerrit to team:", org) def get_org_repo_list(self, org): - params = { 'limit': 50, 'page': 1 } - repos = [] - gitea_data = self.get( - '/api/v1/orgs/{org}/repos'.format(org=org), - params=params - ).json() - while gitea_data: - repos.extend([x['full_name'] for x in gitea_data]) - # Gitea paginates and returns an empty list at the end of the - # listing. 50 items is the max limit. - params['page'] += 1 - gitea_data = self.get( - '/api/v1/orgs/{org}/repos'.format(org=org), - params=params - ).json() - return repos + return [x['full_name'] for x in + self.get_paginated('/api/v1/orgs/{org}/repos'.format(org=org))] def get_csrf_token(self): resp = self.get('/')