From 7ffb27c7d526e7bcb77cb2f93bb9de6e95f418f5 Mon Sep 17 00:00:00 2001 From: Thanh Ha Date: Mon, 11 Jun 2018 11:13:44 -0400 Subject: [PATCH] Fix run_scripts() API Resolve regression caused by I5369a0d35be4bf8b3b197a51e60aba21b5742cc7 preventing run_scripts() from successfully running against the remote Jenkins instance. Switching data to a dictionary and passing the script string without quote() appears to allow the script to successfully run. Add a magic string ')]}.' as an indicator of if the groovy script successfully passed or failed. In theory if the groovy script failed perhaps due to syntax error or some other reason the magic string will not be printed and we can assume failure. Change-Id: Ibaffb768ea82c76b44ec5a6cccde8563afe1783f Signed-off-by: Thanh Ha --- jenkins/__init__.py | 14 ++++++++++---- tests/test_script.py | 2 -- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/jenkins/__init__.py b/jenkins/__init__.py index cd14a03..0a178ce 100755 --- a/jenkins/__init__.py +++ b/jenkins/__init__.py @@ -1294,10 +1294,16 @@ class Jenkins(object): Plugin:mailer, Plugin:jquery, Plugin:antisamy-markup-formatter, Plugin:maven-plugin, Plugin:pam-auth]' ''' - return self.jenkins_open( - requests.Request( - 'POST', self._build_url(SCRIPT_TEXT), - data="script=".encode('utf-8') + quote(script).encode('utf-8'))) + magic_str = ')]}.' + print_magic_str = 'println()\nprint("{}")'.format(magic_str) + groovy = {'script': script.encode('utf-8') + print_magic_str.encode('utf-8')} + result = self.jenkins_open(requests.Request( + 'POST', self._build_url(SCRIPT_TEXT), data=groovy)) + + if not result.endswith(magic_str): + raise JenkinsException(result) + + return result[:result.rfind('\n')] def install_plugin(self, name, include_dependencies=True): '''Install a plugin and its dependencies from the Jenkins public diff --git a/tests/test_script.py b/tests/test_script.py index c73bcd6..ce8e718 100644 --- a/tests/test_script.py +++ b/tests/test_script.py @@ -1,5 +1,4 @@ from mock import patch -from six.moves.urllib.parse import quote import jenkins from tests.base import JenkinsTestBase @@ -23,7 +22,6 @@ class JenkinsScriptTest(JenkinsTestBase): self.assertEqual( jenkins_mock.call_args[0][0].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')