8000 Merge branch 'pr/464' into develop · itsmemattchung/github3.py@e09795f · GitHub
[go: up one dir, main page]

Skip to content

Commit e09795f

Browse files
committed
Merge branch 'pr/464' into develop
2 parents 168eebe + 1dadd2e commit e09795f

File tree

5 files changed

+72
-0
lines changed

5 files changed

+72
-0
lines changed

AUTHORS.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,5 @@ Contributors
110110
- Martin Ek (@ekmartin)
111111

112112
- Chris Thompson (@notyetsecure)
113+
114+
- Bastien Gandouet (@b4stien)

github3/repos/release.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,43 @@ def _update_attributes(self, release):
4646
self.published_at = self._strptime(release.get('published_at'))
4747
#: Name of the tag
4848
self.tag_name = release.get('tag_name')
49+
#: URL to download a tarball of the release
50+
self.tarball_url = release.get('tarball_url')
4951
#: "Commit" that this release targets
5052
self.target_commitish = release.get('target_commitish')
5153
upload_url = release.get('upload_url')
5254
#: URITemplate to upload an asset with
5355
self.upload_urlt = URITemplate(upload_url) if upload_url else None
56+
#: URL to download a zipball of the release
57+
self.zipball_url = release.get('zipball_url')
5458

5559
def _repr(self):
5660
return '<Release [{0}]>'.format(self.name)
5761

62+
def archive(self, format, path=''):
63+
"""Get the tarball or zipball archive for this release.
64+
65+
:param str format: (required), accepted values: ('tarball',
66+
'zipball')
67+
:param path: (optional), path where the file should be saved
68+
to, default is the filename provided in the headers and will be
69+
written in the current directory.
70+
it can take a file-like object as well
71+
:type path: str, file
72+
:returns: bool -- True if successful, False otherwise
73+
74+
"""
75+
resp = None
76+
if format in ('tarball', 'zipball'):
77+
repo_url = self._api[:self._api.rfind('/releases')]
78+
url = self._build_url(format, self.tag_name, base_url=repo_url)
79+
resp = self._get(url, allow_redirects=True, stream=True)
80+
81+
if resp and self._boolean(resp, 200, 404):
82+
utils.stream_response_to_file(resp, path)
83+
return True
84+
return False
85+
5886
def asset(self, asset_id):
5987
"""Retrieve the asset from this release with ``asset_id``.
6088

tests/cassettes/Release_archive.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

tests/integration/test_repos_release.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@
66

77

88
class TestRelease(IntegrationHelper):
9+
def test_archive(self):
10+
"""Test the ability to download a release archive."""
11+
cassette_name = self.cassette_name('archive')
12+
with self.recorder.use_cassette(cassette_name,
13+
preserve_exact_body_bytes=True):
14+
repository = self.gh.repository('sigmavirus24', 'github3.py')
15+
release = repository.release(76677)
16+
_, filename = tempfile.mkstemp()
17+
release.archive('tarball', path=filename)
18+
19+
with open(filename, 'rb') as fd:
20+
assert len(fd.read(1024)) > 0
21+
22+
os.unlink(filename)
23+
924
def test_asset(self):
1025
"""Test the ability to retrieve a single asset from a release."""
1126
cassette_name = self.cassette_name('asset')

tests/unit/test_repos_release.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,32 @@ def test_has_upload_urlt(self):
4949
assert self.instance.upload_urlt is not None
5050

5151
# Method tests
52+
def test_tarball_archive(self):
53+
"""Verify that we generate the correct URL for a tarball archive."""
54+
self.instance.archive(format='tarball')
55+
56+
self.session.get.assert_called_once_with(
57+
'https://api.github.com/repos/octocat/Hello-World/tarball/v1.0.0',
58+
allow_redirects=True,
59+
stream=True
60+
)
61+
62+
def test_zipball_archive(self):
63+
"""Verify that we generate the correct URL for a zipball archive."""
64+
self.instance.archive(format='zipball')
65+
66+
self.session.get.assert_called_once_with(
67+
'https://api.github.com/repos/octocat/Hello-World/zipball/v1.0.0',
68+
allow_redirects=True,
69+
stream=True
70+
)
71+
72+
def test_unsupported_archive(self):
73+
"""Do not make a request if the archive format is unsupported."""
74+
self.instance.archive(format='clearly fake')
75+
76+
assert self.session.get.called is False
77+
5278
def test_delete(self):
5379
self.instance.delete()
5480
self.session.delete.assert_called_once_with(

0 commit comments

Comments
 (0)
0