Skip to content

Commit 0dc5a23

Browse files
committed
Add enhanced support to upload GitHub release assets
Signed-off-by: Tobias Wolf <wolf@b1-systems.de> On-behalf-of: SAP <tobias.wolf@sap.com>
1 parent bc3aa3c commit 0dc5a23

File tree

9 files changed

+390
-302
lines changed

9 files changed

+390
-302
lines changed

.github/actions/setup/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: Installs the given GardenLinux Python library
44
inputs:
55
version:
66
description: GardenLinux Python library version
7-
default: "0.10.18"
7+
default: "0.10.19"
88
python_version:
99
description: Python version to setup
1010
default: "3.13"

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "gardenlinux"
3-
version = "0.10.18"
3+
version = "0.10.19"
44
description = "Contains tools to work with the features directory of gardenlinux, for example deducting dependencies from feature sets or validating cnames"
55
authors = ["Garden Linux Maintainers <contact@gardenlinux.io>"]
66
license = "Apache-2.0"
Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
import json
21
import logging
3-
import os
42
import sys
53

6-
import requests
7-
8-
from ...constants import RELEASE_ID_FILE, REQUESTS_TIMEOUTS
4+
from ...constants import RELEASE_ID_FILE
95
from ...logger import LoggerSetup
106
from .release import Release
117

@@ -63,51 +59,4 @@ def write_to_release_id_file(release_id: str | int) -> None:
6359
sys.exit(1)
6460

6561

66-
def upload_to_github_release_page(
67-
github_owner: str,
68-
github_repo: str,
69-
gardenlinux_release_id: str | int,
70-
file_to_upload: str,
71-
dry_run: bool,
72-
) -> None:
73-
if dry_run:
74-
LOGGER.info(
75-
f"Dry run: would upload {file_to_upload} to release {gardenlinux_release_id} in repo {github_owner}/{github_repo}"
76-
)
77-
return
78-
79-
if os.path.getsize(file_to_upload) < 1:
80-
LOGGER.info(f"{file_to_upload} is empty and will be ignored")
81-
return
82-
83-
token = os.environ.get("GITHUB_TOKEN")
84-
if not token:
85-
raise ValueError("GITHUB_TOKEN environment variable not set")
86-
87-
headers = {
88-
"Authorization": f"token {token}",
89-
"Content-Type": "application/octet-stream",
90-
}
91-
92-
upload_url = f"https://uploads.github.com/repos/{github_owner}/{github_repo}/releases/{gardenlinux_release_id}/assets?name={os.path.basename(file_to_upload)}"
93-
94-
try:
95-
with open(file_to_upload, "rb") as f:
96-
file_contents = f.read()
97-
except IOError as e:
98-
LOGGER.error(f"Error reading file {file_to_upload}: {e}")
99-
return
100-
101-
response = requests.post(
102-
upload_url, headers=headers, data=file_contents, timeout=REQUESTS_TIMEOUTS
103-
)
104-
if response.status_code == 201:
105-
LOGGER.info("Upload successful")
106-
else:
107-
LOGGER.error(
108-
f"Upload failed with status code {response.status_code}: {response.text}"
109-
)
110-
response.raise_for_status()
111-
112-
113-
__all__ = ["Release", "write_to_release_id_file", "upload_to_github_release_page"]
62+
__all__ = ["Release", "write_to_release_id_file"]

src/gardenlinux/github/release/__main__.py

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

77
from ..release_notes import create_github_release_notes
88
from . import (
9-
upload_to_github_release_page,
109
write_to_release_id_file,
1110
)
1211
from .release import Release
@@ -149,6 +148,10 @@ def get_parser() -> argparse.ArgumentParser:
149148
help="Perform a dry run without actually uploading the file.",
150149
)
151150

151+
upload_parser.add_argument(
152+
"--overwrite-same-name", action="store_true", default=False
153+
)
154+
152155
return parser
153156

154157

@@ -169,6 +172,7 @@ def main() -> None:
169172
body = create_github_release_notes(
170173
args.tag, args.commit, GARDENLINUX_GITHUB_RELEASE_BUCKET_NAME
171174
)
175+
172176
if args.dry_run:
173177
print("Dry Run ...")
174178
print("This release would be created:")
@@ -184,9 +188,16 @@ def main() -> None:
184188
write_to_release_id_file(f"{release_id}")
185189
LOGGER.info(f"Release created with ID: {release_id}")
186190
elif args.command == "upload":
187-
upload_to_github_release_page(
188-
args.owner, args.repo, args.release_id, args.file_path, args.dry_run
189-
)
191+
release = Release.get(args.release_id, repo=args.repo, owner=args.owner)
192+
193+
if args.dry_run:
194+
print("Dry Run ...")
195+
196+
print(
197+
f"The file {args.file_path} would be uploaded for release: {release.name}"
198+
)
199+
else:
200+
release.upload_asset(args.file_path, args.overwrite_same_name)
190201
else:
191202
parser.print_help()
192203

0 commit comments

Comments
 (0)