From d29c8d60c79cb39c3ccfff94926ad4a9625e3e7b Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Mon, 23 Mar 2015 12:14:47 -0400 Subject: [PATCH] trigger: pass trigger into manual_start & handle_trigger_request --- buildman/component/buildcomponent.py | 3 ++ endpoints/trigger.py | 68 +++++++++++++++------------- endpoints/webhooks.py | 8 +--- 3 files changed, 41 insertions(+), 38 deletions(-) diff --git a/buildman/component/buildcomponent.py b/buildman/component/buildcomponent.py index 160956f73..002c87602 100644 --- a/buildman/component/buildcomponent.py +++ b/buildman/component/buildcomponent.py @@ -131,6 +131,9 @@ class BuildComponent(BaseComponent): # If the trigger has a private key, it's using git, thus we should add # git data to the build args. + # url: url used to clone the git repository + # sha: the sha1 identifier of the commit to check out + # private_key: the key used to get read access to the git repository if build_job.repo_build.trigger.private_key is not None: build_arguments['git'] = { 'url': build_config['trigger_metadata']['git_url'], diff --git a/endpoints/trigger.py b/endpoints/trigger.py index 7c7c39c60..20fea2cde 100644 --- a/endpoints/trigger.py +++ b/endpoints/trigger.py @@ -10,6 +10,7 @@ from tempfile import SpooledTemporaryFile from app import app, userfiles as user_files, github_trigger from util.tarfileappender import TarfileAppender +from endpoints.api.build import get_trigger_config client = app.config['HTTPCLIENT'] @@ -85,7 +86,7 @@ class BuildTrigger(object): """ raise NotImplementedError - def handle_trigger_request(self, request, auth_token, config): + def handle_trigger_request(self, request, trigger): """ Transform the incoming request data into a set of actions. Returns a tuple of usefiles resource id, docker tags, build name, and resource subdir. @@ -114,7 +115,7 @@ class BuildTrigger(object): """ raise NotImplementedError - def manual_start(self, auth_token, config, run_parameters=None): + def manual_start(self, trigger, run_parameters=None): """ Manually creates a repository build for this trigger. """ @@ -371,38 +372,39 @@ class GithubBuildTrigger(BuildTrigger): return commit_info - # TODO(jzelinskie): update this to support both kinds of triggers @staticmethod - def _prepare_build(config, repo, commit_sha, build_name, ref, git_url): - # Prepare the download and upload URLs - archive_link = repo.get_archive_link('tarball', commit_sha) - download_archive = client.get(archive_link, stream=True) + def _prepare_build(trigger, config, repo, commit_sha, build_name, ref, git_url): + # If the trigger isn't using git, prepare the buildpack. + 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) + 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) + # 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] + # 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) + # 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 - } + 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) + appender = TarfileAppender(tarball, entries).get_stream() + dockerfile_id = user_files.store_file(appender, TARBALL_MIME) - logger.debug('Successfully prepared job') + logger.debug('Successfully prepared job') # compute the tag(s) branch = ref.split('/')[-1] @@ -437,7 +439,7 @@ class GithubBuildTrigger(BuildTrigger): def get_display_name(sha): return sha[0:7] - def handle_trigger_request(self, request, auth_token, config): + def handle_trigger_request(self, request, trigger): payload = request.get_json() if not payload or payload.get('head_commit') is None: raise SkipRequestException() @@ -451,6 +453,7 @@ class GithubBuildTrigger(BuildTrigger): commit_message = payload['head_commit'].get('message', '') git_url = payload['repository']['git_url'] + config = get_trigger_config(trigger) if 'branchtag_regex' in config: try: regex = re.compile(config['branchtag_regex']) @@ -465,7 +468,7 @@ class GithubBuildTrigger(BuildTrigger): short_sha = GithubBuildTrigger.get_display_name(commit_sha) - gh_client = self._get_client(auth_token) + gh_client = self._get_client(trigger.auth_token) repo_full_name = '%s/%s' % (payload['repository']['owner']['name'], payload['repository']['name']) @@ -473,15 +476,16 @@ class GithubBuildTrigger(BuildTrigger): logger.debug('Github repo: %s', repo) - return GithubBuildTrigger._prepare_build(config, repo, commit_sha, + return GithubBuildTrigger._prepare_build(trigger, config, repo, commit_sha, short_sha, ref, git_url) - def manual_start(self, auth_token, config, run_parameters=None): + def manual_start(self, trigger, run_parameters=None): + config = get_trigger_config(trigger) try: source = config['build_source'] run_parameters = run_parameters or {} - gh_client = self._get_client(auth_token) + gh_client = self._get_client(trigger.auth_token) repo = gh_client.get_repo(source) branch_name = run_parameters.get('branch_name') or repo.default_branch branch = repo.get_branch(branch_name) @@ -490,7 +494,7 @@ class GithubBuildTrigger(BuildTrigger): ref = 'refs/heads/%s' % (branch_name) git_url = repo.git_url - return self._prepare_build(config, repo, branch_sha, short_sha, ref, git_url) + return self._prepare_build(trigger, config, repo, branch_sha, short_sha, ref, git_url) except GithubException as ghe: raise TriggerStartException(ghe.data['message']) diff --git a/endpoints/webhooks.py b/endpoints/webhooks.py index 44d788486..6c40609ed 100644 --- a/endpoints/webhooks.py +++ b/endpoints/webhooks.py @@ -1,5 +1,4 @@ import logging -import json from flask import request, make_response, Blueprint @@ -9,7 +8,6 @@ from auth.auth import process_auth from auth.permissions import ModifyRepositoryPermission from util.invoice import renderInvoiceToHtml from util.useremails import send_invoice_email, send_subscription_change, send_payment_failed -from util.names import parse_repository_name from util.http import abort from endpoints.trigger import BuildTrigger, ValidationRequestException, SkipRequestException from endpoints.common import start_build @@ -23,7 +21,7 @@ webhooks = Blueprint('webhooks', __name__) @webhooks.route('/stripe', methods=['POST']) def stripe_webhook(): request_data = request.get_json() - logger.debug('Stripe webhook call: %s' % request_data) + logger.debug('Stripe webhook call: %s', request_data) customer_id = request_data.get('data', {}).get('object', {}).get('customer', None) user = model.get_user_or_org_by_customer_id(customer_id) if customer_id else None @@ -87,10 +85,8 @@ def build_trigger_webhook(trigger_uuid, **kwargs): handler = BuildTrigger.get_trigger_for_service(trigger.service.name) logger.debug('Passing webhook request to handler %s', handler) - config_dict = json.loads(trigger.config) try: - specs = handler.handle_trigger_request(request, trigger.auth_token, - config_dict) + specs = handler.handle_trigger_request(request, trigger) dockerfile_id, tags, name, subdir, metadata = specs except ValidationRequestException: