diff --git a/jenkins/__init__.py b/jenkins/__init__.py index aa5ffce..d68ff9b 100644 --- a/jenkins/__init__.py +++ b/jenkins/__init__.py @@ -94,6 +94,7 @@ VIEW_NAME = 'view/%(name)s/api/json?tree=name' CREATE_VIEW = 'createView?name=%(name)s' CONFIG_VIEW = 'view/%(name)s/config.xml' DELETE_VIEW = 'view/%(name)s/doDelete' +SCRIPT_TEXT = 'scriptText' # for testing only EMPTY_CONFIG_XML = ''' @@ -679,6 +680,24 @@ class Jenkins(object): return self.jenkins_open(Request( self.build_job_url(name, parameters, token), b'')) + def run_script(self, script): + '''Execute a groovy script on the jenkins master. + + :param script: The groovy script, ``string`` + :returns: The result of the script run. + + Example:: + >>> j = Jenkins() + >>> info = j.run_script("println(Jenkins.instance.pluginManager.plugins)") + >>> print(info) + u'[Plugin:windows-slaves, Plugin:ssh-slaves, Plugin:translation, + Plugin:cvs, Plugin:nodelabelparameter, Plugin:external-monitor-job, + Plugin:mailer, Plugin:jquery, Plugin:antisamy-markup-formatter, + Plugin:maven-plugin, Plugin:pam-auth]' + ''' + return self.jenkins_open(Request(self.server + SCRIPT_TEXT, + "script=".encode('utf-8') + script.encode('utf-8'))) + def stop_build(self, name, number): '''Stop a running Jenkins build. diff --git a/tests/test_jenkins.py b/tests/test_jenkins.py index 7301501..c817702 100644 --- a/tests/test_jenkins.py +++ b/tests/test_jenkins.py @@ -450,6 +450,17 @@ class JenkinsTest(unittest.TestCase): self.assertEqual(build_info, {'foo': 'bar'}) self._check_requests(jenkins_mock.call_args_list) + @patch.object(jenkins.Jenkins, 'jenkins_open') + def test_run_script(self, jenkins_mock): + j = jenkins.Jenkins('http://example.com/', 'test', 'test') + + j.run_script(u'println(\"Hello World!\")') + + self.assertEqual( + jenkins_mock.call_args[0][0].get_full_url(), + u'http://example.com/scriptText') + self._check_requests(jenkins_mock.call_args_list) + @patch.object(jenkins.Jenkins, 'jenkins_open') def test_stop_build(self, jenkins_mock): j = jenkins.Jenkins('http://example.com/', 'test', 'test')