Merge lp:~soren/python-jenkins/add-crumb into lp:~python-jenkins-developers/python-jenkins/trunk

Proposed by Soren Hansen
Status: Merged
Merged at revision: 21
Proposed branch: lp:~soren/python-jenkins/add-crumb
Merge into: lp:~python-jenkins-developers/python-jenkins/trunk
Diff against target: 51 lines (+19/-1)
1 file modified
jenkins/__init__.py (+19/-1)
To merge this branch: bzr merge lp:~soren/python-jenkins/add-crumb
Reviewer Review Type Date Requested Status
Python Jenkins Developers Pending
Review via email: mp+188155@code.launchpad.net

Description of the change

Should actually fix bug #811044

To post a comment you must log in.
lp:~soren/python-jenkins/add-crumb updated
22. By Soren Hansen

Recycle crumb. Apparently, it's not a nonce.

23. By Soren Hansen

Change method name to reflect behaviour

24. By Soren Hansen

Add bug metadata

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'jenkins/__init__.py'
--- jenkins/__init__.py 2013-08-14 11:57:11 +0000
+++ jenkins/__init__.py 2013-09-28 20:11:50 +0000
@@ -58,6 +58,7 @@
58LAUNCHER_WINDOWS_SERVICE = 'hudson.os.windows.ManagedWindowsServiceLauncher'58LAUNCHER_WINDOWS_SERVICE = 'hudson.os.windows.ManagedWindowsServiceLauncher'
5959
60INFO = 'api/json'60INFO = 'api/json'
61CRUMB_URL = 'crumbIssuer/api/json'
61JOB_INFO = 'job/%(name)s/api/json?depth=0'62JOB_INFO = 'job/%(name)s/api/json?depth=0'
62JOB_NAME = 'job/%(name)s/api/json?tree=name'63JOB_NAME = 'job/%(name)s/api/json?tree=name'
63Q_INFO = 'queue/api/json?depth=0'64Q_INFO = 'queue/api/json?depth=0'
@@ -153,6 +154,20 @@
153 self.auth = auth_headers(username, password)154 self.auth = auth_headers(username, password)
154 else:155 else:
155 self.auth = None156 self.auth = None
157 self.crumb = None
158
159 def maybe_add_crumb(self, req):
160 # We don't know yet whether we need a crumb
161 if self.crumb is None:
162 response = self.jenkins_open(urllib2.Request(
163 self.server + CRUMB_URL), add_crumb=False)
164 if response:
165 self.crumb = json.loads(response)
166 else:
167 # Don't need crumbs
168 self.crumb = False
169 if self.crumb:
170 req.add_header(self.crumb['crumbRequestField'], self.crumb['crumb'])
156171
157 def get_job_info(self, name):172 def get_job_info(self, name):
158 '''173 '''
@@ -200,14 +215,17 @@
200 for k, v in self.get_job_info(job_name).iteritems():215 for k, v in self.get_job_info(job_name).iteritems():
201 print k, v216 print k, v
202217
203 def jenkins_open(self, req):218 def jenkins_open(self, req, add_crumb=True):
204 '''219 '''
220
205 Utility routine for opening an HTTP request to a Jenkins server. This221 Utility routine for opening an HTTP request to a Jenkins server. This
206 should only be used to extends the :class:`Jenkins` API.222 should only be used to extends the :class:`Jenkins` API.
207 '''223 '''
208 try:224 try:
209 if self.auth:225 if self.auth:
210 req.add_header('Authorization', self.auth)226 req.add_header('Authorization', self.auth)
227 if add_crumb:
228 self.maybe_add_crumb(req)
211 return urllib2.urlopen(req).read()229 return urllib2.urlopen(req).read()
212 except urllib2.HTTPError, e:230 except urllib2.HTTPError, e:
213 # Jenkins's funky authentication means its nigh impossible to231 # Jenkins's funky authentication means its nigh impossible to

Subscribers

People subscribed via source and target branches