Skip to content

Commit 35741dd

Browse files
Cherry-pick maintenance changes of up until 0.10.20
* Increase requests read timeout to 60 seconds * Validate files contain at least one byte before uploading * Add enhanced support to upload GitHub release assets * Add support to create pre-releases with a generated changelog Backport from #271 Signed-off-by: Tobias Wolf <wolf@b1-systems.de> On-behalf-of: SAP <tobias.wolf@sap.com>
1 parent 868783c commit 35741dd

10 files changed

Lines changed: 392 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.16"
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.16"
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"

‎src/gardenlinux/constants.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@
159159

160160
RELEASE_ID_FILE = ".github_release_id"
161161

162-
REQUESTS_TIMEOUTS = (5, 30) # connect, read
162+
REQUESTS_TIMEOUTS = (5, 60) # connect, read
163163

164164
S3_DOWNLOADS_DIR = Path(os.path.dirname(__file__)) / ".." / "s3_downloads"
165165

Lines changed: 2 additions & 90 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,47 +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-
token = os.environ.get("GITHUB_TOKEN")
80-
if not token:
81-
raise ValueError("GITHUB_TOKEN environment variable not set")
82-
83-
headers = {
84-
"Authorization": f"token {token}",
85-
"Content-Type": "application/octet-stream",
86-
}
87-
88-
upload_url = f"https://uploads.github.com/repos/{github_owner}/{github_repo}/releases/{gardenlinux_release_id}/assets?name={os.path.basename(file_to_upload)}"
89-
90-
try:
91-
with open(file_to_upload, "rb") as f:
92-
file_contents = f.read()
93-
except IOError as e:
94-
LOGGER.error(f"Error reading file {file_to_upload}: {e}")
95-
return
96-
97-
response = requests.post(
98-
upload_url, headers=headers, data=file_contents, timeout=REQUESTS_TIMEOUTS
99-
)
100-
if response.status_code == 201:
101-
LOGGER.info("Upload successful")
102-
else:
103-
LOGGER.error(
104-
f"Upload failed with status code {response.status_code}: {response.text}"
105-
)
106-
response.raise_for_status()
107-
108-
109-
__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: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
from gardenlinux.logger import LoggerSetup
66

77
from ..release_notes import create_github_release_notes
8-
from . import (
9-
upload_to_github_release_page,
10-
write_to_release_id_file,
11-
)
8+
from . import write_to_release_id_file
129
from .release import Release
1310

1411
LOGGER = LoggerSetup.get_logger("gardenlinux.github", logging.INFO)
@@ -149,6 +146,10 @@ def get_parser() -> argparse.ArgumentParser:
149146
help="Perform a dry run without actually uploading the file.",
150147
)
151148

149+
upload_parser.add_argument(
150+
"--overwrite-same-name", action="store_true", default=False
151+
)
152+
152153
return parser
153154

154155

@@ -169,6 +170,7 @@ def main() -> None:
169170
body = create_github_release_notes(
170171
args.tag, args.commit, GARDENLINUX_GITHUB_RELEASE_BUCKET_NAME
171172
)
173+
172174
if args.dry_run:
173175
print("Dry Run ...")
174176
print("This release would be created:")
@@ -184,9 +186,16 @@ def main() -> None:
184186
write_to_release_id_file(f"{release_id}")
185187
LOGGER.info(f"Release created with ID: {release_id}")
186188
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-
)
189+
release = Release.get(args.release_id, repo=args.repo, owner=args.owner)
190+
191+
if args.dry_run:
192+
print("Dry Run ...")
193+
194+
print(
195+
f"The file {args.file_path} would be uploaded for release: {release.name}"
196+
)
197+
else:
198+
release.upload_asset(args.file_path, args.overwrite_same_name)
190199
else:
191200
parser.print_help()
192201

0 commit comments

Comments
 (0)