dsh-git-push 2.16.1 → 2.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/app/command-registry.js +2 -0
- package/lib/app/tool-call.js +4 -0
- package/lib/git/ci.js +135 -0
- package/lib/git/push.js +31 -3
- package/lib/self/index.js +1 -1
- package/package.json +1 -1
|
@@ -42,6 +42,8 @@ export const TOOL_REGISTRY = [
|
|
|
42
42
|
{ flag: 'auditExemptPaths', type: 'array', desc: '本次提交按文件临时豁免审计(数组,相对仓库根):所列文件的 finding 不参与拦截判定(含 blocker);仅本次有效,不落盘、不改 .auditignore' },
|
|
43
43
|
{ flag: 'auditExemptReason', type: 'string', desc: '豁免理由(可选,仅回显,不作拦截条件)' },
|
|
44
44
|
{ flag: 'remote', type: 'string', desc: '推送目标 remote(多 remote / fork 场景;缺省 origin)。推送与核验使用同一 remote' },
|
|
45
|
+
{ flag: 'waitCi', type: 'boolean', desc: '推送后等 CI 结果(默认不等):按推送的 sha 查 GitHub Actions,全绿=通过、任一失败=ciFailed、超时/无 CI 如实回报' },
|
|
46
|
+
{ flag: 'ciTimeoutSec', type: 'number', desc: '等 CI 的上限秒数(默认 900)' },
|
|
45
47
|
],
|
|
46
48
|
},
|
|
47
49
|
{
|
package/lib/app/tool-call.js
CHANGED
|
@@ -71,6 +71,10 @@ async function pushWithPostSteps(args, env, cfg) {
|
|
|
71
71
|
// 推送门禁——开启时 push 需显式放行(pushConfirmed:true);未放行返回 PUSH_GATE 拦截
|
|
72
72
|
pushGate: cfg.pushGate === true,
|
|
73
73
|
pushConfirmed: args.pushConfirmed === true,
|
|
74
|
+
// 推送后等 CI 结果(默认不等):按推到远端的那个 sha 查 Actions,
|
|
75
|
+
// 全绿=通过、任一失败=ciFailed、超时/无 CI 如实回报(都不改变推送本身成功的事实)
|
|
76
|
+
waitCi: args.waitCi === true || cfg.waitCi === true,
|
|
77
|
+
ciTimeoutSec: Number(args.ciTimeoutSec) > 0 ? Number(args.ciTimeoutSec) : 900,
|
|
74
78
|
// 精确 add 路径(逗号分隔,相对 repo);空 = git add -A 全量
|
|
75
79
|
paths: String(args.paths || ''),
|
|
76
80
|
});
|
package/lib/git/ci.js
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CI(GitHub Actions)结果等待 —— 推送后确认真跑绿了,而不是"推上去就算完成"。
|
|
3
|
+
*
|
|
4
|
+
* 为什么需要:推送成功只代表代码到了远端,构建/测试是否通过是另一回事。
|
|
5
|
+
* 实测事故:一次提交推送成功,CI 在同一 SHA 上挂了(我加的一个测试假设了 CIFS 特性,
|
|
6
|
+
* 在 ext4 runner 上必然失败)——只看推送结果完全发现不了。
|
|
7
|
+
*
|
|
8
|
+
* 设计取舍:
|
|
9
|
+
* · **只读**(GET /actions/runs),不触发、不重跑;
|
|
10
|
+
* · 按 **head_sha** 查,而不是查"最新一次 run"——否则并发提交会拿到别人的结果;
|
|
11
|
+
* · 多 workflow 同时存在时**全绿才算通过**(任一失败即 failure);
|
|
12
|
+
* · 刚推上去时 run 可能还没创建,给一段 grace(默认 60s)再判定「没有 CI」;
|
|
13
|
+
* · 超时不算失败、也不算通过,单独给 `timeout`,让调用方自己决定怎么处理。
|
|
14
|
+
*/
|
|
15
|
+
import { runGit } from './exec.js';
|
|
16
|
+
import { githubFetch } from './api.js';
|
|
17
|
+
import { parseOwnerRepoFromRemote } from './remote.js';
|
|
18
|
+
|
|
19
|
+
const DEFAULT_TIMEOUT_MS = 15 * 60 * 1000;
|
|
20
|
+
const DEFAULT_POLL_MS = 15 * 1000;
|
|
21
|
+
const DEFAULT_GRACE_MS = 60 * 1000;
|
|
22
|
+
|
|
23
|
+
/** 视为「通过」的结论集合(neutral/skipped 不算红)。 */
|
|
24
|
+
const OK_CONCLUSIONS = new Set(['success', 'neutral', 'skipped']);
|
|
25
|
+
|
|
26
|
+
/** 从本地仓库解析 owner/repo(origin 优先,其次任意 GitHub remote)。 */
|
|
27
|
+
export function resolveRepoIdentity(repoPath) {
|
|
28
|
+
const origin = runGit(['remote', 'get-url', 'origin'], { cwd: repoPath });
|
|
29
|
+
const fromOrigin = parseOwnerRepoFromRemote(origin.stdout || origin.stderr || '');
|
|
30
|
+
if (fromOrigin.owner && fromOrigin.repo) return fromOrigin;
|
|
31
|
+
const all = runGit(['remote', '-v'], { cwd: repoPath });
|
|
32
|
+
for (const line of String(all.stdout || '').split('\n')) {
|
|
33
|
+
const url = line.split(/\s+/)[1] || '';
|
|
34
|
+
const parsed = parseOwnerRepoFromRemote(url);
|
|
35
|
+
if (parsed.owner && parsed.repo) return parsed;
|
|
36
|
+
}
|
|
37
|
+
return { owner: '', repo: '' };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* 查某次提交触发的所有 workflow run。
|
|
42
|
+
* @returns {Promise<{ok:boolean, runs:Array<object>, error?:string}>}
|
|
43
|
+
*/
|
|
44
|
+
export async function fetchRunsForSha({ owner, repo, sha, token = '', fetchImpl = githubFetch, perPage = 50 } = {}) {
|
|
45
|
+
if (!owner || !repo) return { ok: false, runs: [], error: '缺 owner/repo' };
|
|
46
|
+
if (!sha) return { ok: false, runs: [], error: '缺 sha' };
|
|
47
|
+
const res = await fetchImpl(`/repos/${owner}/${repo}/actions/runs?head_sha=${encodeURIComponent(sha)}&per_page=${perPage}`, { token });
|
|
48
|
+
if (res.status !== 200) return { ok: false, runs: [], error: res.json?.message || res.error || `HTTP ${res.status}` };
|
|
49
|
+
const runs = (res.json?.workflow_runs || []).map((r) => ({
|
|
50
|
+
id: r.id,
|
|
51
|
+
name: r.name || r.display_title || `run ${r.id}`,
|
|
52
|
+
status: r.status || '',
|
|
53
|
+
conclusion: r.conclusion || '',
|
|
54
|
+
// html_url 是 GitHub 的原字段;测试注入的桩可能直接用 url —— 两者都认,避免因字段名不同丢链接
|
|
55
|
+
url: r.html_url || r.url || '',
|
|
56
|
+
event: r.event || '',
|
|
57
|
+
headSha: r.head_sha || '',
|
|
58
|
+
}));
|
|
59
|
+
return { ok: true, runs };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 汇总一组 run 的结论(纯函数,便于单测)。
|
|
64
|
+
* @returns {{total:number, pending:number, failing:number, ok:number,
|
|
65
|
+
* conclusion:'none'|'pending'|'success'|'failure', failingRuns:Array<object>}}
|
|
66
|
+
*/
|
|
67
|
+
export function summarizeRuns(runs = []) {
|
|
68
|
+
let pending = 0;
|
|
69
|
+
let failing = 0;
|
|
70
|
+
let ok = 0;
|
|
71
|
+
const failingRuns = [];
|
|
72
|
+
for (const r of runs) {
|
|
73
|
+
if (r.status && r.status !== 'completed') { pending += 1; continue; }
|
|
74
|
+
if (OK_CONCLUSIONS.has(String(r.conclusion || '').toLowerCase())) { ok += 1; continue; }
|
|
75
|
+
failing += 1;
|
|
76
|
+
failingRuns.push(r);
|
|
77
|
+
}
|
|
78
|
+
const conclusion = !runs.length ? 'none' : pending ? 'pending' : failing ? 'failure' : 'success';
|
|
79
|
+
return { total: runs.length, pending, failing, ok, conclusion, failingRuns };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* 等某次提交的 CI 出结果。
|
|
84
|
+
*
|
|
85
|
+
* @param {{repoPath?:string, owner?:string, repo?:string, sha:string, token?:string,
|
|
86
|
+
* timeoutMs?:number, pollMs?:number, graceMs?:number,
|
|
87
|
+
* fetchImpl?:Function, sleep?:Function, now?:Function}} o
|
|
88
|
+
* @returns {Promise<{ok:boolean, conclusion:'success'|'failure'|'pending'|'timeout'|'none',
|
|
89
|
+
* runs:Array<object>, summary:object, failing?:Array<object>, url?:string,
|
|
90
|
+
* timedOut?:boolean, note?:string, error?:string}>}
|
|
91
|
+
*/
|
|
92
|
+
export async function waitForCi({
|
|
93
|
+
repoPath = '', owner = '', repo = '', sha = '', token = '',
|
|
94
|
+
timeoutMs = DEFAULT_TIMEOUT_MS, pollMs = DEFAULT_POLL_MS, graceMs = DEFAULT_GRACE_MS,
|
|
95
|
+
fetchImpl = githubFetch, sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)), now = () => Date.now(),
|
|
96
|
+
} = {}) {
|
|
97
|
+
let own = { owner, repo };
|
|
98
|
+
if ((!own.owner || !own.repo) && repoPath) own = resolveRepoIdentity(repoPath);
|
|
99
|
+
if (!own.owner || !own.repo) return { ok: false, conclusion: 'none', runs: [], summary: summarizeRuns([]), error: '无法解析 owner/repo(origin 不是 GitHub 地址)' };
|
|
100
|
+
const started = now();
|
|
101
|
+
let lastSummary = summarizeRuns([]);
|
|
102
|
+
for (;;) {
|
|
103
|
+
const res = await fetchRunsForSha({ owner: own.owner, repo: own.repo, sha, token, fetchImpl });
|
|
104
|
+
if (!res.ok) return { ok: false, conclusion: 'none', runs: [], summary: lastSummary, error: res.error };
|
|
105
|
+
const summary = summarizeRuns(res.runs);
|
|
106
|
+
lastSummary = summary;
|
|
107
|
+
const elapsed = now() - started;
|
|
108
|
+
if (summary.conclusion === 'none') {
|
|
109
|
+
// run 还没创建:给一段 grace,超过就认为该提交没有 CI
|
|
110
|
+
if (elapsed >= graceMs) {
|
|
111
|
+
return { ok: true, conclusion: 'none', runs: res.runs, summary, note: `该提交未触发 CI(已等 ${Math.round(elapsed / 1000)}s)` };
|
|
112
|
+
}
|
|
113
|
+
await sleep(Math.min(pollMs, Math.max(0, graceMs - elapsed)));
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
if (summary.conclusion !== 'pending') {
|
|
117
|
+
return {
|
|
118
|
+
ok: summary.conclusion === 'success',
|
|
119
|
+
conclusion: summary.conclusion,
|
|
120
|
+
runs: res.runs,
|
|
121
|
+
summary,
|
|
122
|
+
...(summary.failing ? { failing: summary.failingRuns.map((r) => ({ name: r.name, url: r.url, conclusion: r.conclusion })) } : {}),
|
|
123
|
+
url: summary.failingRuns[0]?.url || res.runs[0]?.url || '',
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
if (elapsed >= timeoutMs) {
|
|
127
|
+
return {
|
|
128
|
+
ok: false, conclusion: 'timeout', timedOut: true, runs: res.runs, summary,
|
|
129
|
+
note: `CI 在 ${Math.round(timeoutMs / 1000)}s 内未出结果(仍在跑)`,
|
|
130
|
+
url: res.runs[0]?.url || '',
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
await sleep(Math.min(pollMs, Math.max(0, timeoutMs - elapsed)));
|
|
134
|
+
}
|
|
135
|
+
}
|
package/lib/git/push.js
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import { describeHooks, probeHooks } from './hooks.js';
|
|
10
|
+
import { waitForCi } from './ci.js';
|
|
10
11
|
import { existsSync } from 'node:fs';
|
|
11
12
|
import { join } from 'node:path';
|
|
12
13
|
import { autoTagDSHProject, ensureAuxSshRemote, readPkgVersion, updateRemoteTrackingRef } from './post-push.js';
|
|
@@ -55,7 +56,13 @@ import { resolveRepoVisibility, cachedVisibility } from './visibility.js';
|
|
|
55
56
|
* customIgnorePatterns:逗号/换行分隔的 gitignore pattern,追加进 .gitignore(幂等)
|
|
56
57
|
* @returns {{ok, steps: string[], commitSha?, pushed?, push?, remoteHeads?, error?}}
|
|
57
58
|
*/
|
|
58
|
-
export async function commitAndPush({
|
|
59
|
+
export async function commitAndPush({
|
|
60
|
+
repoPath = '', message = '', messageItems = null, push = true, dryRun = false, token = '',
|
|
61
|
+
customIgnorePatterns = '', requirementsConfirmed = false, force = false, pushMethod = 'ssh',
|
|
62
|
+
pushGate = false, pushConfirmed = false, paths = '', remote = '',
|
|
63
|
+
// 推送后是否等 CI 结果(默认不等:多数推送不需要阻塞);ciTimeoutSec 为等待上限
|
|
64
|
+
waitCi = false, ciTimeoutSec = 900,
|
|
65
|
+
} = {}) {
|
|
59
66
|
const steps = [];
|
|
60
67
|
if (!repoPath || !existsSync(join(repoPath, '.git'))) return { ok: false, steps: ['预检'], error: `非 git 仓库: ${repoPath || '(空)'}` };
|
|
61
68
|
// message 支持三种形态(字符串 / 数组 / {subject,items}):在预检处**单点**组装成规范提交信息,
|
|
@@ -211,6 +218,18 @@ export async function commitAndPush({ repoPath = '', message = '', messageItems
|
|
|
211
218
|
legacyIdentity,
|
|
212
219
|
};
|
|
213
220
|
}
|
|
221
|
+
/**
|
|
222
|
+
* 推送后按需等待 CI。用 commitSha(推到远端的那个提交)而不是"最新 run",
|
|
223
|
+
* 否则并发提交时会把别人的结果当自己的。超时/无 CI 都不算失败,只如实回报。
|
|
224
|
+
*/
|
|
225
|
+
const collectCi = async () => {
|
|
226
|
+
if (!waitCi) return null;
|
|
227
|
+
try {
|
|
228
|
+
return await waitForCi({ repoPath, sha: commitSha, token, timeoutMs: Math.max(30, Number(ciTimeoutSec) || 900) * 1000 });
|
|
229
|
+
} catch (e) {
|
|
230
|
+
return { ok: false, conclusion: 'none', error: `等待 CI 出错:${String(e?.message || e).slice(0, 200)}`, runs: [], summary: { total: 0 } };
|
|
231
|
+
}
|
|
232
|
+
};
|
|
214
233
|
steps.push('push');
|
|
215
234
|
// 通道由 dispatchPush 单点决策(默认 SSH:推本地 HEAD、远端 sha 与本地一致;无密钥才回落 API)。
|
|
216
235
|
// ⚠️ dispatchPush 是 async:必须 await(旧 bug:同步调用导致 pr.ok 恒 undefined,推送从未成功)
|
|
@@ -219,7 +238,10 @@ export async function commitAndPush({ repoPath = '', message = '', messageItems
|
|
|
219
238
|
// D24:pr.pushed=false(内容级短路「远端已是最新」)→ 如实 pushed:false(commitPushDoPush 语义)
|
|
220
239
|
// committed 必须如实带上:cleanSkipCommit 时本次**没有产生新提交**。
|
|
221
240
|
// 此前这条路径缺 committed 字段,job 输出就兜底成「已提交,未推送」——与实际相反 ✗(实测复现)
|
|
222
|
-
if (!pr.pushed)
|
|
241
|
+
if (!pr.pushed) {
|
|
242
|
+
const ci = await collectCi();
|
|
243
|
+
return { ok: true, steps, commitSha, committed: !cleanSkipCommit, pushed: false, push: pr, identity, legacyIdentity, ...(ci ? { ci } : {}) };
|
|
244
|
+
}
|
|
223
245
|
// 推送后增强对两条通道一视同仁:remote-tracking ref + aux remote + autoTag + remote heads。
|
|
224
246
|
// 原先只有 API 分支做这些,SSH 分支只拉 heads → 走 SSH 时本地 origin/<branch> 引用不更新,
|
|
225
247
|
// 下次 ahead/behind 判断就会错位。
|
|
@@ -238,7 +260,13 @@ export async function commitAndPush({ repoPath = '', message = '', messageItems
|
|
|
238
260
|
};
|
|
239
261
|
}
|
|
240
262
|
// committed 如实带上:cleanSkipCommit 时推的是**此前已提交的领先内容**,本次没有新提交
|
|
241
|
-
|
|
263
|
+
const ci = await collectCi();
|
|
264
|
+
if (ci) steps.push(`ci-${ci.conclusion}`);
|
|
265
|
+
return {
|
|
266
|
+
ok: true, steps, commitSha, committed: !cleanSkipCommit, pushed: true, push: enhanced.push,
|
|
267
|
+
autoTag: enhanced.autoTag, ...(enhanced.heads ? { remoteHeads: enhanced.heads } : {}), identity, legacyIdentity,
|
|
268
|
+
...(ci ? { ci, ...(ci.conclusion === 'failure' ? { ciFailed: true } : {}) } : {}),
|
|
269
|
+
};
|
|
242
270
|
}
|
|
243
271
|
// 失败回执附结构化诊断(分类 + 置信度 + 下一步)
|
|
244
272
|
const dg = pr.diagnosis || classifyPushFailure(String(pr.reason || ''), { method: pr.method });
|
package/lib/self/index.js
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
|
|
12
|
-
export const VERSION = '2.
|
|
12
|
+
export const VERSION = '2.17.0'; // 2.17.0:推送后可等 CI 结果(waitCi)。推送成功只代表代码到了远端,构建是否通过是另一回事——实测一次提交推送成功、CI 在同一 SHA 上挂了(我加的一个测试假设了 CIFS 特性、在 ext4 runner 上必然失败),只看推送结果发现不了。实现:lib/git/ci.js 按 **head_sha** 查 Actions runs(不查最新一次,避免并发提交拿错结果),多 workflow 任一失败即 failure;刚推上去 run 可能还没创建,给 grace 期再判「没有 CI」;超时单独给 timeout。commitAndPush 新增 waitCi / ciTimeoutSec,回执带 ci{conclusion,runs,failing,url} 与 ciFailed 标记。同时修掉 2.16.0 引入的 CI 红:那条测试断言了「降级路径会写 origin」,在 ext4 runner(chmod 可用)上不成立——已改为与文件系统无关。历史:2.16.1 按包形态验证。
|
|
13
13
|
// 实测后果:该目录有 package.json ⇒ npm 去打包整棵 DSH 树、输出 2.1MB > spawnSync 默认 maxBuffer 1MB ⇒ ENOBUFS(npm_publish 的体检与真发全部失败);
|
|
14
14
|
// 该目录没有 package.json 时报 ENOENT。现 publish 的两个调用点显式传 cwd=目标包目录,并给 runNpm 加 16MB maxBuffer。
|
|
15
15
|
// 2.8.3:文案修正——「零改动 / 无新提交」明确为**正常成功路径**(注入用法、工具描述、README、skill 同批改),并修 job 输出自相矛盾(cleanSkipCommit 路径补 committed:false,不再显示「已提交,未推送」)
|