Merge lp:~fginther/jenkins-launchpad-plugin/proxy-url-replace into lp:jenkins-launchpad-plugin

Proposed by Francis Ginther
Status: Merged
Approved by: Francis Ginther
Approved revision: 129
Merged at revision: 127
Proposed branch: lp:~fginther/jenkins-launchpad-plugin/proxy-url-replace
Merge into: lp:jenkins-launchpad-plugin
Diff against target: 152 lines (+77/-2)
5 files modified
debian/changelog (+6/-0)
jlp.config (+4/-1)
jlp/__init__.py (+7/-0)
jlp/jsonjenkins.py (+16/-1)
tests/test_jlp.py (+44/-0)
To merge this branch: bzr merge lp:~fginther/jenkins-launchpad-plugin/proxy-url-replace
Reviewer Review Type Date Requested Status
PS Jenkins bot (community) continuous-integration Approve
Para Siva (community) Approve
Review via email: mp+280408@code.launchpad.net

Commit message

Add support for replacing a proxy URL with a backend URL when processing JSON URLs.

Description of the change

Add support for replacing a proxy URL with a backend URL when processing JSON URLs.

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
Para Siva (psivaa) wrote :

Thanks Francis for working on this. Looks OK for me. With an inline comment to fix. Also you'd notice the issue in the CI here.

review: Needs Fixing
128. By Francis Ginther

Fix pep8 issue and removed refactored code that was left in.

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Para Siva (psivaa) wrote :

Thanks for the fix. Approving. I haven't tested this with a proxy_url though

review: Approve
129. By Francis Ginther

Add a changelog entry.

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'debian/changelog'
2--- debian/changelog 2013-04-30 07:53:46 +0000
3+++ debian/changelog 2015-12-14 16:10:30 +0000
4@@ -1,3 +1,9 @@
5+jenkins-launchpad-plugin (0.1.1) trusty; urgency=medium
6+
7+ * Add support for patching URLs when reading jenkins data behind a proxy.
8+
9+ -- Francis Ginther <francis.ginther@canonical.com> Mon, 14 Dec 2015 10:08:24 -0600
10+
11 jenkins-launchpad-plugin (0.1) raring; urgency=low
12
13 * Initial release
14
15=== modified file 'jlp.config'
16--- jlp.config 2013-11-22 21:53:00 +0000
17+++ jlp.config 2015-12-14 16:10:30 +0000
18@@ -22,9 +22,12 @@
19 jenkins_user: jenkins-user
20 jenkins_password: jenkins-password
21
22-#URL of your jenkins.
23+#Actual URL of your jenkins (e.g. the jenkins backend URL)
24 jenkins_url: http://localhost:8080/
25
26+#Proxy URL of your jenkins (e.g. the URL accessed by users)
27+jenkins_proxy_url: https://jenkins-proxy/
28+
29 #Token to pass when triggering a jenkins build (leave blank for none)
30 jenkins_build_token:
31
32
33=== modified file 'jlp/__init__.py'
34--- jlp/__init__.py 2013-05-30 09:19:03 +0000
35+++ jlp/__init__.py 2015-12-14 16:10:30 +0000
36@@ -29,7 +29,14 @@
37 global _json_jenkins
38 if _json_jenkins is not None:
39 return _json_jenkins
40+ try:
41+ # The jenkins_proxy_url is an addition. Don't raise an exception
42+ # if it's not defined, just set it to a default value.
43+ jenkins_proxy_url = get_config_option('jenkins_proxy_url')
44+ except KeyError:
45+ jenkins_proxy_url = ''
46 _json_jenkins = JSONJenkins(get_config_option('jenkins_url'),
47+ jenkins_proxy_url,
48 get_config_option('jenkins_user'),
49 get_config_option('jenkins_password'))
50 return _json_jenkins
51
52=== modified file 'jlp/jsonjenkins.py'
53--- jlp/jsonjenkins.py 2013-04-25 08:45:05 +0000
54+++ jlp/jsonjenkins.py 2015-12-14 16:10:30 +0000
55@@ -5,14 +5,29 @@
56 class JSONJenkins():
57 urllib_opener = None
58
59- def __init__(self, jenkins_url, username, password):
60+ def __init__(self, jenkins_url, jenkins_proxy_url, username, password):
61+ self.jenkins_url = jenkins_url
62+ self.jenkins_proxy_url = jenkins_proxy_url
63+
64 passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
65 passman.add_password(None, jenkins_url, username, password)
66 authhandler = urllib2.HTTPBasicAuthHandler(passman)
67 self.urllib_opener = urllib2.build_opener(authhandler)
68 urllib2.install_opener(self.urllib_opener)
69
70+ def patch_url(self, url):
71+ # Test for presence of a url that needs to be patched.
72+ # When behind a proxy, jenkins-launchpad-plugin can be configured
73+ # to communicate directly with the backend URL. However, data
74+ # retreived from jenkins API calls will have the proxified URL.
75+ # These URLs need to be patched to use the backend URL.
76+ if self.jenkins_url and self.jenkins_proxy_url:
77+ if url.startswith(self.jenkins_proxy_url):
78+ return self.jenkins_url + url[len(self.jenkins_proxy_url):]
79+ return url
80+
81 def get_json_data(self, url, append_api=True):
82+ url = self.patch_url(url)
83 if append_api:
84 url = url + '/api/json'
85 data = urllib2.urlopen(url)
86
87=== modified file 'tests/test_jlp.py'
88--- tests/test_jlp.py 2013-05-16 08:20:42 +0000
89+++ tests/test_jlp.py 2015-12-14 16:10:30 +0000
90@@ -1,4 +1,5 @@
91 from unittest import TestCase
92+import jlp
93 from jlp import (get_json_jenkins, load_config, get_config_option,
94 set_log_level)
95 from mock import patch, MagicMock
96@@ -15,6 +16,9 @@
97 json_jenkins = object()
98 config = object()
99
100+ def setUp(self):
101+ jlp._json_jenkins = None
102+
103 @patch('jlp._json_jenkins', new=json_jenkins)
104 def test_get_json_jenkins_subsequent_time(self):
105 """Existing JSONJenkins class must be returned when calling
106@@ -22,6 +26,46 @@
107 ret = get_json_jenkins()
108 self.assertEquals(ret, self.json_jenkins)
109
110+ @patch('jlp.load_config')
111+ def test_get_json_replace_no_url(self, load_config):
112+ """Ensure the url is unchanged when no jenkins_url is defined."""
113+ load_config.return_value = {
114+ 'jenkins_url': '',
115+ 'jenkins_proxy_url': 'https://proxy/',
116+ 'jenkins_user': 'user',
117+ 'jenkins_password': 'password'
118+ }
119+ json_jenkins = get_json_jenkins()
120+ url = json_jenkins.patch_url('https://proxy/job/my-job')
121+ self.assertEquals(url, 'https://proxy/job/my-job')
122+
123+ @patch('jlp.load_config')
124+ def test_get_json_replace_no_proxy_url(self, load_config):
125+ """Ensure the url is unchanged when no proxy URL is defined."""
126+ load_config.return_value = {
127+ 'jenkins_url': 'https://backend:8080/',
128+ 'jenkins_proxy_url': '',
129+ 'jenkins_user': 'user',
130+ 'jenkins_password': 'password'
131+ }
132+ json_jenkins = get_json_jenkins()
133+ url = json_jenkins.patch_url('https://proxy/subdir/job/my-job')
134+ self.assertEquals(url, 'https://proxy/subdir/job/my-job')
135+
136+ @patch('jlp.load_config')
137+ def test_get_json_replace_proxy_url(self, load_config):
138+ """Ensure that the proxy url gets replaced if both the jenkins_url
139+ and jenkins_proxy_url are defined."""
140+ load_config.return_value = {
141+ 'jenkins_url': 'http://backend:8080/',
142+ 'jenkins_proxy_url': 'https://proxy/',
143+ 'jenkins_user': 'user',
144+ 'jenkins_password': 'password'
145+ }
146+ json_jenkins = get_json_jenkins()
147+ url = json_jenkins.patch_url('https://proxy/subdir/job/my-job')
148+ self.assertEquals(url, 'http://backend:8080/subdir/job/my-job')
149+
150 @patch('jlp._json_jenkins', new=None)
151 @patch('jlp.JSONJenkins')
152 def test_get_json_jenkins_first_time(self, json_jenkins):

Subscribers

People subscribed via source and target branches