From 906781a75272ae0bf986f50f6e6fcf168399f198 Mon Sep 17 00:00:00 2001 From: Darragh Bailey Date: Wed, 23 Aug 2017 14:24:15 +0100 Subject: [PATCH] Migrate hipchat conf to new plugin style conf Remove the old default hipchat configuration style in favour of using the new plugin style and provide a mechanism to copy the url value from the jenkins section automatically as part of a simple migration. This will allow removal of querying the jenkins section from existing module code and allow for better isolation. Change-Id: I6889777904ebabc01c044abcd31c9d8a20c255c4 --- jenkins_jobs/config.py | 37 ++++++++++++++++--- jenkins_jobs/modules/hipchat_notif.py | 2 +- tests/hipchat/fixtures/hipchat004.conf | 3 ++ tests/hipchat/fixtures/hipchat005.conf | 1 + .../fixtures/hipchat_rooms-list001.conf | 1 + 5 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 tests/hipchat/fixtures/hipchat004.conf create mode 120000 tests/hipchat/fixtures/hipchat005.conf create mode 120000 tests/hipchat/fixtures/hipchat_rooms-list001.conf diff --git a/jenkins_jobs/config.py b/jenkins_jobs/config.py index cf172815f..9fae91e41 100644 --- a/jenkins_jobs/config.py +++ b/jenkins_jobs/config.py @@ -45,10 +45,6 @@ allow_empty_variables=False [jenkins] url=http://localhost:8080/ query_plugins_info=True - -[hipchat] -authtoken=dummy -send-as=Jenkins """ CONFIG_REQUIRED_MESSAGE = ("A valid configuration file is required. " @@ -135,9 +131,9 @@ class JJBConfig(object): self.jenkins = defaultdict(None) self.builder = defaultdict(None) self.yamlparser = defaultdict(None) - self.hipchat = defaultdict(None) self._setup() + self._handle_deprecated_hipchat_config() def _init_defaults(self): """ Initialize default configuration values using DEFAULT_CONF @@ -165,6 +161,37 @@ class JJBConfig(object): return config_fp + def _handle_deprecated_hipchat_config(self): + config = self.config_parser + + if config.has_section('hipchat'): + if config.has_section('plugin "hipchat"'): + logger.warning( + "Both [hipchat] and [plugin \"hipchat\"] sections " + "defined, legacy [hipchat] section will be ignored." + ) + else: + logger.warning( + "[hipchat] section is deprecated and should be moved to a " + "[plugins \"hipchat\"] section instead as the [hipchat] " + "section will be ignored in the future." + ) + config.add_section('plugin "hipchat"') + for option in config.options("hipchat"): + config.set('plugin "hipchat"', option, + config.get("hipchat", option)) + + config.remove_section("hipchat") + + # remove need to reference jenkins section when using hipchat plugin + # moving to backports configparser would allow use of extended + # interpolation to remove the need for plugins to need information + # directly from the jenkins section within code and allow variables + # in the config file to refer instead. + if (config.has_section('plugin "hipchat"') and + not config.has_option('plugin "hipchat"', 'url')): + config.set('plugin "hipchat"', "url", config.get('jenkins', 'url')) + def _setup(self): config = self.config_parser diff --git a/jenkins_jobs/modules/hipchat_notif.py b/jenkins_jobs/modules/hipchat_notif.py index a77e2bb0c..dde86dfc6 100644 --- a/jenkins_jobs/modules/hipchat_notif.py +++ b/jenkins_jobs/modules/hipchat_notif.py @@ -114,7 +114,7 @@ class HipChat(jenkins_jobs.modules.base.Base): logger.fatal("The configuration file needs a hipchat section" + " containing authtoken:\n{0}".format(e)) sys.exit(1) - self.jenkinsUrl = jjb_config.jenkins['url'] + self.jenkinsUrl = jjb_config.get_plugin_config('hipchat', 'url') self.sendAs = jjb_config.get_plugin_config('hipchat', 'send-as') def gen_xml(self, xml_parent, data): diff --git a/tests/hipchat/fixtures/hipchat004.conf b/tests/hipchat/fixtures/hipchat004.conf new file mode 100644 index 000000000..b41728f7e --- /dev/null +++ b/tests/hipchat/fixtures/hipchat004.conf @@ -0,0 +1,3 @@ +[plugin "hipchat"] +authtoken=dummy +send-as=Jenkins diff --git a/tests/hipchat/fixtures/hipchat005.conf b/tests/hipchat/fixtures/hipchat005.conf new file mode 120000 index 000000000..9cae12f77 --- /dev/null +++ b/tests/hipchat/fixtures/hipchat005.conf @@ -0,0 +1 @@ +hipchat004.conf \ No newline at end of file diff --git a/tests/hipchat/fixtures/hipchat_rooms-list001.conf b/tests/hipchat/fixtures/hipchat_rooms-list001.conf new file mode 120000 index 000000000..9cae12f77 --- /dev/null +++ b/tests/hipchat/fixtures/hipchat_rooms-list001.conf @@ -0,0 +1 @@ +hipchat004.conf \ No newline at end of file