Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: Installs the given GardenLinux Python library
inputs:
version:
description: GardenLinux Python library version
default: "0.10.16"
default: "0.10.20"
python_version:
description: Python version to setup
default: "3.13"
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "gardenlinux"
version = "0.10.16"
version = "0.10.20"
description = "Contains tools to work with the features directory of gardenlinux, for example deducting dependencies from feature sets or validating cnames"
authors = ["Garden Linux Maintainers <contact@gardenlinux.io>"]
license = "Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion src/gardenlinux/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@

RELEASE_ID_FILE = ".github_release_id"

REQUESTS_TIMEOUTS = (5, 30) # connect, read
REQUESTS_TIMEOUTS = (5, 60) # connect, read

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

Expand Down
92 changes: 2 additions & 90 deletions src/gardenlinux/github/release/__init__.py
Original file line number Diff line number Diff line change
@@ -1,58 +1,13 @@
import json
import logging
import os
import sys

import requests

from ...constants import RELEASE_ID_FILE, REQUESTS_TIMEOUTS
from ...constants import RELEASE_ID_FILE
from ...logger import LoggerSetup
from .release import Release

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


def create_github_release(
owner: str, repo: str, tag: str, commitish: str, latest: bool, body: str
) -> int | None:
token = os.environ.get("GITHUB_TOKEN")
if not token:
raise ValueError("GITHUB_TOKEN environment variable not set")

headers = {
"Authorization": f"token {token}",
"Accept": "application/vnd.github.v3+json",
}

data = {
"tag_name": tag,
"target_commitish": commitish,
"name": tag,
"body": body,
"draft": False,
"prerelease": False,
"make_latest": "true" if latest else "false",
}

response = requests.post(
f"https://api.github.com/repos/{owner}/{repo}/releases",
headers=headers,
data=json.dumps(data),
timeout=REQUESTS_TIMEOUTS,
)

if response.status_code == 201:
LOGGER.info("Release created successfully")
response_json = response.json()
return int(response_json.get("id")) # Will raise KeyError if missing
else:
LOGGER.error("Failed to create release")
LOGGER.debug(response.json())
response.raise_for_status()

return None # Simply to make mypy happy. should not be reached.


def write_to_release_id_file(release_id: str | int) -> None:
try:
with open(RELEASE_ID_FILE, "w") as file:
Expand All @@ -63,47 +18,4 @@ def write_to_release_id_file(release_id: str | int) -> None:
sys.exit(1)


def upload_to_github_release_page(
github_owner: str,
github_repo: str,
gardenlinux_release_id: str | int,
file_to_upload: str,
dry_run: bool,
) -> None:
if dry_run:
LOGGER.info(
f"Dry run: would upload {file_to_upload} to release {gardenlinux_release_id} in repo {github_owner}/{github_repo}"
)
return

token = os.environ.get("GITHUB_TOKEN")
if not token:
raise ValueError("GITHUB_TOKEN environment variable not set")

headers = {
"Authorization": f"token {token}",
"Content-Type": "application/octet-stream",
}

upload_url = f"https://uploads.github.com/repos/{github_owner}/{github_repo}/releases/{gardenlinux_release_id}/assets?name={os.path.basename(file_to_upload)}"

try:
with open(file_to_upload, "rb") as f:
file_contents = f.read()
except IOError as e:
LOGGER.error(f"Error reading file {file_to_upload}: {e}")
return

response = requests.post(
upload_url, headers=headers, data=file_contents, timeout=REQUESTS_TIMEOUTS
)
if response.status_code == 201:
LOGGER.info("Upload successful")
else:
LOGGER.error(
f"Upload failed with status code {response.status_code}: {response.text}"
)
response.raise_for_status()


__all__ = ["Release", "write_to_release_id_file", "upload_to_github_release_page"]
__all__ = ["Release", "write_to_release_id_file"]
23 changes: 19 additions & 4 deletions src/gardenlinux/github/release/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from ..release_notes import create_github_release_notes
from . import (
upload_to_github_release_page,
write_to_release_id_file,
)
from .release import Release
Expand Down Expand Up @@ -132,6 +131,7 @@ def get_parser() -> argparse.ArgumentParser:

upload_parser.add_argument(
"--release_id",
type=int,
required=True,
help="GitHub release ID to upload the file to (required).",
)
Expand All @@ -149,6 +149,13 @@ def get_parser() -> argparse.ArgumentParser:
help="Perform a dry run without actually uploading the file.",
)

upload_parser.add_argument(
"--overwrite-same-name",
action="store_true",
default=False,
help="Overwrite assets with the same name.",
)

return parser


Expand All @@ -169,6 +176,7 @@ def main() -> None:
body = create_github_release_notes(
args.tag, args.commit, GARDENLINUX_GITHUB_RELEASE_BUCKET_NAME
)

if args.dry_run:
print("Dry Run ...")
print("This release would be created:")
Expand All @@ -184,9 +192,16 @@ def main() -> None:
write_to_release_id_file(f"{release_id}")
LOGGER.info(f"Release created with ID: {release_id}")
elif args.command == "upload":
upload_to_github_release_page(
args.owner, args.repo, args.release_id, args.file_path, args.dry_run
)
release = Release.get(args.release_id, repo=args.repo, owner=args.owner)

if args.dry_run:
print("Dry Run ...")

print(
f"The file {args.file_path} would be uploaded for release: {release.name}"
)
else:
release.upload_asset(args.file_path, args.overwrite_same_name)
else:
parser.print_help()

Expand Down
Loading
Loading