ソースを参照

【修复,CI】门闩与清理脚本补齐 gh 凭据

check-issue-gate / check-pr-gate / close-stale-issues 三个脚本在 Actions 中
未设置 GH_TOKEN 时 gh 以 exit 4 报错,被 2>/dev/null 吞掉后门闩恒放行、
过时 Issue 永不关闭(实证:run 34487266068 成功建 PR 后未关闭 Issue #11)。

修复:脚本内回退 GITHUB_TOKEN(GH_TOKEN 优先,与 open-issue.sh 同双层防御),
workflow 三个步骤显式透传 GH_TOKEN;新增 3 条兜底单测(55/55 通过)。
PJ568 2 ヶ月 前
親
コミット
fbb4be5b6d

+ 15 - 0
.github/workflows/upstream-check.yml

@@ -73,6 +73,11 @@ jobs:
           echo "blocked=$BOOL" >> "$GITHUB_OUTPUT"
         env:
           LATEST: ${{ steps.check.outputs.latest }}
+          # GH_TOKEN 显式透传:gh 在 Actions 中需要凭据(脚本内亦有 GITHUB_TOKEN 兜底,双层防御)
+          #
+          # Pass GH_TOKEN explicitly: gh needs credentials in Actions
+          # (the script also falls back to GITHUB_TOKEN, belt-and-suspenders)
+          GH_TOKEN: ${{ github.token }}
 
       # 门闩:auto-upgrade/dsh 分支上已有同版本 open PR 时跳过(避免每日重复推送同版本提交)
       # 仅定时触发生效;手动触发(workflow_dispatch)绕过,用于人工强制刷新 PR
@@ -97,6 +102,11 @@ jobs:
           echo "blocked_pr=$BOOL" >> "$GITHUB_OUTPUT"
         env:
           LATEST: ${{ steps.check.outputs.latest }}
+          # GH_TOKEN 显式透传:gh 在 Actions 中需要凭据(脚本内亦有 GITHUB_TOKEN 兜底,双层防御)
+          #
+          # Pass GH_TOKEN explicitly: gh needs credentials in Actions
+          # (the script also falls back to GITHUB_TOKEN, belt-and-suspenders)
+          GH_TOKEN: ${{ github.token }}
 
       - name: 打印检查结果
         run: |
@@ -177,6 +187,11 @@ jobs:
           scripts/close-stale-issues.sh
         env:
           LATEST: ${{ needs.check.outputs.latest }}
+          # GH_TOKEN 显式透传:gh 在 Actions 中需要凭据(脚本内亦有 GITHUB_TOKEN 兜底,双层防御)
+          #
+          # Pass GH_TOKEN explicitly: gh needs credentials in Actions
+          # (the script also falls back to GITHUB_TOKEN, belt-and-suspenders)
+          GH_TOKEN: ${{ github.token }}
 
       - name: 失败时创建 Issue(同版本去重)
         if: failure()

+ 11 - 0
scripts/check-issue-gate.sh

@@ -14,6 +14,17 @@ set -euo pipefail
 VERSION="$1"
 GH_BIN="${GH:-gh}"
 
+# GitHub Actions 兜底:CI 中 gh 必须有 GH_TOKEN,否则 exit 4;脚本内回退 GITHUB_TOKEN,
+# 调用方显式传入的 GH_TOKEN 仍优先(与 open-issue.sh / push-upgrade-pr.sh 同一双层防御)
+# 回归背景:本步骤曾漏配 env 且脚本无兜底,gh 报错被 2>/dev/null 吞掉 → 门闩静默恒不拦截
+#
+# GitHub Actions fallback: gh needs GH_TOKEN in CI or exits 4; fall back to GITHUB_TOKEN here.
+# An explicit GH_TOKEN still wins (same belt-and-suspenders as open-issue.sh / push-upgrade-pr.sh).
+# Regression: this step once lacked env and fallback, so gh's error was swallowed by 2>/dev/null
+# and the gate silently never blocked
+GH_TOKEN="${GH_TOKEN:-${GITHUB_TOKEN:-}}"
+export GH_TOKEN
+
 # 列出未关闭的自动升级失败 Issue,标题含目标版本即命中
 # simp: gh 仅输出 JSON,匹配交给 jq(mock 易仿真);查询上限 50 条,按版本去重后远不会触及
 #

+ 11 - 0
scripts/check-pr-gate.sh

@@ -14,6 +14,17 @@ set -euo pipefail
 VERSION="$1"
 GH_BIN="${GH:-gh}"
 
+# GitHub Actions 兜底:CI 中 gh 必须有 GH_TOKEN,否则 exit 4;脚本内回退 GITHUB_TOKEN,
+# 调用方显式传入的 GH_TOKEN 仍优先(与 open-issue.sh / push-upgrade-pr.sh 同一双层防御)
+# 回归背景:本步骤曾漏配 env 且脚本无兜底,gh 报错被 2>/dev/null 吞掉 → 门闩静默恒不拦截
+#
+# GitHub Actions fallback: gh needs GH_TOKEN in CI or exits 4; fall back to GITHUB_TOKEN here.
+# An explicit GH_TOKEN still wins (same belt-and-suspenders as open-issue.sh / push-upgrade-pr.sh).
+# Regression: this step once lacked env and fallback, so gh's error was swallowed by 2>/dev/null
+# and the gate silently never blocked
+GH_TOKEN="${GH_TOKEN:-${GITHUB_TOKEN:-}}"
+export GH_TOKEN
+
 # 列出 auto-upgrade/dsh 分支上的 open PR,标题含目标版本即命中
 # simp: gh 仅输出 JSON,匹配交给 jq(mock 易仿真);查询上限 10 条,常驻单 PR 远不会触及
 #

+ 11 - 0
scripts/close-stale-issues.sh

@@ -11,6 +11,17 @@ set -euo pipefail
 
 GH_BIN="${GH:-gh}"
 
+# GitHub Actions 兜底:CI 中 gh 必须有 GH_TOKEN,否则 exit 4;脚本内回退 GITHUB_TOKEN,
+# 调用方显式传入的 GH_TOKEN 仍优先(与 open-issue.sh / push-upgrade-pr.sh 同一双层防御)
+# 回归背景:本步骤曾漏配 env 且脚本无兜底,gh 报错被 2>/dev/null 吞掉 → 静默「无过时的失败 Issue」
+#
+# GitHub Actions fallback: gh needs GH_TOKEN in CI or exits 4; fall back to GITHUB_TOKEN here.
+# An explicit GH_TOKEN still wins (same belt-and-suspenders as open-issue.sh / push-upgrade-pr.sh).
+# Regression: this step once lacked env and fallback, so gh's error was swallowed by 2>/dev/null
+# and stale Issues were silently never closed
+GH_TOKEN="${GH_TOKEN:-${GITHUB_TOKEN:-}}"
+export GH_TOKEN
+
 JSON="$("$GH_BIN" issue list --state open --search 'in:title "[自动升级]"' --json title,number --limit 50 2>/dev/null || true)"
 [ -n "$JSON" ] || { echo "无过时的失败 Issue"; exit 0; }
 

+ 30 - 0
tests/test-scripts.sh

@@ -202,6 +202,16 @@ MOCK_LOG="$TMP/mock6.log" MOCK_LIST_OUT='' GH="$MOCK_GH" \
   "$GATE" 0.2.0 > "$TMP/gate4.out" 2>&1
 assert_eq "gh 查询失败(空输出)不拦截" 0 "$(cat "$TMP/gate4.out")"
 
+# GitHub Actions 兜底:仅设 GITHUB_TOKEN 时应映射为 GH_TOKEN(回归:CI 步骤曾漏配 env 且脚本无兜底,
+# gh exit 4 被 2>/dev/null 吞掉 → 门闩静默恒不拦截)
+#
+# GitHub Actions fallback: GITHUB_TOKEN must be surfaced as GH_TOKEN (regression: the CI step
+# lacked env and the script had no fallback, so gh exit 4 was swallowed and the gate never blocked)
+GUARD_LOG="$TMP/mock_guard_gate.log" MOCK_LOG="$TMP/mock_guard_gate_gh.log" MOCK_LIST_OUT='[]' \
+  GH="$GH_TOKEN_GUARD" GH_REAL="$MOCK_GH" GITHUB_TOKEN=ghp_ci_fallback \
+  "$GATE" 0.2.0 > "$TMP/gate_guard.out" 2>&1
+assert_grep "gate:GITHUB_TOKEN 兜底为 GH_TOKEN" "GH_TOKEN=ghp_ci_fallback" "$TMP/mock_guard_gate.log"
+
 echo "== close-stale-issues.sh (mock gh)=="
 CLOSE="$ROOT/scripts/close-stale-issues.sh"
 CLOSE_LIST='[{"title":"[自动升级] dsh 上游 0.1.9 构建冒烟测试失败","number":9},{"title":"[自动升级] dsh 上游 0.2.0 构建冒烟测试失败","number":7}]'
@@ -218,6 +228,16 @@ MOCK_LOG="$TMP/mock8.log" MOCK_LIST_OUT='[]' GH="$MOCK_GH" \
 assert_grep "无 Issue 时提示" "无过时的失败 Issue" "$TMP/close2.out"
 assert_no_grep "无 Issue 时不调用 close" "issue close" "$TMP/mock8.log"
 
+# GitHub Actions 兜底:仅设 GITHUB_TOKEN 时应映射为 GH_TOKEN(回归:CI 步骤曾漏配 env 且脚本无兜底,
+# gh exit 4 被 2>/dev/null 吞掉 → 静默「无过时的失败 Issue」,Issue 永不关闭)
+#
+# GitHub Actions fallback: GITHUB_TOKEN must be surfaced as GH_TOKEN (regression: the CI step
+# lacked env and the script had no fallback, so gh exit 4 was swallowed and stale Issues were never closed)
+GUARD_LOG="$TMP/mock_guard_close.log" MOCK_LOG="$TMP/mock_guard_close_gh.log" MOCK_LIST_OUT='[]' \
+  GH="$GH_TOKEN_GUARD" GH_REAL="$MOCK_GH" GITHUB_TOKEN=ghp_ci_fallback \
+  "$CLOSE" > "$TMP/close_guard.out" 2>&1
+assert_grep "close:GITHUB_TOKEN 兜底为 GH_TOKEN" "GH_TOKEN=ghp_ci_fallback" "$TMP/mock_guard_close.log"
+
 echo "== check-pr-gate.sh (mock gh)=="
 PRGATE="$ROOT/scripts/check-pr-gate.sh"
 PRGATE_LIST='[{"title":"【维护,构建】升级 dsh 版本至 0.2.0","number":11}]'
@@ -247,6 +267,16 @@ MOCK_LOG="$TMP/prgate5.log" MOCK_LIST_OUT='' GH="$MOCK_GH" \
   "$PRGATE" 0.2.0 > "$TMP/prgate5.out" 2>&1
 assert_eq "gh 查询失败(空输出)不拦截" 0 "$(cat "$TMP/prgate5.out")"
 
+# GitHub Actions 兜底:仅设 GITHUB_TOKEN 时应映射为 GH_TOKEN(回归:CI 步骤曾漏配 env 且脚本无兜底,
+# gh exit 4 被 2>/dev/null 吞掉 → PR 门闩静默恒不拦截)
+#
+# GitHub Actions fallback: GITHUB_TOKEN must be surfaced as GH_TOKEN (regression: the CI step
+# lacked env and the script had no fallback, so gh exit 4 was swallowed and the PR gate never blocked)
+GUARD_LOG="$TMP/mock_guard_prgate.log" MOCK_LOG="$TMP/mock_guard_prgate_gh.log" MOCK_LIST_OUT='[]' \
+  GH="$GH_TOKEN_GUARD" GH_REAL="$MOCK_GH" GITHUB_TOKEN=ghp_ci_fallback \
+  "$PRGATE" 0.2.0 > "$TMP/prgate_guard.out" 2>&1
+assert_grep "prgate:GITHUB_TOKEN 兜底为 GH_TOKEN" "GH_TOKEN=ghp_ci_fallback" "$TMP/mock_guard_prgate.log"
+
 echo "== push-upgrade-pr.sh upsert_pr (mock gh)=="
 # source 载入函数(脚本入口有 BASH_SOURCE 守卫,不执行 main);
 # 需先设置 GH 再 source,使脚本顶部 GH_BIN 指向 mock