almost all PR discussion fixes

This commit is contained in:
Jimmy Zelinskie 2015-04-21 18:04:25 -04:00
parent d25cc4db9c
commit 02498d72ba
13 changed files with 200 additions and 148 deletions

View file

@ -190,7 +190,8 @@ class GithubBuildTrigger(BuildTrigger):
# Add a deploy key to the GitHub repository.
try:
config['public_key'], private_key = generate_ssh_keypair()
deploy_key = gh_repo.create_key('Quay.io Builder', config['public_key'])
deploy_key = gh_repo.create_key('%s Builder' % app.config['REGISTRY_TITLE'],
config['public_key'])
config['deploy_key_id'] = deploy_key.id
except GithubException:
msg = 'Unable to add deploy key to repository: %s' % new_build_source
@ -224,13 +225,16 @@ class GithubBuildTrigger(BuildTrigger):
raise TriggerDeactivationException(msg)
# If the trigger uses a deploy key, remove it.
if config['deploy_key_id']:
try:
try:
if config['deploy_key_id']:
deploy_key = repo.get_key(config['deploy_key_id'])
deploy_key.delete()
except GithubException:
msg = 'Unable to remove deploy key: %s' % config['deploy_key_id']
raise TriggerDeactivationException(msg)
except KeyError:
# There was no config['deploy_key_id'], thus this is an old trigger without a deploy key.
pass
except GithubException:
msg = 'Unable to remove deploy key: %s' % config['deploy_key_id']
raise TriggerDeactivationException(msg)
# Remove the webhook.
try:
@ -329,6 +333,7 @@ class GithubBuildTrigger(BuildTrigger):
master_branch = repo.default_branch or 'master'
return 'https://github.com/%s/blob/%s/%s' % (source, master_branch, path)
except GithubException:
logger.exception('Could not load repository for Dockerfile.')
return None
def load_dockerfile_contents(self, auth_token, config):
@ -383,42 +388,56 @@ class GithubBuildTrigger(BuildTrigger):
return commit_info
@staticmethod
def _prepare_tarball(repo, commit_sha):
# Prepare the download and upload URLs
archive_link = repo.get_archive_link('tarball', commit_sha)
download_archive = client.get(archive_link, stream=True)
tarball_subdir = ''
with SpooledTemporaryFile(CHUNK_SIZE) as tarball:
for chunk in download_archive.iter_content(CHUNK_SIZE):
tarball.write(chunk)
# Seek to position 0 to make tarfile happy
tarball.seek(0)
# Pull out the name of the subdir that GitHub generated
with tarfile.open(fileobj=tarball) as archive:
tarball_subdir = archive.getnames()[0]
# Seek to position 0 to make tarfile happy.
tarball.seek(0)
entries = {
tarball_subdir + '/.git/HEAD': commit_sha,
tarball_subdir + '/.git/objects/': None,
tarball_subdir + '/.git/refs/': None
}
appender = TarfileAppender(tarball, entries).get_stream()
dockerfile_id = user_files.store_file(appender, TARBALL_MIME)
logger.debug('Successfully prepared job')
return tarball_subdir, dockerfile_id
@staticmethod
def _prepare_build(trigger, config, repo, commit_sha, build_name, ref, git_url):
# If the trigger isn't using git, prepare the buildpack.
repo_subdir = config['subdir']
joined_subdir = repo_subdir
dockerfile_id = None
if trigger.private_key is None:
# Prepare the download and upload URLs
archive_link = repo.get_archive_link('tarball', commit_sha)
download_archive = client.get(archive_link, stream=True)
tarball_subdir = ''
with SpooledTemporaryFile(CHUNK_SIZE) as tarball:
for chunk in download_archive.iter_content(CHUNK_SIZE):
tarball.write(chunk)
# Seek to position 0 to make tarfile happy
tarball.seek(0)
# Pull out the name of the subdir that GitHub generated
with tarfile.open(fileobj=tarball) as archive:
tarball_subdir = archive.getnames()[0]
# Seek to position 0 to make tarfile happy.
tarball.seek(0)
entries = {
tarball_subdir + '/.git/HEAD': commit_sha,
tarball_subdir + '/.git/objects/': None,
tarball_subdir + '/.git/refs/': None
}
appender = TarfileAppender(tarball, entries).get_stream()
dockerfile_id = user_files.store_file(appender, TARBALL_MIME)
# If the trigger isn't using git, prepare the buildpack.
tarball_subdir, dockerfile_id = GithubBuildTrigger._prepare_tarball(repo, commit_sha)
logger.debug('Successfully prepared job')
else:
dockerfile_id = None
# Join provided subdir with the tarball subdir.
joined_subdir = os.path.join(tarball_subdir, repo_subdir)
logger.debug('Final subdir: %s', joined_subdir)
# compute the tag(s)
branch = ref.split('/')[-1]
@ -429,14 +448,6 @@ class GithubBuildTrigger(BuildTrigger):
logger.debug('Pushing to tags: %s', tags)
# compute the subdir
repo_subdir = config['subdir']
if trigger.private_key is None:
joined_subdir = os.path.join(tarball_subdir, repo_subdir)
else:
joined_subdir = repo_subdir
logger.debug('Final subdir: %s', joined_subdir)
# compute the metadata
metadata = {
'commit_sha': commit_sha,
@ -551,30 +562,70 @@ class CustomBuildTrigger(BuildTrigger):
payload_schema = {
'type': 'object',
'properties': {
'commits': {'type': 'string'},
'ref': {'type': 'string'},
'default_branch': {'type': 'string'},
'commit': {
'type': 'string',
'description': 'SHA-1 identifier for a git commit',
},
'ref': {
'type': 'string',
'description': 'git reference for a git commit',
'pattern': '^refs\/(heads|tags|remotes)\/(.+)$',
},
'default_branch': {
'type': 'string',
'description': 'default branch of the git repository',
},
'commit_info': {
'type': 'object',
'description': 'metadata about a git commit',
'properties': {
'url': {'type': 'string'},
'message': {'type': 'string'},
'date': {'type': 'string'},
'url': {
'type': 'string',
'description': 'URL to view a git commit',
},
'message': {
'type': 'string',
'description': 'git commit message',
},
'date': {
'type': 'string',
'description': 'timestamp for a git commit'
},
'author': {
'type': 'object',
'description': 'metadata about the author of a git commit',
'properties': {
'username': {'type': 'string'},
'url': {'type': 'string'},
'avatar_url': {'type': 'string'},
'username': {
'type': 'string',
'description': 'username of the author',
},
'url': {
'type': 'string',
'description': 'URL to view the profile of the author',
},
'avatar_url': {
'type': 'string',
'description': 'URL to view the avatar of the author',
},
},
'required': ['username', 'url', 'avatar_url'],
},
'committer': {
'type': 'object',
'description': 'metadata about the committer of a git commit',
'properties': {
'username': {'type': 'string'},
'url': {'type': 'string'},
'avatar_url': {'type': 'string'},
'username': {
'type': 'string',
'description': 'username of the committer',
},
'url': {
'type': 'string',
'description': 'URL to view the profile of the committer',
},
'avatar_url': {
'type': 'string',
'description': 'URL to view the avatar of the committer',
},
},
'required': ['username', 'url', 'avatar_url'],
},
@ -587,7 +638,7 @@ class CustomBuildTrigger(BuildTrigger):
@classmethod
def service_name(cls):
return 'custom'
return 'custom-git'
def is_active(self, config):
return 'public_key' in config
@ -630,9 +681,9 @@ class CustomBuildTrigger(BuildTrigger):
return config
def manual_start(self, trigger, run_parameters=None):
for parameter in ['commit_sha']:
if parameter not in run_parameters:
raise TriggerStartException('missing required parameter')
# commit_sha is the only required parameter
if 'commit_sha' not in run_parameters:
raise TriggerStartException('missing required parameter')
config = get_trigger_config(trigger)
dockerfile_id = None