Fix support for !j2-yaml tag on project level (missing deepcopy impl)

The properties of 'project' objects are deepcopied in
YamlParser._expandYamlForTemplateJob(), including any Jinja2YamlLoader
objects produced by !j2-yaml tag. Because of the missing __deepcopy__()
definition in Jinja2YamlLoader, the parent class' implementation was
used, causing the deepcopied object be of Jinja2Loader type.
Consequently the rendered value was always a string, not an instance of
LateYamlLoader as it supposed to be.

This change fixes the issue in Jinja2YamlLoader and potentially in
LateYamlLoader, however the latter is currently not deepcopied anywhere
so no test could be written to cover it.

Change-Id: I24fda368a4af0e9aac7b78c478ac7b4eddf27b9b
This commit is contained in:
Adam Romanek 2021-02-01 20:24:50 +01:00
parent 575b0520f1
commit c4e2f03776
3 changed files with 72 additions and 0 deletions

View File

@ -609,6 +609,9 @@ class LateYamlLoader(CustomLoader):
self._yaml_str = yaml_str
self._loader = loader
def __deepcopy__(self, memo):
return LateYamlLoader(self._yaml_str, copy.deepcopy(self._loader, memo))
def get_object_to_format(self):
return load(self._yaml_str, search_path=self._loader._search_path)
@ -618,6 +621,9 @@ class Jinja2YamlLoader(Jinja2Loader):
yaml_str = super(Jinja2YamlLoader, self).format(**kwargs)
return LateYamlLoader(yaml_str, self)
def __deepcopy__(self, memo):
return Jinja2YamlLoader(self._contents, self._search_path)
class CustomLoaderCollection(object):
"""Helper class to format a collection of CustomLoader objects"""

View File

@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<project>
<actions/>
<description>&lt;!-- Managed by Jenkins Job Builder --&gt;</description>
<keepDependencies>false</keepDependencies>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<concurrentBuild>false</concurrentBuild>
<canRoam>true</canRoam>
<properties/>
<scm class="hudson.scm.NullSCM"/>
<builders>
<hudson.tasks.Shell>
<command>echo &quot;['repo-1', 'repo-2', 'repo-a1', 'repo-a2']&quot;</command>
</hudson.tasks.Shell>
</builders>
<publishers/>
<buildWrappers/>
</project>
<?xml version="1.0" encoding="utf-8"?>
<project>
<actions/>
<description>&lt;!-- Managed by Jenkins Job Builder --&gt;</description>
<keepDependencies>false</keepDependencies>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<concurrentBuild>false</concurrentBuild>
<canRoam>true</canRoam>
<properties/>
<scm class="hudson.scm.NullSCM"/>
<builders>
<hudson.tasks.Shell>
<command>echo &quot;['repo-1', 'repo-2', 'repo-b1', 'repo-b2']&quot;</command>
</hudson.tasks.Shell>
</builders>
<publishers/>
<buildWrappers/>
</project>

View File

@ -0,0 +1,28 @@
- job-template:
name: 'test-job-template-{variant}'
builders:
- shell:
echo "{repos}"
- project:
name: test-project
repos_common:
- repo-1
- repo-2
jobs:
- 'test-job-template-{variant}':
variant: 'a'
repos: !j2-yaml: |
{% for repo in repos_common %}
- {{ repo }}
{% endfor %}
- repo-a1
- repo-a2
- 'test-job-template-{variant}':
variant: 'b'
repos: !j2-yaml: |
{% for repo in repos_common %}
- {{ repo }}
{% endfor %}
- repo-b1
- repo-b2