From aea6cf6487977e24b1ed6ad93b0dda1d878f3c4f Mon Sep 17 00:00:00 2001 From: "James E. Blair" Date: Mon, 16 Dec 2013 15:38:12 -0800 Subject: [PATCH] Automatically provide 'name' to project templates Provide the short name of a project (anything after the last '/') to project templates as the variable 'name'. If 'openstack/nova' invoked a template, the variable 'name' would automatically be set to 'nova' within the template. Ideally this means that most template invocations in OpenStack's layout will not need any variables defined. Change-Id: I596744917c30c92041b8ea5b1f518d50fb64e59b --- doc/source/zuul.rst | 7 +++++-- tests/fixtures/layout.yaml | 13 ++++++------- tests/test_scheduler.py | 12 ++++++------ zuul/layoutvalidator.py | 3 +++ zuul/scheduler.py | 2 ++ 5 files changed, 22 insertions(+), 15 deletions(-) diff --git a/doc/source/zuul.rst b/doc/source/zuul.rst index 5560317658..0ec9f88062 100644 --- a/doc/source/zuul.rst +++ b/doc/source/zuul.rst @@ -634,8 +634,11 @@ key:: - name: plugin-triggering jobprefix: plugin-foobar -You can pass several parameters to a template. A ``parameter`` value will be -used for expansion of ``{parameter}`` in the template strings. +You can pass several parameters to a template. A ``parameter`` value +will be used for expansion of ``{parameter}`` in the template +strings. The parameter ``name`` will be automatically provided and +will contain the short name of the project, that is the portion of the +project name after the last ``/`` character. Multiple templates can be combined in a project, and the jobs from all of those templates will be added to the project. Individual jobs may diff --git a/tests/fixtures/layout.yaml b/tests/fixtures/layout.yaml index 2e113c2f0e..98dfe86731 100644 --- a/tests/fixtures/layout.yaml +++ b/tests/fixtures/layout.yaml @@ -112,14 +112,14 @@ project-templates: - '{projectname}-test2' - name: test-three-and-four check: - - '{projectname}-test3' - - '{projectname}-test4' + - '{name}-test3' + - '{name}-test4' - name: test-five check: - - '{projectname}-test5' + - '{name}-{something}-test5' - name: test-five-also check: - - '{projectname}-test5' + - '{name}-{something}-test5' projects: - name: org/project @@ -213,11 +213,10 @@ projects: - name: test-one-and-two projectname: project - name: test-three-and-four - projectname: project - name: test-five - projectname: project + something: foo - name: test-five-also - projectname: project + something: foo check: - project-test6 diff --git a/tests/test_scheduler.py b/tests/test_scheduler.py index 8fb84195b5..48f2281284 100755 --- a/tests/test_scheduler.py +++ b/tests/test_scheduler.py @@ -1971,14 +1971,14 @@ class TestScheduler(testtools.TestCase): 'SUCCESS') self.assertEqual(self.getJobFromHistory('project-test2').result, 'SUCCESS') - self.assertEqual(self.getJobFromHistory('project-test3').result, - 'SUCCESS') - self.assertEqual(self.getJobFromHistory('project-test4').result, - 'SUCCESS') - # project-test5 should run twice because two templates define it + self.assertEqual(self.getJobFromHistory('layered-project-test3' + ).result, 'SUCCESS') + self.assertEqual(self.getJobFromHistory('layered-project-test4' + ).result, 'SUCCESS') + # test5 should run twice because two templates define it test5_count = 0 for job in self.worker.build_history: - if job.name == 'project-test5': + if job.name == 'layered-project-foo-test5': test5_count += 1 self.assertEqual(job.result, 'SUCCESS') self.assertEqual(test5_count, 2) diff --git a/zuul/layoutvalidator.py b/zuul/layoutvalidator.py index 70c7101577..bc82501507 100644 --- a/zuul/layoutvalidator.py +++ b/zuul/layoutvalidator.py @@ -156,6 +156,9 @@ class LayoutSchema(object): # Craft the templates schemas schema = {v.Required('name'): v.Any(*template_names)} for required_param in template_parameters: + # special case 'name' which will be automatically provided + if required_param == 'name': + continue # add this template parameters as requirements: schema.update({v.Required(required_param): str}) diff --git a/zuul/scheduler.py b/zuul/scheduler.py index 6eca846c54..96bd624714 100644 --- a/zuul/scheduler.py +++ b/zuul/scheduler.py @@ -286,6 +286,7 @@ class Scheduler(threading.Thread): for config_project in data.get('projects', []): project = Project(config_project['name']) + shortname = config_project['name'].split('/')[-1] # This is reversed due to the prepend operation below, so # the ultimate order is templates (in order) followed by @@ -296,6 +297,7 @@ class Scheduler(threading.Thread): tpl = project_templates.get( requested_template.get('name')) # Expand it with the project context + requested_template['name'] = shortname expanded = deep_format(tpl, requested_template) # Finally merge the expansion with whatever has been # already defined for this project. Prepend our new