8000 Merge pull request #592 from dsvetec/issue#591 · goodwillcoding/github3.py@ae308fd · GitHub
[go: up one dir, main page]

Skip to content

Commit ae308fd

Browse files
committed
Merge pull request sigmavirus24#592 from dsvetec/issue#591
Add License preview header to repository This will automatically retrieve the License information for a repository and store it in Repository.original_license
2 parents f83e58a + 9dcf87c commit ae308fd

File tree

8 files changed

+43
-3
lines changed

8 files changed

+43
-3
lines changed

AUTHORS.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,5 @@ Contributors
126126
- Abhijeet Kasurde (@akasurde)
127127

128128
- Matthew Krueger (@mlkrueger1987)
129+
130+
- Dejan Svetec (@dsvetec)

github3/github.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,8 @@ def repository(self, owner, repository):
11351135
json = None
11361136
if owner and repository:
11371137
url = self._build_url('repos', owner, repository)
1138-
json = self._json(self._get(url), 200)
1138+
json = self._json(self._get(url, headers=License.CUSTOM_HEADERS),
1139+
200)
11391140
return self._instance_or_null(Repository, json)
11401141

11411142
def repository_with_id(self, number):

github3/licenses.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class License(GitHubCore):
1919
}
2020

2121
def _update_attributes(self, license):
22+
self._api = license.get('url', '')
2223
self.name = license.get('name')
2324
self.permitted = license.get('permitted')
2425
self.category = license.get('category')

github3/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ def refresh(self, conditional=False):
262262
as described in the `Conditional Requests`_ section of the docs
263263
:returns: self
264264
"""
265-
headers = {}
265+
headers = getattr(self, 'CUSTOM_HEADERS', {})
266266
if conditional:
267267
if self.last_modified:
268268
headers['If-Modified-Since'] = self.last_modified

github3/repos/repo.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ def _update_attributes(self, repo):
113113
self.id = repo.get('id', 0)
114114
#: Language property.
115115
self.language = repo.get('language', '')
116+
117+
# License containing only key, name, url & featured
118+
#: :class:`License <github3.licenses.License>` object representing the
119+
#: repository license.
120+
self.original_license = License(repo.get('license', {}), self)
121+
116122
#: Mirror property.
117123
self.mirror_url = repo.get('mirror_url', '')
118124

tests/integration/test_repos_repo.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,17 @@ def test_notifications(self):
826826
for notification in notifications:
827827
assert isinstance(notification, github3.notifications.Thread)
828828

829+
def test_original_license(self):
830+
"""
831+
Test that a repository's license can be retrieved at repository load.
832+
"""
833+
cassette_name = self.cassette_name('original_license')
834+
with self.recorder.use_cassette(cassette_name):
835+
repository = self.gh.repository('sigmavirus24', 'github3.py')
836+
assert repository is not None
837+
assert isinstance(repository.original_license,
838+
github3.licenses.License)
839+
829840
def test_pull_request(self):
830841
"""Test that a user can retrieve a pull request from a repo."""
831842
cassette_name = self.cassette_name('pull_request')

tests/unit/test_github.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,10 @@ def test_repository(self):
495495
"""Verify the GET request for a repository."""
496496
self.instance.repository('user', 'repo')
497497

498-
self.session.get.assert_called_once_with(url_for('repos/user/repo'))
498+
self.session.get.assert_called_once_with(
499+
url_for('repos/user/repo'),
500+
headers={'Accept': 'application/vnd.github.drax-preview+json'}
501+
)
499502

500503
def test_repository_with_invalid_repo(self):
501504
"""Verify there is no call made for invalid repo combos."""

tests/unit/test_models.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,22 @@ def test_refresh(self):
141141
headers=expected_headers,
142142
)
143143

144+
def test_refresh_custom_headers(self):
145+
"""Verify the request of refreshing an object."""
146+
self.instance.CUSTOM_HEADERS = {
147+
'Accept': 'application/vnd.github.drax-preview+json'
148+
}
149+
expected_headers = {
150+
'Accept': 'application/vnd.github.drax-preview+json'
151+
}
152+
153+
self.instance.refresh()
154+
155+
self.session.get.assert_called_once_with(
156+
self.url,
157+
headers=expected_headers,
158+
)
159+
144160
def test_refresh_last_modified(self):
145161
"""Verify the request of refreshing an object."""
146162
expected_headers = {

0 commit comments

Comments
 (0)
0