agiledev-cli 0.1.4 → 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.md +300 -173
- package/SKILL.md +114 -98
- package/adev.js +1319 -1285
- package/autofix/agents/antigravity.js +379 -0
- package/autofix/agents/claude.js +147 -0
- package/autofix/agents/codex.js +179 -0
- package/autofix/agents/exec.js +230 -0
- package/autofix/agents/index.js +400 -0
- package/autofix/agents/opencode.js +233 -0
- package/autofix/agents/pi.js +247 -0
- package/autofix/agiledev.js +98 -0
- package/autofix/bridge.js +371 -0
- package/autofix/cli.js +712 -0
- package/autofix/context.js +152 -0
- package/autofix/conversation.js +302 -0
- package/autofix/coordinator.js +2892 -0
- package/autofix/git.js +459 -0
- package/autofix/policy.js +230 -0
- package/autofix/profile.js +578 -0
- package/autofix/public/app.js +3668 -0
- package/autofix/public/i18n-id.js +282 -0
- package/autofix/public/index.html +32 -0
- package/autofix/public/style.css +2463 -0
- package/autofix/queue.js +146 -0
- package/autofix/scheduler.js +994 -0
- package/autofix/scope.js +32 -0
- package/autofix/service.js +1940 -0
- package/autofix/store.js +375 -0
- package/autofix/workspace.js +298 -0
- package/autofix/worktree.js +379 -0
- package/docs/autofix-design.md +801 -0
- package/docs/autofix-profiles.example.json +65 -0
- package/imgs/Bug/350/247/243/345/206/263/345/244/207/346/263/250.png +0 -0
- package/imgs/logo.png +0 -0
- package/imgs//344/277/256/346/224/271Bug/347/212/266/346/200/201.png +0 -0
- package/imgs//345/270/256/346/210/221/344/277/256/345/244/215Bug-20260922-010.png +0 -0
- package/imgs//346/237/245/347/234/213/345/271/266/345/210/206/346/236/220/351/234/200/346/261/202/346/226/207/346/241/243.png +0 -0
- package/imgs//347/234/213/344/270/200/344/270/213/346/210/221/347/216/260/345/234/250/346/234/211/345/223/252/344/272/233/345/274/200/345/217/221/344/273/273/345/212/241.png +0 -0
- package/imgs//347/234/213/344/270/200/344/270/213/346/210/221/350/277/230/346/234/211/345/223/252/344/272/233Bug/346/234/252/350/247/243/345/206/263.png +0 -0
- package/package.json +39 -36
|
@@ -0,0 +1,578 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 工作区配置档(Profile):把「敏捷项目 ↔ 代码仓库 ↔ 分支策略 ↔ 巡检时段」显式声明出来。
|
|
3
|
+
*
|
|
4
|
+
* 为什么需要它:
|
|
5
|
+
* 本地目录与 AgileDev 项目之间没有平台级唯一 ID,一个敏捷项目也往往横跨多个仓库
|
|
6
|
+
* (前端 / 后端 / 移动端)。只靠"当前目录 + 自动探测子目录"无法表达这种结构,
|
|
7
|
+
* 修复 Agent 也就说不清"这个 bug 该改哪个仓库"。
|
|
8
|
+
*
|
|
9
|
+
* 因此引入配置档:一份声明说清楚
|
|
10
|
+
* - 这个档对应哪些敏捷项目(projectIds)
|
|
11
|
+
* - 主目录在哪(root)
|
|
12
|
+
* - 有哪些仓库、每个仓库是什么角色(frontend/backend/mobile)、用哪条分支策略
|
|
13
|
+
* - 巡检时段(schedule)与复核阈值(review)
|
|
14
|
+
*
|
|
15
|
+
* 配置来源(就近覆盖):
|
|
16
|
+
* 1. 项目内 .autofix.json 或 .autofix/profiles.json
|
|
17
|
+
* 2. 全局 ~/.agiledev-autofix/profiles.json
|
|
18
|
+
*
|
|
19
|
+
* 所有路径都会按 root 归一为绝对路径,配置写相对路径即可。
|
|
20
|
+
*/
|
|
21
|
+
import fs from "node:fs";
|
|
22
|
+
import os from "node:os";
|
|
23
|
+
import path from "node:path";
|
|
24
|
+
|
|
25
|
+
import { homeDir, readJsonFile, writeJsonAtomic } from "./store.js";
|
|
26
|
+
|
|
27
|
+
export const PROFILE_VERSION = 1;
|
|
28
|
+
|
|
29
|
+
/** 仓库角色:决定跨端判定与验证命令归属。 */
|
|
30
|
+
export const REPO_ROLES = Object.freeze(["frontend", "backend", "mobile", "other"]);
|
|
31
|
+
|
|
32
|
+
/** 角色中文名,用于控制台与提示词。 */
|
|
33
|
+
export const ROLE_LABELS = Object.freeze({
|
|
34
|
+
frontend: "前端",
|
|
35
|
+
backend: "后端",
|
|
36
|
+
mobile: "移动端",
|
|
37
|
+
other: "其他",
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
/** 项目内配置文件名(按优先级从高到低)。 */
|
|
41
|
+
const LOCAL_PROFILE_FILES = [
|
|
42
|
+
".autofix.json",
|
|
43
|
+
path.join(".autofix", "profiles.json"),
|
|
44
|
+
];
|
|
45
|
+
|
|
46
|
+
/* ======================== 读取配置源 ======================== */
|
|
47
|
+
|
|
48
|
+
export function globalProfilePath() {
|
|
49
|
+
return path.join(homeDir(), "profiles.json");
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 读取全部配置源,按优先级合并:项目内覆盖全局。
|
|
54
|
+
* 返回 { id: rawProfile }。
|
|
55
|
+
*/
|
|
56
|
+
export function readProfileSources(cwd = process.cwd()) {
|
|
57
|
+
const merged = new Map();
|
|
58
|
+
|
|
59
|
+
const apply = (raw, source) => {
|
|
60
|
+
const list = raw?.profiles && typeof raw.profiles === "object" ? raw.profiles : {};
|
|
61
|
+
for (const [id, value] of Object.entries(list)) {
|
|
62
|
+
if (!value || typeof value !== "object") continue;
|
|
63
|
+
const key = String(id).trim();
|
|
64
|
+
if (!key) continue;
|
|
65
|
+
// 项目内配置覆盖全局:整档替换而不是深合并,避免半截字段拼出错误配置
|
|
66
|
+
merged.set(key, { ...value, id: key, __source: source });
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const globalFile = globalProfilePath();
|
|
71
|
+
apply(readJsonFile(globalFile, null), globalFile);
|
|
72
|
+
|
|
73
|
+
for (const rel of LOCAL_PROFILE_FILES) {
|
|
74
|
+
const file = path.join(cwd, rel);
|
|
75
|
+
if (!fs.existsSync(file)) continue;
|
|
76
|
+
apply(readJsonFile(file, null), file);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return merged;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** 写出全局配置文件(供 `adev autofix profile set` 之类的命令使用)。 */
|
|
83
|
+
export function writeGlobalProfiles(profiles) {
|
|
84
|
+
const file = globalProfilePath();
|
|
85
|
+
writeJsonAtomic(file, { version: PROFILE_VERSION, profiles: profiles || {} });
|
|
86
|
+
return file;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/* ======================== 归一化 ======================== */
|
|
90
|
+
|
|
91
|
+
function normalizeRole(raw) {
|
|
92
|
+
const s = String(raw || "").trim().toLowerCase();
|
|
93
|
+
if (REPO_ROLES.includes(s)) return s;
|
|
94
|
+
if (/前端|front|web|h5$|pc/i.test(s)) return "frontend";
|
|
95
|
+
if (/后端|back|server|api|java/i.test(s)) return "backend";
|
|
96
|
+
if (/移动|移动端|app|uniapp|mini|小程序/i.test(s)) return "mobile";
|
|
97
|
+
return "other";
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function normalizeStringList(raw) {
|
|
101
|
+
if (raw == null) return [];
|
|
102
|
+
const list = Array.isArray(raw) ? raw : [raw];
|
|
103
|
+
return list.map(v => String(v ?? "").trim()).filter(Boolean);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function normalizeRepo(raw, root) {
|
|
107
|
+
if (!raw || typeof raw !== "object") return null;
|
|
108
|
+
const rel = String(raw.path || raw.dir || raw.name || "").trim();
|
|
109
|
+
if (!rel) return null;
|
|
110
|
+
const abs = path.isAbsolute(rel) ? path.resolve(rel) : path.resolve(root, rel);
|
|
111
|
+
const name = String(raw.name || path.basename(abs)).trim();
|
|
112
|
+
return {
|
|
113
|
+
name,
|
|
114
|
+
path: abs,
|
|
115
|
+
relPath: path.relative(root, abs).replace(/\\/g, "/") || ".",
|
|
116
|
+
role: normalizeRole(raw.role),
|
|
117
|
+
// 开发分支:修复提交落在哪个分支上
|
|
118
|
+
branch: String(raw.branch || raw.devBranch || raw.featureBranch || "").trim() || null,
|
|
119
|
+
// 测试分支:提测时合入哪个分支
|
|
120
|
+
testBranch: String(raw.testBranch || raw.test || "").trim() || null,
|
|
121
|
+
// 该仓库自己的验证命令(相对仓库根目录执行)
|
|
122
|
+
verify: normalizeStringList(raw.verify || raw.verifyCommands),
|
|
123
|
+
// 是否允许自动 push(默认跟随全局,工作区可覆盖)
|
|
124
|
+
push: raw.push === undefined ? null : raw.push === true,
|
|
125
|
+
// 该仓库是否纳入自动修复范围(默认纳入)
|
|
126
|
+
enabled: raw.enabled !== false,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* 归一化一个配置档。root 缺省时用传入的 cwd。
|
|
132
|
+
* 校验失败返回 { id, error },调用方据此提示用户,不静默丢配置。
|
|
133
|
+
*/
|
|
134
|
+
export function normalizeProfile(raw, { cwd = process.cwd() } = {}) {
|
|
135
|
+
const id = String(raw?.id || "").trim();
|
|
136
|
+
if (!id) return { id: "", error: "配置档缺少 id" };
|
|
137
|
+
|
|
138
|
+
const rootRaw = String(raw?.root || raw?.path || cwd).trim();
|
|
139
|
+
const root = path.isAbsolute(rootRaw) ? path.resolve(rootRaw) : path.resolve(cwd, rootRaw);
|
|
140
|
+
|
|
141
|
+
const repos = [];
|
|
142
|
+
for (const item of Array.isArray(raw?.repos) ? raw.repos : []) {
|
|
143
|
+
const repo = normalizeRepo(item, root);
|
|
144
|
+
if (repo) repos.push(repo);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const schedule = normalizeSchedule(raw?.schedule);
|
|
148
|
+
const review = normalizeReview(raw?.review);
|
|
149
|
+
|
|
150
|
+
return {
|
|
151
|
+
id,
|
|
152
|
+
label: String(raw?.label || raw?.name || id).trim() || id,
|
|
153
|
+
root,
|
|
154
|
+
projectIds: normalizeStringList(raw?.projectIds || raw?.projects),
|
|
155
|
+
projectNames: normalizeStringList(raw?.projectNames),
|
|
156
|
+
repos,
|
|
157
|
+
verifyCommands: normalizeStringList(raw?.verifyCommands || raw?.verify),
|
|
158
|
+
intervalSeconds: Number.parseInt(raw?.intervalSeconds, 10) || null,
|
|
159
|
+
agent: String(raw?.agent || "").trim() || null,
|
|
160
|
+
workerAgent: String(raw?.workerAgent || "").trim() || null,
|
|
161
|
+
model: String(raw?.model || "").trim() || null,
|
|
162
|
+
workerModel: String(raw?.workerModel || "").trim() || null,
|
|
163
|
+
prompt: String(raw?.prompt || "").trim() || null,
|
|
164
|
+
maxWorkers: Number.isFinite(Number(raw?.maxWorkers)) ? Number(raw.maxWorkers) : null,
|
|
165
|
+
autoVerify: raw?.autoVerify === true,
|
|
166
|
+
// 是否自动 push 测试分支;null 表示由调用方用默认值(true)
|
|
167
|
+
push: raw?.push === undefined ? null : raw.push === true,
|
|
168
|
+
schedule,
|
|
169
|
+
review,
|
|
170
|
+
source: raw?.__source || null,
|
|
171
|
+
error: null,
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export function normalizeSchedule(raw) {
|
|
176
|
+
if (!raw || typeof raw !== "object") {
|
|
177
|
+
// 默认:只在工作日巡检,且不限制时段(避免"以为在跑其实被静默跳过")
|
|
178
|
+
return { workdaysOnly: true, from: null, to: null };
|
|
179
|
+
}
|
|
180
|
+
const from = parseClockTime(raw.from ?? raw.start ?? raw.begin);
|
|
181
|
+
const to = parseClockTime(raw.to ?? raw.end ?? raw.finish);
|
|
182
|
+
return {
|
|
183
|
+
workdaysOnly: raw.workdaysOnly !== false,
|
|
184
|
+
from,
|
|
185
|
+
to,
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export function normalizeReview(raw) {
|
|
190
|
+
const src = raw && typeof raw === "object" ? raw : {};
|
|
191
|
+
return {
|
|
192
|
+
// 改动文件数达到该阈值必须复核
|
|
193
|
+
fileThreshold: Number.isFinite(Number(src.fileThreshold)) ? Number(src.fileThreshold) : 6,
|
|
194
|
+
// 跨端(前端 + 后端 / 移动端)改动必须复核
|
|
195
|
+
crossRoleRequiresReview: src.crossRoleRequiresReview !== false,
|
|
196
|
+
// 命中这些关键词的缺陷一律必须复核(权限、金额、订单、事务……)
|
|
197
|
+
keywords: normalizeStringList(src.keywords).length
|
|
198
|
+
? normalizeStringList(src.keywords)
|
|
199
|
+
: ["权限", "授权", "数据范围", "金额", "订单", "结算", "库存", "支付", "退款", "缓存", "事务", "SQL", "鉴权"],
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** "09:30" → 分钟数;非法返回 null。 */
|
|
204
|
+
export function parseClockTime(raw) {
|
|
205
|
+
if (raw == null || raw === "") return null;
|
|
206
|
+
const m = String(raw).trim().match(/^(\d{1,2})\s*[::.]\s*(\d{1,2})$/);
|
|
207
|
+
if (!m) return null;
|
|
208
|
+
const h = Number.parseInt(m[1], 10);
|
|
209
|
+
const min = Number.parseInt(m[2], 10);
|
|
210
|
+
if (!Number.isFinite(h) || !Number.isFinite(min)) return null;
|
|
211
|
+
if (h < 0 || h > 23 || min < 0 || min > 59) return null;
|
|
212
|
+
return h * 60 + min;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/* ======================== 巡检时段 ======================== */
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* 判断当前时刻是否落在巡检时段内。
|
|
219
|
+
*
|
|
220
|
+
* 语义要保守:时段没配时永远为 true(不允许"因为忘配时段而静默不干活");
|
|
221
|
+
* 越界时只是跳过本轮检测,任务记录一条都不删。
|
|
222
|
+
*/
|
|
223
|
+
export function withinSchedule(schedule, now = new Date()) {
|
|
224
|
+
const s = schedule && typeof schedule === "object"
|
|
225
|
+
? schedule
|
|
226
|
+
: { workdaysOnly: true, from: null, to: null };
|
|
227
|
+
|
|
228
|
+
if (s.workdaysOnly) {
|
|
229
|
+
const day = now.getDay();
|
|
230
|
+
if (day === 0 || day === 6) {
|
|
231
|
+
return { ok: false, reason: "weekend", message: "非工作日(周末),本轮不巡检" };
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
if (s.from == null && s.to == null) return { ok: true, reason: null, message: null };
|
|
236
|
+
|
|
237
|
+
const minutes = now.getHours() * 60 + now.getMinutes();
|
|
238
|
+
if (s.from != null && s.to != null) {
|
|
239
|
+
if (s.from <= s.to) {
|
|
240
|
+
if (minutes < s.from || minutes > s.to) {
|
|
241
|
+
return { ok: false, reason: "outside_window", message: `不在巡检时段 ${formatClock(s.from)}–${formatClock(s.to)}` };
|
|
242
|
+
}
|
|
243
|
+
} else if (minutes < s.from && minutes > s.to) {
|
|
244
|
+
// 跨零点时段(例如 22:00–06:00)
|
|
245
|
+
return { ok: false, reason: "outside_window", message: `不在巡检时段 ${formatClock(s.from)}–${formatClock(s.to)}` };
|
|
246
|
+
}
|
|
247
|
+
} else if (s.from != null && minutes < s.from) {
|
|
248
|
+
return { ok: false, reason: "before_window", message: `早于巡检开始时间 ${formatClock(s.from)}` };
|
|
249
|
+
} else if (s.to != null && minutes > s.to) {
|
|
250
|
+
return { ok: false, reason: "after_window", message: `晚于巡检结束时间 ${formatClock(s.to)}` };
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
return { ok: true, reason: null, message: null };
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
export function formatClock(minutes) {
|
|
257
|
+
if (minutes == null) return "?";
|
|
258
|
+
const h = Math.floor(minutes / 60);
|
|
259
|
+
const m = minutes % 60;
|
|
260
|
+
return `${String(h).padStart(2, "0")}:${String(m).padStart(2, "0")}`;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/** 巡检时段的文字描述,用于控制台展示。 */
|
|
264
|
+
export function describeSchedule(schedule) {
|
|
265
|
+
const s = schedule || {};
|
|
266
|
+
const days = s.workdaysOnly ? "工作日" : "每天";
|
|
267
|
+
const from = s.from == null ? null : formatClock(s.from);
|
|
268
|
+
const to = s.to == null ? null : formatClock(s.to);
|
|
269
|
+
if (from && to) return `${days} ${from}–${to}`;
|
|
270
|
+
if (from) return `${days} ${from} 起`;
|
|
271
|
+
if (to) return `${days} 至 ${to}`;
|
|
272
|
+
return days;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/* ======================== 选取与派生 ======================== */
|
|
276
|
+
|
|
277
|
+
/** 列出全部配置档(已归一化)。 */
|
|
278
|
+
export function listProfiles(cwd = process.cwd()) {
|
|
279
|
+
const sources = readProfileSources(cwd);
|
|
280
|
+
const out = [];
|
|
281
|
+
for (const raw of sources.values()) {
|
|
282
|
+
const p = normalizeProfile(raw, { cwd });
|
|
283
|
+
if (!p.error) out.push(p);
|
|
284
|
+
}
|
|
285
|
+
return out;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* 按目录匹配配置档:root 命中当前目录(大小写不敏感、真实路径归一)时优先。
|
|
290
|
+
* 返回匹配到的配置档数组,调用方取第一个或让用户选。
|
|
291
|
+
*/
|
|
292
|
+
export function matchProfilesByPath(profiles = [], cwd = process.cwd()) {
|
|
293
|
+
const target = safeRealPath(cwd).toLowerCase();
|
|
294
|
+
return profiles.filter(p => {
|
|
295
|
+
if (!p?.root) return false;
|
|
296
|
+
return safeRealPath(p.root).toLowerCase() === target;
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
function safeRealPath(p) {
|
|
301
|
+
try { return fs.realpathSync.native(path.resolve(p)); } catch {
|
|
302
|
+
try { return fs.realpathSync(path.resolve(p)); } catch { return path.resolve(p); }
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* 解析要使用的配置档:
|
|
308
|
+
* 1. 显式指定 id → 必须找到,找不到就报错(不静默回退)
|
|
309
|
+
* 2. 当前目录命中某个档 → 用它
|
|
310
|
+
* 3. 只有一个档 → 用它
|
|
311
|
+
* 4. 否则返回 null(由调用方进入配置页让用户选)
|
|
312
|
+
*/
|
|
313
|
+
export function resolveProfile({ cwd = process.cwd(), profileId = null, profiles = null } = {}) {
|
|
314
|
+
const list = profiles || listProfiles(cwd);
|
|
315
|
+
if (!list.length) return { ok: false, code: "no_profile", message: "没有找到任何工作区配置档。" };
|
|
316
|
+
|
|
317
|
+
if (profileId) {
|
|
318
|
+
const hit = list.find(p => p.id === String(profileId).trim());
|
|
319
|
+
if (!hit) {
|
|
320
|
+
return {
|
|
321
|
+
ok: false,
|
|
322
|
+
code: "profile_not_found",
|
|
323
|
+
message: `找不到配置档「${profileId}」。可用:${list.map(p => p.id).join("、") || "(无)"}`,
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
return { ok: true, profile: hit };
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
const matched = matchProfilesByPath(list, cwd);
|
|
330
|
+
if (matched.length) return { ok: true, profile: matched[0] };
|
|
331
|
+
if (list.length === 1) return { ok: true, profile: list[0] };
|
|
332
|
+
|
|
333
|
+
return {
|
|
334
|
+
ok: false,
|
|
335
|
+
code: "profile_ambiguous",
|
|
336
|
+
message: `有多个配置档(${list.map(p => p.id).join("、")}),请用 --profile 指定,或在控制台选择。`,
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* 把配置档派生成运行时工作区结构(scheduler / coordinator 直接消费)。
|
|
342
|
+
* repos 里的相对路径保持"相对 root",方便跨端判定与展示。
|
|
343
|
+
*/
|
|
344
|
+
export function profileToWorkspace(profile, { workspacePath = null, remoteKey = null } = {}) {
|
|
345
|
+
if (!profile) return null;
|
|
346
|
+
const root = profile.root;
|
|
347
|
+
const repos = profile.repos
|
|
348
|
+
.filter(r => r.enabled !== false)
|
|
349
|
+
.map(r => ({
|
|
350
|
+
...r,
|
|
351
|
+
relPath: path.relative(root, r.path).replace(/\\/g, "/") || ".",
|
|
352
|
+
}));
|
|
353
|
+
// 被显式排除的仓库:改动落到这里时不会进入提交/验证流程
|
|
354
|
+
const excludedRepos = profile.repos
|
|
355
|
+
.filter(r => r.enabled === false)
|
|
356
|
+
.map(r => ({ ...r, relPath: path.relative(root, r.path).replace(/\\/g, "/") || "." }));
|
|
357
|
+
|
|
358
|
+
return {
|
|
359
|
+
path: workspacePath || root,
|
|
360
|
+
root,
|
|
361
|
+
profileId: profile.id,
|
|
362
|
+
profileLabel: profile.label,
|
|
363
|
+
repos,
|
|
364
|
+
excludedRepos,
|
|
365
|
+
projectIds: profile.projectIds,
|
|
366
|
+
projectNames: profile.projectNames,
|
|
367
|
+
verifyCommands: profile.verifyCommands,
|
|
368
|
+
schedule: profile.schedule,
|
|
369
|
+
review: profile.review,
|
|
370
|
+
remoteKey,
|
|
371
|
+
multiRepo: repos.length > 0,
|
|
372
|
+
// 仓库角色摘要:前端 + 后端 + 移动端
|
|
373
|
+
roles: [...new Set(repos.map(r => r.role))],
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/* ======================== 启动页的项目文件夹映射 ======================== */
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* 归一化「敏捷项目 → 本地文件夹」映射。
|
|
381
|
+
*
|
|
382
|
+
* 与配置档的区别:配置档是落盘的完整工程声明;这里的映射来自启动页的
|
|
383
|
+
* 可视化选择,一个项目可以挂多个文件夹(前端 / 后端 / 移动端),
|
|
384
|
+
* 路径既可以是绝对路径,也可以是相对当前工作区的路径。
|
|
385
|
+
*/
|
|
386
|
+
export function normalizeProjectFolders(raw, { root = process.cwd() } = {}) {
|
|
387
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return {};
|
|
388
|
+
const out = {};
|
|
389
|
+
|
|
390
|
+
for (const [key, value] of Object.entries(raw)) {
|
|
391
|
+
const projectId = String(key ?? "").trim();
|
|
392
|
+
if (!projectId) continue;
|
|
393
|
+
const entry = value && typeof value === "object" ? value : {};
|
|
394
|
+
const seen = new Set();
|
|
395
|
+
const folders = [];
|
|
396
|
+
|
|
397
|
+
for (const item of Array.isArray(entry.folders) ? entry.folders : []) {
|
|
398
|
+
const rawPath = String(item?.path || item?.dir || "").trim();
|
|
399
|
+
if (!rawPath) continue;
|
|
400
|
+
const abs = path.isAbsolute(rawPath) ? path.resolve(rawPath) : path.resolve(root, rawPath);
|
|
401
|
+
const dedupeKey = abs.toLowerCase();
|
|
402
|
+
if (seen.has(dedupeKey)) continue;
|
|
403
|
+
seen.add(dedupeKey);
|
|
404
|
+
folders.push({
|
|
405
|
+
name: String(item?.name || path.basename(abs)).trim() || path.basename(abs),
|
|
406
|
+
path: abs,
|
|
407
|
+
role: normalizeRole(item?.role),
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
const projectName = String(entry.projectName || entry.name || "").trim() || null;
|
|
412
|
+
if (!folders.length && !projectName) continue;
|
|
413
|
+
out[projectId] = { projectId, projectName, folders };
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
return out;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* 把项目文件夹映射转成运行时 repos(scheduler / coordinator / git 直接消费)。
|
|
421
|
+
* 每个 repo 记住所属项目,修复 Agent 就能知道某个缺陷该改哪些文件夹。
|
|
422
|
+
*/
|
|
423
|
+
/** 文件夹路径是否可用:必须是已存在的目录。 */
|
|
424
|
+
export function folderPathUsable(folderPath) {
|
|
425
|
+
const p = String(folderPath || "").trim();
|
|
426
|
+
if (!p) return false;
|
|
427
|
+
try { return fs.statSync(p).isDirectory(); } catch { return false; }
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* 找出已勾选但缺少可用本地文件夹的项目。
|
|
432
|
+
*
|
|
433
|
+
* 勾了敏捷项目却没告诉系统代码在哪,修复 Agent 只能退回启动目录或别的项目
|
|
434
|
+
* 文件夹里改,所以 /api/start 与 /api/bind 都强制拦截。配置档不豁免:
|
|
435
|
+
* 配置档的仓库没有 projectId,无法回答「这个缺陷该改哪个文件夹」。
|
|
436
|
+
*/
|
|
437
|
+
export function missingProjectFolders(projectIds = [], projectFolders = {}) {
|
|
438
|
+
const missing = [];
|
|
439
|
+
const seen = new Set();
|
|
440
|
+
for (const raw of Array.isArray(projectIds) ? projectIds : [projectIds]) {
|
|
441
|
+
const id = String(raw ?? "").trim();
|
|
442
|
+
if (!id || seen.has(id)) continue;
|
|
443
|
+
seen.add(id);
|
|
444
|
+
const entry = projectFolders?.[id];
|
|
445
|
+
const folders = (Array.isArray(entry?.folders) ? entry.folders : [])
|
|
446
|
+
.filter(f => String(f?.path || "").trim());
|
|
447
|
+
// 至少要有一个文件夹;已配置但路径被删除/移动,同样视为不可用
|
|
448
|
+
if (!folders.length || folders.some(f => !folderPathUsable(f.path))) missing.push(id);
|
|
449
|
+
}
|
|
450
|
+
return missing;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
export function projectFoldersToRepos(projectFolders, { root = process.cwd() } = {}) {
|
|
454
|
+
const repos = [];
|
|
455
|
+
const seen = new Set();
|
|
456
|
+
|
|
457
|
+
for (const entry of Object.values(projectFolders || {})) {
|
|
458
|
+
for (const folder of entry?.folders || []) {
|
|
459
|
+
const folderPath = String(folder?.path || "").trim();
|
|
460
|
+
if (!folderPath) continue;
|
|
461
|
+
const key = folderPath.toLowerCase();
|
|
462
|
+
if (seen.has(key)) continue;
|
|
463
|
+
seen.add(key);
|
|
464
|
+
repos.push({
|
|
465
|
+
name: folder.name || path.basename(folderPath),
|
|
466
|
+
path: folderPath,
|
|
467
|
+
relPath: path.relative(root, folderPath).replace(/\\/g, "/") || ".",
|
|
468
|
+
role: folder.role || "other",
|
|
469
|
+
branch: null,
|
|
470
|
+
testBranch: null,
|
|
471
|
+
verify: [],
|
|
472
|
+
push: null,
|
|
473
|
+
enabled: true,
|
|
474
|
+
projectId: entry.projectId || null,
|
|
475
|
+
projectName: entry.projectName || null,
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
return repos;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/**
|
|
484
|
+
* 一组本地文件夹的公共父目录。
|
|
485
|
+
*
|
|
486
|
+
* 一个敏捷项目可能对应前端 / 后端 / 移动端三个并列文件夹(如
|
|
487
|
+
* D:\Project\retail-admin-ui、D:\Project\retail-cloud)。修复任务需要一个
|
|
488
|
+
* 能同时容纳它们的根目录,否则工作树 / 提交都无法按仓库拆分。
|
|
489
|
+
* 不同盘符没有公共根时返回 null;公共根退化到盘符根(D:\)也算不安全,返回 null。
|
|
490
|
+
*/
|
|
491
|
+
export function commonAncestorPath(paths = []) {
|
|
492
|
+
const list = [];
|
|
493
|
+
for (const p of paths) {
|
|
494
|
+
const s = String(p || "").trim();
|
|
495
|
+
if (s) list.push(path.resolve(s));
|
|
496
|
+
}
|
|
497
|
+
if (!list.length) return null;
|
|
498
|
+
|
|
499
|
+
let common = list[0];
|
|
500
|
+
for (const p of list.slice(1)) {
|
|
501
|
+
let current = common;
|
|
502
|
+
while (true) {
|
|
503
|
+
const rel = path.relative(current, p);
|
|
504
|
+
const inside = rel === "" || (rel !== ".." && !rel.startsWith(`..${path.sep}`) && !path.isAbsolute(rel));
|
|
505
|
+
if (inside) break;
|
|
506
|
+
const parent = path.dirname(current);
|
|
507
|
+
if (parent === current) return null;
|
|
508
|
+
current = parent;
|
|
509
|
+
}
|
|
510
|
+
common = current;
|
|
511
|
+
}
|
|
512
|
+
return common === path.parse(common).root ? null : common;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/**
|
|
516
|
+
* 根据启动目录自动匹配已映射的敏捷项目。
|
|
517
|
+
*
|
|
518
|
+
* 场景:用户在 D:\work\shop-web 里执行 adev autofix,而之前已把
|
|
519
|
+
* 「商城项目」映射到该文件夹,就无需再手动勾选项目。
|
|
520
|
+
* 一个项目可能映射多个文件夹(前端 / 后端 / 移动端),这里取能覆盖
|
|
521
|
+
* 当前目录的最深一层文件夹;若多个项目映射了同一层,则全部命中。
|
|
522
|
+
*/
|
|
523
|
+
export function detectProjectFromFolders(projectFolders, cwd) {
|
|
524
|
+
const target = String(cwd || "").trim();
|
|
525
|
+
if (!target) return { cwd: null, projectIds: [], projects: [] };
|
|
526
|
+
const base = path.resolve(target);
|
|
527
|
+
const hits = [];
|
|
528
|
+
|
|
529
|
+
for (const entry of Object.values(projectFolders || {})) {
|
|
530
|
+
let best = null;
|
|
531
|
+
for (const folder of entry?.folders || []) {
|
|
532
|
+
const dir = String(folder?.path || "").trim();
|
|
533
|
+
if (!dir) continue;
|
|
534
|
+
const resolved = path.resolve(dir);
|
|
535
|
+
const rel = path.relative(resolved, base);
|
|
536
|
+
const inside = rel === "" || (rel !== ".." && !rel.startsWith(`..${path.sep}`) && !path.isAbsolute(rel));
|
|
537
|
+
if (!inside) continue;
|
|
538
|
+
// 越深的文件夹越具体,优先匹配
|
|
539
|
+
if (!best || resolved.length > best.resolved.length) best = { folder, resolved };
|
|
540
|
+
}
|
|
541
|
+
if (best) {
|
|
542
|
+
hits.push({
|
|
543
|
+
projectId: String(entry.projectId || "").trim(),
|
|
544
|
+
projectName: entry.projectName || null,
|
|
545
|
+
folder: best.folder.path,
|
|
546
|
+
depth: best.resolved.length,
|
|
547
|
+
});
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
if (!hits.length) return { cwd: base, projectIds: [], projects: [] };
|
|
552
|
+
const deepest = Math.max(...hits.map(h => h.depth));
|
|
553
|
+
const projects = hits
|
|
554
|
+
.filter(h => h.depth === deepest)
|
|
555
|
+
.map(({ depth, ...rest }) => rest);
|
|
556
|
+
return {
|
|
557
|
+
cwd: base,
|
|
558
|
+
projectIds: [...new Set(projects.map(p => p.projectId).filter(Boolean))],
|
|
559
|
+
projects,
|
|
560
|
+
};
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
/** 配置档的仓库是否覆盖了某个相对路径(用于判断改动落在哪个仓库)。 */
|
|
564
|
+
export function repoForPath(repos = [], relPath) {
|
|
565
|
+
const rel = String(relPath || "").replace(/\\/g, "/").replace(/^\.\//, "").toLowerCase();
|
|
566
|
+
if (!rel) return null;
|
|
567
|
+
let best = null;
|
|
568
|
+
for (const r of repos) {
|
|
569
|
+
const prefix = String(r.relPath || r.name || "").replace(/\\/g, "/").toLowerCase();
|
|
570
|
+
if (!prefix || prefix === ".") continue;
|
|
571
|
+
if (rel === prefix || rel.startsWith(`${prefix}/`)) {
|
|
572
|
+
if (!best || prefix.length > best.prefix.length) best = { ...r, prefix };
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
return best;
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
export { homeDir as profileHomeDir, os };
|