Debian Codegraph @

jenkins_jobs/modules/metadata.py

raw
1 # Copyright 2012 Hewlett-Packard Development Company, L.P.
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
16 """
17 The Metadata plugin module enables the ability to add metadata to the projects
18 that can be exposed to job environment.
19
20 Requires the Jenkins :jenkins-plugins:`Metadata Plugin <metadata>`.
21
22 **Component**: metadata
23 :Macro: metadata
24 :Entry Point: jenkins_jobs.metadata
25
26 Example::
27
28 metadata:
29 - string:
30 name: FOO
31 value: bar
32 expose-to-env: true
33 """
34
35 import xml.etree.ElementTree as XML
36
37 import jenkins_jobs.modules.base
38
39
40 def base_metadata(registry, xml_parent, data, mtype):
41 pdef = XML.SubElement(xml_parent, mtype)
42 XML.SubElement(pdef, "name").text = data["name"]
43 XML.SubElement(pdef, "generated").text = "false"
44 XML.SubElement(
45 pdef, "parent", attrib={"class": "job-metadata", "reference": "../../.."}
46 )
47
48 exposed_to_env = XML.SubElement(pdef, "exposedToEnvironment")
49 exposed_to_env.text = str(data.get("expose-to-env", False)).lower()
50 return pdef
51
52
53 def string_metadata(registry, xml_parent, data):
54 """yaml: string
55 A string metadata.
56
57 :arg str name: the name of the metadata
58 :arg str value: the value of the metadata
59 :arg bool expose-to-env: expose to environment (optional)
60
61 Example::
62
63 metadata:
64 - string:
65 name: FOO
66 value: bar
67 expose-to-env: true
68 """
69 pdef = base_metadata(registry, xml_parent, data, "metadata-string")
70 value = data.get("value", "")
71 XML.SubElement(pdef, "value").text = value
72
73
74 def number_metadata(registry, xml_parent, data):
75 """yaml: number
76 A number metadata.
77
78 :arg str name: the name of the metadata
79 :arg str value: the value of the metadata
80 :arg bool expose-to-env: expose to environment (optional)
81
82 Example::
83
84 metadata:
85 - number:
86 name: FOO
87 value: 1
88 expose-to-env: true
89 """
90 pdef = base_metadata(registry, xml_parent, data, "metadata-number")
91 value = data.get("value", "")
92 XML.SubElement(pdef, "value").text = value
93
94
95 def date_metadata(registry, xml_parent, data):
96 """yaml: date
97 A date metadata
98
99 :arg str name: the name of the metadata
100 :arg str time: time value in millisec since 1970-01-01 00:00:00 UTC
101 :arg str timezone: time zone of the metadata
102 :arg bool expose-to-env: expose to environment (optional)
103
104 Example::
105
106 metadata:
107 - date:
108 name: FOO
109 value: 1371708900268
110 timezone: Australia/Melbourne
111 expose-to-env: true
112 """
113 pdef = base_metadata(registry, xml_parent, data, "metadata-date")
114 # TODO: convert time from any reasonable format into epoch
115 mval = XML.SubElement(pdef, "value")
116 XML.SubElement(mval, "time").text = data["time"]
117 XML.SubElement(mval, "timezone").text = data["timezone"]
118 XML.SubElement(pdef, "checked").text = "true"
119
120
121 class Metadata(jenkins_jobs.modules.base.Base):
122 sequence = 21
123
124 component_type = "metadata"
125 component_list_type = "metadata"
126
127 def gen_xml(self, xml_parent, data):
128 properties = xml_parent.find("properties")
129 if properties is None:
130 properties = XML.SubElement(xml_parent, "properties")
131
132 metadata = data.get("metadata", [])
133 if metadata:
134 pdefp = XML.SubElement(properties, "job-metadata", plugin="[email protected]")
135 pdefs = XML.SubElement(pdefp, "values")
136 for mdata in metadata:
137 self.registry.dispatch("metadata", pdefs, mdata)