From 52caa12068b750cb3174c7d434bb0ba0aace82ee Mon Sep 17 00:00:00 2001 From: David Aguilar Date: Sat, 20 Jun 2020 15:20:03 -0700 Subject: publishers: refactor out a helpers.trigger_threshold() helper Change-Id: Ic698dc7a5f0952e5a7a4fef3350959b3d4f72d45 Signed-off-by: David Aguilar --- jenkins_jobs/modules/helpers.py | 21 +++++++++++++++++++++ jenkins_jobs/modules/publishers.py | 14 +++----------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/jenkins_jobs/modules/helpers.py b/jenkins_jobs/modules/helpers.py index 10945c71..258b13cb 100644 --- a/jenkins_jobs/modules/helpers.py +++ b/jenkins_jobs/modules/helpers.py @@ -19,6 +19,7 @@ import xml.etree.ElementTree as XML from jenkins_jobs.errors import InvalidAttributeError from jenkins_jobs.errors import JenkinsJobsException from jenkins_jobs.errors import MissingAttributeError +from jenkins_jobs.modules import hudson_model def build_trends_publisher(plugin_name, xml_element, data): @@ -613,6 +614,26 @@ def trigger_project(tconfigs, project_def, param_order=None): ) +def trigger_threshold( + parent_element, element_name, threshold_name, supported_thresholds=None +): + """Generate a resultThreshold XML element for build/join triggers""" + element = XML.SubElement(parent_element, element_name) + + try: + threshold = hudson_model.THRESHOLDS[threshold_name.upper()] + except KeyError: + if not supported_thresholds: + supported_thresholds = hudson_model.THRESHOLDS.keys() + raise JenkinsJobsException( + "threshold must be one of %s" % ", ".join(supported_thresholds) + ) + XML.SubElement(element, "name").text = threshold["name"] + XML.SubElement(element, "ordinal").text = threshold["ordinal"] + XML.SubElement(element, "color").text = threshold["color"] + return element + + def convert_mapping_to_xml(parent, data, mapping, fail_required=True): """Convert mapping to XML diff --git a/jenkins_jobs/modules/publishers.py b/jenkins_jobs/modules/publishers.py index 68b93e73..a9254d62 100755 --- a/jenkins_jobs/modules/publishers.py +++ b/jenkins_jobs/modules/publishers.py @@ -608,20 +608,12 @@ def trigger(registry, xml_parent, data): tconfig = XML.SubElement(xml_parent, "hudson.tasks.BuildTrigger") childProjects = XML.SubElement(tconfig, "childProjects") childProjects.text = data["project"] - tthreshold = XML.SubElement(tconfig, "threshold") threshold = data.get("threshold", "SUCCESS") supported_thresholds = ["SUCCESS", "UNSTABLE", "FAILURE"] - if threshold not in supported_thresholds: - raise JenkinsJobsException( - "threshold must be one of %s" % ", ".join(supported_thresholds) - ) - tname = XML.SubElement(tthreshold, "name") - tname.text = hudson_model.THRESHOLDS[threshold]["name"] - tordinal = XML.SubElement(tthreshold, "ordinal") - tordinal.text = hudson_model.THRESHOLDS[threshold]["ordinal"] - tcolor = XML.SubElement(tthreshold, "color") - tcolor.text = hudson_model.THRESHOLDS[threshold]["color"] + helpers.trigger_threshold( + tconfig, "threshold", threshold, supported_thresholds=supported_thresholds + ) def clone_workspace(registry, xml_parent, data): -- cgit