diff --git a/data/database.py b/data/database.py index dd6542106..f245ccb96 100644 --- a/data/database.py +++ b/data/database.py @@ -237,6 +237,7 @@ class RepositoryBuild(BaseModel): uuid = CharField(default=uuid_generator, index=True) repository = ForeignKeyField(Repository, index=True) access_token = ForeignKeyField(AccessToken) + resource_key = CharField(index=True) job_config = TextField() phase = CharField(default='waiting') started = DateTimeField(default=datetime.now) diff --git a/data/model.py b/data/model.py index 1ddb09f2c..e9e695e9f 100644 --- a/data/model.py +++ b/data/model.py @@ -775,14 +775,15 @@ def get_all_repo_users(namespace_name, repository_name): def get_repository_for_resource(resource_key): - joined = Repository.select().join(RepositoryBuild) - query = joined.where(RepositoryBuild.resource_key == resource_key).limit(1) - result = list(query) - if not result: + try: + return (Repository + .select() + .join(RepositoryBuild) + .where(RepositoryBuild.resource_key == resource_key) + .get()) + except Repository.DoesNotExist: return None - return result[0] - def get_repository(namespace_name, repository_name): try: @@ -1420,11 +1421,12 @@ def list_repository_builds(namespace_name, repository_name, return query -def create_repository_build(repo, access_token, job_config_obj, +def create_repository_build(repo, access_token, job_config_obj, dockerfile_id, display_name, trigger=None): return RepositoryBuild.create(repository=repo, access_token=access_token, job_config=json.dumps(job_config_obj), - display_name=display_name, trigger=trigger) + display_name=display_name, trigger=trigger, + resource_key=dockerfile_id) def create_webhook(repo, params_obj): diff --git a/endpoints/api.py b/endpoints/api.py index 3a522c65a..ea4d88318 100644 --- a/endpoints/api.py +++ b/endpoints/api.py @@ -1252,15 +1252,14 @@ def request_repo_build(namespace, repository): logger.debug('**********Md5: %s' % display_name) host = urlparse.urlparse(request.url).netloc - repo = '%s/%s/%s' % (host, repo.namespace, repo.name) + repo_tag_base = '%s/%s/%s' % (host, repo.namespace, repo.name) job_config = { 'docker_tags': ['latest'], 'build_subdir': '', - 'repository': repo, - 'resource_key': dockerfile_id, + 'repository': repo_tag_base, } build_request = model.create_repository_build(repo, token, job_config, - display_name) + dockerfile_id, display_name) dockerfile_build_queue.put(json.dumps({ 'build_uuid': build_request.uuid, 'namespace': namespace, diff --git a/endpoints/webhooks.py b/endpoints/webhooks.py index 65a2ec403..35d61d1a1 100644 --- a/endpoints/webhooks.py +++ b/endpoints/webhooks.py @@ -84,10 +84,10 @@ def build_trigger_webhook(namespace, repository, trigger_uuid): 'docker_tags': tags, 'repository': repo, 'build_subdir': subdir, - 'resource_key': dockerfile_id, } build_request = model.create_repository_build(trigger.repository, token, - job_config, name, trigger) + job_config, dockerfile_id, + name, trigger) dockerfile_build_queue.put(json.dumps({ 'build_uuid': build_request.uuid, diff --git a/initdb.py b/initdb.py index 97ede01d1..071e0d180 100644 --- a/initdb.py +++ b/initdb.py @@ -327,9 +327,9 @@ def populate_database(): 'repository': repo, 'docker_tags': ['latest'], 'build_subdir': '', - 'resource_key': '701dcc3724fb4f2ea6c31400528343cd', } build = model.create_repository_build(building, token, job_config, + '701dcc3724fb4f2ea6c31400528343cd' 'build-name', trigger) build.uuid = 'deadbeef-dead-beef-dead-beefdeadbeef' build.save() diff --git a/test/data/test.db b/test/data/test.db index ec2d396b0..b2ed400b9 100644 Binary files a/test/data/test.db and b/test/data/test.db differ diff --git a/test/test_api_usage.py b/test/test_api_usage.py index 7949d18c1..f6654fdab 100644 --- a/test/test_api_usage.py +++ b/test/test_api_usage.py @@ -858,7 +858,7 @@ class TestGetRepoBuilds(ApiTestCase): assert 'status' in build -class TestRequearRepoBuild(ApiTestCase): +class TestRequestRepoBuild(ApiTestCase): def test_requestrepobuild(self): self.login(ADMIN_ACCESS_USER) @@ -871,7 +871,7 @@ class TestRequearRepoBuild(ApiTestCase): # Request a (fake) build. self.postResponse('api.request_repo_build', params=dict(repository=ADMIN_ACCESS_USER + '/simple'), - data=dict(file_id = 'foobarbaz'), + data=dict(file_id='foobarbaz'), expected_code=201) # Check for the build. diff --git a/workers/dockerfilebuild.py b/workers/dockerfilebuild.py index a6c53c798..d0ee58360 100644 --- a/workers/dockerfilebuild.py +++ b/workers/dockerfilebuild.py @@ -274,7 +274,7 @@ class DockerfileBuildWorker(Worker): job_config = json.loads(repository_build.job_config) - resource_url = user_files.get_file_url(job_config['resource_key']) + resource_url = user_files.get_file_url(repository_build.resource_key) tag_names = job_config['docker_tags'] build_subdir = job_config['build_subdir'] repo = job_config['repository']