#!/usr/bin/env python3 """CI 用: 维护 [待推VPS] issue 信号(Gitea 无 approval gate, 用 issue open/close 当黄/绿)。 被 ci-cd.yml 的 nas-verify 与 vps-deploy.yml 复用: - nas-verify 后: VPS_LOG=vps-deployed..HEAD 的 [vps] commit → 有则开/更新待推 issue - vps-deploy 成功后(已打 vps-deployed=HEAD): VPS_LOG="" → 关闭所有待推 issue env: GTOKEN Gitea token(操作 issue) GITHUB_REPOSITORY sanguo/sanguo_vnpy_v2 GITHUB_SHA 本次 commit VPS_LOG 待推 commit 的 "git log --format='%h %s'" 输出(空=无待推) """ import os import json import urllib.request API_BASE = "http://192.168.2.154:3000/api/v1/repos/" def main(): token = os.environ["GTOKEN"] repo = os.environ["GITHUB_REPOSITORY"] sha = os.environ["GITHUB_SHA"] vps_log = os.environ.get("VPS_LOG", "").strip() api = API_BASE + repo def req(method, path, data=None): r = urllib.request.Request(api + path, method=method) r.add_header("Authorization", "token " + token) r.add_header("Content-Type", "application/json") if data is not None: r.data = json.dumps(data).encode() try: with urllib.request.urlopen(r, timeout=20) as resp: body = resp.read() return json.loads(body) if body else {} except Exception as e: print(f" API {method} {path} 失败: {e}") return {} issues = req("GET", "/issues?state=open&type=issues") or [] pending = [i for i in issues if str(i.get("title", "")).startswith("[待推VPS]")] if not vps_log: # 无待推 → 关闭所有待推 issue(gap 清空 = 绿) print("✅ 无待推 VPS commit (vps-deployed..HEAD 无 [vps])") for i in pending: req("PATCH", f"/issues/{i['number']}", {"state": "closed"}) print(f" 已关闭待推 issue #{i['number']} (gap 清空)") return n = len([l for l in vps_log.splitlines() if l.strip()]) print(f"⚠️ 待推 VPS: {n} 个 [vps] commit") print(vps_log) body = ( f"nas-verify 绿 @{sha[:7]}, 以下 [vps] commit 待推 VPS:\n" f"```\n{vps_log}\n```\n\n" f"推法: Gitea → Actions → vps-deploy.yml → Run workflow, sha={sha}\n" f"agent: dispatch vps-deploy.yml inputs={{sha:{sha}}}" ) if pending: for i in pending: req("POST", f"/issues/{i['number']}/comments", {"body": body}) print(f" 已更新待推 issue #{i['number']}") else: res = req("POST", "/issues", {"title": f"[待推VPS] {n} 个 commit 待推生产", "body": body}) print(f" 已开待推 issue #{res.get('number', '?')} (open=黄/待推)") if __name__ == "__main__": main()