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
This commit is contained in:
Nicolas Glayre 2015-08-12 14:08:38 +02:00
parent 25d81389e1
commit 8855eb2523
7 changed files with 66 additions and 3 deletions

View File

@ -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
-------

View File

@ -16,3 +16,7 @@ query_plugins_info=False
[hipchat]
authtoken=dummy
[stash]
username=user
password=pass

View File

@ -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

View File

@ -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', '')

View File

@ -0,0 +1,3 @@
[stash]
username=user
password=pass

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<project>
<publishers>
<org.jenkinsci.plugins.stashNotifier.StashNotifier>
<stashServerBaseUrl>https://mystash</stashServerBaseUrl>
<stashUserName>user</stashUserName>
<stashUserPassword>pass</stashUserPassword>
<ignoreUnverifiedSSLPeer>true</ignoreUnverifiedSSLPeer>
<commitSha1>c</commitSha1>
<includeBuildNumberInKey>true</includeBuildNumberInKey>
</org.jenkinsci.plugins.stashNotifier.StashNotifier>
</publishers>
</project>

View File

@ -0,0 +1,6 @@
publishers:
- stash:
url: "https://mystash"
ignore-ssl: true
commit-sha1: c
include-build-number: true