Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for build parameters #28

Merged
merged 2 commits into from Dec 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 14 additions & 10 deletions autojenkins/jobs.py
Expand Up @@ -36,6 +36,7 @@ def __str__(self):
JOB_URL = '{0}/job/{1}'
DELETE = '{0}/job/{1}/doDelete'
BUILD = '{0}/job/{1}/build'
BUILD_WITH_PARAMS = '{0}/job/{1}/buildWithParameters'
CONFIG = '{0}/job/{1}/config.xml'
JOBINFO = '{0}/job/{1}/' + API
BUILDINFO = '{0}/job/{1}/{2}/' + API
Expand Down Expand Up @@ -151,7 +152,7 @@ def all_jobs(self):
(SUCCESS, UNSTABLE or FAILED).
"""
response = self._build_get(LIST)
jobs = eval(response.content).get('jobs', [])
jobs = eval(response.text).get('jobs', [])
return [(job['name'], job['color']) for job in jobs]

def job_exists(self, jobname):
Expand All @@ -171,7 +172,7 @@ def job_info(self, jobname):
Get all information for a job as a Python object (dicts & lists).
"""
response = self._build_get(JOBINFO, jobname)
return eval(response.content)
return eval(response.text)

def build_info(self, jobname, build_number=None):
"""
Expand All @@ -184,7 +185,7 @@ def build_info(self, jobname, build_number=None):
else:
args = (LAST_BUILD, jobname)
response = self._build_get(*args)
return eval(response.content)
return eval(response.text)

def build_console(self, jobname, build_number=None):
"""
Expand All @@ -197,7 +198,7 @@ def build_console(self, jobname, build_number=None):
else:
args = (CONSOLE, jobname, "lastBuild")
response = self._build_get(*args)
return response.content
return response.text

def last_build_console(self, jobname):
"""
Expand All @@ -216,29 +217,29 @@ def last_build_report(self, jobname):
Get full report of last build.
"""
response = self._build_get(LAST_REPORT, jobname)
return eval(response.content)
return eval(response.text)

def last_result(self, jobname):
"""
Obtain results from last execution.
"""
last_result_url = self.job_info(jobname)['lastBuild']['url']
response = self._http_get(last_result_url + API)
return eval(response.content)
return eval(response.text)

def last_success(self, jobname):
"""
Return information about the last successful build.
"""
response = self._build_get(LAST_SUCCESS, jobname)
return eval(response.content)
return eval(response.text)

def get_config_xml(self, jobname):
"""
Get the ``config.xml`` file that contains the job definition.
"""
response = self._build_get(CONFIG, jobname)
return response.content
return response.text

def set_config_xml(self, jobname, config):
"""
Expand Down Expand Up @@ -319,10 +320,12 @@ def copy(self, jobname, copy_from='template'):
params = {'name': jobname, 'mode': 'copy', 'from': copy_from}
return self._build_post(NEWJOB, params=params)

def build(self, jobname, wait=False, grace=10):
def build(self, jobname, params=None, wait=False, grace=10):
"""
Trigger Jenkins to build a job.

:param params:
If params are provided, use the "buildWithParameters" endpoint
:param wait:
If ``True``, wait until job completes building before returning
"""
Expand All @@ -331,7 +334,8 @@ def build(self, jobname, wait=False, grace=10):
if not self.job_info(jobname)['buildable']:
raise JobNotBuildable("Job '%s' is not buildable (deactivated)."
% jobname)
response = self._build_post(BUILD, jobname)
url_pattern = BUILD if params is None else BUILD_WITH_PARAMS
response = self._build_post(url_pattern, jobname, params=params)
if not wait:
return response
else:
Expand Down
20 changes: 13 additions & 7 deletions autojenkins/tests/test_unit_jobs.py
Expand Up @@ -25,11 +25,11 @@ def load_fixture(name):
def mock_response(fixture=None, status=200):
response = Mock()
if fixture is None:
response.content = ''
response.text = ''
elif isinstance(fixture, dict):
response.content = str(fixture)
response.text = str(fixture)
else:
response.content = load_fixture(fixture)
response.text = load_fixture(fixture)
response.status_code = status
return response

Expand Down Expand Up @@ -67,7 +67,7 @@ def test_get_job_url(self, *args):

def test_last_result(self, requests, *args):
second_response = Mock(status_code=200)
second_response.content = "{'result': 23}"
second_response.text = "{'result': 23}"
requests.get.side_effect = [
mock_response('job_info.txt'), second_response
]
Expand Down Expand Up @@ -250,11 +250,16 @@ def test_post_methods_with_jobname_no_data(self, case, job_exists,
job_info.return_value = {'buildable': True}
response = getattr(self.jenkins, method)('name')
self.assertEqual(302, response.status_code)
kwargs = {
'auth': None,
'proxies': {},
'verify': True
}
if method == 'build':
kwargs['params'] = None
requests.post.assert_called_once_with(
'http://jenkins/' + url.format('name'),
auth=None,
proxies={},
verify=True)
**kwargs)

def test_set_config_xml(self, requests):
requests.post.return_value = Mock(status_code=200)
Expand Down Expand Up @@ -287,6 +292,7 @@ def test_build_with_wait(self, job_info, job_exists, wait_for_build,
requests.post.assert_called_once_with(
'http://jenkins/job/name/build',
auth=None,
params=None,
proxies={},
verify=True)
last_result.assert_called_once_with('name')
Expand Down