1- import json
21import logging
3- import os
42import sys
53
6- import requests
7-
8- from ...constants import RELEASE_ID_FILE , REQUESTS_TIMEOUTS
4+ from ...constants import RELEASE_ID_FILE
95from ...logger import LoggerSetup
106from .release import Release
117
128LOGGER = 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-
5611def 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" ]
0 commit comments