From 70ed22362a9c13ea9f3165e5747dcca281ad439c Mon Sep 17 00:00:00 2001
From: Joao Vale <jpvale@gmail.com>
Date: Thu, 17 Apr 2014 16:14:50 +0100
Subject: [PATCH] Allow using template variables in "defaults" field.

Change-Id: I876ab7fa61258e84b00a67325aeb9234aff8fa62
---
 doc/source/configuration.rst                |  5 +++
 jenkins_jobs/builder.py                     |  7 ++--
 tests/yamlparser/fixtures/templates003.xml  | 40 +++++++++++++++++++++
 tests/yamlparser/fixtures/templates003.yaml | 29 +++++++++++++++
 4 files changed, 76 insertions(+), 5 deletions(-)
 create mode 100644 tests/yamlparser/fixtures/templates003.xml
 create mode 100644 tests/yamlparser/fixtures/templates003.yaml

diff --git a/doc/source/configuration.rst b/doc/source/configuration.rst
index f640719ce..aaf877840 100644
--- a/doc/source/configuration.rst
+++ b/doc/source/configuration.rst
@@ -290,6 +290,11 @@ definitions unless they specify a different Default object with the
 
 Will set the job description for every job created.
 
+Template variables can also be used to specify an appropriate set of
+defaults for each generated job:
+
+.. literalinclude::  /../../tests/yamlparser/fixtures/templates003.yaml
+
 .. _advanced:
 
 Advanced
diff --git a/jenkins_jobs/builder.py b/jenkins_jobs/builder.py
index eb762f0c2..24104b102 100644
--- a/jenkins_jobs/builder.py
+++ b/jenkins_jobs/builder.py
@@ -162,10 +162,7 @@ class YamlParser(object):
         return self.data.get('job-group', {}).get(name, None)
 
     def getJobTemplate(self, name):
-        job = self.data.get('job-template', {}).get(name, None)
-        if not job:
-            return job
-        return self.applyDefaults(job)
+        return self.data.get('job-template', {}).get(name, None)
 
     def applyDefaults(self, data):
         whichdefaults = data.get('defaults', 'global')
@@ -271,7 +268,7 @@ class YamlParser(object):
                     expanded_values[k] = v
 
             params.update(expanded_values)
-            expanded = deep_format(template, params)
+            expanded = self.applyDefaults(deep_format(template, params))
 
             # Keep track of the resulting expansions to avoid
             # regenerating the exact same job.  Whenever a project has
diff --git a/tests/yamlparser/fixtures/templates003.xml b/tests/yamlparser/fixtures/templates003.xml
new file mode 100644
index 000000000..9d22458f4
--- /dev/null
+++ b/tests/yamlparser/fixtures/templates003.xml
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="utf-8"?>
+<project>
+  <actions/>
+  <description>&lt;!-- Managed by Jenkins Job Builder --&gt;</description>
+  <keepDependencies>false</keepDependencies>
+  <disabled>false</disabled>
+  <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
+  <blockBuildWhenUpstreamBuilding>true</blockBuildWhenUpstreamBuilding>
+  <concurrentBuild>false</concurrentBuild>
+  <canRoam>true</canRoam>
+  <properties/>
+  <scm class="hudson.scm.NullSCM"/>
+  <builders/>
+  <publishers/>
+  <buildWrappers>
+    <hudson.plugins.timestamper.TimestamperBuildWrapper/>
+    <hudson.plugins.ws__cleanup.PreBuildCleanup plugin="ws-cleanup@0.14">
+      <deleteDirs>false</deleteDirs>
+    </hudson.plugins.ws__cleanup.PreBuildCleanup>
+  </buildWrappers>
+</project>
+<BLANKLINE>
+<?xml version="1.0" encoding="utf-8"?>
+<project>
+  <actions/>
+  <description>&lt;!-- Managed by Jenkins Job Builder --&gt;</description>
+  <keepDependencies>false</keepDependencies>
+  <disabled>false</disabled>
+  <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
+  <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
+  <concurrentBuild>false</concurrentBuild>
+  <canRoam>true</canRoam>
+  <properties/>
+  <scm class="hudson.scm.NullSCM"/>
+  <builders/>
+  <publishers/>
+  <buildWrappers>
+    <hudson.plugins.timestamper.TimestamperBuildWrapper/>
+  </buildWrappers>
+</project>
diff --git a/tests/yamlparser/fixtures/templates003.yaml b/tests/yamlparser/fixtures/templates003.yaml
new file mode 100644
index 000000000..e160d8a80
--- /dev/null
+++ b/tests/yamlparser/fixtures/templates003.yaml
@@ -0,0 +1,29 @@
+- defaults:
+    name: Foo
+    disabled: false
+    block-downstream: false
+    block-upstream: false
+    wrappers:
+      - timestamps
+
+- defaults:
+    name: Bar
+    disabled: false
+    block-downstream: false
+    block-upstream: true
+    wrappers:
+      - timestamps
+      - workspace-cleanup
+
+- job-template:
+    name: '{component}-Test'
+    project-type: freestyle
+    defaults: '{component}'
+
+- project:
+    name: Test-project
+    component:
+      - Foo
+      - Bar
+    jobs:
+      - '{component}-Test'