Merge "Support scriptText api to execute groovy scripts on the server"

This commit is contained in:
Jenkins 2015-07-13 17:44:10 +00:00 committed by Gerrit Code Review
commit b5260c0d12
2 changed files with 30 additions and 0 deletions

View File

@ -94,6 +94,7 @@ VIEW_NAME = 'view/%(name)s/api/json?tree=name'
CREATE_VIEW = 'createView?name=%(name)s' CREATE_VIEW = 'createView?name=%(name)s'
CONFIG_VIEW = 'view/%(name)s/config.xml' CONFIG_VIEW = 'view/%(name)s/config.xml'
DELETE_VIEW = 'view/%(name)s/doDelete' DELETE_VIEW = 'view/%(name)s/doDelete'
SCRIPT_TEXT = 'scriptText'
# for testing only # for testing only
EMPTY_CONFIG_XML = '''<?xml version='1.0' encoding='UTF-8'?> EMPTY_CONFIG_XML = '''<?xml version='1.0' encoding='UTF-8'?>
@ -679,6 +680,24 @@ class Jenkins(object):
return self.jenkins_open(Request( return self.jenkins_open(Request(
self.build_job_url(name, parameters, token), b'')) 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): def stop_build(self, name, number):
'''Stop a running Jenkins build. '''Stop a running Jenkins build.

View File

@ -450,6 +450,17 @@ class JenkinsTest(unittest.TestCase):
self.assertEqual(build_info, {'foo': 'bar'}) self.assertEqual(build_info, {'foo': 'bar'})
self._check_requests(jenkins_mock.call_args_list) 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') @patch.object(jenkins.Jenkins, 'jenkins_open')
def test_stop_build(self, jenkins_mock): def test_stop_build(self, jenkins_mock):
j = jenkins.Jenkins('http://example.com/', 'test', 'test') j = jenkins.Jenkins('http://example.com/', 'test', 'test')