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

Proposed by Francis Ginther on 2015-12-14
Status: Merged
Approved by: Francis Ginther on 2015-12-14
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 on 2015-12-14
Para Siva (community) 2015-12-14 Approve on 2015-12-14
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.
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 on 2015-12-14

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

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 on 2015-12-14

Add a changelog entry.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'debian/changelog'
--- debian/changelog 2013-04-30 07:53:46 +0000
+++ debian/changelog 2015-12-14 16:10:30 +0000
@@ -1,3 +1,9 @@
1jenkins-launchpad-plugin (0.1.1) trusty; urgency=medium
2
3 * Add support for patching URLs when reading jenkins data behind a proxy.
4
5 -- Francis Ginther <francis.ginther@canonical.com> Mon, 14 Dec 2015 10:08:24 -0600
6
1jenkins-launchpad-plugin (0.1) raring; urgency=low7jenkins-launchpad-plugin (0.1) raring; urgency=low
28
3 * Initial release9 * Initial release
410
=== modified file 'jlp.config'
--- jlp.config 2013-11-22 21:53:00 +0000
+++ jlp.config 2015-12-14 16:10:30 +0000
@@ -22,9 +22,12 @@
22jenkins_user: jenkins-user22jenkins_user: jenkins-user
23jenkins_password: jenkins-password23jenkins_password: jenkins-password
2424
25#URL of your jenkins.25#Actual URL of your jenkins (e.g. the jenkins backend URL)
26jenkins_url: http://localhost:8080/26jenkins_url: http://localhost:8080/
2727
28#Proxy URL of your jenkins (e.g. the URL accessed by users)
29jenkins_proxy_url: https://jenkins-proxy/
30
28#Token to pass when triggering a jenkins build (leave blank for none)31#Token to pass when triggering a jenkins build (leave blank for none)
29jenkins_build_token: 32jenkins_build_token:
3033
3134
=== modified file 'jlp/__init__.py'
--- jlp/__init__.py 2013-05-30 09:19:03 +0000
+++ jlp/__init__.py 2015-12-14 16:10:30 +0000
@@ -29,7 +29,14 @@
29 global _json_jenkins29 global _json_jenkins
30 if _json_jenkins is not None:30 if _json_jenkins is not None:
31 return _json_jenkins31 return _json_jenkins
32 try:
33 # The jenkins_proxy_url is an addition. Don't raise an exception
34 # if it's not defined, just set it to a default value.
35 jenkins_proxy_url = get_config_option('jenkins_proxy_url')
36 except KeyError:
37 jenkins_proxy_url = ''
32 _json_jenkins = JSONJenkins(get_config_option('jenkins_url'),38 _json_jenkins = JSONJenkins(get_config_option('jenkins_url'),
39 jenkins_proxy_url,
33 get_config_option('jenkins_user'),40 get_config_option('jenkins_user'),
34 get_config_option('jenkins_password'))41 get_config_option('jenkins_password'))
35 return _json_jenkins42 return _json_jenkins
3643
=== modified file 'jlp/jsonjenkins.py'
--- jlp/jsonjenkins.py 2013-04-25 08:45:05 +0000
+++ jlp/jsonjenkins.py 2015-12-14 16:10:30 +0000
@@ -5,14 +5,29 @@
5class JSONJenkins():5class JSONJenkins():
6 urllib_opener = None6 urllib_opener = None
77
8 def __init__(self, jenkins_url, username, password):8 def __init__(self, jenkins_url, jenkins_proxy_url, username, password):
9 self.jenkins_url = jenkins_url
10 self.jenkins_proxy_url = jenkins_proxy_url
11
9 passman = urllib2.HTTPPasswordMgrWithDefaultRealm()12 passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
10 passman.add_password(None, jenkins_url, username, password)13 passman.add_password(None, jenkins_url, username, password)
11 authhandler = urllib2.HTTPBasicAuthHandler(passman)14 authhandler = urllib2.HTTPBasicAuthHandler(passman)
12 self.urllib_opener = urllib2.build_opener(authhandler)15 self.urllib_opener = urllib2.build_opener(authhandler)
13 urllib2.install_opener(self.urllib_opener)16 urllib2.install_opener(self.urllib_opener)
1417
18 def patch_url(self, url):
19 # Test for presence of a url that needs to be patched.
20 # When behind a proxy, jenkins-launchpad-plugin can be configured
21 # to communicate directly with the backend URL. However, data
22 # retreived from jenkins API calls will have the proxified URL.
23 # These URLs need to be patched to use the backend URL.
24 if self.jenkins_url and self.jenkins_proxy_url:
25 if url.startswith(self.jenkins_proxy_url):
26 return self.jenkins_url + url[len(self.jenkins_proxy_url):]
27 return url
28
15 def get_json_data(self, url, append_api=True):29 def get_json_data(self, url, append_api=True):
30 url = self.patch_url(url)
16 if append_api:31 if append_api:
17 url = url + '/api/json'32 url = url + '/api/json'
18 data = urllib2.urlopen(url)33 data = urllib2.urlopen(url)
1934
=== modified file 'tests/test_jlp.py'
--- tests/test_jlp.py 2013-05-16 08:20:42 +0000
+++ tests/test_jlp.py 2015-12-14 16:10:30 +0000
@@ -1,4 +1,5 @@
1from unittest import TestCase1from unittest import TestCase
2import jlp
2from jlp import (get_json_jenkins, load_config, get_config_option,3from jlp import (get_json_jenkins, load_config, get_config_option,
3 set_log_level)4 set_log_level)
4from mock import patch, MagicMock5from mock import patch, MagicMock
@@ -15,6 +16,9 @@
15 json_jenkins = object()16 json_jenkins = object()
16 config = object()17 config = object()
1718
19 def setUp(self):
20 jlp._json_jenkins = None
21
18 @patch('jlp._json_jenkins', new=json_jenkins)22 @patch('jlp._json_jenkins', new=json_jenkins)
19 def test_get_json_jenkins_subsequent_time(self):23 def test_get_json_jenkins_subsequent_time(self):
20 """Existing JSONJenkins class must be returned when calling24 """Existing JSONJenkins class must be returned when calling
@@ -22,6 +26,46 @@
22 ret = get_json_jenkins()26 ret = get_json_jenkins()
23 self.assertEquals(ret, self.json_jenkins)27 self.assertEquals(ret, self.json_jenkins)
2428
29 @patch('jlp.load_config')
30 def test_get_json_replace_no_url(self, load_config):
31 """Ensure the url is unchanged when no jenkins_url is defined."""
32 load_config.return_value = {
33 'jenkins_url': '',
34 'jenkins_proxy_url': 'https://proxy/',
35 'jenkins_user': 'user',
36 'jenkins_password': 'password'
37 }
38 json_jenkins = get_json_jenkins()
39 url = json_jenkins.patch_url('https://proxy/job/my-job')
40 self.assertEquals(url, 'https://proxy/job/my-job')
41
42 @patch('jlp.load_config')
43 def test_get_json_replace_no_proxy_url(self, load_config):
44 """Ensure the url is unchanged when no proxy URL is defined."""
45 load_config.return_value = {
46 'jenkins_url': 'https://backend:8080/',
47 'jenkins_proxy_url': '',
48 'jenkins_user': 'user',
49 'jenkins_password': 'password'
50 }
51 json_jenkins = get_json_jenkins()
52 url = json_jenkins.patch_url('https://proxy/subdir/job/my-job')
53 self.assertEquals(url, 'https://proxy/subdir/job/my-job')
54
55 @patch('jlp.load_config')
56 def test_get_json_replace_proxy_url(self, load_config):
57 """Ensure that the proxy url gets replaced if both the jenkins_url
58 and jenkins_proxy_url are defined."""
59 load_config.return_value = {
60 'jenkins_url': 'http://backend:8080/',
61 'jenkins_proxy_url': 'https://proxy/',
62 'jenkins_user': 'user',
63 'jenkins_password': 'password'
64 }
65 json_jenkins = get_json_jenkins()
66 url = json_jenkins.patch_url('https://proxy/subdir/job/my-job')
67 self.assertEquals(url, 'http://backend:8080/subdir/job/my-job')
68
25 @patch('jlp._json_jenkins', new=None)69 @patch('jlp._json_jenkins', new=None)
26 @patch('jlp.JSONJenkins')70 @patch('jlp.JSONJenkins')
27 def test_get_json_jenkins_first_time(self, json_jenkins):71 def test_get_json_jenkins_first_time(self, json_jenkins):

Subscribers

People subscribed via source and target branches