Navigation Menu

Skip to content

Commit

Permalink
Merge branch 'master' of github.com:txels/autojenkins
Browse files Browse the repository at this point in the history
  • Loading branch information
txels committed Apr 30, 2012
2 parents ddc0013 + 131bcd7 commit 628de9c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -5,3 +5,4 @@ Additional contributors, in chronological order:
Hans Insulander
Jason R. Coombs
Frank Becker
Jonas Baumann
2 changes: 1 addition & 1 deletion ajk_version.py
@@ -1 +1 @@
__version__ = '0.5.4'
__version__ = '0.6.1'
32 changes: 28 additions & 4 deletions autojenkins/jobs.py
Expand Up @@ -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()
return response


class Jenkins(object):
"""Main class to interact with a Jenkins server."""

Expand All @@ -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):
"""
Expand Down Expand Up @@ -105,6 +120,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.
Expand Down
9 changes: 8 additions & 1 deletion autojenkins/tests/test_unit_jobs.py
Expand Up @@ -4,7 +4,7 @@
from ddt import ddt, data
from mock import Mock, patch

from autojenkins.jobs import Jenkins
from autojenkins.jobs import Jenkins, HttpNotFoundError


fixture_path = path.dirname(__file__)
Expand Down Expand Up @@ -117,3 +117,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')

0 comments on commit 628de9c

Please sign in to comment.