Metadata plugin module - largely based on Parameters module.

I implemented this plugin as I use Jenkins Metadata plugin heavily in all my
projects instead of the default Parameters plugin and think that everyone else
would benefit from this addition to JJB as well.
Current implementation handles dates in epoch format only and does not handle
'node' element - so no metatree creation is possible.

Change-Id: I1e67224df9339f1319a371d049585c8343301f2e
Reviewed-on: https://review.openstack.org/33773
Reviewed-by: James E. Blair <corvus@inaugust.com>
Approved: Clark Boylan <clark.boylan@gmail.com>
Reviewed-by: Clark Boylan <clark.boylan@gmail.com>
Tested-by: Jenkins
This commit is contained in:
Roman Revyakin 2013-06-20 15:44:57 +10:00 committed by Jenkins
parent 624fd659cc
commit 51b736b2dd
4 changed files with 156 additions and 0 deletions

View File

@ -302,6 +302,7 @@ The bulk of the job definitions come from the following modules.
general
builders
hipchat
metadata
notifications
parameters
properties

7
doc/source/metadata.rst Normal file
View File

@ -0,0 +1,7 @@
.. _metadata:
Metadata
==========
.. automodule:: metadata
:members:

View File

@ -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.
<https://wiki.jenkins-ci.org/display/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)

View File

@ -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',