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
1=== modified file 'jenkins/__init__.py'
2--- jenkins/__init__.py 2013-08-14 11:57:11 +0000
3+++ jenkins/__init__.py 2013-09-28 20:11:50 +0000
4@@ -58,6 +58,7 @@
5 LAUNCHER_WINDOWS_SERVICE = 'hudson.os.windows.ManagedWindowsServiceLauncher'
6
7 INFO = 'api/json'
8+CRUMB_URL = 'crumbIssuer/api/json'
9 JOB_INFO = 'job/%(name)s/api/json?depth=0'
10 JOB_NAME = 'job/%(name)s/api/json?tree=name'
11 Q_INFO = 'queue/api/json?depth=0'
12@@ -153,6 +154,20 @@
13 self.auth = auth_headers(username, password)
14 else:
15 self.auth = None
16+ self.crumb = None
17+
18+ def maybe_add_crumb(self, req):
19+ # We don't know yet whether we need a crumb
20+ if self.crumb is None:
21+ response = self.jenkins_open(urllib2.Request(
22+ self.server + CRUMB_URL), add_crumb=False)
23+ if response:
24+ self.crumb = json.loads(response)
25+ else:
26+ # Don't need crumbs
27+ self.crumb = False
28+ if self.crumb:
29+ req.add_header(self.crumb['crumbRequestField'], self.crumb['crumb'])
30
31 def get_job_info(self, name):
32 '''
33@@ -200,14 +215,17 @@
34 for k, v in self.get_job_info(job_name).iteritems():
35 print k, v
36
37- def jenkins_open(self, req):
38+ def jenkins_open(self, req, add_crumb=True):
39 '''
40+
41 Utility routine for opening an HTTP request to a Jenkins server. This
42 should only be used to extends the :class:`Jenkins` API.
43 '''
44 try:
45 if self.auth:
46 req.add_header('Authorization', self.auth)
47+ if add_crumb:
48+ self.maybe_add_crumb(req)
49 return urllib2.urlopen(req).read()
50 except urllib2.HTTPError, e:
51 # Jenkins's funky authentication means its nigh impossible to

Subscribers

People subscribed via source and target branches