1
- import os
1
+ import os , requests , argparse
2
+ import support as support
3
+
2
4
from datetime import datetime
3
5
from packaging .version import Version
4
6
7
+ # Gets latest release headers from repository
8
+ def get_headers (api , token ):
9
+ if api :
10
+ return {
11
+ 'Authorization' : f'token { token } '
12
+ }
13
+ else :
14
+ return {
15
+ 'Authorization' : f'Bearer { token } ' ,
16
+ 'Accept' : 'application/octet-stream'
17
+ }
18
+
5
19
def write_output_to_file (file , content ):
6
20
with open (file , 'w' ) as file_write :
7
21
file_write .write (content )
@@ -13,21 +27,43 @@ def find_file(root_folder, filename):
13
27
return os .path .join (dirpath , filename ), dirpath
14
28
return None , None
15
29
16
- found_file , file_dir = find_file (os .path .join (os .getcwd (), "changelog" ), "new_hw.md" )
17
-
18
- if found_file :
19
- current_date = datetime .now ().strftime ("%Y-%m-%d" )
20
- os .rename (found_file , os .path .join (file_dir , f'{ current_date } .md' ))
21
- with open (os .path .join (file_dir , f'{ current_date } .md' ), 'r' ) as hw_changelog_file :
22
- board_changelog = hw_changelog_file .read ()
23
- hw_changelog_file .close ()
24
- board_changelog = board_changelog .replace ('`DATE`' , current_date ).replace ('#date' , f'#{ current_date } ' )
25
- with open (os .path .join (file_dir , f'{ current_date } .md' ), 'w' ) as hw_changelog_file :
26
- hw_changelog_file .write (board_changelog )
27
- hw_changelog_file .close ()
28
-
29
- write_output_to_file (os .path .join (os .getcwd (), 'sdk_tag.txt' ), file_dir .split (os .path .sep )[- 2 ][1 :])
30
- else :
31
- # Extract and sort versions, removing the 'v' prefix
32
- latest_version = max (os .listdir (os .path .join (os .getcwd (), 'changelog' )), key = lambda v : Version (v .lstrip ('v' ))).lstrip ('v' )
33
- write_output_to_file (os .path .join (os .getcwd (), 'sdk_tag.txt' ), latest_version )
30
+ def fetch_latest_release_version (repo , token ):
31
+ api_headers = get_headers (True , token )
32
+ url = f'https://api.github.com/repos/{ repo } /releases'
33
+ response = requests .get (url , headers = api_headers )
34
+ response .raise_for_status () # Raise an exception for HTTP errors
35
+ return support .get_latest_release (response .json ())
36
+
37
+ def edit_changelog (version ):
38
+ found_file , file_dir = find_file (os .path .join (os .getcwd (), "changelog" ), "new_hw.md" )
39
+
40
+ if found_file :
41
+ current_date = datetime .now ().strftime ("%Y-%m-%d" )
42
+ os .rename (found_file , os .path .join (file_dir , f'{ current_date } .md' ))
43
+ with open (os .path .join (file_dir , f'{ current_date } .md' ), 'r' ) as hw_changelog_file :
44
+ board_changelog = hw_changelog_file .read ()
45
+ hw_changelog_file .close ()
46
+ board_changelog = board_changelog .replace ('`DATE`' , current_date ).replace ('#date' , f'#{ current_date } ' )
47
+ with open (os .path .join (file_dir , f'{ current_date } .md' ), 'w' ) as hw_changelog_file :
48
+ hw_changelog_file .write (board_changelog )
49
+ hw_changelog_file .close ()
50
+
51
+ os .makedirs (os .path .join (file_dir , f'v{ version } ' , 'new_hw' ), exist_ok = True )
52
+ os .rename (os .path .join (file_dir , f'{ current_date } .md' ), os .path .join (file_dir , f'v{ version } ' , f'new_hw/{ current_date } .md' ))
53
+
54
+ write_output_to_file (os .path .join (os .getcwd (), 'sdk_tag.txt' ), version )
55
+ else :
56
+ # Extract and sort versions, removing the 'v' prefix
57
+ # Take the ighest version present in the repo itself, not github
58
+ latest_version = max (os .listdir (os .path .join (os .getcwd (), 'changelog' )), key = lambda v : Version (v .lstrip ('v' ))).lstrip ('v' )
59
+ write_output_to_file (os .path .join (os .getcwd (), 'sdk_tag.txt' ), latest_version )
60
+
61
+ if __name__ == '__main__' :
62
+ # Get arguments
63
+ parser = argparse .ArgumentParser (description = "Upload directories as release assets." )
64
+ parser .add_argument ("token" , type = str , help = "GitHub Token" )
65
+ parser .add_argument ("repo" , type = str , help = "Repository name, e.g., 'username/repo'" )
66
+ args = parser .parse_args ()
67
+
68
+ release = fetch_latest_release_version (args .repo , args .token )
69
+ edit_changelog (release ['tag_name' ].replace ('mikroSDK-' , '' ))
0 commit comments