Skip to content

Commit 3001eb8

Browse files
committed
Clean up Release classes
This started as an effort to break up Release classes for "Short" versions. However I discovered that there is no difference in data when iterating over a Release or an Asset vs a direct GET of them. I still cleaned up the classes to be more in line with other efforts. Remove custom header as Releases are out of prerelease. Update the example data sets with real release data from our own repository. Account for upload url differences from real data. Related: sigmavirus24#670 Signed-off-by: Jesse Keating <jkeating@j2solutions.net>
1 parent 977560f commit 3001eb8

File tree

6 files changed

+191
-130
lines changed

6 files changed

+191
-130
lines changed

github3/repos/release.py

Lines changed: 43 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,77 +6,66 @@
66
from uritemplate import URITemplate
77

88
from .. import utils
9+
from .. import models
910
from ..decorators import requires_auth
1011
from ..exceptions import error_for
11-
from ..models import GitHubCore
1212

1313

14-
class Release(GitHubCore):
14+
class Release(models.GitHubCore):
1515

1616
"""The :class:`Release <Release>` object.
1717
1818
It holds the information GitHub returns about a release from a
1919
:class:`Repository <github3.repos.repo.Repository>`.
2020
21-
"""
21+
Please see GitHub's `Releases Documentation`_ for more information.
2222
23-
CUSTOM_HEADERS = {'Accept': 'application/vnd.github.manifold-preview'}
23+
.. _Releases Documentation:
24+
https://developer.github.com/v3/repos/releases/
25+
"""
2426

2527
def _update_attributes(self, release):
26-
self._api = self._get_attribute(release, 'url')
28+
self._api = self.url = release['url']
2729

2830
#: List of :class:`Asset <Asset>` objects for this release
29-
self.original_assets = self._get_attribute(release, 'assets', [])
30-
if self.original_assets:
31-
self.original_assets = [
32-
Asset(i, self) for i in self.original_assets
33-
]
34-
35-
#: URL for uploaded assets
36-
self.assets_url = self._get_attribute(release, 'assets_url')
31+
self.original_assets = [
32+
Asset(i, self) for i in release['assets']
33+
]
3734

3835
#: Body of the release (the description)
39-
self.body = self._get_attribute(release, 'body')
36+
self.body = release['body']
4037

4138
#: Date the release was created
4239
self.created_at = self._strptime_attribute(release, 'created_at')
4340

4441
#: Boolean whether value is True or False
45-
self.draft = self._get_attribute(release, 'draft')
46-
47-
#: HTML URL of the release
48-
self.html_url = self._get_attribute(release, 'html_url')
42+
self.draft = release['draft']
4943

5044
#: GitHub id
51-
self.id = self._get_attribute(release, 'id')
45+
self.id = release['id']
5246

5347
#: Name given to the release
54-
self.name = self._get_attribute(release, 'name')
48+
self.name = release['name']
5549

5650
#: Boolean whether release is a prerelease
57-
self.prerelease = self._get_attribute(release, 'prerelease')
51+
self.prerelease = release['prerelease']
5852

5953
#: Date the release was published
6054
self.published_at = self._strptime_attribute(release, 'published_at')
6155

6256
#: Name of the tag
63-
self.tag_name = self._get_attribute(release, 'tag_name')
64-
65-
#: URL to download a tarball of the release
66-
self.tarball_url = self._get_attribute(release, 'tarball_url')
57+
self.tag_name = release['tag_name']
6758

6859
#: "Commit" that this release targets
69-
self.target_commitish = self._get_attribute(
70-
release, 'target_commitish'
71-
)
60+
self.target_commitish = release['target_commitish']
7261

7362
#: URITemplate to upload an asset with
74-
self.upload_urlt = self._class_attribute(
75-
release, 'upload_url', URITemplate
76-
)
63+
self.upload_urlt = URITemplate(release['upload_url'])
7764

78-
#: URL to download a zipball of the release
79-
self.zipball_url = self._get_attribute(release, 'zipball_url')
65+
#: URLs to various attributes
66+
for urltype in ['assets_url', 'html_url', 'tarball_url',
67+
'zipball_url']:
68+
setattr(self, urltype, release[urltype])
8069

8170
def _repr(self):
8271
return '<Release [{0}]>'.format(self.name)
@@ -131,21 +120,25 @@ def assets(self, number=-1, etag=None):
131120

132121
@requires_auth
133122
def delete(self):
134-
"""Users with push access to the repository can delete a release.
123+
"""Delete this release.
124+
125+
Users with push access to the repository can delete a release.
135126
136127
:returns: True if successful; False if not successful
137128
"""
138129
url = self._api
139130
return self._boolean(
140-
self._delete(url, headers=Release.CUSTOM_HEADERS),
131+
self._delete(url),
141132
204,
142133
404
143134
)
144135

145136
@requires_auth
146137
def edit(self, tag_name=None, target_commitish=None, name=None, body=None,
147138
draft=None, prerelease=None):
148-
"""Users with push access to the repository can edit a release.
139+
"""Edit this release.
140+
141+
Users with push access to the repository can edit a release.
149142
150143
If the edit is successful, this object will update itself.
151144
@@ -171,8 +164,7 @@ def edit(self, tag_name=None, target_commitish=None, name=None, body=None,
171164
self._remove_none(data)
172165

173166
r = self.session.patch(
174-
url, data=json.dumps(data), headers=Release.CUSTOM_HEADERS
175-
)
167+
url, data=json.dumps(data))
176168

177169
successful = self._boolean(r, 200, 404)
178170
if successful:
@@ -204,42 +196,41 @@ def upload_asset(self, content_type, name, asset, label=None):
204196
raise error_for(r)
205197

206198

207-
class Asset(GitHubCore):
199+
class Asset(models.GitHubCore):
208200

209201
def _update_attributes(self, asset):
210-
self._api = self._get_attribute(asset, 'url')
202+
self._api = asset['url']
211203

212204
#: Content-Type provided when the asset was created
213-
self.content_type = self._get_attribute(asset, 'content_type')
205+
self.content_type = asset['content_type']
214206

215207
#: Date the asset was created
216-
self.created_at = self._strptime_attribute(asset, 'created_at')
208+
self.created_at = asset['created_at']
217209

218210
#: Number of times the asset was downloaded
219-
self.download_count = self._get_attribute(asset, 'download_count')
211+
self.download_count = asset['download_count']
220212

221213
#: URL to download the asset.
222214
#: Request headers must include ``Accept: application/octet-stream``.
223215
self.download_url = self._api
224216

225217
# User friendly download URL
226-
self.browser_download_url = self._get_attribute(
227-
asset, 'browser_download_url')
218+
self.browser_download_url = asset['browser_download_url']
228219

229220
#: GitHub id of the asset
230-
self.id = self._get_attribute(asset, 'id')
221+
self.id = asset['id']
231222

232223
#: Short description of the asset
233-
self.label = self._get_attribute(asset, 'label')
224+
self.label = asset['label']
234225

235226
#: Name of the asset
236-
self.name = self._get_attribute(asset, 'name')
227+
self.name = asset['name']
237228

238229
#: Size of the asset
239-
self.size = self._get_attribute(asset, 'size')
230+
self.size = asset['size']
240231

241232
#: State of the asset, e.g., "uploaded"
242-
self.state = self._get_attribute(asset, 'state')
233+
self.state = asset['state']
243234

244235
#: Date the asset was updated
245236
self.updated_at = self._strptime_attribute(asset, 'updated_at')
@@ -287,9 +278,7 @@ def delete(self):
287278
"""
288279
url = self._api
289280
return self._boolean(
290-
self._delete(url, headers=Release.CUSTOM_HEADERS),
291-
204,
292-
404
281+
self._delete(url), 204, 404
293282
)
294283

295284
def edit(self, name, label=None):
@@ -305,8 +294,7 @@ def edit(self, name, label=None):
305294
self._remove_none(edit_data)
306295
r = self._patch(
307296
self._api,
308-
data=json.dumps(edit_data),
309-
headers=Release.CUSTOM_HEADERS
297+
data=json.dumps(edit_data)
310298
)
311299
successful = self._boolean(r, 200, 404)
312300
if successful:

github3/repos/repo.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,7 @@ def asset(self, id):
166166
if int(id) > 0:
167167
url = self._build_url('releases', 'assets', str(id),
168168
base_url=self._api)
169-
data = self._json(self._get(url, headers=Release.CUSTOM_HEADERS),
170-
200)
169+
data = self._json(self._get(url), 200)
171170
return self._instance_or_null(Asset, data)
172171

173172
def assignees(self, number=-1, etag=None):
@@ -778,9 +777,7 @@ def create_release(self, tag_name, target_commitish=None, name=None,
778777
self._remove_none(data)
779778

780779
url = self._build_url('releases', base_url=self._api)
781-
json = self._json(self._post(
782-
url, data=data, headers=Release.CUSTOM_HEADERS
783-
), 201)
780+
json = self._json(self._post(url, data=data), 201)
784781
return self._instance_or_null(Release, json)
785782

786783
@requires_auth
@@ -1659,7 +1656,6 @@ def releases(self, number=-1, etag=None):
16591656
"""
16601657
url = self._build_url('releases', base_url=self._api)
16611658
iterator = self._iter(int(number), url, Release, etag=etag)
1662-
iterator.headers.update(Release.CUSTOM_HEADERS)
16631659
return iterator
16641660

16651661
@requires_auth

tests/unit/json/repos_asset_example

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
{
2-
"url": "https://api.github.com/repos/octocat/Hello-World/releases/assets/1",
3-
"browser_download_url": "https://github.com/octocat/Hello-World/releases/download/v1.0.0/example.zip",
4-
"id": 1,
5-
"name": "example.zip",
6-
"label": "short description",
7-
"state": "uploaded",
8-
"content_type": "application/zip",
9-
"size": 1024,
10-
"download_count": 42,
11-
"created_at": "2013-02-27T19:35:32Z",
12-
"updated_at": "2013-02-27T19:35:32Z",
2+
"url": "https://api.github.com/repos/sigmavirus24/github3.py/releases/assets/37944",
3+
"id": 37944,
4+
"name": "github3.py-0.7.1-py2.py3-none-any.whl",
5+
"label": "github3.py-0.7.1-py2.py3-none-any.whl",
136
"uploader": {
14-
"login": "octocat",
15-
"id": 1,
16-
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
7+
"login": "sigmavirus24",
8+
"id": 240830,
9+
"avatar_url": "https://avatars3.githubusercontent.com/u/240830?v=4",
1710
"gravatar_id": "",
18-
"url": "https://api.github.com/users/octocat",
19-
"html_url": "https://github.com/octocat",
20-
"followers_url": "https://api.github.com/users/octocat/followers",
21-
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
22-
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
23-
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
24-
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
25-
"organizations_url": "https://api.github.com/users/octocat/orgs",
26-
"repos_url": "https://api.github.com/users/octocat/repos",
27-
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
28-
"received_events_url": "https://api.github.com/users/octocat/received_events",
11+
"url": "https://api.github.com/users/sigmavirus24",
12+
"html_url": "https://github.com/sigmavirus24",
13+
"followers_url": "https://api.github.com/users/sigmavirus24/followers",
14+
"following_url": "https://api.github.com/users/sigmavirus24/following{/other_user}",
15+
"gists_url": "https://api.github.com/users/sigmavirus24/gists{/gist_id}",
16+
"starred_url": "https://api.github.com/users/sigmavirus24/starred{/owner}{/repo}",
17+
"subscriptions_url": "https://api.github.com/users/sigmavirus24/subscriptions",
18+
"organizations_url": "https://api.github.com/users/sigmavirus24/orgs",
19+
"repos_url": "https://api.github.com/users/sigmavirus24/repos",
20+
"events_url": "https://api.github.com/users/sigmavirus24/events{/privacy}",
21+
"received_events_url": "https://api.github.com/users/sigmavirus24/received_events",
2922
"type": "User",
3023
"site_admin": false
31-
}
24+
},
25+
"content_type": "application/octet-stream",
26+
"state": "uploaded",
27+
"size": 117140,
28+
"download_count": 31,
29+
"created_at": "2013-11-15T22:35:21Z",
30+
"updated_at": "2013-11-15T22:35:59Z",
31+
"browser_download_url": "https://github.com/sigmavirus24/github3.py/releases/download/v0.7.1/github3.py-0.7.1-py2.py3-none-any.whl"
3232
}

0 commit comments

Comments
 (0)