Skip to content

Commit 86e685a

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 86e685a

9 files changed

Lines changed: 390 additions & 343 deletions

File tree

.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 & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,13 @@
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

128
LOGGER = LoggerSetup.get_logger("gardenlinux.github.release", logging.INFO)
139

1410

15-
def create_github_release(
16-
owner: str, repo: str, tag: str, commitish: str, latest: bool, body: str
17-
) -> int | None:
18-
token = os.environ.get("GITHUB_TOKEN")
19-
if not token:
20-
raise ValueError("GITHUB_TOKEN environment variable not set")
21-
22-
headers = {
23-
"Authorization": f"token {token}",
24-
"Accept": "application/vnd.github.v3+json",
25-
}
26-
27-
data = {
28-
"tag_name": tag,
29-
"target_commitish": commitish,
30-
"name": tag,
31-
"body": body,
32-
"draft": False,
33-
"prerelease": False,
34-
"make_latest": "true" if latest else "false",
35-
}
36-
37-
response = requests.post(
38-
f"https://api.github.com/repos/{owner}/{repo}/releases",
39-
headers=headers,
40-
data=json.dumps(data),
41-
timeout=REQUESTS_TIMEOUTS,
42-
)
43-
44-
if response.status_code == 201:
45-
LOGGER.info("Release created successfully")
46-
response_json = response.json()
47-
return int(response_json.get("id")) # Will raise KeyError if missing
48-
else:
49-
LOGGER.error("Failed to create release")
50-
LOGGER.debug(response.json())
51-
response.raise_for_status()
52-
53-
return None # Simply to make mypy happy. should not be reached.
54-
55-
5611
def write_to_release_id_file(release_id: str | int) -> None:
5712
try:
5813
with open(RELEASE_ID_FILE, "w") as file:
@@ -63,51 +18,4 @@ def write_to_release_id_file(release_id: str | int) -> None:
6318
sys.exit(1)
6419

6520

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"]
21+
__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)