summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Aguilar <davvid@gmail.com>2020-06-20 15:20:03 -0700
committerDavid Aguilar <davvid@gmail.com>2020-06-23 23:35:05 -0700
commit52caa12068b750cb3174c7d434bb0ba0aace82ee (patch)
tree7823b17287905442fc426528aff1ebd3498c9a46
parent804df6c9eb275d966f66c01b3e97c65dbbde6ac4 (diff)
downloadpython-jenkins-job-builder-52caa12068b750cb3174c7d434bb0ba0aace82ee.tar.gz
python-jenkins-job-builder-52caa12068b750cb3174c7d434bb0ba0aace82ee.tar.xz
python-jenkins-job-builder-52caa12068b750cb3174c7d434bb0ba0aace82ee.zip
publishers: refactor out a helpers.trigger_threshold() helper
Change-Id: Ic698dc7a5f0952e5a7a4fef3350959b3d4f72d45 Signed-off-by: David Aguilar <davvid@gmail.com>
-rw-r--r--jenkins_jobs/modules/helpers.py21
-rwxr-xr-xjenkins_jobs/modules/publishers.py14
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):