修 Gitee 附件清理:assets 字段没 id,要用 attach_files 接口

Gitee 的坑:GET /releases/{id} 返回的 assets 里
**只有 name 和 download_url,没有 id**,
照着它删是删不掉的(之前静默失败,附件一路堆到 11 个)。

必须换接口才拿得到 id:
    GET    /releases/{id}/attach_files        -> [{id, name, size}]
    DELETE /releases/{id}/attach_files/{id}

已清理:Gitee 11 -> 5 个(含 2 个自动生成的源码包),
EzHub 5 -> 3 个。清理动作也加了重试(网络会抖)。
This commit is contained in:
xmz7891
2026-10-07 12:54:33 +08:00
parent ae7e1cf269
commit 9692ef1f32
3 changed files with 254 additions and 12 deletions
+26 -12
View File
@@ -133,27 +133,41 @@ def gitee_release(token: str, version: str, body: str,
return False
# 复用已有发行版时先把同名旧附件删掉,否则每跑一次多一份。
#
# ⚠ Gitee 的坑:`GET /releases/{id}` 返回的 `assets` **只有 name 和
# download_url,没有 id**,照着它删是删不掉的。
# 必须换成单独这个接口才带 id:
# GET /releases/{id}/attach_files -> [{id, name, size, ...}]
# 删也是:DELETE /releases/{id}/attach_files/{attach_id}
try:
with urllib.request.urlopen(
"%s/releases/%s?access_token=%s" % (api, rid, token),
timeout=60) as resp:
existing = json.loads(resp.read().decode("utf-8", "replace"))
"%s/releases/%s/attach_files?access_token=%s"
% (api, rid, token), timeout=90) as resp:
old_assets = json.loads(resp.read().decode("utf-8", "replace"))
if not isinstance(old_assets, list):
old_assets = []
wanted = {p.name for p in files}
# Gitee 叫 attach_files,Gitea 叫 assets
old_assets = (existing.get("assets")
or existing.get("attach_files") or [])
for asset in old_assets:
name = asset.get("name") or asset.get("filename")
name = asset.get("name")
aid = asset.get("id")
if name in wanted and aid:
del_url = ("%s/releases/%s/attach_files/%s"
"?access_token=%s" % (api, rid, aid, token))
dreq = urllib.request.Request(del_url, method="DELETE")
try:
urllib.request.urlopen(dreq, timeout=60)
print(" • 删掉同名旧附件 %s" % name)
except urllib.error.HTTPError as exc:
print(" • 删 %s 失败 HTTP %s(继续)" % (name, exc.code))
for attempt in range(1, 4):
try:
urllib.request.urlopen(dreq, timeout=90)
print(" • 删掉同名旧附件 %s" % name)
break
except urllib.error.HTTPError as exc:
print(" • 删 %s 失败 HTTP %s(继续)"
% (name, exc.code))
break
except Exception: # noqa: BLE001
if attempt == 3:
print(" • 删 %s 网络失败,跳过" % name)
else:
time.sleep(4)
except Exception as exc: # noqa: BLE001
print(" • 清理旧附件时出错(继续): %s" % exc)