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
diff --git a/doc/source/zuul.rst b/doc/source/zuul.rst
index 5560317..0ec9f88 100644
--- a/doc/source/zuul.rst
+++ b/doc/source/zuul.rst
@@ -634,8 +634,11 @@
- 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 2e113c2..98dfe86 100644
--- a/tests/fixtures/layout.yaml
+++ b/tests/fixtures/layout.yaml
@@ -112,14 +112,14 @@
- '{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 @@
- 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 8fb8419..48f2281 100755
--- a/tests/test_scheduler.py
+++ b/tests/test_scheduler.py
@@ -1971,14 +1971,14 @@
'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 70c7101..bc82501 100644
--- a/zuul/layoutvalidator.py
+++ b/zuul/layoutvalidator.py
@@ -156,6 +156,9 @@
# 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 6eca846..96bd624 100644
--- a/zuul/scheduler.py
+++ b/zuul/scheduler.py
@@ -286,6 +286,7 @@
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 @@
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