jenkins-job-builder/jenkins_jobs/modules/metadata.py
Wayne Warren ae1fb60f16
Disentangle YamlParser and ModuleRegistry classes
Create the ModuleRegistry anywhere other than inside the YamlParser
class. This will make it slightly easier to factor a XmlGenerator out
of YamlParser, but I also want to work toward eliminating the circular
references between YamlParser and ModuleRegistry which have been
making it difficult to understand overall program flow.

This commit also replaces all YamlParser instances being passed to
Jenkins job config generating functions with a ModuleRegistry. Mostly
it seems like the parser was only needed to call the ModuleRegistry's
'dispatch' method which to be honest I don't fully understand. This is
where the circular references mentioned in previously come in...it
seems like the "dispatch" function needs access to the (mostly) raw
data contained by the parser, so it took that as a parameter.

The need for the YamlParser's job data can be satisfied by assigning
it to a property on the ModuleRegistry object before Yaml expansion or
XML generation begins; by doing this, we allow the ModuleRegistry to
avoid referencing the parser.

Change-Id: I4b571299b81e708540392ad963163fe092acf1d9
2016-08-18 22:42:24 -04:00

140 lines
4.1 KiB
Python

# Copyright 2012 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""
The Metadata plugin module enables the ability to add metadata to the projects
that can be exposed to job environment.
Requires the Jenkins :jenkins-wiki:`Metadata Plugin <Metadata+plugin>`.
**Component**: metadata
:Macro: metadata
:Entry Point: jenkins_jobs.metadata
Example::
metadata:
- string:
name: FOO
value: bar
expose-to-env: true
"""
import xml.etree.ElementTree as XML
import jenkins_jobs.modules.base
def base_metadata(registry, xml_parent, data, mtype):
pdef = XML.SubElement(xml_parent, mtype)
XML.SubElement(pdef, 'name').text = data['name']
XML.SubElement(pdef, 'generated').text = 'false'
XML.SubElement(pdef, 'parent', attrib={"class": "job-metadata",
"reference": "../../.."})
exposed_to_env = XML.SubElement(pdef, 'exposedToEnvironment')
exposed_to_env.text = str(data.get('expose-to-env', False)).lower()
return pdef
def string_metadata(registry, xml_parent, data):
"""yaml: string
A string metadata.
:arg str name: the name of the metadata
:arg str value: the value of the metadata
:arg bool expose-to-env: expose to environment (optional)
Example::
metadata:
- string:
name: FOO
value: bar
expose-to-env: true
"""
pdef = base_metadata(registry, xml_parent, data,
'metadata-string')
value = data.get('value', '')
XML.SubElement(pdef, 'value').text = value
def number_metadata(registry, xml_parent, data):
"""yaml: number
A number metadata.
:arg str name: the name of the metadata
:arg str value: the value of the metadata
:arg bool expose-to-env: expose to environment (optional)
Example::
metadata:
- number:
name: FOO
value: 1
expose-to-env: true
"""
pdef = base_metadata(registry, xml_parent, data,
'metadata-number')
value = data.get('value', '')
XML.SubElement(pdef, 'value').text = value
def date_metadata(registry, xml_parent, data):
"""yaml: date
A date metadata
:arg str name: the name of the metadata
:arg str time: time value in millisec since 1970-01-01 00:00:00 UTC
:arg str timezone: time zone of the metadata
:arg bool expose-to-env: expose to environment (optional)
Example::
metadata:
- date:
name: FOO
value: 1371708900268
timezone: Australia/Melbourne
expose-to-env: true
"""
pdef = base_metadata(registry, xml_parent, data,
'metadata-date')
# TODO: convert time from any reasonable format into epoch
mval = XML.SubElement(pdef, 'value')
XML.SubElement(mval, 'time').text = data['time']
XML.SubElement(mval, 'timezone').text = data['timezone']
XML.SubElement(pdef, 'checked').text = 'true'
class Metadata(jenkins_jobs.modules.base.Base):
sequence = 21
component_type = 'metadata'
component_list_type = 'metadata'
def gen_xml(self, xml_parent, data):
properties = xml_parent.find('properties')
if properties is None:
properties = XML.SubElement(xml_parent, 'properties')
metadata = data.get('metadata', [])
if metadata:
pdefp = XML.SubElement(properties,
'job-metadata', plugin="metadata@1.0b")
pdefs = XML.SubElement(pdefp, 'values')
for mdata in metadata:
self.registry.dispatch('metadata', pdefs, mdata)