From 25ee46f5a2f1e447ed4815077840f19bb158a062 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Mon, 1 Jun 2015 15:35:59 -0400 Subject: [PATCH] Fix bitbucket triggers when the branch tag filter removes all branches --- endpoints/trigger.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/endpoints/trigger.py b/endpoints/trigger.py index 311c6aaf5..692c23990 100644 --- a/endpoints/trigger.py +++ b/endpoints/trigger.py @@ -257,6 +257,13 @@ class BitbucketBuildTrigger(BuildTriggerHandler): bitbucket_client = self._get_authorized_client() return bitbucket_client.for_namespace(namespace).repositories().get(name) + def _get_default_branch(self, repository, default_value='master'): + (result, data, _) = repository.get_main_branch() + if result: + return data['name'] + + return default_value + def get_oauth_url(self): bitbucket_client = self._get_client() (result, data, err_msg) = bitbucket_client.get_authorization_url() @@ -376,6 +383,9 @@ class BitbucketBuildTrigger(BuildTriggerHandler): # Find the first matching branch. repo_branches = self.list_field_values('branch_name') or [] branches = find_matching_branches(config, repo_branches) + if not branches: + branches = [self._get_default_branch(repository)] + (result, data, err_msg) = repository.get_path_contents('', revision=branches[0]) if not result: raise RepositoryReadException(err_msg) @@ -436,10 +446,7 @@ class BitbucketBuildTrigger(BuildTriggerHandler): # Lookup the default branch associated with the repository. We use this when building # the tags. - default_branch = '' - (result, data, _) = repository.get_main_branch() - if result: - default_branch = data['name'] + default_branch = self._get_default_branch(repository) # Lookup the commit sha. (result, data, _) = repository.changesets().get(commit_sha) @@ -533,10 +540,7 @@ class BitbucketBuildTrigger(BuildTriggerHandler): repository = self._get_repository_client() # Find the branch to build. - branch_name = run_parameters.get('branch_name') - (result, data, _) = repository.get_main_branch() - if result: - branch_name = branch_name or data['name'] + branch_name = run_parameters.get('branch_name') or self._get_default_branch(repository) # Lookup the commit SHA for the branch. (result, data, _) = repository.get_branches()