Skip to content

Commit 4b0fcb1

Browse files
committed
proper logging, upload command
1 parent 5b657d6 commit 4b0fcb1

File tree

1 file changed

+60
-14
lines changed

1 file changed

+60
-14
lines changed

src/gardenlinux/github/__main__.py

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def get_platform_release_note_data(metadata, platform):
9494
case 'openstackbaremetal':
9595
return _openstackbaremetal_release_note(metadata)
9696
case _:
97-
print(f"unknown platform {platform}")
97+
LOGGER.error(f"unknown platform {platform}")
9898
return None
9999

100100

@@ -532,7 +532,7 @@ def download_all_metadata_files(version, commitish):
532532
break
533533

534534
if not flavor_matches_variant:
535-
print(f"INFO: Skipping flavor {cname.cname} - not matching image variants filter")
535+
LOGGER.info(f"Skipping flavor {cname.cname} - not matching image variants filter")
536536
continue
537537

538538
try:
@@ -542,7 +542,7 @@ def download_all_metadata_files(version, commitish):
542542
commitish_short,
543543
local_dest_path)
544544
except IndexError:
545-
print(f"WARNING: No artifacts found for flavor {cname.cname}, skipping...")
545+
LOGGER.warn(f"No artifacts found for flavor {cname.cname}, skipping...")
546546
continue
547547

548548
return [str(artifact) for artifact in local_dest_path.iterdir()]
@@ -640,9 +640,9 @@ def release_notes_compare_package_versions_section(gardenlinux_version, package_
640640
output += "\n</details>\n\n"
641641

642642
except ValueError:
643-
print(f"Could not parse {gardenlinux_version} as the Garden Linux version, skipping version compare section")
643+
LOGGER.error(f"Could not parse {gardenlinux_version} as the Garden Linux version, skipping version compare section")
644644
else:
645-
print(f"Unexpected version number format {gardenlinux_version}, expected format (major is int).(patch is int)")
645+
LOGGER.error(f"Unexpected version number format {gardenlinux_version}, expected format (major is int).(patch is int)")
646646
return output
647647

648648

@@ -699,9 +699,9 @@ def write_to_release_id_file(release_id):
699699
try:
700700
with open('.github_release_id', 'w') as file:
701701
file.write(release_id)
702-
print("Created .github_release_id successfully.")
702+
LOGGER.info("Created .github_release_id successfully.")
703703
except IOError as e:
704-
print(f"Could not create .github_release_id file: {e}")
704+
LOGGER.error(f"Could not create .github_release_id file: {e}")
705705
sys.exit(1)
706706

707707

@@ -728,12 +728,44 @@ def create_github_release(owner, repo, tag, commitish, body):
728728
response = requests.post(f'https://api.github.com/repos/{owner}/{repo}/releases', headers=headers, data=json.dumps(data))
729729

730730
if response.status_code == 201:
731-
print("Release created successfully")
731+
LOGGER.info("Release created successfully")
732732
response_json = response.json()
733733
return response_json.get('id')
734734
else:
735-
print("Failed to create release")
736-
print(response.json())
735+
LOGGER.error("Failed to create release")
736+
LOGGER.debug(response.json())
737+
response.raise_for_status()
738+
739+
740+
def upload_to_github_release_page(github_owner, github_repo, gardenlinux_release_id, file_to_upload, dry_run):
741+
if dry_run:
742+
LOGGER.info(
743+
f"Dry run: would upload {file_to_upload} to release {gardenlinux_release_id} in repo {github_owner}/{github_repo}")
744+
return
745+
746+
token = os.environ.get('GITHUB_TOKEN')
747+
if not token:
748+
raise ValueError("GITHUB_TOKEN environment variable not set")
749+
750+
headers = {
751+
'Authorization': f'token {token}',
752+
'Content-Type': 'application/octet-stream'
753+
}
754+
755+
upload_url = f"https://uploads.github.com/repos/{github_owner}/{github_repo}/releases/{gardenlinux_release_id}/assets?name={os.path.basename(file_to_upload)}"
756+
757+
try:
758+
with open(file_to_upload, 'rb') as f:
759+
file_contents = f.read()
760+
except IOError as e:
761+
LOGGER.error(f"Error reading file {file_to_upload}: {e}")
762+
return
763+
764+
response = requests.post(upload_url, headers=headers, data=file_contents)
765+
if response.status_code == 201:
766+
LOGGER.info("Upload successful")
767+
else:
768+
LOGGER.error(f"Upload failed with status code {response.status_code}: {response.text}")
737769
response.raise_for_status()
738770

739771

@@ -747,16 +779,30 @@ def main():
747779
create_parser.add_argument('--tag', required=True)
748780
create_parser.add_argument('--commit', required=True)
749781
create_parser.add_argument('--dry-run', action='store_true', default=False)
782+
783+
upload_parser = subparsers.add_parser('upload')
784+
upload_parser.add_argument('--owner', default="gardenlinux")
785+
upload_parser.add_argument('--repo', default="gardenlinux")
786+
upload_parser.add_argument('--release_id', required=True)
787+
upload_parser.add_argument('--file_path', required=True)
788+
upload_parser.add_argument('--dry-run', action='store_true', default=False)
789+
750790
args = parser.parse_args()
751791

752792
if args.command == 'create':
753793
body = create_github_release_notes(args.tag, args.commit)
754-
if not args.dry_run:
794+
if args.dry_run:
795+
print(body)
796+
else:
755797
release_id = create_github_release(args.owner, args.repo, args.tag, args.commit, body)
756798
write_to_release_id_file(f"{release_id}")
757-
print(f"Release created with ID: {release_id}")
758-
else:
759-
print(body)
799+
LOGGER.info(f"Release created with ID: {release_id}")
800+
elif args.command == 'upload':
801+
upload_to_github_release_page(args.owner,
802+
args.repo,
803+
args.release_id,
804+
args.file_path,
805+
args.dry_run)
760806
else:
761807
parser.print_help()
762808

0 commit comments

Comments
 (0)