diff --git a/doc/source/configuration.rst b/doc/source/configuration.rst index 0054d2c27..984dda94d 100644 --- a/doc/source/configuration.rst +++ b/doc/source/configuration.rst @@ -302,6 +302,7 @@ The bulk of the job definitions come from the following modules. general builders hipchat + metadata notifications parameters properties diff --git a/doc/source/metadata.rst b/doc/source/metadata.rst new file mode 100644 index 000000000..0ff324d66 --- /dev/null +++ b/doc/source/metadata.rst @@ -0,0 +1,7 @@ +.. _metadata: + +Metadata +========== + +.. automodule:: metadata + :members: diff --git a/jenkins_jobs/modules/metadata.py b/jenkins_jobs/modules/metadata.py new file mode 100644 index 000000000..ee2ae1b7a --- /dev/null +++ b/jenkins_jobs/modules/metadata.py @@ -0,0 +1,142 @@ +# 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 `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(parser, 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(parser, 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(parser, xml_parent, data, + 'metadata-string') + value = data.get('value', '') + XML.SubElement(pdef, 'value').text = value + + +def number_metadata(parser, 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(parser, xml_parent, data, + 'metadata-number') + value = data.get('value', '') + XML.SubElement(pdef, 'value').text = value + + +def date_metadata(parser, 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(parser, 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, parser, 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', + parser, pdefs, mdata) diff --git a/setup.py b/setup.py index 8cf782a53..d9969cd30 100644 --- a/setup.py +++ b/setup.py @@ -96,6 +96,11 @@ setuptools.setup( 'validating_string_param', 'svn-tags=jenkins_jobs.modules.parameters:svn_tags_param', ], + 'jenkins_jobs.metadata': [ + 'string=jenkins_jobs.modules.metadata:string_metadata', + 'number=jenkins_jobs.modules.metadata:number_metadata', + 'date=jenkins_jobs.modules.metadata:date_metadata', + ], 'jenkins_jobs.notifications': [ 'http=jenkins_jobs.modules.notifications:http_endpoint', ], @@ -170,6 +175,7 @@ setuptools.setup( 'builders=jenkins_jobs.modules.builders:Builders', 'properties=jenkins_jobs.modules.properties:Properties', 'parameters=jenkins_jobs.modules.parameters:Parameters', + 'metadata=jenkins_jobs.modules.metadata:Metadata', 'notifications=jenkins_jobs.modules.notifications:Notifications', 'publishers=jenkins_jobs.modules.publishers:Publishers', 'reporters=jenkins_jobs.modules.reporters:Reporters',