| 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 |
Enable HipChat notifications of build execution. |
| 17 |
|
| 18 |
Supports hipchat plugin versions < 1.9. Will automatically redirect to the |
| 19 |
publishers module for newer versions, but still recommended that you convert |
| 20 |
to the newer module. |
| 21 |
|
| 22 |
:Parameters: |
| 23 |
* **enabled** *(bool)*: general cut off switch. If not explicitly set to |
| 24 |
``true``, no hipchat parameters are written to XML. For Jenkins HipChat |
| 25 |
plugin of version prior to 0.1.5, also enables all build results to be |
| 26 |
reported in HipChat room. For later plugin versions, explicit notify-* |
| 27 |
setting is required (see below). |
| 28 |
* **room** *(str)*: name of HipChat room to post messages to (default '') |
| 29 |
|
| 30 |
.. deprecated:: 1.2.0 Please use 'rooms'. |
| 31 |
|
| 32 |
* **rooms** *(list)*: list of HipChat rooms to post messages to |
| 33 |
(default empty) |
| 34 |
* **start-notify** *(bool)*: post messages about build start event |
| 35 |
|
| 36 |
.. deprecated:: 1.2.0 use notify-start parameter instead |
| 37 |
|
| 38 |
* **notify-start** *(bool)*: post messages about build start event |
| 39 |
(default false) |
| 40 |
* **notify-success** *(bool)*: post messages about successful build event |
| 41 |
(Jenkins HipChat plugin >= 0.1.5) (default false) |
| 42 |
* **notify-aborted** *(bool)*: post messages about aborted build event |
| 43 |
(Jenkins HipChat plugin >= 0.1.5) (default false) |
| 44 |
* **notify-not-built** *(bool)*: post messages about build set to NOT_BUILT |
| 45 |
status (Jenkins HipChat plugin >= 0.1.5). This status code is used in a |
| 46 |
multi-stage build (like maven2) where a problem in earlier stage prevented |
| 47 |
later stages from building. (default false) |
| 48 |
* **notify-unstable** *(bool)*: post messages about unstable build event |
| 49 |
(Jenkins HipChat plugin >= 0.1.5) (default false) |
| 50 |
* **notify-failure** *(bool)*: post messages about build failure event |
| 51 |
(Jenkins HipChat plugin >= 0.1.5) (default false) |
| 52 |
* **notify-back-to-normal** *(bool)*: post messages about build being back to |
| 53 |
normal after being unstable or failed (Jenkins HipChat plugin >= 0.1.5) |
| 54 |
(default false) |
| 55 |
|
| 56 |
|
| 57 |
Example: |
| 58 |
|
| 59 |
.. literalinclude:: /../../tests/hipchat/fixtures/hipchat001.yaml |
| 60 |
:language: yaml |
| 61 |
|
| 62 |
""" |
| 63 |
|
| 64 |
# Enabling hipchat notifications on a job requires specifying the hipchat |
| 65 |
# config in job properties, and adding the hipchat notifier to the job's |
| 66 |
# publishers list. |
| 67 |
# The publisher configuration contains extra details not specified per job: |
| 68 |
# - the hipchat authorisation token. |
| 69 |
# - the jenkins server url. |
| 70 |
# - a default room name/id. |
| 71 |
# This complicates matters somewhat since the sensible place to store these |
| 72 |
# details is in the global config file. |
| 73 |
# The global config object is therefore passed down to the registry object, |
| 74 |
# and this object is passed to the HipChat() class initialiser. |
| 75 |
|
| 76 |
import logging |
| 77 |
import sys |
| 78 |
import xml.etree.ElementTree as XML |
| 79 |
|
| 80 |
from six.moves import configparser |
| 81 |
|
| 82 |
import jenkins_jobs.errors |
| 83 |
import jenkins_jobs.modules.base |
| 84 |
import jenkins_jobs.modules.helpers as helpers |
| 85 |
|
| 86 |
|
| 87 |
logger = logging.getLogger(__name__) |
| 88 |
|
| 89 |
|
| 90 |
class HipChat(jenkins_jobs.modules.base.Base): |
| 91 |
sequence = 80 |
| 92 |
|
| 93 |
def __init__(self, registry): |
| 94 |
self.authToken = None |
| 95 |
self.jenkinsUrl = None |
| 96 |
self.registry = registry |
| 97 |
|
| 98 |
def _load_global_data(self): |
| 99 |
"""Load data from the global config object. |
| 100 |
|
| 101 |
This is done lazily to avoid looking up the '[hipchat]' section |
| 102 |
unless actually required. |
| 103 |
""" |
| 104 |
jjb_config = self.registry.jjb_config |
| 105 |
if not self.authToken: |
| 106 |
try: |
| 107 |
self.authToken = jjb_config.get_plugin_config("hipchat", "authtoken") |
| 108 |
# Require that the authtoken is non-null |
| 109 |
if self.authToken == "": |
| 110 |
raise jenkins_jobs.errors.JenkinsJobsException( |
| 111 |
"Hipchat authtoken must not be a blank string" |
| 112 |
) |
| 113 |
except ( |
| 114 |
configparser.NoSectionError, |
| 115 |
jenkins_jobs.errors.JenkinsJobsException, |
| 116 |
) as e: |
| 117 |
logger.fatal( |
| 118 |
"The configuration file needs a hipchat section" |
| 119 |
+ " containing authtoken:\n{0}".format(e) |
| 120 |
) |
| 121 |
sys.exit(1) |
| 122 |
self.jenkinsUrl = jjb_config.get_plugin_config("hipchat", "url") |
| 123 |
self.sendAs = jjb_config.get_plugin_config("hipchat", "send-as") |
| 124 |
|
| 125 |
def gen_xml(self, xml_parent, data): |
| 126 |
hipchat = data.get("hipchat") |
| 127 |
if not hipchat or not hipchat.get("enabled", True): |
| 128 |
return |
| 129 |
self._load_global_data() |
| 130 |
|
| 131 |
# convert for compatibility before dispatch |
| 132 |
if "room" in hipchat: |
| 133 |
if "rooms" in hipchat: |
| 134 |
logger.warning("Ignoring deprecated 'room' as 'rooms' also " "defined.") |
| 135 |
else: |
| 136 |
logger.warning("'room' is deprecated, please use 'rooms'") |
| 137 |
hipchat["rooms"] = [hipchat["room"]] |
| 138 |
|
| 139 |
plugin_ver = self.registry.get_plugin_version("Jenkins HipChat Plugin") |
| 140 |
|
| 141 |
if plugin_ver >= "0.1.9": |
| 142 |
publishers = xml_parent.find("publishers") |
| 143 |
if publishers is None: |
| 144 |
publishers = XML.SubElement(xml_parent, "publishers") |
| 145 |
|
| 146 |
logger.warning( |
| 147 |
"'hipchat' module supports the old plugin versions <1.9, " |
| 148 |
"newer versions are supported via the 'publishers' module. " |
| 149 |
"Please upgrade you job definition" |
| 150 |
) |
| 151 |
component = {"hipchat": hipchat} |
| 152 |
return self.registry.dispatch("publisher", publishers, component) |
| 153 |
else: |
| 154 |
properties = xml_parent.find("properties") |
| 155 |
if properties is None: |
| 156 |
properties = XML.SubElement(xml_parent, "properties") |
| 157 |
pdefhip = XML.SubElement( |
| 158 |
properties, |
| 159 |
"jenkins.plugins.hipchat." "HipChatNotifier_-HipChatJobProperty", |
| 160 |
) |
| 161 |
|
| 162 |
room = XML.SubElement(pdefhip, "room") |
| 163 |
if "rooms" in hipchat: |
| 164 |
room.text = ",".join(hipchat["rooms"]) |
| 165 |
|
| 166 |
# Handle backwards compatibility 'start-notify' but all add an element |
| 167 |
# of standardization with notify-* |
| 168 |
if hipchat.get("start-notify"): |
| 169 |
logger.warning("'start-notify' is deprecated, please use " "'notify-start'") |
| 170 |
XML.SubElement(pdefhip, "startNotification").text = str( |
| 171 |
hipchat.get("notify-start", hipchat.get("start-notify", False)) |
| 172 |
).lower() |
| 173 |
|
| 174 |
if plugin_ver >= "0.1.5": |
| 175 |
mapping = [ |
| 176 |
("notify-success", "notifySuccess", False), |
| 177 |
("notify-aborted", "notifyAborted", False), |
| 178 |
("notify-not-built", "notifyNotBuilt", False), |
| 179 |
("notify-unstable", "notifyUnstable", False), |
| 180 |
("notify-failure", "notifyFailure", False), |
| 181 |
("notify-back-to-normal", "notifyBackToNormal", False), |
| 182 |
] |
| 183 |
helpers.convert_mapping_to_xml( |
| 184 |
pdefhip, hipchat, mapping, fail_required=True |
| 185 |
) |
| 186 |
|
| 187 |
publishers = xml_parent.find("publishers") |
| 188 |
if publishers is None: |
| 189 |
publishers = XML.SubElement(xml_parent, "publishers") |
| 190 |
hippub = XML.SubElement(publishers, "jenkins.plugins.hipchat.HipChatNotifier") |
| 191 |
|
| 192 |
if plugin_ver >= "0.1.8": |
| 193 |
XML.SubElement(hippub, "buildServerUrl").text = self.jenkinsUrl |
| 194 |
XML.SubElement(hippub, "sendAs").text = self.sendAs |
| 195 |
else: |
| 196 |
XML.SubElement(hippub, "jenkinsUrl").text = self.jenkinsUrl |
| 197 |
|
| 198 |
XML.SubElement(hippub, "authToken").text = self.authToken |
| 199 |
# The room specified here is the default room. The default is |
| 200 |
# redundant in this case since a room must be specified. Leave empty. |
| 201 |
XML.SubElement(hippub, "room").text = "" |