From a949a44cb29177917f6edc69dc2ab9f031bf7f74 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Wed, 7 Jun 2017 15:05:29 -0400 Subject: [PATCH] Remove expensive call in build badge We don't need to list all the tags to determine if any exist, and showing the repo is ready when it is empty is probably correct behavior anyway --- data/model/repository.py | 11 +++++++++++ data/model/test/test_repository.py | 9 ++++++++- endpoints/web.py | 5 +---- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/data/model/repository.py b/data/model/repository.py index e94b8b4fa..ee63c68c2 100644 --- a/data/model/repository.py +++ b/data/model/repository.py @@ -616,3 +616,14 @@ def list_popular_public_repos(action_count_threshold, time_span, repo_kind='imag .group_by(RepositoryActionCount.repository, Repository.name, Namespace.username) .having(fn.Sum(RepositoryActionCount.count) >= action_count_threshold) .tuples()) + + +def is_empty(namespace_name, repository_name): + """ Returns if the repository referenced by the given namespace and name is empty. If the repo + doesn't exist, returns True. + """ + try: + tag.list_repository_tags(namespace_name, repository_name).limit(1).get() + return False + except RepositoryTag.DoesNotExist: + return True diff --git a/data/model/test/test_repository.py b/data/model/test/test_repository.py index ee6bd0165..b5c6f741b 100644 --- a/data/model/test/test_repository.py +++ b/data/model/test/test_repository.py @@ -2,7 +2,7 @@ import pytest from peewee import IntegrityError -from data.model.repository import create_repository, purge_repository +from data.model.repository import create_repository, purge_repository, is_empty from test.fixtures import * def test_duplicate_repository_different_kinds(initialized_db): @@ -12,3 +12,10 @@ def test_duplicate_repository_different_kinds(initialized_db): # Try to create an app repo with the same name, which should fail. with pytest.raises(IntegrityError): create_repository('devtable', 'somenewrepo', None, repo_kind='application') + + +def test_is_empty(initialized_db): + create_repository('devtable', 'somenewrepo', None, repo_kind='image') + + assert is_empty('devtable', 'somenewrepo') + assert not is_empty('devtable', 'simple') diff --git a/endpoints/web.py b/endpoints/web.py index cd158bbc6..d4eaca385 100644 --- a/endpoints/web.py +++ b/endpoints/web.py @@ -479,11 +479,8 @@ def build_status_badge(namespace_name, repo_name): if not repo or token != repo.badge_token: abort(404) - # Lookup the tags for the repository. - tags = model.tag.list_repository_tags(namespace_name, repo_name) - is_empty = len(list(tags)) == 0 + is_empty = model.repository.is_empty(namespace_name, repo_name) recent_build = model.build.get_recent_repository_build(namespace_name, repo_name) - if not is_empty and (not recent_build or recent_build.phase == 'complete'): status_name = 'ready' elif recent_build and recent_build.phase == 'error':