From 8855eb2523ab04f9c6e34bda192e6dc30064a371 Mon Sep 17 00:00:00 2001 From: Nicolas Glayre Date: Wed, 12 Aug 2015 14:08:38 +0200 Subject: [PATCH] Stash publisher's credentials configuration As per now if you want to configure the stash publisher and give it a username and password, you need to set them in clear text within the yaml file. This pull request gives the possibility to set these credentials within the jenkins_jobs.ini file and therefore makes it possible to not expose them to everyone. Change-Id: I2e5d6caefb87ded2468c7b7e015e20464ffef99e --- doc/source/execution.rst | 14 ++++++++++++++ etc/jenkins_jobs.ini-sample | 4 ++++ jenkins_jobs/modules/helpers.py | 19 +++++++++++++++++++ jenkins_jobs/modules/publishers.py | 10 +++++++--- tests/publishers/fixtures/stash002.conf | 3 +++ tests/publishers/fixtures/stash002.xml | 13 +++++++++++++ tests/publishers/fixtures/stash002.yaml | 6 ++++++ 7 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 tests/publishers/fixtures/stash002.conf create mode 100644 tests/publishers/fixtures/stash002.xml create mode 100644 tests/publishers/fixtures/stash002.yaml diff --git a/doc/source/execution.rst b/doc/source/execution.rst index 034bfad45..161c166a2 100644 --- a/doc/source/execution.rst +++ b/doc/source/execution.rst @@ -96,6 +96,20 @@ hipchat section __ https://www.hipchat.com/docs/apiv2/auth +stash section +^^^^^^^^^^^^^^^^^^^^^^^ + +**username** + This is the stash user name that will be used to connect to stash + when using the stash publisher plugin and not defining it in the + yaml part. + +**password** + This is the related password that will be used with the stash username + when using the stash publisher plugin and not defining it in the + yaml part. + + Running ------- diff --git a/etc/jenkins_jobs.ini-sample b/etc/jenkins_jobs.ini-sample index bb1744742..55515e239 100644 --- a/etc/jenkins_jobs.ini-sample +++ b/etc/jenkins_jobs.ini-sample @@ -16,3 +16,7 @@ query_plugins_info=False [hipchat] authtoken=dummy + +[stash] +username=user +password=pass \ No newline at end of file diff --git a/jenkins_jobs/modules/helpers.py b/jenkins_jobs/modules/helpers.py index a694c86e2..317938ed4 100644 --- a/jenkins_jobs/modules/helpers.py +++ b/jenkins_jobs/modules/helpers.py @@ -13,8 +13,10 @@ # under the License. import xml.etree.ElementTree as XML +import logging from jenkins_jobs.errors import JenkinsJobsException +from six.moves import configparser def build_trends_publisher(plugin_name, xml_element, data): @@ -170,3 +172,20 @@ def findbugs_settings(xml_parent, data): False)).lower() XML.SubElement(xml_parent, 'usePreviousBuildAsReference').text = use_previous_build + + +def get_value_from_yaml_or_config_file(key, section, data, parser): + logger = logging.getLogger(__name__) + result = data.get(key, '') + if result == '': + try: + result = parser.config.get( + section, key + ) + except (configparser.NoSectionError, configparser.NoOptionError, + JenkinsJobsException) as e: + logger.warning("You didn't set a " + key + + " neither in the yaml job definition nor in" + + " the " + section + " section, blank default" + + " value will be applied:\n{0}".format(e)) + return result diff --git a/jenkins_jobs/modules/publishers.py b/jenkins_jobs/modules/publishers.py index 724b5680f..883aa6615 100644 --- a/jenkins_jobs/modules/publishers.py +++ b/jenkins_jobs/modules/publishers.py @@ -32,6 +32,7 @@ from jenkins_jobs.modules import hudson_model from jenkins_jobs.modules.helpers import build_trends_publisher from jenkins_jobs.modules.helpers import config_file_provider_settings from jenkins_jobs.modules.helpers import findbugs_settings +from jenkins_jobs.modules.helpers import get_value_from_yaml_or_config_file from jenkins_jobs.errors import (InvalidAttributeError, JenkinsJobsException, MissingAttributeError) @@ -3463,13 +3464,16 @@ def stash(parser, xml_parent, data): .. literalinclude:: /../../tests/publishers/fixtures/stash001.yaml :language: yaml """ - top = XML.SubElement(xml_parent, 'org.jenkinsci.plugins.stashNotifier.StashNotifier') XML.SubElement(top, 'stashServerBaseUrl').text = data.get('url', '') - XML.SubElement(top, 'stashUserName').text = data.get('username', '') - XML.SubElement(top, 'stashUserPassword').text = data.get('password', '') + XML.SubElement(top, 'stashUserName' + ).text = get_value_from_yaml_or_config_file( + 'username', 'stash', data, parser) + XML.SubElement(top, 'stashUserPassword' + ).text = get_value_from_yaml_or_config_file( + 'password', 'stash', data, parser) XML.SubElement(top, 'ignoreUnverifiedSSLPeer').text = str( data.get('ignore-ssl', False)).lower() XML.SubElement(top, 'commitSha1').text = data.get('commit-sha1', '') diff --git a/tests/publishers/fixtures/stash002.conf b/tests/publishers/fixtures/stash002.conf new file mode 100644 index 000000000..8df10973a --- /dev/null +++ b/tests/publishers/fixtures/stash002.conf @@ -0,0 +1,3 @@ +[stash] +username=user +password=pass diff --git a/tests/publishers/fixtures/stash002.xml b/tests/publishers/fixtures/stash002.xml new file mode 100644 index 000000000..ba3ec97ab --- /dev/null +++ b/tests/publishers/fixtures/stash002.xml @@ -0,0 +1,13 @@ + + + + + https://mystash + user + pass + true + c + true + + + diff --git a/tests/publishers/fixtures/stash002.yaml b/tests/publishers/fixtures/stash002.yaml new file mode 100644 index 000000000..1e0a3da7d --- /dev/null +++ b/tests/publishers/fixtures/stash002.yaml @@ -0,0 +1,6 @@ +publishers: + - stash: + url: "https://mystash" + ignore-ssl: true + commit-sha1: c + include-build-number: true