Permalink
Browse files

Centralise HTTP requests to remove argument duplication. Plus PEP8.

  • Loading branch information...
1 parent d64eee3 commit a1c650e527d956cec2544118f6d3c5f798379cdc @txels committed Nov 2, 2012
Showing with 95 additions and 69 deletions.
  1. +1 −0 AUTHORS
  2. +62 −46 autojenkins/jobs.py
  3. +4 −5 autojenkins/run.py
  4. +4 −4 autojenkins/tests/test_run.py
  5. +24 −14 autojenkins/tests/test_unit_jobs.py
View
@@ -7,3 +7,4 @@ Jason R. Coombs
Frank Becker
Jonas Baumann
Will Maier
+Ruben Orduz
View
@@ -36,7 +36,7 @@ class HttpNotFoundError(HttpStatusError):
def _validate(response):
"""
- Verify the status code of the response and raise exception on 404
+ Verify the status code of the response and raise exception on codes > 400.
"""
message = 'HTTP Status: {0}'.format(response.status_code)
if response.status_code >= 400:
@@ -55,7 +55,7 @@ def __init__(self, base_url, auth=None, verify_ssl_cert=True):
self.auth = auth
self.verify_ssl_cert = verify_ssl_cert
- def _build_url(self, command, *args):
+ def _url(self, command, *args):
"""
Build the proper Jenkins URL for the command.
"""
@@ -67,38 +67,64 @@ def _other_url(self, root, command, *args):
"""
return command.format(root, *args)
- def _get(self, url_pattern, *args):
- response = requests.get(self._build_url(url_pattern, *args),
- auth=self.auth, verify=self.verify_ssl_cert)
+ def _http_get(self, url, **kwargs):
+ """
+ Perform an HTTP GET request.
+
+ This will add required authentication and SSL verification arguments.
+ """
+ response = requests.get(url,
+ auth=self.auth,
+ verify=self.verify_ssl_cert,
+ **kwargs)
return _validate(response)
- def _post(self, url_pattern, *args):
- response = requests.post(self._build_url(url_pattern, *args),
- auth=self.auth, verify=self.verify_ssl_cert)
+ def _http_post(self, url, **kwargs):
+ """
+ Perform an HTTP POST request.
+
+ This will add required authentication and SSL verification arguments.
+ """
+ response = requests.post(url,
+ auth=self.auth,
+ verify=self.verify_ssl_cert,
+ **kwargs)
return _validate(response)
+ def _build_get(self, url_pattern, *args, **kwargs):
+ """
+ Build proper URL from pattern and args, and perform an HTTP GET.
+ """
+ return self._http_get(self._url(url_pattern, *args), **kwargs)
+
+ def _build_post(self, url_pattern, *args, **kwargs):
+ """
+ Build proper URL from pattern and args, and perform an HTTP POST.
+ """
+ return self._http_post(self._url(url_pattern, *args), **kwargs)
+
def all_jobs(self):
"""
Get a list of tuples with (name, color) of all jobs in the server.
Color is ``blue``, ``yellow`` or ``red`` depending on build results
(SUCCESS, UNSTABLE or FAILED).
"""
- response = self._get(LIST)
+ response = self._build_get(LIST)
jobs = eval(response.content).get('jobs', [])
return [(job['name'], job['color']) for job in jobs]
def job_url(self, jobname):
"""
Get the human-browseable URL for a job.
"""
- return self._build_url(JOB_URL, jobname)
+ return self._url(JOB_URL, jobname)
def job_info(self, jobname):
"""
Get all information for a job as a Python object (dicts & lists).
"""
- response = self._get(JOBINFO, jobname)
+ response = self._build_get(JOBINFO, jobname)
return eval(response.content)
def build_info(self, jobname, build_number=None):
@@ -111,7 +137,7 @@ def build_info(self, jobname, build_number=None):
args = (BUILDINFO, jobname, build_number)
else:
args = (LAST_BUILD, jobname)
- response = self._get(*args)
+ response = self._build_get(*args)
return eval(response.content)
def last_build_info(self, jobname):
@@ -124,40 +150,38 @@ def last_build_report(self, jobname):
"""
Get full report of last build.
"""
- response = self._get(LAST_REPORT, jobname)
+ response = self._build_get(LAST_REPORT, jobname)
return eval(response.content)
def last_result(self, jobname):
"""
Obtain results from last execution.
"""
last_result_url = self.job_info(jobname)['lastBuild']['url']
- response = requests.get(last_result_url + API, auth=self.auth, verify=self.verify_ssl_cert)
+ response = self._http_get(last_result_url + API)
return eval(response.content)
def last_success(self, jobname):
"""
Return information about the last successful build.
"""
- response = self._get(LAST_SUCCESS, jobname)
+ response = self._build_get(LAST_SUCCESS, jobname)
return eval(response.content)
def get_config_xml(self, jobname):
"""
Get the ``config.xml`` file that contains the job definition.
"""
- response = self._get(CONFIG, jobname)
+ response = self._build_get(CONFIG, jobname)
return response.content
def set_config_xml(self, jobname, config):
"""
Replace the ``config.xml`` of an existing job.
"""
- return requests.post(self._build_url(CONFIG, jobname),
- data=config,
- headers={'Content-Type': 'application/xml'},
- auth=self.auth,
- verify=self.verify_ssl_cert)
+ return self._build_post(CONFIG, jobname,
+ data=config,
+ headers={'Content-Type': 'application/xml'})
def create(self, jobname, config_file, **context):
"""
@@ -170,12 +194,10 @@ def create(self, jobname, config_file, **context):
template = Template(content)
content = template.render(**context)
- return requests.post(self._build_url(NEWJOB),
- data=content,
- params=params,
- headers={'Content-Type': 'application/xml'},
- auth=self.auth,
- verify=self.verify_ssl_cert)
+ return self._build_post(NEWJOB,
+ data=content,
+ params=params,
+ headers={'Content-Type': 'application/xml'})
def create_copy(self, jobname, template_job, enable=True, **context):
"""
@@ -193,33 +215,27 @@ def create_copy(self, jobname, template_job, enable=True, **context):
config = config.replace('<disabled>true</disabled>',
'<disabled>false</disabled>')
- return requests.post(self._build_url(NEWJOB),
- data=config,
- params={'name': jobname},
- headers={'Content-Type': 'application/xml'},
- auth=self.auth,
- verify=self.verify_ssl_cert)
+ return self._build_post(NEWJOB,
+ data=config,
+ params={'name': jobname},
+ headers={'Content-Type': 'application/xml'})
def transfer(self, jobname, to_server):
"""
Copy a job to another server.
"""
config = self.get_config_xml(jobname)
- return requests.post(self._other_url(to_server, NEWJOB),
- data=config,
- params={'name': jobname},
- headers={'Content-Type': 'application/xml'},
- auth=self.auth,
- verify=self.verify_ssl_cert)
+ return self._http_post(self._other_url(to_server, NEWJOB),
+ data=config,
+ params={'name': jobname},
+ headers={'Content-Type': 'application/xml'})
def copy(self, jobname, copy_from='template'):
"""
Copy a job from another one (by default from one called ``template``).
"""
params = {'name': jobname, 'mode': 'copy', 'from': copy_from}
- return requests.post(self._build_url(NEWJOB), params=params,
- auth=self.auth,
- verify=self.verify_ssl_cert)
+ return self._build_post(NEWJOB, params=params)
def build(self, jobname, wait=False, grace=10):
"""
@@ -228,7 +244,7 @@ def build(self, jobname, wait=False, grace=10):
:param wait:
If ``True``, wait until job completes building before returning
"""
- response = self._post(BUILD, jobname)
+ response = self._build_post(BUILD, jobname)
if not wait:
return response
else:
@@ -240,19 +256,19 @@ def delete(self, jobname):
"""
Delete a job.
"""
- return self._post(DELETE, jobname)
+ return self._build_post(DELETE, jobname)
def enable(self, jobname):
"""
Trigger Jenkins to enable a job.
"""
- return self._post(ENABLE, jobname)
+ return self._build_post(ENABLE, jobname)
def disable(self, jobname):
"""
Trigger Jenkins to disable a job.
"""
- return self._post(DISABLE, jobname)
+ return self._build_post(DISABLE, jobname)
def is_building(self, jobname):
"""
View
@@ -22,9 +22,9 @@ def create_opts_parser(command, params="[jobname] [options]"):
desc = 'Run autojenkins to {0}.'.format(command)
parser = optparse.OptionParser(description=desc, usage=usage)
parser.add_option('-u', '--user',
- help='username')
+ help='username')
parser.add_option('-p', '--password',
- help='password or token')
+ help='password or token')
return parser
@@ -172,9 +172,8 @@ def build():
@staticmethod
def delete():
- parser = create_opts_parser(
- 'delete one or more jobs',
- params="[jobname]+ [options]")
+ parser = create_opts_parser('delete one or more jobs',
+ params="[jobname]+ [options]")
(options, args) = parser.parse_args()
@@ -11,8 +11,8 @@ def test_delete_jobs(jenkins):
jenkins.assert_called_with('http://jenkins', auth=None)
assert_equals(2, jenkins.return_value.delete.call_count)
assert_equals(
- [(('hello',), {}), (('bye',), {})],
- jenkins.return_value.delete.call_args_list)
+ [(('hello',), {}), (('bye',), {})],
+ jenkins.return_value.delete.call_args_list)
@patch('autojenkins.run.Jenkins')
@@ -25,5 +25,5 @@ def test_delete_jobs_authenticated(jenkins):
jenkins.assert_called_with('http://jenkins', auth=('carles', 'secret'))
assert_equals(1, jenkins.return_value.delete.call_count)
assert_equals(
- [(('hello',), {})],
- jenkins.return_value.delete.call_args_list)
+ [(('hello',), {})],
+ jenkins.return_value.delete.call_args_list)
Oops, something went wrong.

0 comments on commit a1c650e

Please sign in to comment.