Adding hipchat notification capability (needs work)

Change-Id: I3017658336b949c0fda7c82945e7014dbcf6e152
This commit is contained in:
desbonne 2012-09-11 10:16:08 +01:00 committed by Manuel Desbonnet
parent e7acca6504
commit 0d6369d4a0
6 changed files with 123 additions and 1 deletions

View File

@ -33,7 +33,10 @@ def main():
conf = options.conf
else:
conf = '/etc/jenkins_jobs/jenkins_jobs.ini'
# Kludge alert - making the config object global so that it can
# be accessed from elsewhere.
global config
if not options.command == 'test':
logger.debug("Reading config from {0}".format(conf))
conffp = open(conf, 'r')

7
jenkins_jobs/errors.py Normal file
View File

@ -0,0 +1,7 @@
"""Exception classes for jenkins_jobs errors"""
class JenkinsJobsException(Exception):
pass
class YAMLFormatError(JenkinsJobsException):
pass

View File

@ -0,0 +1,96 @@
# 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.
# Jenkins Job module for HipChat notifications
# - job:
# hipchat:
# enabled: <true|false>
# room: <hipchat room name>
# start-notify: <true|false>
# Enabling hipchat notifications on a job requires specifying the hipchat
# config in job properties, and adding the hipchat notifyier to the job's
# publishers list.
# The publisher configuration requires the hipchat authorisation token as well
# as the room id (and the jenkins server url for some reason).
# This complicates matters quite a bit:
# - The HipChat class needs access to the global config object (where the
# hipchat authtoken & jenkins url are stored.
# - A lookup needs to be done to the hipchat service to determine the room id
# for the specified room name.
#
# The global config access is especially kludgy, and should be given proper
# thought and reworked.
import xml.etree.ElementTree as XML
import jenkins_jobs.modules.base
import jenkins_jobs.errors
import logging
import hipchat
logger = logging.getLogger(__name__)
class HipChat(jenkins_jobs.modules.base.Base):
sequence = 15
def __init__(self, registry):
self.client = None
def _connect(self):
# Kludge alert - delving directly into __main__ namespace to
# get at the config object.
if(not self.client):
import __main__
logger.debug("Config obj: {0}".format(__main__.config))
self.authToken = __main__.config.get('hipchat', 'authtoken')
# Not sure why, but the jenkins server url is included in the
# hipchat publisher section.
self.jenkinsUrl = __main__.config.get('jenkins', 'url')
import hipchat
logger.debug("Connecting to HipChat service (token={0})".format(
self.authToken))
self.client = hipchat.HipChat(token=self.authToken)
self.rooms = dict( [ ( m['name'], m['room_id'] )
for m in self.client.list_rooms()['rooms']] )
def handle_data(self, parser):
changed = False
jobs = (parser.data.get('job', {}).values() +
parser.data.get('job-template', {}).values())
for job in jobs:
hc_props = job.get('hipchat')
if(not hc_props or not hc_props.get('enabled', True)):
continue
if('room' not in hc_props):
raise jenkins_jobs.errors.YAMLFormatError (
"Job '{0}' missing hipchat 'room' specifier".format(
job['name']))
self._connect()
if(hc_props['room'] not in self.rooms):
raise jenkins_jobs.errors.JenkinsJobsException(
"Cannot find hipchat room '{0}' (job {1})".format(
hc_props['room'], job['name']))
hc_props['room_id'] = str(self.rooms[hc_props['room']])
logger.debug("HipChat room '{0}' => {1}".format(
hc_props['room'], hc_props['room_id']))
hc_props['authtoken'] = self.authToken
hc_props['jenkins_url'] = self.jenkinsUrl
hc_props = { 'hipchat': hc_props }
job.setdefault('properties', []).append(hc_props)
job.setdefault('publishers', []).append(hc_props)
# Delete the toplevel 'hipchat' entry to prevent reprocessing
del job['hipchat']
changed = True
return changed

View File

@ -73,6 +73,12 @@ def authenticated_build(parser, xml_parent, data):
XML.SubElement(security, 'permission').text = \
'hudson.model.Item.Build:authenticated'
def hipchat(parser, xml_parent, data):
pdefhip = XML.SubElement(xml_parent,
'jenkins.plugins.hipchat.HipChatNotifier_-HipChatJobProperty')
XML.SubElement(pdefhip, 'room').text = data['room']
XML.SubElement(pdefhip, 'startNotification').text = str(
data.get('start-notify', 'false')).lower()
def base_param(parser, xml_parent, data, do_default, ptype):
pdef = XML.SubElement(xml_parent, ptype)

View File

@ -261,6 +261,13 @@ def pipeline(parser, xml_parent, data):
'au.com.centrumsystems.hudson.plugin.buildpipeline.trigger.BuildPipelineTrigger')
XML.SubElement(pippub, 'downstreamProjectNames').text = data
def hipchat(parser, xml_parent, data):
hippub = XML.SubElement(xml_parent,
'jenkins.plugins.hipchat.HipChatNotifier')
XML.SubElement(hippub, 'jenkinsUrl').text = data['jenkins_url']
XML.SubElement(hippub, 'authToken').text = data['authtoken']
XML.SubElement(hippub, 'room').text = data['room_id']
class Publishers(jenkins_jobs.modules.base.Base):
sequence = 70

View File

@ -42,6 +42,7 @@ setup(name='jenkins_job_builder',
'inject=jenkins_jobs.modules.properties:inject',
'authenticated-build=jenkins_jobs.modules.properties:'
'authenticated_build',
'hipchat=jenkins_jobs.modules.properties:hipchat',
],
'jenkins_jobs.parameters': [
'string=jenkins_jobs.modules.properties:string_param',
@ -63,6 +64,7 @@ setup(name='jenkins_job_builder',
'violations=jenkins_jobs.modules.publishers:violations',
'scp=jenkins_jobs.modules.publishers:scp',
'pipeline=jenkins_jobs.modules.publishers:pipeline',
'hipchat=jenkins_jobs.modules.publishers:hipchat',
],
'jenkins_jobs.scm': [
'git=jenkins_jobs.modules.scm:git',
@ -87,6 +89,7 @@ setup(name='jenkins_job_builder',
'triggers=jenkins_jobs.modules.triggers:Triggers',
'wrappers=jenkins_jobs.modules.wrappers:Wrappers',
'zuul=jenkins_jobs.modules.zuul:Zuul',
'hipchat=jenkins_jobs.modules.hipchat_notif:HipChat',
]
}