close-stale-issues.sh 1.7 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #!/usr/bin/env bash
  2. # 关闭过时的自动升级失败 Issue(升级成功后,旧版本的失败记录均已过时)
  3. #
  4. # Close stale auto-upgrade failure Issues (after a successful upgrade, older failure records are all stale)
  5. # Usage: close-stale-issues.sh
  6. #
  7. # 测试注入:设置 GH=/path/to/mock 可替换 gh 命令(用于单测)
  8. #
  9. # Test injection: set GH=/path/to/mock to substitute the gh command (used by unit tests)
  10. set -euo pipefail
  11. GH_BIN="${GH:-gh}"
  12. # GitHub Actions 兜底:CI 中 gh 必须有 GH_TOKEN,否则 exit 4;脚本内回退 GITHUB_TOKEN,
  13. # 调用方显式传入的 GH_TOKEN 仍优先(与 open-issue.sh / push-upgrade-pr.sh 同一双层防御)
  14. # 回归背景:本步骤曾漏配 env 且脚本无兜底,gh 报错被 2>/dev/null 吞掉 → 静默「无过时的失败 Issue」
  15. #
  16. # GitHub Actions fallback: gh needs GH_TOKEN in CI or exits 4; fall back to GITHUB_TOKEN here.
  17. # An explicit GH_TOKEN still wins (same belt-and-suspenders as open-issue.sh / push-upgrade-pr.sh).
  18. # Regression: this step once lacked env and fallback, so gh's error was swallowed by 2>/dev/null
  19. # and stale Issues were silently never closed
  20. GH_TOKEN="${GH_TOKEN:-${GITHUB_TOKEN:-}}"
  21. export GH_TOKEN
  22. JSON="$("$GH_BIN" issue list --state open --search 'in:title "[自动升级]"' --json title,number --limit 50 2>/dev/null || true)"
  23. [ -n "$JSON" ] || { echo "无过时的失败 Issue"; exit 0; }
  24. # 本次升级已成功:所有自动升级失败 Issue 均视为过时,全部关闭
  25. #
  26. # This upgrade succeeded: every auto-upgrade failure Issue is now stale; close them all
  27. STALE="$(jq -r '.[] | .number' <<<"$JSON")"
  28. for n in $STALE; do
  29. echo "关闭过时 Issue #$n"
  30. "$GH_BIN" issue close "$n"
  31. done
  32. [ -n "$STALE" ] || echo "无过时的失败 Issue"