dsh-github-flow 0.2.1 → 0.3.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.en.md +38 -11
- package/README.md +37 -10
- package/docs/UPGRADE-NOTES.md +9 -9
- package/docs/architecture.md +53 -23
- package/docs/images/2.png +0 -0
- package/docs/npm-publish-guide.md +12 -1
- package/lib/api/routes-overview.js +104 -1
- package/lib/client.js +583 -14
- package/lib/executor.d.ts +118 -1
- package/lib/executor.js +357 -0
- package/lib/main.js +1 -0
- package/lib/renderers/markdown.d.ts +6 -0
- package/lib/renderers/markdown.js +109 -2
- package/lib/tools/help.js +132 -11
- package/lib/tools/issue.d.ts +9 -0
- package/lib/tools/issue.js +88 -4
- package/lib/tools/pr.d.ts +4 -0
- package/lib/tools/pr.js +50 -3
- package/lib/tools/repo.d.ts +8 -0
- package/lib/tools/repo.js +127 -3
- package/lib/tools/run.d.ts +8 -0
- package/lib/tools/run.js +104 -3
- package/lib/tools/secret.d.ts +5 -0
- package/lib/tools/secret.js +108 -23
- package/lib/types.d.ts +73 -0
- package/lib/version.d.ts +1 -1
- package/lib/version.js +1 -1
- package/package.json +1 -1
package/lib/executor.d.ts
CHANGED
|
@@ -1,5 +1,36 @@
|
|
|
1
|
-
import type { GhExecutionOptions, GhResult, AuthStatus, RepoMetadata, PluginConfig } from './types.js';
|
|
1
|
+
import type { GhExecutionOptions, GhResult, AuthStatus, RepoMetadata, PluginConfig, GitLocalStatus, GitCommitLog, GitCommitEntry } from './types.js';
|
|
2
2
|
import type { ProxyManager } from './proxy.js';
|
|
3
|
+
/**
|
|
4
|
+
* 从 `gh --version` 输出中解析语义化版本号。
|
|
5
|
+
* 抽成导出纯函数的原因与 truncateOutput 相同:解析逻辑是「看似简单实则易错」的边界区,
|
|
6
|
+
* 必须能用单元测试钉死(不匹配输入返回 null 而非抛错,调用方据此走 fail-open)。
|
|
7
|
+
*/
|
|
8
|
+
export declare function parseGhVersion(output: string): string | null;
|
|
9
|
+
/**
|
|
10
|
+
* 数值分段比较两个 gh 版本:a>b 返回 1、a<b 返回 -1、相等返回 0。
|
|
11
|
+
* 为什么不能用字符串比较:`'2.96.0' < '2.100.0'` 在字典序下为 false('1'<'9'),
|
|
12
|
+
* 这会把 2.100 误判为低于 2.96,导致可用命令被错误降级。逐段 Number 比较是唯一正确姿势。
|
|
13
|
+
*/
|
|
14
|
+
export declare function compareGhVersions(a: string, b: string): number;
|
|
15
|
+
/**
|
|
16
|
+
* 解析 `git status -sb --porcelain` 输出为结构化状态。
|
|
17
|
+
* 抽导出纯函数(parseGhVersion 同理):三形态解析是易错边界区,单测钉死。
|
|
18
|
+
*
|
|
19
|
+
* 输出形态(实测):
|
|
20
|
+
* `## master...origin/master [ahead 2, behind 1]` 有 ahead/behind
|
|
21
|
+
* `## master...origin/master` 与远程同步
|
|
22
|
+
* `## master` / `## HEAD (no branch)` 无 upstream(ahead/behind 未知=-1)
|
|
23
|
+
* 后续每行 `<XY> <path>` 为 porcelain 变更项(XY 两字符状态码,?? = untracked)。
|
|
24
|
+
*/
|
|
25
|
+
export declare function parseGitStatusOutput(output: string): GitLocalStatus | null;
|
|
26
|
+
/**
|
|
27
|
+
* 解析 `git log` 输出为提交条目数组。
|
|
28
|
+
* 支持两种形态:
|
|
29
|
+
* 1. 结构化带详情:%x1e 为 commit 间分隔符,%x1f 为字段间分隔符(含 fullSha/email/date/body/stat);
|
|
30
|
+
* 2. 简易管道符:%h|%an|%ar|%s 行格式(兼容旧单测输入)。
|
|
31
|
+
* 空输出返回 []。
|
|
32
|
+
*/
|
|
33
|
+
export declare function parseGitLogLines(output: string): GitCommitEntry[];
|
|
3
34
|
/**
|
|
4
35
|
* 按「字符数 + UTF-8 字节数」双口径截断输出。
|
|
5
36
|
* 抽成纯函数是刻意的:截断逻辑是崩溃高发区(切碎多字节字符、误伤 JSON、把二进制流截成垃圾),
|
|
@@ -22,6 +53,12 @@ export declare class GhExecutor {
|
|
|
22
53
|
* 且热更新路径 updateConfig 能同步刷新。工具内以 `?? 20` 兜底保证旧配置兼容。
|
|
23
54
|
*/
|
|
24
55
|
defaultListLimit: number;
|
|
56
|
+
/**
|
|
57
|
+
* 是否注入 AI_AGENT='dsh'(gh 官方 agent 识别信号,默认 true)。
|
|
58
|
+
* 挂在 executor 而非工具参数:注入点是「每次 gh 子进程拉起」这一全局咽喉,
|
|
59
|
+
* 与 GH_NO_UPDATE_NOTIFIER/NO_COLOR 同级,语义上属于执行层默认值。
|
|
60
|
+
*/
|
|
61
|
+
agentEnvInjection: boolean;
|
|
25
62
|
/**
|
|
26
63
|
* 代理管理器引用(可选依赖)。
|
|
27
64
|
* 注入后每次 gh 子进程拉起都会合并代理环境变量——这是「工具调用 + Web 页面数据
|
|
@@ -31,6 +68,11 @@ export declare class GhExecutor {
|
|
|
31
68
|
private proxyManager?;
|
|
32
69
|
/** 认证状态缓存。`gh auth status` 实测 5.4 秒,而它位于全局驾驶舱关键路径的最前面 */
|
|
33
70
|
private authCache;
|
|
71
|
+
/**
|
|
72
|
+
* gh 版本号缓存。只缓存成功解析值(authCache 哲学:不确定结果不缓存)——
|
|
73
|
+
* 探测失败/未安装时不落缓存,用户装好 gh 后下一次探测立即生效。
|
|
74
|
+
*/
|
|
75
|
+
private ghVersionCache;
|
|
34
76
|
constructor(config?: PluginConfig);
|
|
35
77
|
updateConfig(config?: PluginConfig): void;
|
|
36
78
|
/** 注入代理管理器(main.ts 装配时调用一次;测试可注入 mock) */
|
|
@@ -57,6 +99,81 @@ export declare class GhExecutor {
|
|
|
57
99
|
* 而 Web 面板每次挂载都会来问一次,重复执行实测要 5.4 秒,属于纯浪费。
|
|
58
100
|
*/
|
|
59
101
|
checkAuth(cwd?: string, bypassCache?: boolean): Promise<AuthStatus>;
|
|
102
|
+
/**
|
|
103
|
+
* 获取当前 gh 版本号(带 30s TTL 缓存),失败返回 null。
|
|
104
|
+
* 底层直连 execFileAsync 而不走 this.run():run() 会先解析工作区(可能读
|
|
105
|
+
* workspace.json 与目录扫描)再做截断/JSON 解析,对一次 10ms 的版本字符串
|
|
106
|
+
* 查询是纯开销;直连也彻底规避 runGuarded → 探测 → run 的递归风险。
|
|
107
|
+
*/
|
|
108
|
+
getGhVersion(): Promise<string | null>;
|
|
109
|
+
/**
|
|
110
|
+
* 底层版本探测(独立方法而非内联:测试需要在不触发真实子进程的前提下
|
|
111
|
+
* 验证 getGhVersion 的缓存分支——覆写此方法即注入确定输出)。
|
|
112
|
+
* 直连 execFileAsync 而不走 this.run():run() 会先解析工作区(可能读
|
|
113
|
+
* workspace.json 与目录扫描)再做截断/JSON 解析,对一次 10ms 的版本字符串
|
|
114
|
+
* 查询是纯开销;直连也彻底规避 runGuarded → 探测 → run 的递归风险。
|
|
115
|
+
*/
|
|
116
|
+
protected probeGhVersionRaw(): Promise<string>;
|
|
117
|
+
/**
|
|
118
|
+
* 守卫执行器:按 gh 版本门槛路由「主命令 → 降级链路 → 升级指引」三态。
|
|
119
|
+
*
|
|
120
|
+
* 为什么是「检查+执行」一体的方法而非纯探测接口:检查与执行分离会在两者之间
|
|
121
|
+
* 留下竞隙(版本可能在间隙变化),且降级标注逻辑会被迫散落到每个工具的 if/else。
|
|
122
|
+
*
|
|
123
|
+
* fail-open 纪律:版本探测自身失败(null)时直接执行主命令——守卫是路由优化
|
|
124
|
+
* 不是安全边界,探测故障不应拦截业务;gh 未安装场景由主命令产出既有 ENOENT
|
|
125
|
+
* 安装引导,这里不重复造错误路径。
|
|
126
|
+
*/
|
|
127
|
+
runGuarded<T = unknown>(args: string[], options?: GhExecutionOptions & {
|
|
128
|
+
/** 该能力要求的最低 gh 版本,如 '2.95.0';缺省时等价于直接 run(渐进采用) */
|
|
129
|
+
minVersion?: string;
|
|
130
|
+
/** 人类可读能力名,用于错误信息,如 'gh repo read-file' */
|
|
131
|
+
capability?: string;
|
|
132
|
+
/** 版本不足时的降级链路(如 github_api contents 端点封装);无则报错带升级指引 */
|
|
133
|
+
fallback?: () => Promise<GhResult<T>>;
|
|
134
|
+
}): Promise<GhResult<T> & {
|
|
135
|
+
degraded?: boolean;
|
|
136
|
+
degradedReason?: string;
|
|
137
|
+
}>;
|
|
138
|
+
/** gh 升级命令按平台提示(复用三态状态机的平台判断口径) */
|
|
139
|
+
private getGhUpgradeHint;
|
|
140
|
+
/**
|
|
141
|
+
* 本地 Git 状态只读探测(白名单通道)。
|
|
142
|
+
*
|
|
143
|
+
* 这是本插件首次执行 git(此前仅 gh)——git 存在大量写命令,风险等级高于 gh,
|
|
144
|
+
* 因此白名单不落在「命令名过滤」而落在「命令形态常量」:spawn 参数是下方
|
|
145
|
+
* 硬编码的数组字面量,无任何动态拼接路径,写操作物理不可达(cwd 之外零输入,
|
|
146
|
+
* 而 cwd 本身也是插件解析的工作区路径)。
|
|
147
|
+
*
|
|
148
|
+
* 零网络:status 基于 remote-tracking ref(上次 fetch 的快照),毫秒级本地读取,
|
|
149
|
+
* 符合 global-overview 端点「工作区探测不发网络」的性能红线。
|
|
150
|
+
*/
|
|
151
|
+
getLocalGitStatus(cwd?: string): Promise<GitLocalStatus | null>;
|
|
152
|
+
/**
|
|
153
|
+
* 本地 Git 提交记录(三段一次返回:本地领先 / 远程领先 / 最近提交)。
|
|
154
|
+
*
|
|
155
|
+
* 白名单纪律与 getLocalGitStatus 同源:下方两个命令形态常量硬编码
|
|
156
|
+
* (log + 修订范围语法,range 段由 hasUpstream 守卫后才拼接且仅两个
|
|
157
|
+
* 固定形态 @{u}..HEAD / HEAD..@{u}——不含任何用户输入路径)。
|
|
158
|
+
* 领先/落后列表基于 remote-tracking ref(上次 fetch 快照),与 status
|
|
159
|
+
* 的 ahead/behind 计数同一数据口径;格式 %h|%an|%ar|%s 实测可结构化解析。
|
|
160
|
+
*/
|
|
161
|
+
getGitCommitLog(cwd?: string, hasUpstream?: boolean, limit?: number): Promise<GitCommitLog | null>;
|
|
162
|
+
/**
|
|
163
|
+
* 分页获取最近提交记录(用于 Git 面板下滑触底继续加载)。
|
|
164
|
+
*/
|
|
165
|
+
getGitRecentLog(cwd?: string, limit?: number, skip?: number): Promise<{
|
|
166
|
+
commits: GitCommitEntry[];
|
|
167
|
+
totalCount?: number;
|
|
168
|
+
}>;
|
|
169
|
+
/**
|
|
170
|
+
* 单条提交详情(点击提交行弹窗的数据源)。
|
|
171
|
+
* 白名单纪律延续:show 命令形态常量硬编码;sha 参数经 ^[0-9a-f]{4,40}$
|
|
172
|
+
* 十六进制校验后才拼接——这是 git 白名单通道首次接受「外部传入的单值」,
|
|
173
|
+
* 正则把值域锁死在纯十六进制(注入面为零:任何 shell 元字符/子命令
|
|
174
|
+
* 语法都不可能通过校验)。输出为 git show --stat 原文(含文件变更统计)。
|
|
175
|
+
*/
|
|
176
|
+
getGitCommitDetail(cwd: string | undefined, sha: string): Promise<string | null>;
|
|
60
177
|
/** 生成当前操作系统平台的推荐安装引导与命令 */
|
|
61
178
|
private getPlatformInstallGuide;
|
|
62
179
|
/** 实际执行 `gh auth status` 并解析结果,不含缓存逻辑 */
|
package/lib/executor.js
CHANGED
|
@@ -84,6 +84,130 @@ function spawnWithStdin(ghPath, args, opts) {
|
|
|
84
84
|
}
|
|
85
85
|
/** 认证状态缓存时长。用户手动 `gh auth login/logout` 后最多 30 秒内面板才会反映,属可接受代价 */
|
|
86
86
|
const AUTH_CACHE_TTL_MS = 30_000;
|
|
87
|
+
/** gh 版本缓存时长。与 authCache 同哲学:gh 升级是分钟级低频事件,30 秒陈旧窗口无实际影响 */
|
|
88
|
+
const GH_VERSION_CACHE_TTL_MS = 30_000;
|
|
89
|
+
/**
|
|
90
|
+
* 从 `gh --version` 输出中解析语义化版本号。
|
|
91
|
+
* 抽成导出纯函数的原因与 truncateOutput 相同:解析逻辑是「看似简单实则易错」的边界区,
|
|
92
|
+
* 必须能用单元测试钉死(不匹配输入返回 null 而非抛错,调用方据此走 fail-open)。
|
|
93
|
+
*/
|
|
94
|
+
export function parseGhVersion(output) {
|
|
95
|
+
const m = /gh version (\d+\.\d+\.\d+)/.exec(output || '');
|
|
96
|
+
return m ? m[1] : null;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* 数值分段比较两个 gh 版本:a>b 返回 1、a<b 返回 -1、相等返回 0。
|
|
100
|
+
* 为什么不能用字符串比较:`'2.96.0' < '2.100.0'` 在字典序下为 false('1'<'9'),
|
|
101
|
+
* 这会把 2.100 误判为低于 2.96,导致可用命令被错误降级。逐段 Number 比较是唯一正确姿势。
|
|
102
|
+
*/
|
|
103
|
+
export function compareGhVersions(a, b) {
|
|
104
|
+
const pa = a.split('.').map(Number);
|
|
105
|
+
const pb = b.split('.').map(Number);
|
|
106
|
+
for (let i = 0; i < Math.max(pa.length, pb.length); i++) {
|
|
107
|
+
const da = pa[i] ?? 0;
|
|
108
|
+
const db = pb[i] ?? 0;
|
|
109
|
+
if (da > db)
|
|
110
|
+
return 1;
|
|
111
|
+
if (da < db)
|
|
112
|
+
return -1;
|
|
113
|
+
}
|
|
114
|
+
return 0;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* 解析 `git status -sb --porcelain` 输出为结构化状态。
|
|
118
|
+
* 抽导出纯函数(parseGhVersion 同理):三形态解析是易错边界区,单测钉死。
|
|
119
|
+
*
|
|
120
|
+
* 输出形态(实测):
|
|
121
|
+
* `## master...origin/master [ahead 2, behind 1]` 有 ahead/behind
|
|
122
|
+
* `## master...origin/master` 与远程同步
|
|
123
|
+
* `## master` / `## HEAD (no branch)` 无 upstream(ahead/behind 未知=-1)
|
|
124
|
+
* 后续每行 `<XY> <path>` 为 porcelain 变更项(XY 两字符状态码,?? = untracked)。
|
|
125
|
+
*/
|
|
126
|
+
export function parseGitStatusOutput(output) {
|
|
127
|
+
const lines = (output || '').split('\n').filter((l) => l.length > 0);
|
|
128
|
+
if (lines.length === 0 || !lines[0].startsWith('## '))
|
|
129
|
+
return null;
|
|
130
|
+
const branchLine = lines[0].slice(3); // 去掉 '## ' 前缀
|
|
131
|
+
const hasUpstream = branchLine.includes('...');
|
|
132
|
+
const branchPart = hasUpstream ? branchLine.split('...')[0] : branchLine.split(' ')[0];
|
|
133
|
+
let ahead = -1;
|
|
134
|
+
let behind = -1;
|
|
135
|
+
if (hasUpstream) {
|
|
136
|
+
// ahead/behind 段在 upstream 之后(可能与 #id 同现:origin/master #12)。
|
|
137
|
+
// git 输出形态实测:`[ahead 2]` / `[behind 1]` / `[ahead 2, behind 1]`——
|
|
138
|
+
// 单括号内逗号分隔,behind 不必然前邻 '['(正则不可锚定 '[',否则
|
|
139
|
+
// `[ahead 2, behind 1]` 形态的 behind 永远漏解析——单测抓到的真实 Bug)
|
|
140
|
+
const trackingPart = branchLine.split('...')[1] || '';
|
|
141
|
+
ahead = 0;
|
|
142
|
+
behind = 0;
|
|
143
|
+
const aheadMatch = /ahead (\d+)/.exec(trackingPart);
|
|
144
|
+
const behindMatch = /behind (\d+)/.exec(trackingPart);
|
|
145
|
+
if (aheadMatch)
|
|
146
|
+
ahead = Number(aheadMatch[1]);
|
|
147
|
+
if (behindMatch)
|
|
148
|
+
behind = Number(behindMatch[1]);
|
|
149
|
+
// 仅 ahead 无 behind 时 behind=0(反之亦然)——git 输出只列非零侧
|
|
150
|
+
}
|
|
151
|
+
const dirtyFiles = [];
|
|
152
|
+
for (let i = 1; i < lines.length && dirtyFiles.length < 20; i++) {
|
|
153
|
+
const line = lines[i];
|
|
154
|
+
if (line.length < 4)
|
|
155
|
+
continue;
|
|
156
|
+
const status = line.slice(0, 2).trim() || 'M';
|
|
157
|
+
// 路径可能含空格(git 不加引号场景);rename 形态 `R old -> new` 取 new
|
|
158
|
+
let p = line.slice(3);
|
|
159
|
+
const renameArrow = p.indexOf(' -> ');
|
|
160
|
+
if (renameArrow >= 0)
|
|
161
|
+
p = p.slice(renameArrow + 4);
|
|
162
|
+
dirtyFiles.push({ status, path: p });
|
|
163
|
+
}
|
|
164
|
+
return {
|
|
165
|
+
branch: branchPart.replace(/\s*\(no branch\)$/, '') || 'HEAD',
|
|
166
|
+
ahead,
|
|
167
|
+
behind,
|
|
168
|
+
dirtyCount: Math.max(0, lines.length - 1),
|
|
169
|
+
hasUpstream,
|
|
170
|
+
dirtyFiles,
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* 解析 `git log` 输出为提交条目数组。
|
|
175
|
+
* 支持两种形态:
|
|
176
|
+
* 1. 结构化带详情:%x1e 为 commit 间分隔符,%x1f 为字段间分隔符(含 fullSha/email/date/body/stat);
|
|
177
|
+
* 2. 简易管道符:%h|%an|%ar|%s 行格式(兼容旧单测输入)。
|
|
178
|
+
* 空输出返回 []。
|
|
179
|
+
*/
|
|
180
|
+
export function parseGitLogLines(output) {
|
|
181
|
+
const out = (output || '').trim();
|
|
182
|
+
if (!out)
|
|
183
|
+
return [];
|
|
184
|
+
if (out.includes('\x1e') || out.includes('\x1f')) {
|
|
185
|
+
const records = out.split('\x1e').filter((c) => c.trim().length > 0);
|
|
186
|
+
return records.map((chunk) => {
|
|
187
|
+
const parts = chunk.split('\x1f');
|
|
188
|
+
return {
|
|
189
|
+
sha: (parts[0] || '').trim(),
|
|
190
|
+
fullSha: (parts[1] || '').trim() || undefined,
|
|
191
|
+
author: (parts[2] || '').trim(),
|
|
192
|
+
email: (parts[3] || '').trim() || undefined,
|
|
193
|
+
date: (parts[4] || '').trim() || undefined,
|
|
194
|
+
relative: (parts[5] || '').trim(),
|
|
195
|
+
subject: (parts[6] || '').trim(),
|
|
196
|
+
body: (parts[7] || '').trim() || undefined,
|
|
197
|
+
stat: (parts[8] || '').trim() || undefined,
|
|
198
|
+
};
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
return out.split('\n').filter(Boolean).map((line) => {
|
|
202
|
+
const parts = line.split('|');
|
|
203
|
+
return {
|
|
204
|
+
sha: parts[0] || '',
|
|
205
|
+
author: parts[1] || '',
|
|
206
|
+
relative: parts[2] || '',
|
|
207
|
+
subject: parts.slice(3).join('|'),
|
|
208
|
+
};
|
|
209
|
+
});
|
|
210
|
+
}
|
|
87
211
|
/**
|
|
88
212
|
* 按码点安全截取字符串,等价于 String.prototype.slice 但对孤立的代理对(surrogate pair)做占位替换。
|
|
89
213
|
* 为什么必须这样截:本文件末尾会按 UTF-8 字节数截断,而 node:child_process 把 stdout 按字节缓冲区
|
|
@@ -180,6 +304,12 @@ export class GhExecutor {
|
|
|
180
304
|
* 且热更新路径 updateConfig 能同步刷新。工具内以 `?? 20` 兜底保证旧配置兼容。
|
|
181
305
|
*/
|
|
182
306
|
defaultListLimit;
|
|
307
|
+
/**
|
|
308
|
+
* 是否注入 AI_AGENT='dsh'(gh 官方 agent 识别信号,默认 true)。
|
|
309
|
+
* 挂在 executor 而非工具参数:注入点是「每次 gh 子进程拉起」这一全局咽喉,
|
|
310
|
+
* 与 GH_NO_UPDATE_NOTIFIER/NO_COLOR 同级,语义上属于执行层默认值。
|
|
311
|
+
*/
|
|
312
|
+
agentEnvInjection;
|
|
183
313
|
/**
|
|
184
314
|
* 代理管理器引用(可选依赖)。
|
|
185
315
|
* 注入后每次 gh 子进程拉起都会合并代理环境变量——这是「工具调用 + Web 页面数据
|
|
@@ -189,12 +319,18 @@ export class GhExecutor {
|
|
|
189
319
|
proxyManager;
|
|
190
320
|
/** 认证状态缓存。`gh auth status` 实测 5.4 秒,而它位于全局驾驶舱关键路径的最前面 */
|
|
191
321
|
authCache = null;
|
|
322
|
+
/**
|
|
323
|
+
* gh 版本号缓存。只缓存成功解析值(authCache 哲学:不确定结果不缓存)——
|
|
324
|
+
* 探测失败/未安装时不落缓存,用户装好 gh 后下一次探测立即生效。
|
|
325
|
+
*/
|
|
326
|
+
ghVersionCache = null;
|
|
192
327
|
constructor(config = {}) {
|
|
193
328
|
this.ghPath = config.ghPath || 'gh';
|
|
194
329
|
this.defaultTimeoutMs = config.defaultTimeoutMs || 30_000;
|
|
195
330
|
this.maxOutputChars = config.maxOutputChars || 24_000;
|
|
196
331
|
this.maxOutputBytes = config.maxOutputBytes || this.maxOutputChars * 4;
|
|
197
332
|
this.defaultListLimit = config.defaultListLimit || 20;
|
|
333
|
+
this.agentEnvInjection = config.agentEnvInjection !== false;
|
|
198
334
|
}
|
|
199
335
|
updateConfig(config = {}) {
|
|
200
336
|
if (config.ghPath)
|
|
@@ -203,6 +339,8 @@ export class GhExecutor {
|
|
|
203
339
|
this.defaultTimeoutMs = config.defaultTimeoutMs;
|
|
204
340
|
if (config.defaultListLimit)
|
|
205
341
|
this.defaultListLimit = config.defaultListLimit;
|
|
342
|
+
if (config.agentEnvInjection !== undefined)
|
|
343
|
+
this.agentEnvInjection = config.agentEnvInjection;
|
|
206
344
|
if (config.maxOutputChars) {
|
|
207
345
|
this.maxOutputChars = config.maxOutputChars;
|
|
208
346
|
if (!config.maxOutputBytes)
|
|
@@ -280,6 +418,11 @@ export class GhExecutor {
|
|
|
280
418
|
// 代理环境变量注入在 options.env 之前:调用方显式传入的 env 仍可覆盖
|
|
281
419
|
// 代理(保持 options.env 的既有语义优先级),正常调用方不传则代理生效。
|
|
282
420
|
...(this.proxyManager?.getEnv('gh') || {}),
|
|
421
|
+
// gh 官方 agent 识别信号(detect.go 最高优先级通用变量):≥2.99 切 text-only
|
|
422
|
+
// spinner、≥2.100 误用时打印完整 help、telemetry 归因到 dsh;<2.99 忽略之。
|
|
423
|
+
// 覆盖继承自宿主环境的同名变量——插件比外层环境更清楚自己的直接驱动者是
|
|
424
|
+
// DSH(嵌套 agent 场景归因不失真);options.env 仍可覆盖(与代理变量同语义)。
|
|
425
|
+
...(this.agentEnvInjection ? { AI_AGENT: 'dsh' } : {}),
|
|
283
426
|
...options.env,
|
|
284
427
|
},
|
|
285
428
|
};
|
|
@@ -377,6 +520,220 @@ export class GhExecutor {
|
|
|
377
520
|
}
|
|
378
521
|
return value;
|
|
379
522
|
}
|
|
523
|
+
/**
|
|
524
|
+
* 获取当前 gh 版本号(带 30s TTL 缓存),失败返回 null。
|
|
525
|
+
* 底层直连 execFileAsync 而不走 this.run():run() 会先解析工作区(可能读
|
|
526
|
+
* workspace.json 与目录扫描)再做截断/JSON 解析,对一次 10ms 的版本字符串
|
|
527
|
+
* 查询是纯开销;直连也彻底规避 runGuarded → 探测 → run 的递归风险。
|
|
528
|
+
*/
|
|
529
|
+
async getGhVersion() {
|
|
530
|
+
const now = Date.now();
|
|
531
|
+
if (this.ghVersionCache && now - this.ghVersionCache.at < GH_VERSION_CACHE_TTL_MS) {
|
|
532
|
+
return this.ghVersionCache.value;
|
|
533
|
+
}
|
|
534
|
+
const version = parseGhVersion(await this.probeGhVersionRaw());
|
|
535
|
+
// 只缓存成功解析值:失败不落缓存,用户装好 gh 后下一次探测立即生效
|
|
536
|
+
if (version) {
|
|
537
|
+
this.ghVersionCache = { at: now, value: version };
|
|
538
|
+
}
|
|
539
|
+
return version;
|
|
540
|
+
}
|
|
541
|
+
/**
|
|
542
|
+
* 底层版本探测(独立方法而非内联:测试需要在不触发真实子进程的前提下
|
|
543
|
+
* 验证 getGhVersion 的缓存分支——覆写此方法即注入确定输出)。
|
|
544
|
+
* 直连 execFileAsync 而不走 this.run():run() 会先解析工作区(可能读
|
|
545
|
+
* workspace.json 与目录扫描)再做截断/JSON 解析,对一次 10ms 的版本字符串
|
|
546
|
+
* 查询是纯开销;直连也彻底规避 runGuarded → 探测 → run 的递归风险。
|
|
547
|
+
*/
|
|
548
|
+
async probeGhVersionRaw() {
|
|
549
|
+
try {
|
|
550
|
+
const { stdout } = await execFileAsync(this.ghPath, ['--version'], {
|
|
551
|
+
timeout: 3_000,
|
|
552
|
+
windowsHide: true,
|
|
553
|
+
});
|
|
554
|
+
return String(stdout);
|
|
555
|
+
}
|
|
556
|
+
catch {
|
|
557
|
+
// 未安装/超时/PATH 抖动:返回空串,由 parseGhVersion 归一为 null → fail-open
|
|
558
|
+
return '';
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
/**
|
|
562
|
+
* 守卫执行器:按 gh 版本门槛路由「主命令 → 降级链路 → 升级指引」三态。
|
|
563
|
+
*
|
|
564
|
+
* 为什么是「检查+执行」一体的方法而非纯探测接口:检查与执行分离会在两者之间
|
|
565
|
+
* 留下竞隙(版本可能在间隙变化),且降级标注逻辑会被迫散落到每个工具的 if/else。
|
|
566
|
+
*
|
|
567
|
+
* fail-open 纪律:版本探测自身失败(null)时直接执行主命令——守卫是路由优化
|
|
568
|
+
* 不是安全边界,探测故障不应拦截业务;gh 未安装场景由主命令产出既有 ENOENT
|
|
569
|
+
* 安装引导,这里不重复造错误路径。
|
|
570
|
+
*/
|
|
571
|
+
async runGuarded(args, options = {}) {
|
|
572
|
+
const { minVersion, capability, fallback, ...runOptions } = options;
|
|
573
|
+
if (!minVersion) {
|
|
574
|
+
return this.run(args, runOptions);
|
|
575
|
+
}
|
|
576
|
+
const current = await this.getGhVersion();
|
|
577
|
+
// 探测失败 fail-open:直接执行主命令(见方法注释),错误路径交由主命令的既有处理
|
|
578
|
+
if (current === null || compareGhVersions(current, minVersion) >= 0) {
|
|
579
|
+
return this.run(args, runOptions);
|
|
580
|
+
}
|
|
581
|
+
if (fallback) {
|
|
582
|
+
const res = await fallback();
|
|
583
|
+
return {
|
|
584
|
+
...res,
|
|
585
|
+
degraded: true,
|
|
586
|
+
degradedReason: `当前 gh ${current} 低于 ${capability || minVersion} 所需版本 ${minVersion},已切换 API 降级链路`,
|
|
587
|
+
};
|
|
588
|
+
}
|
|
589
|
+
return {
|
|
590
|
+
ok: false,
|
|
591
|
+
error: `此能力需要 gh ≥ ${minVersion}(当前 ${current})。${this.getGhUpgradeHint()}`,
|
|
592
|
+
};
|
|
593
|
+
}
|
|
594
|
+
/** gh 升级命令按平台提示(复用三态状态机的平台判断口径) */
|
|
595
|
+
getGhUpgradeHint() {
|
|
596
|
+
const p = process.platform;
|
|
597
|
+
if (p === 'win32')
|
|
598
|
+
return '升级命令:`winget upgrade GitHub.cli`(或从 https://cli.github.com/ 下载最新 .msi)。';
|
|
599
|
+
if (p === 'darwin')
|
|
600
|
+
return '升级命令:`brew upgrade gh`(或从 https://cli.github.com/ 下载最新安装包)。';
|
|
601
|
+
return '升级方式:更新系统包(Debian/Ubuntu: `sudo apt update && sudo apt install gh`;Fedora: `sudo dnf upgrade gh`),或见 https://cli.github.com/。';
|
|
602
|
+
}
|
|
603
|
+
/**
|
|
604
|
+
* 本地 Git 状态只读探测(白名单通道)。
|
|
605
|
+
*
|
|
606
|
+
* 这是本插件首次执行 git(此前仅 gh)——git 存在大量写命令,风险等级高于 gh,
|
|
607
|
+
* 因此白名单不落在「命令名过滤」而落在「命令形态常量」:spawn 参数是下方
|
|
608
|
+
* 硬编码的数组字面量,无任何动态拼接路径,写操作物理不可达(cwd 之外零输入,
|
|
609
|
+
* 而 cwd 本身也是插件解析的工作区路径)。
|
|
610
|
+
*
|
|
611
|
+
* 零网络:status 基于 remote-tracking ref(上次 fetch 的快照),毫秒级本地读取,
|
|
612
|
+
* 符合 global-overview 端点「工作区探测不发网络」的性能红线。
|
|
613
|
+
*/
|
|
614
|
+
async getLocalGitStatus(cwd) {
|
|
615
|
+
try {
|
|
616
|
+
// 纯本地毫秒级;3s 超时兜底异常文件系统
|
|
617
|
+
const { stdout } = await execFileAsync('git', ['status', '-sb', '--porcelain'], {
|
|
618
|
+
cwd: cwd || undefined,
|
|
619
|
+
timeout: 3_000,
|
|
620
|
+
windowsHide: true,
|
|
621
|
+
});
|
|
622
|
+
return parseGitStatusOutput(String(stdout));
|
|
623
|
+
}
|
|
624
|
+
catch {
|
|
625
|
+
// 非 Git 目录 / git 未安装 / 超时:静默降级为 null(调用方不渲染该行)
|
|
626
|
+
return null;
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
/**
|
|
630
|
+
* 本地 Git 提交记录(三段一次返回:本地领先 / 远程领先 / 最近提交)。
|
|
631
|
+
*
|
|
632
|
+
* 白名单纪律与 getLocalGitStatus 同源:下方两个命令形态常量硬编码
|
|
633
|
+
* (log + 修订范围语法,range 段由 hasUpstream 守卫后才拼接且仅两个
|
|
634
|
+
* 固定形态 @{u}..HEAD / HEAD..@{u}——不含任何用户输入路径)。
|
|
635
|
+
* 领先/落后列表基于 remote-tracking ref(上次 fetch 快照),与 status
|
|
636
|
+
* 的 ahead/behind 计数同一数据口径;格式 %h|%an|%ar|%s 实测可结构化解析。
|
|
637
|
+
*/
|
|
638
|
+
async getGitCommitLog(cwd, hasUpstream = true, limit = 15) {
|
|
639
|
+
const empty = { aheadCommits: [], behindCommits: [], recent: [] };
|
|
640
|
+
const logFmt = '--pretty=format:%x1e%h%x1f%H%x1f%an%x1f%ae%x1f%ad%x1f%ar%x1f%s%x1f%b%x1f';
|
|
641
|
+
try {
|
|
642
|
+
const recentRes = await execFileAsync('git', ['log', `-${Math.max(1, limit)}`, logFmt, '--stat', '--no-decorate'], {
|
|
643
|
+
cwd: cwd || undefined,
|
|
644
|
+
timeout: 4_000,
|
|
645
|
+
windowsHide: true,
|
|
646
|
+
});
|
|
647
|
+
empty.recent = parseGitLogLines(String(recentRes.stdout));
|
|
648
|
+
try {
|
|
649
|
+
const countRes = await execFileAsync('git', ['rev-list', '--count', 'HEAD'], {
|
|
650
|
+
cwd: cwd || undefined,
|
|
651
|
+
timeout: 2_000,
|
|
652
|
+
windowsHide: true,
|
|
653
|
+
});
|
|
654
|
+
const cnt = parseInt(String(countRes.stdout).trim(), 10);
|
|
655
|
+
if (!isNaN(cnt))
|
|
656
|
+
empty.totalCount = cnt;
|
|
657
|
+
}
|
|
658
|
+
catch { }
|
|
659
|
+
if (!hasUpstream)
|
|
660
|
+
return empty; // 无 upstream:领先/落后无从谈起,仅最近提交
|
|
661
|
+
const aheadRes = await execFileAsync('git', ['log', '@{u}..HEAD', logFmt, '--stat', '--no-decorate'], {
|
|
662
|
+
cwd: cwd || undefined,
|
|
663
|
+
timeout: 3_000,
|
|
664
|
+
windowsHide: true,
|
|
665
|
+
});
|
|
666
|
+
empty.aheadCommits = parseGitLogLines(String(aheadRes.stdout));
|
|
667
|
+
const behindRes = await execFileAsync('git', ['log', 'HEAD..@{u}', logFmt, '--stat', '--no-decorate'], {
|
|
668
|
+
cwd: cwd || undefined,
|
|
669
|
+
timeout: 3_000,
|
|
670
|
+
windowsHide: true,
|
|
671
|
+
});
|
|
672
|
+
empty.behindCommits = parseGitLogLines(String(behindRes.stdout));
|
|
673
|
+
return empty;
|
|
674
|
+
}
|
|
675
|
+
catch {
|
|
676
|
+
return null;
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
/**
|
|
680
|
+
* 分页获取最近提交记录(用于 Git 面板下滑触底继续加载)。
|
|
681
|
+
*/
|
|
682
|
+
async getGitRecentLog(cwd, limit = 15, skip = 0) {
|
|
683
|
+
const logFmt = '--pretty=format:%x1e%h%x1f%H%x1f%an%x1f%ae%x1f%ad%x1f%ar%x1f%s%x1f%b%x1f';
|
|
684
|
+
const safeLimit = Math.min(Math.max(1, limit), 50);
|
|
685
|
+
const safeSkip = Math.max(0, skip);
|
|
686
|
+
const args = ['log', `-${safeLimit}`, logFmt, '--stat', '--no-decorate'];
|
|
687
|
+
if (safeSkip > 0) {
|
|
688
|
+
args.push('--skip', String(safeSkip));
|
|
689
|
+
}
|
|
690
|
+
try {
|
|
691
|
+
const { stdout } = await execFileAsync('git', args, {
|
|
692
|
+
cwd: cwd || undefined,
|
|
693
|
+
timeout: 5_000,
|
|
694
|
+
windowsHide: true,
|
|
695
|
+
});
|
|
696
|
+
const commits = parseGitLogLines(String(stdout));
|
|
697
|
+
let totalCount;
|
|
698
|
+
try {
|
|
699
|
+
const countRes = await execFileAsync('git', ['rev-list', '--count', 'HEAD'], {
|
|
700
|
+
cwd: cwd || undefined,
|
|
701
|
+
timeout: 2_000,
|
|
702
|
+
windowsHide: true,
|
|
703
|
+
});
|
|
704
|
+
const cnt = parseInt(String(countRes.stdout).trim(), 10);
|
|
705
|
+
if (!isNaN(cnt))
|
|
706
|
+
totalCount = cnt;
|
|
707
|
+
}
|
|
708
|
+
catch { }
|
|
709
|
+
return { commits, totalCount };
|
|
710
|
+
}
|
|
711
|
+
catch {
|
|
712
|
+
return { commits: [] };
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
/**
|
|
716
|
+
* 单条提交详情(点击提交行弹窗的数据源)。
|
|
717
|
+
* 白名单纪律延续:show 命令形态常量硬编码;sha 参数经 ^[0-9a-f]{4,40}$
|
|
718
|
+
* 十六进制校验后才拼接——这是 git 白名单通道首次接受「外部传入的单值」,
|
|
719
|
+
* 正则把值域锁死在纯十六进制(注入面为零:任何 shell 元字符/子命令
|
|
720
|
+
* 语法都不可能通过校验)。输出为 git show --stat 原文(含文件变更统计)。
|
|
721
|
+
*/
|
|
722
|
+
async getGitCommitDetail(cwd, sha) {
|
|
723
|
+
if (!/^[0-9a-f]{4,40}$/i.test(sha))
|
|
724
|
+
return null; // 形态校验:纯十六进制短/全 sha
|
|
725
|
+
try {
|
|
726
|
+
const { stdout } = await execFileAsync('git', ['show', '--stat', '--pretty=format:%H%n%an <%ae>%n%ad%n%s%n%b', sha], {
|
|
727
|
+
cwd: cwd || undefined,
|
|
728
|
+
timeout: 3_000,
|
|
729
|
+
windowsHide: true,
|
|
730
|
+
});
|
|
731
|
+
return String(stdout);
|
|
732
|
+
}
|
|
733
|
+
catch {
|
|
734
|
+
return null;
|
|
735
|
+
}
|
|
736
|
+
}
|
|
380
737
|
/** 生成当前操作系统平台的推荐安装引导与命令 */
|
|
381
738
|
getPlatformInstallGuide() {
|
|
382
739
|
const p = process.platform;
|
package/lib/main.js
CHANGED
|
@@ -45,6 +45,7 @@ export const Config = {
|
|
|
45
45
|
cacheTtlMs: typeof cfg.cacheTtlMs === 'number' ? cfg.cacheTtlMs : 15_000,
|
|
46
46
|
defaultListLimit: typeof cfg.defaultListLimit === 'number' ? cfg.defaultListLimit : 20,
|
|
47
47
|
checkUpdate: typeof cfg.checkUpdate === 'boolean' ? cfg.checkUpdate : true,
|
|
48
|
+
agentEnvInjection: cfg.agentEnvInjection !== false,
|
|
48
49
|
updateCheckIntervalMs: typeof cfg.updateCheckIntervalMs === 'number' ? cfg.updateCheckIntervalMs : 86_400_000,
|
|
49
50
|
registryUrl: typeof cfg.registryUrl === 'string' ? cfg.registryUrl : undefined,
|
|
50
51
|
proxyUrl: typeof cfg.proxyUrl === 'string' ? cfg.proxyUrl : undefined,
|
|
@@ -20,6 +20,12 @@ export declare function formatStateBadge(state?: string, isDraft?: boolean): str
|
|
|
20
20
|
export declare function formatCiBadge(status?: string, conclusion?: string): string;
|
|
21
21
|
/** 格式化合并冲突状态 */
|
|
22
22
|
export declare function formatConflictBadge(status?: string): string;
|
|
23
|
+
/**
|
|
24
|
+
* 降级链路可见标注(runGuarded 契约的渲染兑现)。
|
|
25
|
+
* 为什么必须可见:降级若是静默的,Agent 就无法感知能力边界(同一 action 在
|
|
26
|
+
* 不同 gh 版本上走不同链路);一条 blockquote 注释成本一行,换链路透明。
|
|
27
|
+
*/
|
|
28
|
+
export declare function appendDegradedNote(value: any): string;
|
|
23
29
|
export declare function renderPrMarkdown(args: any, value: any): string;
|
|
24
30
|
export declare function renderIssueMarkdown(args: any, value: any): string;
|
|
25
31
|
export declare function renderRunMarkdown(args: any, value: any): string;
|