From b093fee5017ca4b71b0856ce88f4e9f3d28f7e3d Mon Sep 17 00:00:00 2001 From: Thanh Ha Date: Fri, 19 Aug 2016 11:14:34 -0400 Subject: [PATCH] Add convenience function for plugin namespace Plugins can use get_plugin_config() which will search in a plugin namespace. For example: [plugin "hipchat"] authtoken = 123token - Updated hipchat plugin to use get_plugin_config() - Updated stash plugin to use get_plugin_config() - Backwards compatibility is kept by falling back to the old configuration setting if the new one is not found. - Warning is displayed if the old configuration method is used. Change-Id: I7cff063e2d179a5d9a3f221c85de6864382bc477 Signed-off-by: Thanh Ha --- etc/jenkins_jobs.ini-sample | 6 +++--- jenkins_jobs/config.py | 15 +++++++++++++++ jenkins_jobs/modules/helpers.py | 2 +- jenkins_jobs/modules/hipchat_notif.py | 6 +++--- tests/hipchat/fixtures/hipchat003.conf | 2 +- 5 files changed, 23 insertions(+), 8 deletions(-) diff --git a/etc/jenkins_jobs.ini-sample b/etc/jenkins_jobs.ini-sample index 55515e239..5fd655f52 100644 --- a/etc/jenkins_jobs.ini-sample +++ b/etc/jenkins_jobs.ini-sample @@ -14,9 +14,9 @@ query_plugins_info=False ##### This is deprecated, use job_builder section instead #ignore_cache=True -[hipchat] +[plugin "hipchat"] authtoken=dummy -[stash] +[plugin "stash"] username=user -password=pass \ No newline at end of file +password=pass diff --git a/jenkins_jobs/config.py b/jenkins_jobs/config.py index a22c48af9..19d13b82d 100644 --- a/jenkins_jobs/config.py +++ b/jenkins_jobs/config.py @@ -310,3 +310,18 @@ class JJBConfig(object): " the " + section + " section, blank default" + " value will be applied:\n{0}".format(e)) return result + + def get_plugin_config(self, plugin, key): + value = self.get_module_config('plugin "{}"'.format(plugin), key) + + # Backwards compatibility for users who have not switched to the new + # plugin configuration format in their config. This code should be + # removed in future versions of JJB after 2.0. + if not value: + value = self.get_module_config(plugin, key) + logger.warning( + "Defining plugin configuration using [" + plugin + "] is" + " deprecated. The recommended way to define plugins now is by" + " configuring [plugin \"" + plugin + "\"]") + + return value diff --git a/jenkins_jobs/modules/helpers.py b/jenkins_jobs/modules/helpers.py index ff824555a..bf0bb9f43 100644 --- a/jenkins_jobs/modules/helpers.py +++ b/jenkins_jobs/modules/helpers.py @@ -250,7 +250,7 @@ def findbugs_settings(xml_parent, data): def get_value_from_yaml_or_config_file(key, section, data, jjb_config): result = data.get(key, '') if result == '': - result = jjb_config.get_module_config(section, key) + result = jjb_config.get_plugin_config(section, key) return result diff --git a/jenkins_jobs/modules/hipchat_notif.py b/jenkins_jobs/modules/hipchat_notif.py index a1dbeb423..a77e2bb0c 100644 --- a/jenkins_jobs/modules/hipchat_notif.py +++ b/jenkins_jobs/modules/hipchat_notif.py @@ -101,9 +101,9 @@ class HipChat(jenkins_jobs.modules.base.Base): unless actually required. """ jjb_config = self.registry.jjb_config - if(not self.authToken): + if not self.authToken: try: - self.authToken = jjb_config.get_module_config('hipchat', + self.authToken = jjb_config.get_plugin_config('hipchat', 'authtoken') # Require that the authtoken is non-null if self.authToken == '': @@ -115,7 +115,7 @@ class HipChat(jenkins_jobs.modules.base.Base): " containing authtoken:\n{0}".format(e)) sys.exit(1) self.jenkinsUrl = jjb_config.jenkins['url'] - self.sendAs = jjb_config.get_module_config('hipchat', 'send-as') + self.sendAs = jjb_config.get_plugin_config('hipchat', 'send-as') def gen_xml(self, xml_parent, data): hipchat = data.get('hipchat') diff --git a/tests/hipchat/fixtures/hipchat003.conf b/tests/hipchat/fixtures/hipchat003.conf index 93ee35f62..2c61787f0 100644 --- a/tests/hipchat/fixtures/hipchat003.conf +++ b/tests/hipchat/fixtures/hipchat003.conf @@ -1,4 +1,4 @@ -[hipchat] +[plugin "hipchat"] authtoken=blue send-as=Jenkins [jenkins]