From b6502d9302be26cbd1b05e81df47b8f551826b55 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Wed, 19 Aug 2015 17:49:37 -0400 Subject: [PATCH] Limit the number of branches and tags loaded to 30 Fixes #380 --- endpoints/api/trigger.py | 3 ++- endpoints/trigger.py | 40 ++++++++++++++++++++++++++++++++-------- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/endpoints/api/trigger.py b/endpoints/api/trigger.py index 72d1ee7a2..1582f3890 100644 --- a/endpoints/api/trigger.py +++ b/endpoints/api/trigger.py @@ -461,6 +461,7 @@ class TriggerBuildList(RepositoryParamResource): } +FIELD_VALUE_LIMIT = 30 @resource('/v1/repository//trigger//fields/') @internal_only @@ -479,7 +480,7 @@ class BuildTriggerFieldValues(RepositoryParamResource): user_permission = UserAdminPermission(trigger.connected_user.username) if user_permission.can(): handler = BuildTriggerHandler.get_handler(trigger, config) - values = handler.list_field_values(field_name) + values = handler.list_field_values(field_name, limit=FIELD_VALUE_LIMIT) if values is None: raise NotFound() diff --git a/endpoints/trigger.py b/endpoints/trigger.py index 13d4803bb..5e174cdb9 100644 --- a/endpoints/trigger.py +++ b/endpoints/trigger.py @@ -197,7 +197,7 @@ class BuildTriggerHandler(object): """ raise NotImplementedError - def list_field_values(self, field_name): + def list_field_values(self, field_name, limit=None): """ Lists all values for the given custom trigger field. For example, a trigger might have a field named "branches", and this method would return all branches. @@ -434,7 +434,7 @@ class BitbucketBuildTrigger(BuildTriggerHandler): return data - def list_field_values(self, field_name): + def list_field_values(self, field_name, limit=None): source = self.config['build_source'] (namespace, name) = source.split('/') @@ -457,14 +457,22 @@ class BitbucketBuildTrigger(BuildTriggerHandler): if not result: return None - return data.keys() + tags = list(data.keys()) + if limit: + tags = tags[0:limit] + + return tags if field_name == 'branch_name': (result, data, _) = repository.get_branches() if not result: return None - return data.keys() + branches = list(data.keys()) + if limit: + branches = branches[0:limit] + + return branches return None @@ -1039,7 +1047,7 @@ class GithubBuildTrigger(BuildTriggerHandler): return self._prepare_build(ref, commit_sha, True, repo=repo) - def list_field_values(self, field_name): + def list_field_values(self, field_name, limit=None): if field_name == 'refs': branches = self.list_field_values('branch_name') tags = self.list_field_values('tag_name') @@ -1053,7 +1061,11 @@ class GithubBuildTrigger(BuildTriggerHandler): gh_client = self._get_client() source = config['build_source'] repo = gh_client.get_repo(source) - return [tag.name for tag in repo.get_tags()] + gh_tags = repo.get_tags() + if limit: + gh_tags = repo.get_tags()[0:limit] + + return [tag.name for tag in gh_tags] except GitHubBadCredentialsException: return [] except GithubException: @@ -1066,7 +1078,11 @@ class GithubBuildTrigger(BuildTriggerHandler): gh_client = self._get_client() source = config['build_source'] repo = gh_client.get_repo(source) - branches = [branch.name for branch in repo.get_branches()] + gh_branches = repo.get_branches() + if limit: + gh_branches = repo.get_branches()[0:limit] + + branches = [branch.name for branch in gh_branches] if not repo.default_branch in branches: branches.insert(0, repo.default_branch) @@ -1417,7 +1433,7 @@ class GitLabBuildTrigger(BuildTriggerHandler): return contents - def list_field_values(self, field_name): + def list_field_values(self, field_name, limit=None): if field_name == 'refs': branches = self.list_field_values('branch_name') tags = self.list_field_values('tag_name') @@ -1434,12 +1450,20 @@ class GitLabBuildTrigger(BuildTriggerHandler): tags = gl_client.getrepositorytags(repo['id']) if tags is False: return [] + + if limit: + tags = tags[0:limit] + return [tag['name'] for tag in tags] if field_name == 'branch_name': branches = gl_client.getbranches(repo['id']) if branches is False: return [] + + if limit: + branches = branches[0:limit] + return [branch['name'] for branch in branches] return None