From c9ed6891cf292e00c454ab8c0aa6041c88b1aac9 Mon Sep 17 00:00:00 2001 From: Darragh Bailey Date: Mon, 1 Sep 2014 15:27:02 +0100 Subject: [PATCH] Retain user specified order of post build scripts Avoid imposing hardcoded order of the scripts within the module and instead favour the order of the input data. Combined with use of ordered dicts for yaml, this ensures that users may control the order of the various steps to use with the post build scripts from yaml. Change-Id: Icf540a53cb6f1b13f80f09d9103610aee10d3861 --- jenkins_jobs/modules/publishers.py | 55 ++++++++++--------- .../fixtures/postbuildscript004.xml | 42 ++++++++++++++ .../fixtures/postbuildscript004.yaml | 16 ++++++ 3 files changed, 88 insertions(+), 25 deletions(-) create mode 100644 tests/publishers/fixtures/postbuildscript004.xml create mode 100644 tests/publishers/fixtures/postbuildscript004.yaml diff --git a/jenkins_jobs/modules/publishers.py b/jenkins_jobs/modules/publishers.py index d3f683f54..94fb91b02 100644 --- a/jenkins_jobs/modules/publishers.py +++ b/jenkins_jobs/modules/publishers.py @@ -2396,40 +2396,45 @@ postbuildscript003.yaml # Shell/Groovy in a file script_types = { - 'generic': 'GenericScript', - 'groovy': 'GroovyScriptFile', + 'generic-script': 'GenericScript', + 'groovy-script': 'GroovyScriptFile', } - for script_type in sorted(script_types.keys()): - if script_type + '-script' not in data: - continue - scripts_xml = XML.SubElement(pbs_xml, script_type + 'ScriptFileList') - for shell_scripts in [data.get(script_type + '-script', [])]: - for shell_script in shell_scripts: + # Assuming yaml preserves order of input data make sure + # corresponding XML steps are generated in the same order + build_scripts = [(k, v) for k, v in data.items() + if k in script_types or k in ['groovy', 'builders']] + + for step, script_data in build_scripts: + if step in script_types: + scripts_xml = XML.SubElement(pbs_xml, step[:-len('-script')] + + 'ScriptFileList') + for shell_script in script_data: script_xml = XML.SubElement( scripts_xml, 'org.jenkinsci.plugins.postbuildscript.' - + script_types[script_type]) + + script_types[step]) file_path_xml = XML.SubElement(script_xml, 'filePath') file_path_xml.text = shell_script - # Inlined Groovy - if 'groovy' in data: - groovy_inline_xml = XML.SubElement(pbs_xml, 'groovyScriptContentList') - for groovy in data.get('groovy', []): - groovy_xml = XML.SubElement( - groovy_inline_xml, - 'org.jenkinsci.plugins.postbuildscript.GroovyScriptContent' - ) - groovy_content = XML.SubElement(groovy_xml, 'content') - groovy_content.text = groovy + # Inlined Groovy + if step == 'groovy': + groovy_inline_xml = XML.SubElement(pbs_xml, + 'groovyScriptContentList') + for groovy in script_data: + groovy_xml = XML.SubElement( + groovy_inline_xml, + 'org.jenkinsci.plugins.postbuildscript.GroovyScriptContent' + ) + groovy_content = XML.SubElement(groovy_xml, 'content') + groovy_content.text = groovy - # Inject builders - if 'builders' in data: - build_steps_xml = XML.SubElement(pbs_xml, 'buildSteps') - for builder in data.get('builders', []): - parser.registry.dispatch('builder', parser, build_steps_xml, - builder) + # Inject builders + if step == 'builders': + build_steps_xml = XML.SubElement(pbs_xml, 'buildSteps') + for builder in script_data: + parser.registry.dispatch('builder', parser, build_steps_xml, + builder) # When to run the build? Note the plugin let one specify both options # although they are antinomic diff --git a/tests/publishers/fixtures/postbuildscript004.xml b/tests/publishers/fixtures/postbuildscript004.xml new file mode 100644 index 000000000..cc82f30be --- /dev/null +++ b/tests/publishers/fixtures/postbuildscript004.xml @@ -0,0 +1,42 @@ + + + + + + + echo "Shell execution should be first" + + + ant_target + default + + + + + /** This is some inlined groovy */ + + + /** Some more inlined groovy */ + + + + + /tmp/one.sh + + + /tmp/two.sh + + + + + /tmp/one.groovy + + + /tmp/two.groovy + + + false + true + + + diff --git a/tests/publishers/fixtures/postbuildscript004.yaml b/tests/publishers/fixtures/postbuildscript004.yaml new file mode 100644 index 000000000..1e883165f --- /dev/null +++ b/tests/publishers/fixtures/postbuildscript004.yaml @@ -0,0 +1,16 @@ +publishers: + - postbuildscript: + builders: + - shell: 'echo "Shell execution should be first"' + - ant: 'ant_target' + groovy: + - "/** This is some inlined groovy */" + - "/** Some more inlined groovy */" + generic-script: + - '/tmp/one.sh' + - '/tmp/two.sh' + groovy-script: + - '/tmp/one.groovy' + - '/tmp/two.groovy' + onsuccess: False + onfailure: True