url-proof scripts passed to run_script

Groovy scripts may contain characters that need to be escaped prior
to being passed to Jenkins' REST API; for example "&&" in condition
testing. Not doing so will result in a script execution failure on
the server.

Change-Id: Ied6c2a48392cadbadb84865dad47ceed99e694a1
changes/79/407479/4
mhuin 6 years ago committed by Matthieu Huin
parent 234aa6550c
commit cf4f3d17ed
  1. 2
      jenkins/__init__.py
  2. 11
      tests/test_script.py

@ -1064,7 +1064,7 @@ class Jenkins(object):
Plugin:maven-plugin, Plugin:pam-auth]'
'''
return self.jenkins_open(Request(self._build_url(SCRIPT_TEXT),
"script=".encode('utf-8') + script.encode('utf-8')))
"script=".encode('utf-8') + quote(script).encode('utf-8')))
def install_plugin(self, name, include_dependencies=True):
'''Install a plugin and its dependencies from the Jenkins public

@ -1,4 +1,5 @@
from mock import patch
from six.moves.urllib.parse import quote
import jenkins
from tests.base import JenkinsTestBase
@ -15,6 +16,16 @@ class JenkinsScriptTest(JenkinsTestBase):
self.make_url('scriptText'))
self._check_requests(jenkins_mock.call_args_list)
@patch.object(jenkins.Jenkins, 'jenkins_open')
def test_run_script_urlproof(self, jenkins_mock):
self.j.run_script(u'if (a == b && c ==d) { println(\"Yes\")}')
self.assertEqual(
jenkins_mock.call_args[0][0].get_full_url(),
self.make_url('scriptText'))
self.assertIn(quote('&&'), jenkins_mock.call_args[0][0].data.decode('utf8'))
self._check_requests(jenkins_mock.call_args_list)
@patch.object(jenkins.Jenkins, 'jenkins_open')
def test_install_plugin(self, jenkins_mock):
'''Installation of plugins is done with the run_script method

Loading…
Cancel
Save