incubator-allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From john...@apache.org
Subject [35/44] git commit: [#6656] ticket:437 Use OAuth token for github api calls
Date Wed, 13 Nov 2013 22:57:55 GMT
[#6656] ticket:437 Use OAuth token for github api calls


Project: http://git-wip-us.apache.org/repos/asf/incubator-allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-allura/commit/d396c9e6
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/d396c9e6
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/d396c9e6

Branch: refs/heads/cj/6815
Commit: d396c9e6db9e45c07465d5d73375068787c3c594
Parents: 2c6339a
Author: Igor Bondarenko <jetmind2@gmail.com>
Authored: Fri Oct 25 11:15:52 2013 +0300
Committer: Cory Johns <cjohns@slashdotmedia.com>
Committed: Wed Nov 13 20:43:45 2013 +0000

----------------------------------------------------------------------
 .../forgeimporters/github/__init__.py           | 19 +++++++++++++--
 .../tests/github/test_extractor.py              | 25 +++++++++++++++++++-
 2 files changed, 41 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/d396c9e6/ForgeImporters/forgeimporters/github/__init__.py
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/github/__init__.py b/ForgeImporters/forgeimporters/github/__init__.py
index 2963067..5726326 100644
--- a/ForgeImporters/forgeimporters/github/__init__.py
+++ b/ForgeImporters/forgeimporters/github/__init__.py
@@ -18,8 +18,6 @@
 import re
 import logging
 import json
-import urllib
-import urllib2
 
 from forgeimporters import base
 
@@ -36,6 +34,23 @@ class GitHubProjectExtractor(base.ProjectExtractor):
     SUPPORTED_ISSUE_EVENTS = ('closed', 'reopened', 'assigned')
     NEXT_PAGE_URL_RE = re.compile(r'<([^>]*)>; rel="next"')
 
+    def __init__(self, *args, **kw):
+        self.token = None
+        user = kw.pop('user', None)
+        if user:
+            self.token = user.get_tool_data('GitHubProjectExtractor', 'token')
+        super(GitHubProjectExtractor, self).__init__(*args, **kw)
+
+    def add_token(self, url):
+        if self.token:
+            glue = '&' if '?' in url else '?'
+            url += glue + 'access_token=' + self.token
+        return url
+
+    def urlopen(self, url, **kw):
+        url = self.add_token(url)
+        return super(GitHubProjectExtractor, self).urlopen(url, **kw)
+
     def get_next_page_url(self, link):
         if not link:
             return

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/d396c9e6/ForgeImporters/forgeimporters/tests/github/test_extractor.py
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/tests/github/test_extractor.py b/ForgeImporters/forgeimporters/tests/github/test_extractor.py
index 11601a5..a26438a 100644
--- a/ForgeImporters/forgeimporters/tests/github/test_extractor.py
+++ b/ForgeImporters/forgeimporters/tests/github/test_extractor.py
@@ -18,6 +18,8 @@
 import json
 from unittest import TestCase
 
+from mock import patch, Mock
+
 from ... import github
 
 # Can't use cStringIO here, because we cannot set attributes or subclass it,
@@ -84,6 +86,7 @@ class TestGitHubProjectExtractor(TestCase):
     def setUp(self):
         self.extractor = github.GitHubProjectExtractor('test_project')
         self.extractor.urlopen = self.mocked_urlopen
+
     def test_get_next_page_url(self):
         self.assertIsNone(self.extractor.get_next_page_url(None))
         self.assertIsNone(self.extractor.get_next_page_url(''))
@@ -97,7 +100,7 @@ class TestGitHubProjectExtractor(TestCase):
 
         link = '<https://api.github.com/repositories/8560576/issues?state=open&page=1>;
rel="prev"'
         self.assertIsNone(self.extractor.get_next_page_url(link))
-        
+
     def test_get_summary(self):
         self.assertEqual(self.extractor.get_summary(), 'project description')
 
@@ -126,3 +129,23 @@ class TestGitHubProjectExtractor(TestCase):
 
     def test_get_wiki_url(self):
         self.assertEqual(self.extractor.get_page_url('wiki_url'), 'https://github.com/test_project.wiki')
+
+    @patch('forgeimporters.base.h.urlopen')
+    def test_urlopen(self, urlopen):
+        e = github.GitHubProjectExtractor('test_project')
+        url = 'https://github.com/u/p/'
+        e.urlopen(url)
+        request = urlopen.call_args[0][0]
+        self.assertEqual(request.get_full_url(), url)
+
+        user = Mock()
+        user.get_tool_data.return_value = 'abc'
+        e = github.GitHubProjectExtractor('test_project', user=user)
+        e.urlopen(url)
+        request = urlopen.call_args[0][0]
+        self.assertEqual(request.get_full_url(), url + '?access_token=abc')
+
+        url = 'https://github.com/u/p/?p=1'
+        e.urlopen(url)
+        request = urlopen.call_args[0][0]
+        self.assertEqual(request.get_full_url(), url + '&access_token=abc')


Mime
View raw message