8000 Add License preview header to repository, to get a repository's license by dnsv · Pull Request #592 · sigmavirus24/github3.py · GitHub
[go: up one dir, main page]

Skip to content

Add License preview header to repository, to get a repository's license #592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions AUTHORS.rst
8000
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,5 @@ Contributors
- Abhijeet Kasurde (@akasurde)

- Matthew Krueger (@mlkrueger1987)

- Dejan Svetec (@dsvetec)
3 changes: 2 additions & 1 deletion github3/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions github3/licenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class License(GitHubCore):
}

def _update_attributes(self, license):
self._api = license.get('url', '')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch

self.name = license.get('name')
self.permitted = license.get('permitted')
self.category = license.get('category')
Expand Down
2 changes: 1 addition & 1 deletion github3/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions github3/repos/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <github3.licenses.License>` object representing the
#: repository license.
self.original_license = License(repo.get('license', {}), self)

#: Mirror property.
self.mirror_url = repo.get('mirror_url', '')

Expand Down
11 changes: 11 additions & 0 deletions tests/integration/test_repos_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
5 changes: 4 additions & 1 deletion tests/unit/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
0