From 6fd2440294c232f85f6eb5ccc2d8a5eac7eec624 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Fri, 28 Mar 2014 15:32:56 -0400 Subject: [PATCH] Handle empty GitHub repositories and do not 500 if the repository cannot be read --- endpoints/api/trigger.py | 10 ++++++++-- endpoints/trigger.py | 12 +++++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/endpoints/api/trigger.py b/endpoints/api/trigger.py index 1eb7cd169..c28188057 100644 --- a/endpoints/api/trigger.py +++ b/endpoints/api/trigger.py @@ -13,7 +13,8 @@ from endpoints.api.build import (build_status_view, trigger_view, RepositoryBuil get_trigger_config) from endpoints.common import start_build from endpoints.trigger import (BuildTrigger as BuildTriggerBase, TriggerDeactivationException, - TriggerActivationException, EmptyRepositoryException) + TriggerActivationException, EmptyRepositoryException, + RepositoryReadException) from data import model from auth.permissions import UserAdminPermission @@ -116,9 +117,14 @@ class BuildTriggerSubdirs(RepositoryParamResource): 'status': 'success' } except EmptyRepositoryException as exc: + return { + 'status': 'success', + 'subdir': [] + } + except RepositoryReadException as exc: return { 'status': 'error', - 'message': exc.msg + 'message': exc.message } else: raise Unauthorized() diff --git a/endpoints/trigger.py b/endpoints/trigger.py index 82a3284ab..873f51c9b 100644 --- a/endpoints/trigger.py +++ b/endpoints/trigger.py @@ -38,6 +38,9 @@ class ValidationRequestException(Exception): class EmptyRepositoryException(Exception): pass +class RepositoryReadException(Exception): + pass + class BuildTrigger(object): def __init__(self): @@ -209,9 +212,12 @@ class GithubBuildTrigger(BuildTrigger): return [os.path.dirname(elem.path) for elem in commit_tree.tree if (elem.type == u'blob' and os.path.basename(elem.path) == u'Dockerfile')] - except GithubException: - msg = 'Unable to list contents of repository: %s' % source - raise EmptyRepositoryException(msg) + except GithubException as ge: + message = ge.data.get('message', 'Unable to list contents of repository: %s' % source) + if message == 'Branch not found': + raise EmptyRepositoryException() + + raise RepositoryReadException(message) @staticmethod def _prepare_build(config, repo, commit_sha, build_name, ref):