Skip to content

Commit a1b9f34

Browse files
Merge pull request #84 from MikroElektronika/fix/release-to-latest
Fix/release to latest
2 parents 519d16e + b068036 commit a1b9f34

File tree

4 files changed

+60
-22
lines changed

4 files changed

+60
-22
lines changed

.github/workflows/boardCardReleaseLive.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ jobs:
7070
- name: Update Changelogs
7171
id: changelog_update
7272
run: |
73-
python -u scripts/update_board_changelog.py
73+
python -u scripts/update_board_changelog.py ${{ secrets.GITHUB_TOKEN }} ${{ github.repository }}
7474
sdk_tag=$(<sdk_tag.txt)
7575
rm sdk_tag.txt
7676
echo "SDK Tag name is mikroSDK-$sdk_tag"

.github/workflows/boardCardReleaseTest.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ jobs:
7373
- name: Update Changelogs
7474
id: changelog_update
7575
run: |
76-
python -u scripts/update_board_changelog.py
76+
python -u scripts/update_board_changelog.py ${{ secrets.GITHUB_TOKEN }} ${{ github.repository }}
7777
sdk_tag=$(<sdk_tag.txt)
7878
rm sdk_tag.txt
7979
echo "SDK Tag name is mikroSDK-$sdk_tag"

scripts/index.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ def index_release_to_elasticsearch(es : Elasticsearch, index_name, release_detai
200200
## 0 is new one being indexed, 1 in previously indexed release
201201
if 'mikrosdk' in metadata_content[0]:
202202
version = metadata_content[0]['mikrosdk']['version']
203+
version_index = check_from_index_version(es, index_name, 'mikrosdk')
203204
else:
204205
for asset in release_details[0].get('assets', []):
205206
if 'mikrosdk.7z' == asset['name']:
@@ -212,6 +213,7 @@ def index_release_to_elasticsearch(es : Elasticsearch, index_name, release_detai
212213

213214
# Then fetch version from manifest file
214215
version = support.fetch_version_from_asset(os.path.join(os.path.dirname(__file__), 'tmp'))
216+
version_index = check_from_index_version(es, index_name, 'mikrosdk')
215217
break
216218

217219
import urllib.request
@@ -270,7 +272,7 @@ def index_release_to_elasticsearch(es : Elasticsearch, index_name, release_detai
270272
'category': 'Software Development Kit',
271273
'download_link': asset['url'], # Adjust as needed for actual URL
272274
"install_location" : "%APPLICATION_DATA_DIR%/packages/sdk",
273-
'package_changed': version != version
275+
'package_changed': version != version_index
274276
}
275277
elif 'templates' == name_without_extension:
276278
package_changed = True

scripts/update_board_changelog.py

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
1-
import os
1+
import os, requests, argparse
2+
import support as support
3+
24
from datetime import datetime
35
from packaging.version import Version
46

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+
519
def write_output_to_file(file, content):
620
with open(file, 'w') as file_write:
721
file_write.write(content)
@@ -13,21 +27,43 @@ def find_file(root_folder, filename):
1327
return os.path.join(dirpath, filename), dirpath
1428
return None, None
1529

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

Comments
 (0)