Skip to content

Commit

Permalink
Change: Improve uploading release assets (#381)
Browse files Browse the repository at this point in the history
Improve uploading release assets
  • Loading branch information
bjoernricks authored Aug 1, 2022
2 parents 05453fa + b698e2e commit ec10a15
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
31 changes: 26 additions & 5 deletions pontos/github/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
Iterator,
List,
Optional,
Tuple,
Union,
)

import httpx
Expand Down Expand Up @@ -69,12 +71,15 @@ def _request_internal(
data: Optional[Dict[str, str]] = None,
content: Optional[bytes] = None,
request: Optional[Callable] = None,
content_type: Optional[str] = None,
) -> httpx.Response:
headers = {
"Accept": "application/vnd.github.v3+json",
}
if self.token:
headers["Authorization"] = f"token {self.token}"
if content_type:
headers["Content-Type"] = content_type

request = request or httpx.get
kwargs = {}
Expand All @@ -83,7 +88,7 @@ def _request_internal(
if content is not None:
kwargs["content"] = content
return request(
f"{url}",
url,
headers=headers,
params=params,
follow_redirects=True,
Expand Down Expand Up @@ -409,7 +414,10 @@ def download_release_assets(
yield progress

def upload_release_assets(
self, repo: str, tag: str, files: Iterable[Path]
self,
repo: str,
tag: str,
files: Iterable[Union[Path, Tuple[Path, str]]],
) -> Iterator[Path]:
# pylint: disable=line-too-long
"""
Expand All @@ -418,27 +426,40 @@ def upload_release_assets(
Args:
repo: GitHub repository (owner/name) to use
tag: The git tag for the release
files: File paths to upload as an asset
files: An iterable of file paths or an iterable of tuples
containing a file path and content types to upload as an asset
Returns:
yields each file after its upload is finished
Raises:
HTTPError if an upload request was invalid
Example:
Examples:
>>> files = (Path("foo.txt"), Path("bar.txt"),)
>>> for uploaded_file in git.upload_release_assets("foo/bar", "1.2.3", files):
>>> print(f"Uploaded: {uploaded_file}")
>>> files = [(Path("foo.txt"), "text/ascii"), (Path("bar.pdf"), "application/pdf")]
>>> for uploaded_file in git.upload_release_assets("foo/bar", "1.2.3", files):
>>> print(f"Uploaded: {uploaded_file}")
"""
github_json = self.release(repo, tag)
asset_url = github_json["upload_url"].replace("{?name,label}", "")

for file_path in files:
if isinstance(file_path, Tuple):
file_path, content_type = file_path
else:
content_type = "application/octet-stream"

to_upload = file_path.read_bytes()

response = self._request_internal(
f"{asset_url}?name={file_path.name}",
asset_url,
params={"name": file_path.name},
content=to_upload,
content_type=content_type,
request=httpx.post,
)
response.raise_for_status()
Expand Down
6 changes: 4 additions & 2 deletions pontos/release/sign.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,10 @@ def sign(
if args.dry_run:
return True

upload_files = [Path(f"{str(p)}.asc") for p in file_paths]
terminal.info(f"Uploading assets: {[str(p) for p in upload_files]}")
upload_files = [
(Path(f"{str(p)}.asc"), "application/pgp-signature") for p in file_paths
]
terminal.info(f"Uploading assets: {[str(p[0]) for p in upload_files]}")

try:
for uploaded_file in github.upload_release_assets(
Expand Down
15 changes: 9 additions & 6 deletions tests/github/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,25 +624,27 @@ def test_upload_release_assets(
)
uploaded_file = next(upload_it)
post_mock.assert_called_with(
"https://uploads.github.com/repos/greenbone/pontos/releases/52499047/assets?name=foo.txt", # pylint: disable=line-too-long
"https://uploads.github.com/repos/greenbone/pontos/releases/52499047/assets", # pylint: disable=line-too-long
headers={
"Accept": "application/vnd.github.v3+json",
"Authorization": "token 12345",
"Content-Type": "application/octet-stream",
},
params=None,
params={"name": "foo.txt"},
follow_redirects=True,
content=content1,
)
self.assertEqual(uploaded_file, file1)

uploaded_file = next(upload_it)
post_mock.assert_called_with(
"https://uploads.github.com/repos/greenbone/pontos/releases/52499047/assets?name=bar.pdf", # pylint: disable=line-too-long
"https://uploads.github.com/repos/greenbone/pontos/releases/52499047/assets", # pylint: disable=line-too-long
headers={
"Accept": "application/vnd.github.v3+json",
"Authorization": "token 12345",
"Content-Type": "application/octet-stream",
},
params=None,
params={"name": "bar.pdf"},
follow_redirects=True,
content=content2,
)
Expand Down Expand Up @@ -737,12 +739,13 @@ def test_upload_release_assets_upload_fails(
)
uploaded_file = next(upload_it)
post_mock.assert_called_with(
"https://uploads.github.com/repos/greenbone/pontos/releases/52499047/assets?name=foo.txt", # pylint: disable=line-too-long
"https://uploads.github.com/repos/greenbone/pontos/releases/52499047/assets", # pylint: disable=line-too-long
headers={
"Accept": "application/vnd.github.v3+json",
"Authorization": "token 12345",
"Content-Type": "application/octet-stream",
},
params=None,
params={"name": "foo.txt"},
follow_redirects=True,
content=content1,
)
Expand Down

0 comments on commit ec10a15

Please sign in to comment.