diff --git a/.github/sync.py b/.github/sync.py index 02c5cd9..32084ea 100644 --- a/.github/sync.py +++ b/.github/sync.py @@ -31,6 +31,40 @@ def get_github_latest_release(): print("No releases found.") +def delete_gitee_releases(latest_id, client, uri, token): + get_data = { + 'access_token': token + } + + release_info = [] + release_response = client.get(uri, json=get_data) + if release_response.status_code == 200: + release_info = release_response.json() + else: + print( + f"Request failed with status code {release_response.status_code}") + + release_ids = [] + for block in release_info: + if 'id' in block: + release_ids.append(block['id']) + + print(f'Current release ids: {release_ids}') + release_ids.remove(latest_id) + + for id in release_ids: + release_uri = f"{uri}/{id}" + delete_data = { + 'access_token': access_token + } + delete_response = client.delete(release_uri, json=delete_data) + if delete_response.status_code == 204: + print(f'Successfully deleted release #{id}.') + else: + raise ValueError( + f"Request failed with status code {release_api_response.status_code}") + + def sync_to_gitee(tag: str, body: str, files: slice): release_id = "" owner = "naibahq" @@ -78,6 +112,13 @@ def sync_to_gitee(tag: str, body: str, files: slice): print( f"Request failed with status code {asset_api_response.status_code}") + # 仅保留最新 Release 以防超出 Gitee 仓库配额 + try: + delete_gitee_releases(release_id, api_client, + release_api_uri, access_token) + except ValueError as e: + print(e) + api_client.close() print("Sync is completed!")