From ae7e1cf269939e1507fbf49197686635e1c2075c Mon Sep 17 00:00:00 2001 From: xmz7891 Date: Wed, 7 Oct 2026 12:37:06 +0800 Subject: [PATCH] =?UTF-8?q?Gitee=20=E4=B9=9F=E5=81=9A=E6=88=90=E5=B9=82?= =?UTF-8?q?=E7=AD=89=EF=BC=9A=E5=B7=B2=E5=AD=98=E5=9C=A8=E5=B0=B1=E5=A4=8D?= =?UTF-8?q?=E7=94=A8=20+=20=E5=85=88=E5=88=A0=E5=90=8C=E5=90=8D=E9=99=84?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 重发时 Gitee 直接 400「该标签已经存在发行版」, 而且拿不到 id,后面的附件也传不上。 改成和 EzHub 一样的流程: 先按标签查已有的(查到就复用 id),复用前删掉同名旧附件,再传。 这样重跑多少次,两边的附件数都稳定。 --- _build/release_all.py | 73 +++++++++++++++++++++++++++++++++---------- 1 file changed, 56 insertions(+), 17 deletions(-) diff --git a/_build/release_all.py b/_build/release_all.py index 4e97e83..a3bf2b9 100644 --- a/_build/release_all.py +++ b/_build/release_all.py @@ -99,24 +99,63 @@ def gitee_release(token: str, version: str, body: str, api = "https://gitee.com/api/v5/repos/xmz7891/ykxmz" tag = "v" + version - payload = json.dumps({ - "access_token": token, "tag_name": tag, - "name": "ykxmz " + version, "body": body, - "target_commitish": "main", "prerelease": False, - }).encode("utf-8") - req = urllib.request.Request( - api + "/releases", data=payload, method="POST", - headers={"Content-Type": "application/json;charset=UTF-8", - "User-Agent": "ykxmz-release"}) + # 先看标签有没有发行版。 + # 不先查的话,重发会直接 400「该标签已经存在发行版」—— + # 而且没有可复用的 id,后面的附件也就传不上。 + rid = None try: - with urllib.request.urlopen(req, timeout=90) as resp: - reply = json.loads(resp.read().decode("utf-8", "replace")) - rid = reply.get("id") - print(" ✓ 发行版已建 id=%s" % rid) - except urllib.error.HTTPError as exc: - detail = exc.read().decode("utf-8", "replace")[:300] - print(" ✗ 建发行版失败 HTTP %s: %s" % (exc.code, detail)) - return False + with urllib.request.urlopen( + "%s/releases/tags/%s?access_token=%s" % (api, tag, token), + timeout=60) as resp: + rid = json.loads(resp.read().decode("utf-8", "replace")).get("id") + print(" • 该标签已有发行版 id=%s,复用" % rid) + except urllib.error.HTTPError: + pass + + if not rid: + payload = json.dumps({ + "access_token": token, "tag_name": tag, + "name": "ykxmz " + version, "body": body, + "target_commitish": "main", "prerelease": False, + }).encode("utf-8") + req = urllib.request.Request( + api + "/releases", data=payload, method="POST", + headers={"Content-Type": "application/json;charset=UTF-8", + "User-Agent": "ykxmz-release"}) + try: + with urllib.request.urlopen(req, timeout=90) as resp: + reply = json.loads(resp.read().decode("utf-8", "replace")) + rid = reply.get("id") + print(" ✓ 发行版已建 id=%s" % rid) + except urllib.error.HTTPError as exc: + detail = exc.read().decode("utf-8", "replace")[:300] + print(" ✗ 建发行版失败 HTTP %s: %s" % (exc.code, detail)) + return False + + # 复用已有发行版时先把同名旧附件删掉,否则每跑一次多一份。 + 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")) + 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") + 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)) + except Exception as exc: # noqa: BLE001 + print(" • 清理旧附件时出错(继续): %s" % exc) ok = 0 for path in files: