Debian Codegraph @

jenkins_jobs/modules/helpers.py

raw
1 # Copyright 2015 Thanh Ha
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14
15 from functools import wraps
16 import logging
17
18 import xml.etree.ElementTree as XML
19
20 from jenkins_jobs.errors import InvalidAttributeError
21 from jenkins_jobs.errors import JenkinsJobsException
22 from jenkins_jobs.errors import MissingAttributeError
23 from jenkins_jobs.modules import hudson_model
24
25
26 def build_trends_publisher(plugin_name, xml_element, data):
27 """Helper to create various trend publishers."""
28
29 def append_thresholds(element, data, only_totals):
30 """Appends the status thresholds."""
31
32 for status in ["unstable", "failed"]:
33 status_data = data.get(status, {})
34
35 limits = [
36 ("total-all", "TotalAll"),
37 ("total-high", "TotalHigh"),
38 ("total-normal", "TotalNormal"),
39 ("total-low", "TotalLow"),
40 ]
41
42 if only_totals is False:
43 limits.extend(
44 [
45 ("new-all", "NewAll"),
46 ("new-high", "NewHigh"),
47 ("new-normal", "NewNormal"),
48 ("new-low", "NewLow"),
49 ]
50 )
51
52 for key, tag_suffix in limits:
53 tag_name = status + tag_suffix
54 XML.SubElement(element, tag_name).text = str(status_data.get(key, ""))
55
56 # Tuples containing: setting name, tag name, default value
57 settings = [
58 ("healthy", "healthy", ""),
59 ("unhealthy", "unHealthy", ""),
60 ("health-threshold", "thresholdLimit", "low"),
61 ("plugin-name", "pluginName", plugin_name),
62 ("default-encoding", "defaultEncoding", ""),
63 ("can-run-on-failed", "canRunOnFailed", False),
64 ("use-stable-build-as-reference", "useStableBuildAsReference", False),
65 ("use-previous-build-as-reference", "usePreviousBuildAsReference", False),
66 ("use-delta-values", "useDeltaValues", False),
67 ("thresholds", "thresholds", {}),
68 ("should-detect-modules", "shouldDetectModules", False),
69 ("dont-compute-new", "dontComputeNew", True),
70 ("do-not-resolve-relative-paths", "doNotResolveRelativePaths", False),
71 ("pattern", "pattern", ""),
72 ]
73
74 thresholds = ["low", "normal", "high"]
75
76 for key, tag_name, default in settings:
77 xml_config = XML.SubElement(xml_element, tag_name)
78 config_value = data.get(key, default)
79
80 if key == "thresholds":
81 append_thresholds(
82 xml_config, config_value, data.get("dont-compute-new", True)
83 )
84 elif key == "health-threshold" and config_value not in thresholds:
85 raise JenkinsJobsException(
86 "health-threshold must be one of %s" % ", ".join(thresholds)
87 )
88 else:
89 if isinstance(default, bool):
90 xml_config.text = str(config_value).lower()
91 else:
92 xml_config.text = str(config_value)
93
94
95 def config_file_provider_builder(xml_parent, data):
96 """Builder / Wrapper helper"""
97 xml_files = XML.SubElement(xml_parent, "managedFiles")
98
99 files = data.get("files", [])
100 for file in files:
101 xml_file = XML.SubElement(
102 xml_files, "org.jenkinsci.plugins." "configfiles.buildwrapper.ManagedFile"
103 )
104 mapping = [
105 ("file-id", "fileId", None),
106 ("target", "targetLocation", ""),
107 ("variable", "variable", ""),
108 ("replace-tokens", "replaceTokens", False),
109 ]
110 convert_mapping_to_xml(xml_file, file, mapping, fail_required=True)
111
112
113 def config_file_provider_settings(xml_parent, data):
114 SETTINGS_TYPES = ["file", "cfp"]
115 settings = {
116 "default-settings": "jenkins.mvn.DefaultSettingsProvider",
117 "settings": "jenkins.mvn.FilePathSettingsProvider",
118 "config-file-provider-settings": "org.jenkinsci.plugins.configfiles.maven.job.MvnSettingsProvider",
119 "default-global-settings": "jenkins.mvn.DefaultGlobalSettingsProvider",
120 "global-settings": "jenkins.mvn.FilePathGlobalSettingsProvider",
121 "config-file-provider-global-settings": "org.jenkinsci.plugins.configfiles.maven.job."
122 "MvnGlobalSettingsProvider",
123 }
124
125 if "settings" in data:
126 # Support for Config File Provider
127 settings_file = str(data["settings"])
128 settings_type = data.get("settings-type", "file")
129
130 # For cfp versions <2.10.0 we are able to detect cfp via the config
131 # settings name.
132 text = "org.jenkinsci.plugins.configfiles.maven.MavenSettingsConfig"
133 if settings_file.startswith(text):
134 settings_type = "cfp"
135
136 if settings_type == "file":
137 lsettings = XML.SubElement(
138 xml_parent, "settings", {"class": settings["settings"]}
139 )
140 XML.SubElement(lsettings, "path").text = settings_file
141 elif settings_type == "cfp":
142 lsettings = XML.SubElement(
143 xml_parent,
144 "settings",
145 {"class": settings["config-file-provider-settings"]},
146 )
147 XML.SubElement(lsettings, "settingsConfigId").text = settings_file
148 else:
149 raise InvalidAttributeError("settings-type", settings_type, SETTINGS_TYPES)
150 else:
151 XML.SubElement(xml_parent, "settings", {"class": settings["default-settings"]})
152
153 if "global-settings" in data:
154 # Support for Config File Provider
155 global_settings_file = str(data["global-settings"])
156 global_settings_type = data.get("global-settings-type", "file")
157
158 # For cfp versions <2.10.0 we are able to detect cfp via the config
159 # settings name.
160 text = "org.jenkinsci.plugins.configfiles.maven." "GlobalMavenSettingsConfig"
161 if global_settings_file.startswith(text):
162 global_settings_type = "cfp"
163
164 if global_settings_type == "file":
165 gsettings = XML.SubElement(
166 xml_parent, "globalSettings", {"class": settings["global-settings"]}
167 )
168 XML.SubElement(gsettings, "path").text = global_settings_file
169 elif global_settings_type == "cfp":
170 gsettings = XML.SubElement(
171 xml_parent,
172 "globalSettings",
173 {"class": settings["config-file-provider-global-settings"]},
174 )
175 XML.SubElement(gsettings, "settingsConfigId").text = global_settings_file
176 else:
177 raise InvalidAttributeError(
178 "settings-type", global_settings_type, SETTINGS_TYPES
179 )
180 else:
181 XML.SubElement(
182 xml_parent, "globalSettings", {"class": settings["default-global-settings"]}
183 )
184
185
186 def copyartifact_build_selector(xml_parent, data, select_tag="selector"):
187
188 select = data.get("which-build", "last-successful")
189 selectdict = {
190 "last-successful": "StatusBuildSelector",
191 "last-completed": "LastCompletedBuildSelector",
192 "specific-build": "SpecificBuildSelector",
193 "last-saved": "SavedBuildSelector",
194 "upstream-build": "TriggeredBuildSelector",
195 "permalink": "PermalinkBuildSelector",
196 "workspace-latest": "WorkspaceSelector",
197 "build-param": "ParameterizedBuildSelector",
198 "downstream-build": "DownstreamBuildSelector",
199 "multijob-build": "MultiJobBuildSelector",
200 }
201 if select not in selectdict:
202 raise InvalidAttributeError("which-build", select, selectdict.keys())
203 permalink = data.get("permalink", "last")
204 permalinkdict = {
205 "last": "lastBuild",
206 "last-stable": "lastStableBuild",
207 "last-successful": "lastSuccessfulBuild",
208 "last-failed": "lastFailedBuild",
209 "last-unstable": "lastUnstableBuild",
210 "last-unsuccessful": "lastUnsuccessfulBuild",
211 }
212 if permalink not in permalinkdict:
213 raise InvalidAttributeError("permalink", permalink, permalinkdict.keys())
214 if select == "multijob-build":
215 selector = XML.SubElement(
216 xml_parent,
217 select_tag,
218 {"class": "com.tikal.jenkins.plugins.multijob." + selectdict[select]},
219 )
220 else:
221 selector = XML.SubElement(
222 xml_parent,
223 select_tag,
224 {"class": "hudson.plugins.copyartifact." + selectdict[select]},
225 )
226 mapping = []
227 if select == "specific-build":
228 mapping.append(("build-number", "buildNumber", ""))
229 if select == "last-successful":
230 mapping.append(("stable", "stable", False))
231 if select == "upstream-build":
232 mapping.append(
233 ("fallback-to-last-successful", "fallbackToLastSuccessful", False)
234 )
235 if select == "permalink":
236 mapping.append(("", "id", permalinkdict[permalink]))
237 if select == "build-param":
238 mapping.append(("param", "parameterName", ""))
239 if select == "downstream-build":
240 mapping.append(("upstream-project-name", "upstreamProjectName", ""))
241 mapping.append(("upstream-build-number", "upstreamBuildNumber", ""))
242 convert_mapping_to_xml(selector, data, mapping, fail_required=False)
243
244
245 def findbugs_settings(xml_parent, data):
246 # General Options
247 mapping = [
248 ("rank-priority", "isRankActivated", False),
249 ("include-files", "includePattern", ""),
250 ("exclude-files", "excludePattern", ""),
251 ]
252 convert_mapping_to_xml(xml_parent, data, mapping, fail_required=True)
253
254
255 def get_value_from_yaml_or_config_file(key, section, data, jjb_config):
256 return jjb_config.get_plugin_config(section, key, data.get(key, ""))
257
258
259 def cloudformation_region_dict():
260 region_dict = {
261 "us-east-1": "US_East_Northern_Virginia",
262 "us-west-1": "US_WEST_Northern_California",
263 "us-west-2": "US_WEST_Oregon",
264 "eu-central-1": "EU_Frankfurt",
265 "eu-west-1": "EU_Ireland",
266 "ap-southeast-1": "Asia_Pacific_Singapore",
267 "ap-southeast-2": "Asia_Pacific_Sydney",
268 "ap-northeast-1": "Asia_Pacific_Tokyo",
269 "sa-east-1": "South_America_Sao_Paulo",
270 }
271 return region_dict
272
273
274 def cloudformation_init(xml_parent, data, xml_tag):
275 cloudformation = XML.SubElement(
276 xml_parent,
277 "com.syncapse.jenkinsci." "plugins.awscloudformationwrapper." + xml_tag,
278 )
279 return XML.SubElement(cloudformation, "stacks")
280
281
282 def cloudformation_stack(xml_parent, stack, xml_tag, stacks, region_dict):
283 if "name" not in stack or stack["name"] == "":
284 raise MissingAttributeError("name")
285 step = XML.SubElement(
286 stacks, "com.syncapse.jenkinsci.plugins." "awscloudformationwrapper." + xml_tag
287 )
288
289 if xml_tag == "SimpleStackBean":
290 mapping = [("prefix", "isPrefixSelected", False)]
291 else:
292 parameters_value = ",".join(stack.get("parameters", []))
293 mapping = [
294 ("description", "description", ""),
295 ("", "parameters", parameters_value),
296 ("timeout", "timeout", "0"),
297 ("sleep", "sleep", "0"),
298 ("recipe", "cloudFormationRecipe", None),
299 ]
300
301 cloudformation_stack_mapping = [
302 ("name", "stackName", None),
303 ("access-key", "awsAccessKey", None),
304 ("secret-key", "awsSecretKey", None),
305 ("region", "awsRegion", None, region_dict),
306 ]
307 for map in mapping:
308 cloudformation_stack_mapping.append(map)
309
310 convert_mapping_to_xml(
311 step, stack, cloudformation_stack_mapping, fail_required=True
312 )
313
314
315 def include_exclude_patterns(xml_parent, data, yaml_prefix, xml_elem_name):
316 xml_element = XML.SubElement(xml_parent, xml_elem_name)
317 XML.SubElement(xml_element, "includePatterns").text = ",".join(
318 data.get(yaml_prefix + "-include-patterns", [])
319 )
320 XML.SubElement(xml_element, "excludePatterns").text = ",".join(
321 data.get(yaml_prefix + "-exclude-patterns", [])
322 )
323
324
325 def artifactory_deployment_patterns(xml_parent, data):
326 include_exclude_patterns(
327 xml_parent, data, "deployment", "artifactDeploymentPatterns"
328 )
329
330
331 def artifactory_env_vars_patterns(xml_parent, data):
332 include_exclude_patterns(xml_parent, data, "env-vars", "envVarsPatterns")
333
334
335 def artifactory_optional_props(xml_parent, data, target):
336 optional_str_props = [
337 ("scopes", "scopes"),
338 ("violationRecipients", "violation-recipients"),
339 ("blackDuckAppName", "black-duck-app-name"),
340 ("blackDuckAppVersion", "black-duck-app-version"),
341 ("blackDuckReportRecipients", "black-duck-report-recipients"),
342 ("blackDuckScopes", "black-duck-scopes"),
343 ]
344
345 for (xml_prop, yaml_prop) in optional_str_props:
346 XML.SubElement(xml_parent, xml_prop).text = data.get(yaml_prop, "")
347
348 common_bool_props = [
349 # yaml property name, xml property name, default value
350 ("deploy-artifacts", "deployArtifacts", True),
351 ("discard-old-builds", "discardOldBuilds", False),
352 ("discard-build-artifacts", "discardBuildArtifacts", False),
353 ("publish-build-info", "deployBuildInfo", False),
354 ("env-vars-include", "includeEnvVars", False),
355 ("run-checks", "runChecks", False),
356 ("include-publish-artifacts", "includePublishArtifacts", False),
357 ("license-auto-discovery", "licenseAutoDiscovery", True),
358 ("enable-issue-tracker-integration", "enableIssueTrackerIntegration", False),
359 ("aggregate-build-issues", "aggregateBuildIssues", False),
360 ("black-duck-run-checks", "blackDuckRunChecks", False),
361 (
362 "black-duck-include-published-artifacts",
363 "blackDuckIncludePublishedArtifacts",
364 False,
365 ),
366 (
367 "auto-create-missing-component-requests",
368 "autoCreateMissingComponentRequests",
369 True,
370 ),
371 (
372 "auto-discard-stale-component-requests",
373 "autoDiscardStaleComponentRequests",
374 True,
375 ),
376 (
377 "filter-excluded-artifacts-from-build",
378 "filterExcludedArtifactsFromBuild",
379 False,
380 ),
381 ]
382 convert_mapping_to_xml(xml_parent, data, common_bool_props, fail_required=True)
383
384 if "wrappers" in target:
385 wrapper_bool_props = [
386 ("enable-resolve-artifacts", "enableResolveArtifacts", False),
387 ("disable-license-auto-discovery", "disableLicenseAutoDiscovery", False),
388 ("record-all-dependencies", "recordAllDependencies", False),
389 ]
390 convert_mapping_to_xml(xml_parent, data, wrapper_bool_props, fail_required=True)
391
392 if "publishers" in target:
393 publisher_bool_props = [
394 ("even-if-unstable", "evenIfUnstable", False),
395 ("pass-identified-downstream", "passIdentifiedDownstream", False),
396 (
397 "allow-promotion-of-non-staged-builds",
398 "allowPromotionOfNonStagedBuilds",
399 False,
400 ),
401 ]
402 convert_mapping_to_xml(
403 xml_parent, data, publisher_bool_props, fail_required=True
404 )
405
406
407 def artifactory_common_details(details, data):
408 mapping = [("name", "artifactoryName", ""), ("url", "artifactoryUrl", "")]
409 convert_mapping_to_xml(details, data, mapping, fail_required=True)
410
411
412 def artifactory_repository(xml_parent, data, target):
413 if "release" in target:
414 release_mapping = [
415 ("deploy-release-repo-key", "keyFromText", ""),
416 ("deploy-release-repo-key", "keyFromSelect", ""),
417 ("deploy-dynamic-mode", "dynamicMode", False),
418 ]
419 convert_mapping_to_xml(xml_parent, data, release_mapping, fail_required=True)
420
421 if "snapshot" in target:
422 snapshot_mapping = [
423 ("deploy-snapshot-repo-key", "keyFromText", ""),
424 ("deploy-snapshot-repo-key", "keyFromSelect", ""),
425 ("deploy-dynamic-mode", "dynamicMode", False),
426 ]
427 convert_mapping_to_xml(xml_parent, data, snapshot_mapping, fail_required=True)
428
429
430 def append_git_revision_config(parent, config_def):
431 params = XML.SubElement(parent, "hudson.plugins.git.GitRevisionBuildParameters")
432
433 try:
434 # If git-revision is a boolean, the get() will
435 # throw an AttributeError
436 combine_commits = str(config_def.get("combine-queued-commits", False)).lower()
437 except AttributeError:
438 combine_commits = "false"
439
440 XML.SubElement(params, "combineQueuedCommits").text = combine_commits
441
442
443 def test_fairy_common(xml_element, data):
444 xml_element.set("plugin", "TestFairy")
445 valid_max_duration = ["10m", "60m", "300m", "1440m"]
446 valid_interval = [1, 2, 5]
447 valid_video_quality = ["high", "medium", "low"]
448
449 mappings = [
450 # General
451 ("apikey", "apiKey", None),
452 ("appfile", "appFile", None),
453 ("tester-groups", "testersGroups", ""),
454 ("notify-testers", "notifyTesters", True),
455 ("autoupdate", "autoUpdate", True),
456 # Session
457 ("max-duration", "maxDuration", "10m", valid_max_duration),
458 ("record-on-background", "recordOnBackground", False),
459 ("data-only-wifi", "dataOnlyWifi", False),
460 # Video
461 ("video-enabled", "isVideoEnabled", True),
462 ("screenshot-interval", "screenshotInterval", 1, valid_interval),
463 ("video-quality", "videoQuality", "high", valid_video_quality),
464 # Metrics
465 ("cpu", "cpu", True),
466 ("memory", "memory", True),
467 ("logs", "logs", True),
468 ("network", "network", False),
469 ("phone-signal", "phoneSignal", False),
470 ("wifi", "wifi", False),
471 ("gps", "gps", False),
472 ("battery", "battery", False),
473 ("opengl", "openGl", False),
474 # Advanced options
475 ("advanced-options", "advancedOptions", ""),
476 ]
477 convert_mapping_to_xml(xml_element, data, mappings, fail_required=True)
478
479
480 def trigger_get_parameter_order(registry, plugin):
481 logger = logging.getLogger("%s:trigger_get_parameter_order" % __name__)
482
483 if (
484 str(
485 registry.jjb_config.get_plugin_config(plugin, "param_order_from_yaml", True)
486 ).lower()
487 == "false"
488 ):
489 logger.warning(
490 "Using deprecated order for parameter sets in %s. It is "
491 "recommended that you update your job definition instead of "
492 "enabling use of the old hardcoded order",
493 plugin,
494 )
495
496 # deprecated order
497 return [
498 "predefined-parameters",
499 "git-revision",
500 "property-file",
501 "current-parameters",
502 "node-parameters",
503 "svn-revision",
504 "restrict-matrix-project",
505 "node-label-name",
506 "node-label",
507 "boolean-parameters",
508 ]
509
510 return None
511
512
513 def trigger_project(tconfigs, project_def, registry, param_order=None):
514
515 plugin_ver = registry.get_plugin_version("parameterized-trigger")
516
517 logger = logging.getLogger("%s:trigger_project" % __name__)
518 pt_prefix = "hudson.plugins.parameterizedtrigger."
519 if param_order:
520 parameters = param_order
521 else:
522 parameters = project_def.keys()
523
524 for param_type in parameters:
525 param_value = project_def.get(param_type)
526 if param_value is None:
527 continue
528
529 if param_type == "predefined-parameters":
530 params = XML.SubElement(tconfigs, pt_prefix + "PredefinedBuildParameters")
531 properties = XML.SubElement(params, "properties")
532 properties.text = param_value
533 elif param_type == "git-revision" and param_value:
534 if "combine-queued-commits" in project_def:
535 logger.warning(
536 "'combine-queued-commit' has moved to reside under "
537 "'git-revision' configuration, please update your "
538 "configs as support for this will be removed."
539 )
540 git_revision = {
541 "combine-queued-commits": project_def["combine-queued-commits"]
542 }
543 else:
544 git_revision = project_def["git-revision"]
545 append_git_revision_config(tconfigs, git_revision)
546 elif param_type == "property-file":
547 params = XML.SubElement(tconfigs, pt_prefix + "FileBuildParameters")
548 property_file_mapping = [
549 ("property-file", "propertiesFile", None),
550 ("fail-on-missing", "failTriggerOnMissing", False),
551 ]
552
553 if plugin_ver >= "2.35.2":
554 property_file_mapping.append(
555 ("property-multiline", "textParamValueOnNewLine", False)
556 )
557
558 convert_mapping_to_xml(
559 params, project_def, property_file_mapping, fail_required=True
560 )
561 if "file-encoding" in project_def:
562 XML.SubElement(params, "encoding").text = project_def["file-encoding"]
563 if "use-matrix-child-files" in project_def:
564 # TODO: These parameters only affect execution in
565 # publishers of matrix projects; we should warn if they are
566 # used in other contexts.
567 use_matrix_child_files_mapping = [
568 ("use-matrix-child-files", "useMatrixChild", None),
569 ("matrix-child-combination-filter", "combinationFilter", ""),
570 ("only-exact-matrix-child-runs", "onlyExactRuns", False),
571 ]
572 convert_mapping_to_xml(
573 params,
574 project_def,
575 use_matrix_child_files_mapping,
576 fail_required=True,
577 )
578 elif param_type == "current-parameters" and param_value:
579 XML.SubElement(tconfigs, pt_prefix + "CurrentBuildParameters")
580 elif param_type == "node-parameters" and param_value:
581 XML.SubElement(tconfigs, pt_prefix + "NodeParameters")
582 elif param_type == "svn-revision" and param_value:
583 param = XML.SubElement(
584 tconfigs, pt_prefix + "SubversionRevisionBuildParameters"
585 )
586 XML.SubElement(param, "includeUpstreamParameters").text = str(
587 project_def.get("include-upstream", False)
588 ).lower()
589 elif param_type == "restrict-matrix-project" and param_value:
590 subset = XML.SubElement(
591 tconfigs, pt_prefix + "matrix.MatrixSubsetBuildParameters"
592 )
593 XML.SubElement(subset, "filter").text = project_def[
594 "restrict-matrix-project"
595 ]
596 elif param_type == "node-label-name" or param_type == "node-label":
597 tag_name = (
598 "org.jvnet.jenkins.plugins.nodelabelparameter."
599 "parameterizedtrigger.NodeLabelBuildParameter"
600 )
601 if tconfigs.find(tag_name) is not None:
602 # already processed and can only have one
603 continue
604 params = XML.SubElement(tconfigs, tag_name)
605 name = XML.SubElement(params, "name")
606 if "node-label-name" in project_def:
607 name.text = project_def["node-label-name"]
608 label = XML.SubElement(params, "nodeLabel")
609 if "node-label" in project_def:
610 label.text = project_def["node-label"]
611 elif param_type == "boolean-parameters" and param_value:
612 params = XML.SubElement(tconfigs, pt_prefix + "BooleanParameters")
613 config_tag = XML.SubElement(params, "configs")
614 param_tag_text = pt_prefix + "BooleanParameterConfig"
615 params_list = param_value
616 for name, value in params_list.items():
617 param_tag = XML.SubElement(config_tag, param_tag_text)
618 mapping = [("", "name", name), ("", "value", value or False)]
619 convert_mapping_to_xml(
620 param_tag, project_def, mapping, fail_required=True
621 )
622
623
624 def trigger_threshold(
625 parent_element, element_name, threshold_name, supported_thresholds=None
626 ):
627 """Generate a resultThreshold XML element for build/join triggers"""
628 element = XML.SubElement(parent_element, element_name)
629
630 try:
631 threshold = hudson_model.THRESHOLDS[threshold_name.upper()]
632 except KeyError:
633 if not supported_thresholds:
634 supported_thresholds = hudson_model.THRESHOLDS.keys()
635 raise JenkinsJobsException(
636 "threshold must be one of %s" % ", ".join(supported_thresholds)
637 )
638 XML.SubElement(element, "name").text = threshold["name"]
639 XML.SubElement(element, "ordinal").text = threshold["ordinal"]
640 XML.SubElement(element, "color").text = threshold["color"]
641 return element
642
643
644 def convert_mapping_to_xml(parent, data, mapping, fail_required=True):
645 """Convert mapping to XML
646
647 fail_required affects the last parameter of the mapping field when it's
648 parameter is set to 'None'. When fail_required is True then a 'None' value
649 represents a required configuration so will raise a MissingAttributeError
650 if the user does not provide the configuration.
651
652 If fail_required is False parameter is treated as optional. Logic will skip
653 configuring the XML tag for the parameter. We recommend for new plugins to
654 set fail_required=True and instead of optional parameters provide a default
655 value for all parameters that are not required instead.
656
657 valid_options provides a way to check if the value the user input is from a
658 list of available options. When the user pass a value that is not supported
659 from the list, it raise an InvalidAttributeError.
660
661 valid_dict provides a way to set options through their key and value. If
662 the user input corresponds to a key, the XML tag will use the key's value
663 for its element. When the user pass a value that there are no keys for,
664 it raise an InvalidAttributeError.
665 """
666 for elem in mapping:
667 (optname, xmlname, val) = elem[:3]
668 val = data.get(optname, val)
669
670 valid_options = []
671 valid_dict = {}
672 if len(elem) == 4:
673 if type(elem[3]) is list:
674 valid_options = elem[3]
675 if type(elem[3]) is dict:
676 valid_dict = elem[3]
677
678 # Use fail_required setting to allow support for optional parameters
679 if val is None and fail_required is True:
680 raise MissingAttributeError(optname)
681
682 # if no value is provided then continue else leave it
683 # up to the user if they want to use an empty XML tag
684 if val is None and fail_required is False:
685 continue
686
687 if valid_dict:
688 if val not in valid_dict:
689 raise InvalidAttributeError(optname, val, valid_dict.keys())
690
691 if valid_options:
692 if val not in valid_options:
693 raise InvalidAttributeError(optname, val, valid_options)
694
695 if type(val) is bool:
696 val = str(val).lower()
697
698 if val in valid_dict:
699 XML.SubElement(parent, xmlname).text = str(valid_dict[val])
700 else:
701 XML.SubElement(parent, xmlname).text = str(val)
702
703
704 def jms_messaging_common(parent, subelement, data):
705 """JMS common helper function
706
707 Pass the XML parent and the specific subelement from the builder or the
708 publisher.
709
710 data is passed to mapper helper function to map yaml fields to XML fields
711 """
712 namespace = XML.SubElement(parent, subelement)
713
714 if "override-topic" in data:
715 overrides = XML.SubElement(namespace, "overrides")
716 XML.SubElement(overrides, "topic").text = str(data.get("override-topic", ""))
717
718 mapping = [
719 # option, xml name, default value
720 ("provider-name", "providerName", ""),
721 ("msg-type", "messageType", "CodeQualityChecksDone"),
722 ("msg-props", "messageProperties", ""),
723 ("msg-content", "messageContent", ""),
724 ]
725 convert_mapping_to_xml(namespace, data, mapping, fail_required=True)
726
727
728 def check_mutual_exclusive_data_args(data_arg_position, *args):
729 mutual_exclusive_args = set(args)
730
731 def validator(f):
732 @wraps(f)
733 def wrap(*args):
734 actual_args = set(args[data_arg_position].keys())
735 if len(actual_args & mutual_exclusive_args) > 1:
736 raise JenkinsJobsException(
737 "Args: {} in {} are mutual exclusive. Please define one of it.".format(
738 mutual_exclusive_args, f.__name__
739 )
740 )
741 return f(*args)
742
743 return wrap
744
745 return validator