Allow name to be specified for upload_asset (#1151)

GitRelease.upload_asset() calculates the name based on the filename, and
doesn't allow it to be overridden. Add a new name argument that is used
if it is set. Drive by changing the default of the content_type argument
to NotSet.

Closes #1095
This commit is contained in:
Steve Kowalik
2019-06-24 15:14:23 +08:00
committed by Wan Liuyang
parent 03939fe55c
commit 8d2a6b534d
3 changed files with 58 additions and 4 deletions
+8 -4
View File
@@ -216,20 +216,24 @@ class GitRelease(github.GithubObject.CompletableGithubObject):
)
return github.GitRelease.GitRelease(self._requester, headers, data, completed=True)
def upload_asset(self, path, label="", content_type=""):
def upload_asset(self, path, label="", content_type=github.GithubObject.NotSet, name=github.GithubObject.NotSet):
"""
:calls: `POST https://<upload_url>/repos/:owner/:repo/releases/:release_id/assets?name=foo.zip <https://developer.github.com/v3/repos/releases/#upload-a-release-asset>`_
:calls: `POST https://<upload_url>/repos/:owner/:repo/releases/:release_id/assets <https://developer.github.com/v3/repos/releases/#upload-a-release-asset>`_
:rtype: :class:`github.GitReleaseAsset.GitReleaseAsset`
"""
assert isinstance(path, (str, unicode)), path
assert isinstance(label, (str, unicode)), label
assert name is github.GithubObject.NotSet or isinstance(name, (str, unicode)), name
post_parameters = {
"name": basename(path),
"label": label
}
if name is github.GithubObject.NotSet:
post_parameters["name"] = basename(path)
else:
post_parameters["name"] = name
headers = {}
if len(content_type) > 0:
if content_type is not github.GithubObject.NotSet:
headers["Content-Type"] = content_type
resp_headers, data = self._requester.requestBlobAndCheck(
"POST",
+7
View File
@@ -150,3 +150,10 @@ class Release(Framework.TestCase):
the_release.upload_asset(self.artifact_path,
"unit test artifact",
"application/zip")
def testUploadAssetWithName(self):
release_id = 1210837
repo = self.g.get_user().get_repo("PyGithub")
release = repo.get_release(release_id)
r = release.upload_asset(self.artifact_path, name="foobar.zip")
self.assertEqual(r.name, "foobar.zip")
File diff suppressed because one or more lines are too long