From 9dcf87cae45e60a0124f19e68a2b5a78c5249f40 Mon Sep 17 00:00:00 2001 From: Dejan Svetec Date: Sun, 27 Mar 2016 15:08:46 +0200 Subject: [PATCH] Add License preview header to repository, to get a repository's license --- AUTHORS.rst | 2 ++ github3/github.py | 3 ++- github3/licenses.py | 1 + github3/models.py | 2 +- github3/repos/repo.py | 6 ++++++ tests/integration/test_repos_repo.py | 11 +++++++++++ tests/unit/test_github.py | 5 ++++- tests/unit/test_models.py | 16 ++++++++++++++++ 8 files changed, 43 insertions(+), 3 deletions(-) diff --git a/AUTHORS.rst b/AUTHORS.rst index fa47fdb88..82dcc5004 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -126,3 +126,5 @@ Contributors - Abhijeet Kasurde (@akasurde) - Matthew Krueger (@mlkrueger1987) + +- Dejan Svetec (@dsvetec) \ No newline at end of file diff --git a/github3/github.py b/github3/github.py index 0b6120e0a..0539a042f 100644 --- a/github3/github.py +++ b/github3/github.py @@ -1135,7 +1135,8 @@ def repository(self, owner, repository): json = None if owner and repository: url = self._build_url('repos', owner, repository) - json = self._json(self._get(url), 200) + json = self._json(self._get(url, headers=License.CUSTOM_HEADERS), + 200) return self._instance_or_null(Repository, json) def repository_with_id(self, number): diff --git a/github3/licenses.py b/github3/licenses.py index e27caef5a..0f455b212 100644 --- a/github3/licenses.py +++ b/github3/licenses.py @@ -19,6 +19,7 @@ class License(GitHubCore): } def _update_attributes(self, license): + self._api = license.get('url', '') self.name = license.get('name') self.permitted = license.get('permitted') self.category = license.get('category') diff --git a/github3/models.py b/github3/models.py index 5b3a79aca..4d71f0d37 100644 --- a/github3/models.py +++ b/github3/models.py @@ -262,7 +262,7 @@ def refresh(self, conditional=False): as described in the `Conditional Requests`_ section of the docs :returns: self """ - headers = {} + headers = getattr(self, 'CUSTOM_HEADERS', {}) if conditional: if self.last_modified: headers['If-Modified-Since'] = self.last_modified diff --git a/github3/repos/repo.py b/github3/repos/repo.py index d94486cec..3baeb753c 100644 --- a/github3/repos/repo.py +++ b/github3/repos/repo.py @@ -113,6 +113,12 @@ def _update_attributes(self, repo): self.id = repo.get('id', 0) #: Language property. self.language = repo.get('language', '') + + # License containing only key, name, url & featured + #: :class:`License ` object representing the + #: repository license. + self.original_license = License(repo.get('license', {}), self) + #: Mirror property. self.mirror_url = repo.get('mirror_url', '') diff --git a/tests/integration/test_repos_repo.py b/tests/integration/test_repos_repo.py index 5d8a7ad3d..7adf139f2 100644 --- a/tests/integration/test_repos_repo.py +++ b/tests/integration/test_repos_repo.py @@ -826,6 +826,17 @@ def test_notifications(self): for notification in notifications: assert isinstance(notification, github3.notifications.Thread) + def test_original_license(self): + """ + Test that a repository's license can be retrieved at repository load. + """ + cassette_name = self.cassette_name('original_license') + with self.recorder.use_cassette(cassette_name): + repository = self.gh.repository('sigmavirus24', 'github3.py') + assert repository is not None + assert isinstance(repository.original_license, + github3.licenses.License) + def test_pull_request(self): """Test that a user can retrieve a pull request from a repo.""" cassette_name = self.cassette_name('pull_request') diff --git a/tests/unit/test_github.py b/tests/unit/test_github.py index 845019af7..5662174dd 100644 --- a/tests/unit/test_github.py +++ b/tests/unit/test_github.py @@ -495,7 +495,10 @@ def test_repository(self): """Verify the GET request for a repository.""" self.instance.repository('user', 'repo') - self.session.get.assert_called_once_with(url_for('repos/user/repo')) + self.session.get.assert_called_once_with( + url_for('repos/user/repo'), + headers={'Accept': 'application/vnd.github.drax-preview+json'} + ) def test_repository_with_invalid_repo(self): """Verify there is no call made for invalid repo combos.""" diff --git a/tests/unit/test_models.py b/tests/unit/test_models.py index 41c8cc91b..c8c41be44 100644 --- a/tests/unit/test_models.py +++ b/tests/unit/test_models.py @@ -141,6 +141,22 @@ def test_refresh(self): headers=expected_headers, ) + def test_refresh_custom_headers(self): + """Verify the request of refreshing an object.""" + self.instance.CUSTOM_HEADERS = { + 'Accept': 'application/vnd.github.drax-preview+json' + } + expected_headers = { + 'Accept': 'application/vnd.github.drax-preview+json' + } + + self.instance.refresh() + + self.session.get.assert_called_once_with( + self.url, + headers=expected_headers, + ) + def test_refresh_last_modified(self): """Verify the request of refreshing an object.""" expected_headers = {