parent
c3a738c7c2
commit
da120a1ef2
1 changed files with 70 additions and 11 deletions
|
@ -9,7 +9,8 @@ import json
|
||||||
import gitlab
|
import gitlab
|
||||||
|
|
||||||
from endpoints.building import PreparedBuild
|
from endpoints.building import PreparedBuild
|
||||||
from github import Github, UnknownObjectException, GithubException
|
from github import (Github, UnknownObjectException, GithubException,
|
||||||
|
BadCredentialsException as GitHubBadCredentialsException)
|
||||||
from bitbucket import BitBucket
|
from bitbucket import BitBucket
|
||||||
from tempfile import SpooledTemporaryFile
|
from tempfile import SpooledTemporaryFile
|
||||||
from jsonschema import validate
|
from jsonschema import validate
|
||||||
|
@ -737,7 +738,45 @@ class GithubBuildTrigger(BuildTriggerHandler):
|
||||||
raise RepositoryReadException(message)
|
raise RepositoryReadException(message)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _build_commit_info(repo, commit_sha):
|
def _build_commit_info(repo, payload, commit_sha):
|
||||||
|
if repo:
|
||||||
|
return GithubBuildTrigger._build_repo_commit_info(repo, commit_sha)
|
||||||
|
else:
|
||||||
|
return GithubBuildTrigger._build_payload_commit_info(payload, commit_sha)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _build_payload_commit_info(payload, commit_sha):
|
||||||
|
head_commit = payload.get('head_commit', {})
|
||||||
|
sender = payload.get('sender', {})
|
||||||
|
|
||||||
|
commit_info = {
|
||||||
|
'url': head_commit.get('url', ''),
|
||||||
|
'message': head_commit.get('message', ''),
|
||||||
|
'date': head_commit.get('timestamp', ''),
|
||||||
|
}
|
||||||
|
|
||||||
|
if 'author' in head_commit:
|
||||||
|
commit_info['author'] = {
|
||||||
|
'username': head_commit['author'].get('username'),
|
||||||
|
}
|
||||||
|
|
||||||
|
if head_commit['author']['username'] == sender.get('login'):
|
||||||
|
commit_info['author']['avatar_url'] = sender.get('avatar_url', '')
|
||||||
|
commit_info['author']['url'] = sender.get('html_url', '')
|
||||||
|
|
||||||
|
if 'committer' in head_commit:
|
||||||
|
commit_info['author'] = {
|
||||||
|
'username': head_commit['committer'].get('username'),
|
||||||
|
}
|
||||||
|
|
||||||
|
if head_commit['committer']['username'] == sender.get('login'):
|
||||||
|
commit_info['committer']['avatar_url'] = sender.get('avatar_url', '')
|
||||||
|
commit_info['committer']['url'] = sender.get('html_url', '')
|
||||||
|
|
||||||
|
return commit_info
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _build_repo_commit_info(repo, commit_sha):
|
||||||
try:
|
try:
|
||||||
commit = repo.get_commit(commit_sha)
|
commit = repo.get_commit(commit_sha)
|
||||||
except GithubException:
|
except GithubException:
|
||||||
|
@ -801,12 +840,23 @@ class GithubBuildTrigger(BuildTriggerHandler):
|
||||||
return tarball_subdir, dockerfile_id
|
return tarball_subdir, dockerfile_id
|
||||||
|
|
||||||
|
|
||||||
def _prepare_build(self, repo, ref, commit_sha, is_manual):
|
def _get_payload(self, payload, *args):
|
||||||
|
current = payload
|
||||||
|
for arg in args:
|
||||||
|
current = current.get(arg, {})
|
||||||
|
|
||||||
|
return current
|
||||||
|
|
||||||
|
|
||||||
|
def _prepare_build(self, ref, commit_sha, is_manual, repo=None, payload=None):
|
||||||
config = self.config
|
config = self.config
|
||||||
prepared = PreparedBuild(self.trigger)
|
prepared = PreparedBuild(self.trigger)
|
||||||
|
|
||||||
# If the trigger isn't using git, prepare the buildpack.
|
# If the trigger isn't using git, prepare the buildpack.
|
||||||
if self.trigger.private_key is None:
|
if self.trigger.private_key is None:
|
||||||
|
if repo is None:
|
||||||
|
raise SkipRequestException()
|
||||||
|
|
||||||
tarball_subdir, dockerfile_id = GithubBuildTrigger._prepare_tarball(repo, commit_sha)
|
tarball_subdir, dockerfile_id = GithubBuildTrigger._prepare_tarball(repo, commit_sha)
|
||||||
|
|
||||||
prepared.subdirectory = os.path.join(tarball_subdir, config['subdir'])
|
prepared.subdirectory = os.path.join(tarball_subdir, config['subdir'])
|
||||||
|
@ -818,18 +868,23 @@ class GithubBuildTrigger(BuildTriggerHandler):
|
||||||
prepared.name_from_sha(commit_sha)
|
prepared.name_from_sha(commit_sha)
|
||||||
|
|
||||||
# Set the tag(s).
|
# Set the tag(s).
|
||||||
prepared.tags_from_ref(ref, repo.default_branch)
|
if repo:
|
||||||
|
default_branch = repo.default_branch
|
||||||
|
else:
|
||||||
|
default_branch = self._get_payload(payload, 'repository', 'default_branch')
|
||||||
|
|
||||||
|
prepared.tags_from_ref(ref, default_branch)
|
||||||
|
|
||||||
# Build and set the metadata.
|
# Build and set the metadata.
|
||||||
metadata = {
|
metadata = {
|
||||||
'commit': commit_sha,
|
'commit': commit_sha,
|
||||||
'ref': ref,
|
'ref': ref,
|
||||||
'default_branch': repo.default_branch,
|
'default_branch': default_branch,
|
||||||
'git_url': repo.ssh_url,
|
'git_url': repo.ssh_url if repo else self._get_payload(payload, 'repository', 'ssh_url'),
|
||||||
}
|
}
|
||||||
|
|
||||||
# add the commit info.
|
# add the commit info.
|
||||||
commit_info = GithubBuildTrigger._build_commit_info(repo, commit_sha)
|
commit_info = GithubBuildTrigger._build_commit_info(repo, payload, commit_sha)
|
||||||
if commit_info is not None:
|
if commit_info is not None:
|
||||||
metadata['commit_info'] = commit_info
|
metadata['commit_info'] = commit_info
|
||||||
|
|
||||||
|
@ -867,10 +922,14 @@ class GithubBuildTrigger(BuildTriggerHandler):
|
||||||
|
|
||||||
gh_client = self._get_client()
|
gh_client = self._get_client()
|
||||||
repo = gh_client.get_repo(repo_full_name)
|
repo = gh_client.get_repo(repo_full_name)
|
||||||
|
return self._prepare_build(ref, commit_sha, False, repo=repo)
|
||||||
|
except GitHubBadCredentialsException:
|
||||||
|
logger.exception('Got GitHub Credentials Exception, retrying with a manual payload')
|
||||||
|
return self._prepare_build(ref, commit_sha, False, payload=payload)
|
||||||
|
except GithubException:
|
||||||
|
logger.exception("Got GitHub Exception when trying to start trigger %s", self.trigger.id)
|
||||||
|
raise SkipRequestException()
|
||||||
|
|
||||||
return self._prepare_build(repo, ref, commit_sha, False)
|
|
||||||
except GithubException as ghe:
|
|
||||||
raise TriggerStartException(ghe.data['message'])
|
|
||||||
|
|
||||||
def manual_start(self, run_parameters=None):
|
def manual_start(self, run_parameters=None):
|
||||||
config = self.config
|
config = self.config
|
||||||
|
@ -887,7 +946,7 @@ class GithubBuildTrigger(BuildTriggerHandler):
|
||||||
commit_sha = branch.commit.sha
|
commit_sha = branch.commit.sha
|
||||||
ref = 'refs/heads/%s' % (branch_name)
|
ref = 'refs/heads/%s' % (branch_name)
|
||||||
|
|
||||||
return self._prepare_build(repo, ref, commit_sha, True)
|
return self._prepare_build(ref, commit_sha, True, repo=repo)
|
||||||
except GithubException as ghe:
|
except GithubException as ghe:
|
||||||
raise TriggerStartException(ghe.data['message'])
|
raise TriggerStartException(ghe.data['message'])
|
||||||
|
|
||||||
|
|
Reference in a new issue