Skip to content

Commit 989e521

Browse files
committed
Update.
1 parent 431e9bc commit 989e521

File tree

2 files changed

+63
-10
lines changed

2 files changed

+63
-10
lines changed

.github/sync.py

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def get_github_latest_release():
2828
print(f"Latest release tag is: {release.tag_name}")
2929
print(f"Latest release info is: {release.body}")
3030
files = []
31+
asset_links = {}
3132
for asset in release.get_assets():
3233
url = asset.browser_download_url
3334
name = asset.name
@@ -52,7 +53,8 @@ def get_github_latest_release():
5253
raise RuntimeError(f"Failed to download {name} after {MAX_DOWNLOAD_RETRIES} attempts")
5354
file_abs_path = get_abs_path(asset.name)
5455
files.append(file_abs_path)
55-
sync_to_gitee(release.tag_name, release.body, files)
56+
asset_links[name] = url
57+
sync_to_gitee(release.tag_name, release.body, files, asset_links)
5658
else:
5759
print("No releases found.")
5860

@@ -90,7 +92,7 @@ def delete_gitee_releases(latest_id, client, uri, token):
9092
raise ValueError(f"Delete release #{rid} failed: {delete_response.status_code} {delete_response.text}")
9193

9294

93-
def sync_to_gitee(tag: str, body: str, files: slice):
95+
def sync_to_gitee(tag: str, body: str, files: slice, asset_links=None):
9496
release_id = ""
9597
owner = os.environ.get('GITEE_OWNER', 'Ten')
9698
repo = os.environ.get('GITEE_REPO', 'ServerStatus')
@@ -158,6 +160,17 @@ def sync_to_gitee(tag: str, body: str, files: slice):
158160

159161
# 附件上传回退开关与出错是否继续
160162
allow_partial = os.environ.get('SYNC_CONTINUE_ON_UPLOAD_ERROR', 'false').lower() == 'true'
163+
# 链接模式:跳过上传,直接把 GitHub 资产链接写入 release body
164+
link_only = os.environ.get('SYNC_LINK_ONLY', 'false').lower() == 'true'
165+
try:
166+
link_threshold_mb = float(os.environ.get('SYNC_LINK_THRESHOLD_MB', '16'))
167+
except ValueError:
168+
link_threshold_mb = 16.0
169+
170+
if asset_links is None:
171+
asset_links = {}
172+
173+
body_additions = []
161174

162175
for file_path in files:
163176
file_name = os.path.basename(file_path)
@@ -168,6 +181,22 @@ def sync_to_gitee(tag: str, body: str, files: slice):
168181
pass
169182
success = False
170183
for attempt in range(1, MAX_UPLOAD_RETRIES + 1):
184+
# 若启用链接模式或超过阈值,直接跳过上传改为追加链接
185+
try:
186+
size_bytes = os.path.getsize(file_path)
187+
except OSError:
188+
size_bytes = 0
189+
if link_only or (link_threshold_mb > 0 and size_bytes >= link_threshold_mb * 1024 * 1024):
190+
url = asset_links.get(file_name)
191+
if url:
192+
line = f"- {file_name}: {url}"
193+
if line not in body_additions:
194+
body_additions.append(line)
195+
print(f"Link-only mode: skip upload and add link for {file_name}")
196+
success = True
197+
break
198+
else:
199+
print(f"Link-only mode requested but no URL for {file_name}, will attempt upload.")
171200
try:
172201
# 如果已存在同名资产,则跳过上传
173202
if file_name in existing_assets:
@@ -231,7 +260,7 @@ def sync_to_gitee(tag: str, body: str, files: slice):
231260
time.sleep(min(60, 2 ** (attempt - 1)))
232261
if not success:
233262
print(f"Primary upload failed for {file_name} after {MAX_UPLOAD_RETRIES} attempts, trying attachments fallback...")
234-
# 回退:通过 attachments 接口上传,并把链接附加到 release body
263+
# 回退:通过 attachments 接口上传,并把链接收集,末尾统一追加到 release body
235264
attach_uri = f"https://gitee.com/api/v5/repos/{owner}/{repo}/attachments"
236265
try:
237266
with open(file_path, 'rb') as fh:
@@ -245,13 +274,11 @@ def sync_to_gitee(tag: str, body: str, files: slice):
245274
aj = ar.json()
246275
url = aj.get('url') or aj.get('browser_download_url') or aj.get('html_url')
247276
if url:
248-
new_body = (body or '') + f"\n\n- {file_name}: {url}"
249-
ur = api_client.patch(f"{release_api_uri}/{release_id}", data={'access_token': access_token, 'body': new_body}, timeout=REQUEST_TIMEOUT)
250-
if ur.status_code in (200, 201):
251-
print(f"Fallback: attached {file_name} via attachments, link added to release body.")
252-
success = True
253-
else:
254-
print(f"Fallback: update release body failed: {ur.status_code} {ur.text}")
277+
line = f"- {file_name}: {url}"
278+
if line not in body_additions:
279+
body_additions.append(line)
280+
print(f"Fallback: attached {file_name} via attachments, link collected to append.")
281+
success = True
255282
else:
256283
print(f"Fallback: attachments API did not return a URL. Resp: {aj}")
257284
else:
@@ -266,6 +293,26 @@ def sync_to_gitee(tag: str, body: str, files: slice):
266293
else:
267294
raise RuntimeError(msg)
268295

296+
# 若收集到需要追加的链接,统一更新 release body 一次
297+
if body_additions:
298+
combined = (body or '').rstrip() + "\n\n" + "\n".join(body_additions)
299+
ur = api_client.patch(
300+
f"{release_api_uri}/{release_id}",
301+
data={
302+
'access_token': access_token,
303+
'body': combined,
304+
# Gitee 对 PATCH 可能要求带上这些字段
305+
'tag_name': tag,
306+
'name': tag,
307+
},
308+
timeout=REQUEST_TIMEOUT,
309+
)
310+
if ur.status_code in (200, 201):
311+
print("Release body updated with asset links.")
312+
body = combined
313+
else:
314+
print(f"Update release body failed: {ur.status_code} {ur.text}")
315+
269316
# 仅保留最新 Release 以防超出 Gitee 仓库配额
270317
try:
271318
delete_gitee_releases(release_id, api_client,

.github/workflows/sync-release.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ jobs:
1010
env:
1111
GITEE_TOKEN: ${{ secrets.GITEE_TOKEN }}
1212
GH_TOKEN: ${{ github.token }}
13+
# 临时:跳过大文件上传,直接把 GitHub 资产链接追加到 Gitee Release 说明
14+
SYNC_LINK_ONLY: true
15+
# 若仅想跳过大于阈值(MiB)的文件,可改用如下设置(默认16):
16+
# SYNC_LINK_THRESHOLD_MB: '16'
17+
# 如需不中断整个任务,即便单个上传失败:
18+
# SYNC_CONTINUE_ON_UPLOAD_ERROR: true
1319
steps:
1420
- uses: actions/checkout@v4
1521
- name: Sync to Gitee

0 commit comments

Comments
 (0)