From 6f3b876455b19a1be36dc6503a7926c954281df3 Mon Sep 17 00:00:00 2001 From: Jonas Baumann Date: Sun, 29 Apr 2012 10:57:18 +0200 Subject: [PATCH 1/3] Add a set_config_xml method for updating the job configuration. --- autojenkins/jobs.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/autojenkins/jobs.py b/autojenkins/jobs.py index 0c89c3a..a9035e4 100644 --- a/autojenkins/jobs.py +++ b/autojenkins/jobs.py @@ -105,6 +105,15 @@ def get_config_xml(self, jobname): response = self._get(CONFIG, jobname) return response.content + def set_config_xml(self, jobname, config): + """ + Update the ``config.xml`` of a existing job. + """ + return requests.post(self._build_url(CONFIG, jobname), + data=config, + headers={'Content-Type': 'application/xml'}, + auth=self.auth) + def create(self, jobname, config_file, **context): """ Create a job from a configuration file. From be7fb2feaa9d56dc25163fa5d580f4554e7130e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carles=20Barrob=C3=A9s?= Date: Mon, 30 Apr 2012 15:07:11 +0100 Subject: [PATCH 2/3] Raise proper exception upon http 404 not found --- AUTHORS | 1 + autojenkins/jobs.py | 23 +++++++++++++++++++---- autojenkins/tests/test_unit_jobs.py | 10 ++++++++-- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/AUTHORS b/AUTHORS index 558db1e..cc7eb59 100644 --- a/AUTHORS +++ b/AUTHORS @@ -5,3 +5,4 @@ Additional contributors, in chronological order: Hans Insulander Jason R. Coombs Frank Becker +Jonas Baumann diff --git a/autojenkins/jobs.py b/autojenkins/jobs.py index a9035e4..710859e 100644 --- a/autojenkins/jobs.py +++ b/autojenkins/jobs.py @@ -18,6 +18,19 @@ DISABLE = '{0}/job/{1}/disable' +class HttpNotFoundError(Exception): + pass + + +def _validate(response): + """ + Verify the status code of the response and raise exception on 404 + """ + if response.status_code == 404: + raise HttpNotFoundError(response.message) + return response + + class Jenkins(object): """Main class to interact with a Jenkins server.""" @@ -38,12 +51,14 @@ def _other_url(self, root, command, *args): return command.format(root, *args) def _get(self, url_pattern, *args): - return requests.get(self._build_url(url_pattern, *args), - auth=self.auth) + response = requests.get(self._build_url(url_pattern, *args), + auth=self.auth) + return _validate(response) def _post(self, url_pattern, *args): - return requests.post(self._build_url(url_pattern, *args), - auth=self.auth) + response = requests.post(self._build_url(url_pattern, *args), + auth=self.auth) + return _validate(response) def all_jobs(self): """ diff --git a/autojenkins/tests/test_unit_jobs.py b/autojenkins/tests/test_unit_jobs.py index 8b2feb5..a254dec 100644 --- a/autojenkins/tests/test_unit_jobs.py +++ b/autojenkins/tests/test_unit_jobs.py @@ -3,9 +3,8 @@ from ddt import ddt, data from mock import Mock, patch -from nose.tools import assert_equal -from autojenkins.jobs import Jenkins +from autojenkins.jobs import Jenkins, HttpNotFoundError fixture_path = path.dirname(__file__) @@ -116,3 +115,10 @@ def test_post_methods_with_jobname(self, case, requests, Template): 'http://jenkins/' + url.format('name'), auth=None) self.assertEqual(302, response.status_code) + + def test_404_raises_job_not_found(self, requests, Template): + http404_response = Mock() + http404_response.status_code = 404 + requests.get.return_value = http404_response + with self.assertRaises(HttpNotFoundError): + self.jenkins.last_build_info('job123') From 131bcd75a639d5ad772ce66ebed05cc2e94378bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carles=20Barrob=C3=A9s?= Date: Mon, 30 Apr 2012 15:39:18 +0100 Subject: [PATCH 3/3] Error generating 404 message --- ajk_version.py | 2 +- autojenkins/jobs.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ajk_version.py b/ajk_version.py index 19283fe..8411e55 100644 --- a/ajk_version.py +++ b/ajk_version.py @@ -1 +1 @@ -__version__ = '0.5.4' +__version__ = '0.6.1' diff --git a/autojenkins/jobs.py b/autojenkins/jobs.py index 710859e..047a370 100644 --- a/autojenkins/jobs.py +++ b/autojenkins/jobs.py @@ -27,7 +27,7 @@ def _validate(response): Verify the status code of the response and raise exception on 404 """ if response.status_code == 404: - raise HttpNotFoundError(response.message) + raise HttpNotFoundError() return response