dsh-git-push 2.17.0 → 2.18.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/README.md CHANGED
@@ -17,7 +17,8 @@ DSH 的 git 提交推送与代码审计插件:提交前自动过审计门禁
17
17
  | 改名 | 给远程仓库改名(dryRun 默认开,列出旧名出现位置) | `git_remote_rename` / `rename` |
18
18
  | 凭据与账号 | 校验 GitHub token 与 SSH 公钥、生成密钥对 | `git_account_check` / `git_gen_ssh_key` |
19
19
  | 身份改写 | 把历史提交身份统一成登录账号 | `git_identity_rewrite` / `identity-rewrite` |
20
- | npm 发包 | 发布前体检,默认 dry-run,真发需二次确认 | `npm_publish` / `npm-publish` |
20
+ | 发布 | 发布 npm 包(dryRun 默认只体检;含发布前冒烟) | `npm_publish` / — |
21
+ | 撤包 | 从 registry 撤版本(默认 dryRun 预检,不可逆) | `npm_unpublish` / `unpublish` |
21
22
  | 豁免标记 | 给文件加/删豁免注释并校验是否真生效 | `exempt` |
22
23
  | I/O 风险扫描 | 扫描读写操作的风险分级 | `io_scan` / `file-io` |
23
24
  | 链接检查 | 检查文档里的 URL 是否有效 | `link_check` / `link-check` |
@@ -122,6 +122,16 @@ export const TOOL_REGISTRY = [
122
122
  { flag: 'writeOnly', type: 'boolean', desc: '只看写操作' },
123
123
  ],
124
124
  },
125
+ {
126
+ name: 'npm_unpublish', cli: 'unpublish', description: '从 npm registry 撤包(不可逆):默认 dryRun 只预检——查版本是否存在、是否被 dist-tag 指向、发布距今是否在 72h 窗口内、是否该包唯一版本,并说明撤掉的后果;真撤需同时传 dryRun:false 与 confirm:true。超出 72h 只有「无依赖者+周下载<300+单一维护者」才允许,不满足请改用 deprecate',
127
+ params: [
128
+ { flag: 'name', type: 'string', positional: 0, required: true, desc: '包名(支持 @scope/name)' },
129
+ { flag: 'version', type: 'string', positional: 1, desc: '要撤的版本(整包撤请改用 wholePackage:true)' },
130
+ { flag: 'wholePackage', type: 'boolean', desc: '撤整个包(所有版本,会给 npm unpublish 加 --force)' },
131
+ { flag: 'dryRun', type: 'boolean', desc: '只预检不真撤(默认 true)' },
132
+ { flag: 'confirm', type: 'boolean', desc: '真撤确认(dryRun:false 时必填)' },
133
+ ],
134
+ },
125
135
  {
126
136
  name: 'npm_publish', cli: 'npm-publish',
127
137
  description: 'npm 发包(后台 job + dryRun):默认 dryRun=true 只做体检与 npm publish --dry-run(不写 registry);真发必须同时 confirm=true 且 dryRun=false。dir=目标包目录(缺省工作区)、tag=发布 tag(默认 latest)、otp=2FA 一次性口令(可选)。前置体检:private:true 拦截、同名版本占用检测、npm pack 文件清单',
@@ -602,6 +602,20 @@ export async function callTool(toolName, args = {}, env = {}, cfg = defaultConfi
602
602
  }
603
603
  case 'npm_publish':
604
604
  return callNpmPublishJob(args, jobs, exec, log, env, cfg);
605
+ case 'npm_unpublish': {
606
+ // 撤包不可逆:默认 dryRun(只预检并说明后果),真撤要 dryRun:false + confirm:true
607
+ const dryRun = !(args.dryRun === false || args.dryRun === 'false' || args.noDryRun === true);
608
+ const { unpublishPackage } = await import('../npm/unpublish.js');
609
+ return unpublishPackage({
610
+ name: String(args.name || args.pkg || ''),
611
+ version: String(args.version || ''),
612
+ wholePackage: args.wholePackage === true,
613
+ registry: cfg?.npmRegistry || '',
614
+ workspaceRoot: env.workspaceRoot || '',
615
+ dryRun,
616
+ confirm: args.confirm === true,
617
+ });
618
+ }
605
619
  case 'git_remote_create': {
606
620
  if (!args.repo) return { ok: false, error: MSG_REPO_REQUIRED };
607
621
  const created = await ensureRemoteRepo({ repoPath: args.repo, visibility: args.visibility || 'private', dryRun: args.dryRun === true });
@@ -0,0 +1,131 @@
1
+ /**
2
+ * npm 撤包(`npm unpublish`)。
3
+ *
4
+ * 为什么单独做、且**默认 dryRun**:撤包不可逆——
5
+ * · 一个 `包@版本` 一旦用过,即使撤掉也**永久不能再发**同一个号;
6
+ * · 官方只承诺 registry 不再提供,不承诺清 CDN/第三方镜像/已下载副本;
7
+ * · 撤掉 `latest` 会让 dist-tags 悬空,影响所有 `npm i <包>` 的人。
8
+ * 所以调用方必须先看清「要撤什么、撤完会怎样」,再显式 `confirm` 才真撤。
9
+ *
10
+ * 预检能回答什么(都不联网到第三方,只查 registry 元数据):
11
+ * · 版本是否存在;dist-tags 是否指向它(撤 latest 有额外影响);
12
+ * · 发布距今是否在 72 小时窗口内(窗口内且无依赖者才符合官方条件);
13
+ * · 是否该包唯一版本(撤光会让包名冷却 24h)。
14
+ * 预检**不能**回答:是否有其它包依赖它(公共 registry 没有 reverse-deps 接口),
15
+ * 这一条只能由 npm 服务端判定,失败时如实把 npm 的报错回传。
16
+ */
17
+ import { DEFAULT_REGISTRY, NPM_TIMEOUT_MS, normalizeRegistry, runNpm } from './registry.js';
18
+ import { npmArgs, writeNpmrc } from './npmrc.js';
19
+ import { readNpmToken } from '../core/credentials.js';
20
+
21
+ /** 官方 unpublish 政策里的时间窗:发布 72h 内(且无依赖者)可自由撤。 */
22
+ const UNPUBLISH_WINDOW_MS = 72 * 60 * 60 * 1000;
23
+ /** 整包撤光后,同名包 24h 内不能发布。 */
24
+ const WHOLE_PACKAGE_COOLDOWN_MS = 24 * 60 * 60 * 1000;
25
+
26
+ /** 归一化包名/版本入参,做前置校验(不联网)。 */
27
+ export function parseTarget({ name = '', version = '', wholePackage = false } = {}) {
28
+ const pkg = String(name || '').trim();
29
+ const ver = String(version || '').trim();
30
+ if (!pkg) return { ok: false, error: '缺 name(要撤的包名)' };
31
+ if (!/^(@[a-z0-9][\w.-]*\/)?[a-z0-9][\w.-]*$/i.test(pkg)) return { ok: false, error: `包名不合法:${pkg}` };
32
+ if (!wholePackage && !ver) return { ok: false, error: '缺 version;若确实要撤整个包,请显式传 wholePackage:true(会连带撤所有版本)' };
33
+ if (!wholePackage && !/^\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.-]+)?$/.test(ver)) return { ok: false, error: `版本号不合法:${ver}` };
34
+ return { ok: true, name: pkg, version: ver, wholePackage: wholePackage === true };
35
+ }
36
+
37
+ /**
38
+ * 预检:查 registry 元数据,给出「撤这个版本会发生什么」。
39
+ * @param {{name:string, version:string, wholePackage?:boolean,
40
+ * registry?:string, timeoutMs?:number, fetchImpl?:Function}} o
41
+ */
42
+ export async function preflightUnpublish({ name = '', version = '', wholePackage = false, registry = DEFAULT_REGISTRY, timeoutMs = NPM_TIMEOUT_MS, fetchImpl = globalThis.fetch } = {}) {
43
+ const target = parseTarget({ name, version, wholePackage });
44
+ if (!target.ok) return { ok: false, error: target.error };
45
+ const reg = normalizeRegistry(registry);
46
+ const url = `${reg.replace(/\/$/, '')}/${name.replace('/', '%2f')}`;
47
+ const ctrl = new AbortController();
48
+ const timer = setTimeout(() => ctrl.abort(), timeoutMs);
49
+ try {
50
+ const res = await fetchImpl(url, { signal: ctrl.signal, headers: { accept: 'application/json' } });
51
+ if (res.status === 404) return { ok: false, error: `registry 上不存在该包:${name}` };
52
+ if (res.status !== 200) return { ok: false, error: `查询失败:HTTP ${res.status}` };
53
+ const doc = await res.json();
54
+ const versions = Object.keys(doc.versions || {});
55
+ const publishedAt = (doc.time || {})[target.version] || '';
56
+ const ageMs = publishedAt ? Date.now() - Date.parse(publishedAt) : null;
57
+ const tagsPointingHere = Object.entries(doc['dist-tags'] || {}).filter(([, v]) => v === target.version).map(([t]) => t);
58
+ if (!wholePackage && !versions.includes(target.version)) {
59
+ return { ok: false, error: `版本不存在:${name}@${target.version}(现有版本:${versions.join(', ')})` };
60
+ }
61
+ const warnings = [];
62
+ if (tagsPointingHere.length) {
63
+ warnings.push(`dist-tag ${tagsPointingHere.join('/')} 指向该版本:撤掉后这些 tag 会失效/悬空(建议先把 tag 移到其它版本)`);
64
+ }
65
+ if (versions.length <= 1) {
66
+ warnings.push(`该包只有 1 个版本:撤光后同名包名将冷却 ${WHOLE_PACKAGE_COOLDOWN_MS / 3600000} 小时`);
67
+ }
68
+ return {
69
+ ok: true,
70
+ name, version: target.version, wholePackage: target.wholePackage,
71
+ exists: true,
72
+ versions,
73
+ publishedAt,
74
+ ageHours: ageMs === null ? null : Math.round(ageMs / 3600000),
75
+ withinWindow: ageMs === null ? null : ageMs <= UNPUBLISH_WINDOW_MS,
76
+ tagsPointingHere,
77
+ warnings,
78
+ // 72h 内只是「可以申请撤」的必要条件之一;依赖者一条只有服务端能判
79
+ policyHint: ageMs !== null && ageMs > UNPUBLISH_WINDOW_MS
80
+ ? '已超出 72h 窗口:只有「无依赖者 + 近一周下载<300 + 单一维护者」三者同时满足才允许撤,否则只能 npm deprecate'
81
+ : '在 72h 窗口内:无其它包依赖它时可直接撤',
82
+ // 撤包不可逆的提醒:即使成功,该版本号也永久作废
83
+ irreversible: `撤掉后 ${name}@${target.version} 永久不能再发布(只能发新版本号)`,
84
+ };
85
+ } catch (e) {
86
+ return { ok: false, error: `查询失败:${String(e?.message || e).slice(0, 200)}` };
87
+ } finally { clearTimeout(timer); }
88
+ }
89
+
90
+ /**
91
+ * 执行撤包。
92
+ * @param {{name:string, version?:string, wholePackage?:boolean, registry?:string,
93
+ * workspaceRoot?:string, dryRun?:boolean, confirm?:boolean, timeoutMs?:number}} o
94
+ */
95
+ export async function unpublishPackage({
96
+ name = '', version = '', wholePackage = false,
97
+ registry = DEFAULT_REGISTRY, workspaceRoot = '',
98
+ dryRun = true, confirm = false, timeoutMs = 120000,
99
+ } = {}) {
100
+ const pre = await preflightUnpublish({ name, version, wholePackage, registry });
101
+ if (!pre.ok) return { ok: false, blocked: 'preflight-failed', error: pre.error };
102
+ if (dryRun) {
103
+ return {
104
+ ok: true, dryRun: true, ...pre,
105
+ wouldRun: `npm unpublish ${name}${wholePackage ? '' : `@${pre.version}`}${wholePackage ? ' --force' : ''}`,
106
+ hint: '确认无误后传 dryRun:false + confirm:true 才真正撤包(不可逆)',
107
+ };
108
+ }
109
+ if (confirm !== true) {
110
+ return { ok: false, blocked: 'need-confirm', ...pre, error: '真实撤包不可逆:需同时传 dryRun:false 与 confirm:true' };
111
+ }
112
+ const token = (() => { try { return readNpmToken({ workspaceRoot }); } catch { return ''; } })();
113
+ const rc = writeNpmrc({ workspaceRoot, registry, token });
114
+ if (!rc.ok) return { ok: false, error: rc.error || 'npmrc 写入失败' };
115
+ const args = ['unpublish', wholePackage ? name : `${name}@${pre.version}`, ...npmArgs({ npmrcFile: rc.file, registry })];
116
+ if (wholePackage) args.push('--force');
117
+ const r = runNpm(args, { timeoutMs, env: { NO_UPDATE_NOTIFIER: '1' } });
118
+ if (!r.ok) {
119
+ return {
120
+ ok: false, ...pre,
121
+ error: `npm unpublish 失败:${String(r.error || r.stderr || r.stdout || '').slice(0, 400)}`,
122
+ hint: '若提示 72h/依赖者/下载量限制,说明不满足官方撤包条件——改用 npm deprecate 让使用者看到警告而不破坏构建',
123
+ };
124
+ }
125
+ return {
126
+ ok: true, dryRun: false, ...pre,
127
+ unpublishd: wholePackage ? `${name}(全部版本)` : `${name}@${pre.version}`,
128
+ stdout: String(r.stdout || '').slice(-2000),
129
+ note: '撤包不可逆:该版本号永久作废;对外索引(第三方镜像/文档)不会自动清理',
130
+ };
131
+ }
package/lib/self/index.js CHANGED
@@ -9,7 +9,7 @@
9
9
  */
10
10
 
11
11
 
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 按包形态验证。
12
+ export const VERSION = '2.18.0'; // 2.18.0:新增 npm_unpublish(撤包)。撤包不可逆——同一 `包@版本` 撤掉后永久不能再发,撤掉 latest 会让 `npm i <包>` 悬空——所以默认 dryRun:预检版本是否存在、是否被 dist-tag 指向、发布距今是否在官方 72h 窗口内、是否该包唯一版本(撤光会冷却 24h),并说明撤掉后的后果;真撤必须 dryRun:false + confirm:true 双确认。超出 72h 只有「无依赖者 + 周下载<300 + 单一维护者」三条件同时满足才允许,否则只能 deprecate——预检会把这条政策原文写进 policyHint。历史:2.17.0 推送后等 CI。
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,不再显示「已提交,未推送」)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-git-push",
3
- "version": "2.17.0",
3
+ "version": "2.18.0",
4
4
  "description": "DSH git 自动提交推送插件——统一函数入口架构(规则/审计/git/自身/侧边栏/评分/豁免/上下文/HTTP/测试十大总入口)",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",