From 7c81d90cda162ec3bd4c9a314af156f9c963a351 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Wed, 18 Feb 2015 14:12:59 -0500 Subject: [PATCH] Start recording the commit sha and other metadata about github triggered builds. We'll eventually show this information in the UI --- endpoints/api/trigger.py | 4 ++-- endpoints/common.py | 5 +++-- endpoints/trigger.py | 11 ++++++++++- endpoints/webhooks.py | 4 ++-- test/test_api_usage.py | 7 +++++-- 5 files changed, 22 insertions(+), 9 deletions(-) diff --git a/endpoints/api/trigger.py b/endpoints/api/trigger.py index ed5262852..1c59be1ee 100644 --- a/endpoints/api/trigger.py +++ b/endpoints/api/trigger.py @@ -415,13 +415,13 @@ class ActivateBuildTrigger(RepositoryParamResource): try: run_parameters = request.get_json() specs = handler.manual_start(trigger.auth_token, config_dict, run_parameters=run_parameters) - dockerfile_id, tags, name, subdir = specs + dockerfile_id, tags, name, subdir, metadata = specs repo = model.get_repository(namespace, repository) pull_robot_name = model.get_pull_robot_name(trigger) build_request = start_build(repo, dockerfile_id, tags, name, subdir, True, - pull_robot_name=pull_robot_name) + pull_robot_name=pull_robot_name, trigger_metadata=metadata) except TriggerStartException as tse: raise InvalidRequest(tse.message) diff --git a/endpoints/common.py b/endpoints/common.py index 9d4be7771..50c6239c8 100644 --- a/endpoints/common.py +++ b/endpoints/common.py @@ -211,7 +211,7 @@ def check_repository_usage(user_or_org, plan_found): def start_build(repository, dockerfile_id, tags, build_name, subdir, manual, - trigger=None, pull_robot_name=None): + trigger=None, pull_robot_name=None, trigger_metadata=None): host = urlparse.urlparse(request.url).netloc repo_path = '%s/%s/%s' % (host, repository.namespace_user.username, repository.name) @@ -223,7 +223,8 @@ def start_build(repository, dockerfile_id, tags, build_name, subdir, manual, job_config = { 'docker_tags': tags, 'registry': host, - 'build_subdir': subdir + 'build_subdir': subdir, + 'trigger_metadata': trigger_metadata or {} } with app.config['DB_TRANSACTION_FACTORY'](db): diff --git a/endpoints/trigger.py b/endpoints/trigger.py index c84766d16..cc248f798 100644 --- a/endpoints/trigger.py +++ b/endpoints/trigger.py @@ -345,8 +345,10 @@ class GithubBuildTrigger(BuildTrigger): # compute the tag(s) branch = ref.split('/')[-1] tags = {branch} + if branch == repo.default_branch: tags.add('latest') + logger.debug('Pushing to tags: %s' % tags) # compute the subdir @@ -354,7 +356,14 @@ class GithubBuildTrigger(BuildTrigger): joined_subdir = os.path.join(tarball_subdir, repo_subdir) logger.debug('Final subdir: %s' % joined_subdir) - return dockerfile_id, list(tags), build_name, joined_subdir + # compute the metadata + metadata = { + 'commit_sha': commit_sha, + 'ref': ref, + 'default_branch': repo.default_branch + } + + return dockerfile_id, list(tags), build_name, joined_subdir, metadata @staticmethod def get_display_name(sha): diff --git a/endpoints/webhooks.py b/endpoints/webhooks.py index 2a8627100..44d788486 100644 --- a/endpoints/webhooks.py +++ b/endpoints/webhooks.py @@ -91,7 +91,7 @@ def build_trigger_webhook(trigger_uuid, **kwargs): try: specs = handler.handle_trigger_request(request, trigger.auth_token, config_dict) - dockerfile_id, tags, name, subdir = specs + dockerfile_id, tags, name, subdir, metadata = specs except ValidationRequestException: # This was just a validation request, we don't need to build anything @@ -104,7 +104,7 @@ def build_trigger_webhook(trigger_uuid, **kwargs): pull_robot_name = model.get_pull_robot_name(trigger) repo = model.get_repository(namespace, repository) start_build(repo, dockerfile_id, tags, name, subdir, False, trigger, - pull_robot_name=pull_robot_name) + pull_robot_name=pull_robot_name, trigger_metadata=metadata) return make_response('Okay') diff --git a/test/test_api_usage.py b/test/test_api_usage.py index 1c97d2983..c0cdf767f 100644 --- a/test/test_api_usage.py +++ b/test/test_api_usage.py @@ -2276,7 +2276,7 @@ class FakeBuildTrigger(BuildTriggerBase): return [auth_token, 'foo', 'bar', config['somevalue']] def handle_trigger_request(self, request, auth_token, config): - return ('foo', ['bar'], 'build-name', 'subdir') + return ('foo', ['bar'], 'build-name', 'subdir', {'foo': 'bar'}) def is_active(self, config): return 'active' in config and config['active'] @@ -2290,7 +2290,7 @@ class FakeBuildTrigger(BuildTriggerBase): return config def manual_start(self, auth_token, config, run_parameters=None): - return ('foo', ['bar'], 'build-name', 'subdir') + return ('foo', ['bar'], 'build-name', 'subdir', {'foo': 'bar'}) def dockerfile_url(self, auth_token, config): return 'http://some/url' @@ -2501,6 +2501,9 @@ class TestBuildTriggers(ApiTestCase): self.assertEquals("build-name", start_json['display_name']) self.assertEquals(['bar'], start_json['job_config']['docker_tags']) + # Verify the metadata was added. + build_obj = database.RepositoryBuild.get(database.RepositoryBuild.uuid == start_json['id']) + self.assertEquals('bar', py_json.loads(build_obj.job_config)['trigger_metadata']['foo']) def test_invalid_robot_account(self): self.login(ADMIN_ACCESS_USER)