parent
82287926ab
commit
143036be9c
7 changed files with 312 additions and 56 deletions
|
@ -65,6 +65,27 @@ class TriggerProviderException(Exception):
|
|||
pass
|
||||
|
||||
|
||||
def _determine_build_ref(run_parameters, get_branch_sha, get_tag_sha, default_branch):
|
||||
run_parameters = run_parameters or {}
|
||||
|
||||
kind = ''
|
||||
value = ''
|
||||
|
||||
if 'refs' in run_parameters:
|
||||
kind = run_parameters['refs']['kind']
|
||||
value = run_parameters['refs']['name']
|
||||
elif 'branch_name' in run_parameters:
|
||||
kind = 'branch'
|
||||
value = run_parameters['branch_name']
|
||||
|
||||
kind = kind or 'branch'
|
||||
value = value or default_branch
|
||||
|
||||
ref = 'refs/tags/' + value if kind == 'tag' else 'refs/heads/' + value
|
||||
commit_sha = get_tag_sha(value) if kind == 'tag' else get_branch_sha(value)
|
||||
return (commit_sha, ref)
|
||||
|
||||
|
||||
def find_matching_branches(config, branches):
|
||||
if 'branchtag_regex' in config:
|
||||
try:
|
||||
|
@ -548,16 +569,25 @@ class BitbucketBuildTrigger(BuildTriggerHandler):
|
|||
run_parameters = run_parameters or {}
|
||||
repository = self._get_repository_client()
|
||||
|
||||
# Find the branch to build.
|
||||
branch_name = run_parameters.get('branch_name') or self._get_default_branch(repository)
|
||||
def get_branch_sha(branch_name):
|
||||
# Lookup the commit SHA for the branch.
|
||||
(result, data, _) = repository.get_branches()
|
||||
if not result or not branch_name in data:
|
||||
raise TriggerStartException('Could not find branch commit SHA')
|
||||
|
||||
# Lookup the commit SHA for the branch.
|
||||
(result, data, _) = repository.get_branches()
|
||||
if not result or not branch_name in data:
|
||||
raise TriggerStartException('Could not find branch commit SHA')
|
||||
return data[branch_name]['node']
|
||||
|
||||
commit_sha = data[branch_name]['node']
|
||||
ref = 'refs/heads/%s' % (branch_name)
|
||||
def get_tag_sha(tag_name):
|
||||
# Lookup the commit SHA for the tag.
|
||||
(result, data, _) = repository.get_tags()
|
||||
if not result or not tag_name in data:
|
||||
raise TriggerStartException('Could not find tag commit SHA')
|
||||
|
||||
return data[tag_name]['node']
|
||||
|
||||
# Find the branch or tag to build.
|
||||
(commit_sha, ref) = _determine_build_ref(run_parameters, get_branch_sha, get_tag_sha,
|
||||
self._get_default_branch(repository))
|
||||
|
||||
return self._prepare_build(commit_sha, ref, True)
|
||||
|
||||
|
@ -942,22 +972,32 @@ class GithubBuildTrigger(BuildTriggerHandler):
|
|||
def manual_start(self, run_parameters=None):
|
||||
config = self.config
|
||||
source = config['build_source']
|
||||
run_parameters = run_parameters or {}
|
||||
|
||||
try:
|
||||
gh_client = self._get_client()
|
||||
|
||||
# Lookup the branch and its associated current SHA.
|
||||
repo = gh_client.get_repo(source)
|
||||
branch_name = run_parameters.get('branch_name') or repo.default_branch
|
||||
branch = repo.get_branch(branch_name)
|
||||
commit_sha = branch.commit.sha
|
||||
ref = 'refs/heads/%s' % (branch_name)
|
||||
|
||||
return self._prepare_build(ref, commit_sha, True, repo=repo)
|
||||
default_branch = repo.default_branch
|
||||
except GithubException as ghe:
|
||||
raise TriggerStartException(ghe.data['message'])
|
||||
|
||||
def get_branch_sha(branch_name):
|
||||
branch = repo.get_branch(branch_name)
|
||||
return branch.commit.sha
|
||||
|
||||
def get_tag_sha(tag_name):
|
||||
tags = {tag.name: tag for tag in repo.get_tags()}
|
||||
if not tag_name in tags:
|
||||
raise TriggerStartException('Could not find tag in repository')
|
||||
|
||||
return tags[tag_name].commit.sha
|
||||
|
||||
# Find the branch or tag to build.
|
||||
(commit_sha, ref) = _determine_build_ref(run_parameters, get_branch_sha, get_tag_sha,
|
||||
default_branch)
|
||||
|
||||
return self._prepare_build(ref, commit_sha, True, repo=repo)
|
||||
|
||||
|
||||
def list_field_values(self, field_name):
|
||||
if field_name == 'refs':
|
||||
branches = self.list_field_values('branch_name')
|
||||
|
@ -1443,29 +1483,36 @@ class GitLabBuildTrigger(BuildTriggerHandler):
|
|||
return self._prepare_build(commit['id'], ref, False)
|
||||
|
||||
def manual_start(self, run_parameters=None):
|
||||
run_parameters = run_parameters or {}
|
||||
gl_client = self._get_authorized_client()
|
||||
|
||||
repo = gl_client.getproject(self.config['build_source'])
|
||||
if repo is False:
|
||||
raise TriggerStartException('Could not find repository')
|
||||
|
||||
branch_name = run_parameters.get('branch_name') or repo['default_branch']
|
||||
def get_tag_sha(tag_name):
|
||||
tags = gl_client.getrepositorytags(repo['id'])
|
||||
if tags is False:
|
||||
raise TriggerStartException('Could not find tags')
|
||||
|
||||
branches = gl_client.getbranches(repo['id'])
|
||||
if branches is False:
|
||||
raise TriggerStartException('Could not find branches')
|
||||
for tag in tags:
|
||||
if tag['name'] == tag_name:
|
||||
return tag['commit']['id']
|
||||
|
||||
commit = None
|
||||
for branch in branches:
|
||||
if branch['name'] == branch_name:
|
||||
commit = branch['commit']
|
||||
if commit is None:
|
||||
raise TriggerStartException('Could not find commit')
|
||||
|
||||
ref = 'refs/heads/%s' % branch_name
|
||||
def get_branch_sha(branch_name):
|
||||
branch = gl_client.getbranch(repo['id'], branch_name)
|
||||
if branch is False:
|
||||
raise TriggerStartException('Could not find branch')
|
||||
|
||||
return self._prepare_build(commit['id'], ref, True)
|
||||
return branch['commit']['id']
|
||||
|
||||
# Find the branch or tag to build.
|
||||
(commit_sha, ref) = _determine_build_ref(run_parameters, get_branch_sha, get_tag_sha,
|
||||
repo['default_branch'])
|
||||
|
||||
|
||||
return self._prepare_build(commit_sha, ref, True)
|
||||
|
||||
def get_repository_url(self):
|
||||
gl_client = self._get_authorized_client()
|
||||
|
|
Reference in a new issue