ai-project-manage-cli 5.0.2 → 5.0.4

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.
Files changed (2) hide show
  1. package/dist/index.js +47 -62
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -283,37 +283,17 @@ import { execFile } from "child_process";
283
283
  import { resolve as resolve2 } from "path";
284
284
  import { promisify } from "util";
285
285
  var execFileAsync = promisify(execFile);
286
- async function fetchBaselineBranchFromApi(sessionId, cwd) {
287
- const cfg = await ensureLoggedConfig();
288
- const api = createApmApiClient(cfg);
289
- const workdirPath = resolve2(cwd);
290
- const res = await api.cli.branchBaseline({
291
- sessionId,
292
- workdirPath
293
- });
294
- if (!res.repositoryId) {
295
- throw new Error(
296
- `[apm] \u672A\u5728\u5E73\u53F0\u627E\u5230\u4E0E\u5F53\u524D\u76EE\u5F55\u5339\u914D\u7684\u5DE5\u4F5C\u76EE\u5F55\u767B\u8BB0\uFF1A${workdirPath}
297
- \u8BF7\u5148\u5728\u5E73\u53F0\u767B\u8BB0\u8BE5\u8DEF\u5F84\u5BF9\u5E94\u7684\u5DE5\u4F5C\u76EE\u5F55\u4E0E\u4ED3\u5E93\u3002`
298
- );
299
- }
300
- const name = (res.defaultBranch ?? "").trim();
301
- if (!name) {
302
- throw new Error("[apm] \u5E73\u53F0\u8FD4\u56DE\u7684\u57FA\u7EBF\u5206\u652F\u540D\u4E3A\u7A7A");
303
- }
304
- return name;
305
- }
306
- function branchNameForRequirement(requirementId) {
307
- const id = requirementId.trim();
286
+ function branchNameForSession(sessionId) {
287
+ const id = sessionId.trim();
308
288
  if (!id) {
309
- throw new Error("[apm] \u9700\u6C42 ID \u4E0D\u80FD\u4E3A\u7A7A");
289
+ throw new Error("[apm] \u4F1A\u8BDD ID \u4E0D\u80FD\u4E3A\u7A7A");
310
290
  }
311
291
  if (/[\s/\\]/.test(id)) {
312
292
  throw new Error(
313
- "[apm] \u9700\u6C42 ID \u4E0D\u80FD\u5305\u542B\u7A7A\u767D\u6216\u8DEF\u5F84\u5206\u9694\u7B26\uFF0C\u8BF7\u4F7F\u7528\u5B57\u6BCD\u3001\u6570\u5B57\u3001._- \u7B49"
293
+ "[apm] \u4F1A\u8BDD ID \u4E0D\u80FD\u5305\u542B\u7A7A\u767D\u6216\u8DEF\u5F84\u5206\u9694\u7B26\uFF0C\u8BF7\u4F7F\u7528\u5B57\u6BCD\u3001\u6570\u5B57\u3001._- \u7B49"
314
294
  );
315
295
  }
316
- return `feat/req-${id}`;
296
+ return `feat/session-${id}`;
317
297
  }
318
298
  async function execGit(cwd, args, quiet) {
319
299
  try {
@@ -376,10 +356,15 @@ async function commitWorkingTreeIfDirty(cwd, message) {
376
356
  console.log(`[apm] \u5DF2\u63D0\u4EA4\u5DE5\u4F5C\u533A\u53D8\u66F4: ${commitMessage}`);
377
357
  return true;
378
358
  }
379
- async function ensureFeatureBranch(branch, options, fetchBaseline) {
359
+ async function ensureFeatureBranch(branch, baselineBranch, options) {
380
360
  const cwd = options.cwd ?? process.cwd();
381
361
  const commitMessage = options.message?.trim() || `chore(apm): \u540C\u6B65\u5DE5\u4F5C\u533A (${branch})`;
382
362
  await ensureGitRepo(cwd);
363
+ if (!await remoteHeadBranchExists(cwd, baselineBranch)) {
364
+ throw new Error(
365
+ `[apm] \u8FDC\u7A0B\u4E0D\u5B58\u5728\u57FA\u7EBF\u5206\u652F ${baselineBranch}\uFF0C\u8BF7\u786E\u8BA4\u4ED3\u5E93\u9ED8\u8BA4\u5206\u652F\u5DF2\u63A8\u9001\u5230 origin`
366
+ );
367
+ }
383
368
  const current = await getCurrentBranch(cwd);
384
369
  const dirty = await isWorkingTreeDirty(cwd);
385
370
  if (dirty) {
@@ -395,34 +380,29 @@ async function ensureFeatureBranch(branch, options, fetchBaseline) {
395
380
  ]);
396
381
  }
397
382
  }
398
- const remoteExists = await remoteHeadBranchExists(cwd, branch);
399
- if (remoteExists) {
400
- await execGit(cwd, ["fetch", "origin", branch]);
401
- const hasLocal = await localBranchExists(cwd, branch);
402
- if (hasLocal) {
403
- await execGit(cwd, ["checkout", branch]);
404
- await execGit(cwd, ["pull", "--no-edit"]);
405
- } else {
406
- await execGit(cwd, ["checkout", "-b", branch, `origin/${branch}`]);
407
- }
383
+ const onTargetBranch = await getCurrentBranch(cwd) === branch;
384
+ if (onTargetBranch) {
385
+ await execGit(cwd, ["fetch", "origin", baselineBranch]);
386
+ await execGit(cwd, ["merge", `origin/${baselineBranch}`, "--no-edit"]);
408
387
  } else {
409
- const onBranch = await getCurrentBranch(cwd) === branch;
410
- if (!onBranch) {
411
- const hasLocal = await localBranchExists(cwd, branch);
412
- if (hasLocal) {
413
- await execGit(cwd, ["checkout", branch]);
414
- } else {
415
- const baselineBranch = await fetchBaseline();
416
- await execGit(cwd, ["fetch", "origin", baselineBranch]);
417
- await execGit(cwd, [
418
- "checkout",
419
- "-b",
420
- branch,
421
- `origin/${baselineBranch}`
422
- ]);
423
- }
388
+ const remoteExists = await remoteHeadBranchExists(cwd, branch);
389
+ if (remoteExists) {
390
+ await execGit(cwd, ["fetch", "origin", branch]);
391
+ await execGit(cwd, ["checkout", "-B", branch, `origin/${branch}`]);
392
+ } else if (await localBranchExists(cwd, branch)) {
393
+ throw new Error(
394
+ `[apm] \u8FDC\u7A0B\u4E0D\u5B58\u5728\u5206\u652F ${branch}\uFF0C\u4F46\u672C\u5730\u5DF2\u5B58\u5728\u8BE5\u5206\u652F\uFF0C\u8BF7\u5148\u63A8\u9001\u6216\u5220\u9664\u672C\u5730\u5206\u652F`
395
+ );
396
+ } else {
397
+ await execGit(cwd, ["fetch", "origin", baselineBranch]);
398
+ await execGit(cwd, [
399
+ "checkout",
400
+ "-b",
401
+ branch,
402
+ `origin/${baselineBranch}`
403
+ ]);
404
+ await execGit(cwd, ["push", "-u", "origin", branch]);
424
405
  }
425
- await execGit(cwd, ["push", "-u", "origin", branch]);
426
406
  }
427
407
  console.log(`[apm] \u5DF2\u5C31\u7EEA\u5206\u652F ${branch}`);
428
408
  return branch;
@@ -436,16 +416,23 @@ async function runBranch(sessionId, options = {}) {
436
416
  const cfg = await ensureLoggedConfig();
437
417
  const api = createApmApiClient(cfg);
438
418
  const cwd = options.cwd ?? process.cwd();
419
+ const workdirPath = resolve2(cwd);
439
420
  const baseline = await api.cli.branchBaseline({
440
421
  sessionId: trimmedSessionId,
441
- workdirPath: resolve2(cwd)
422
+ workdirPath
442
423
  });
443
- const branch = branchNameForRequirement(baseline.requirementId);
444
- return ensureFeatureBranch(
445
- branch,
446
- options,
447
- () => fetchBaselineBranchFromApi(trimmedSessionId, cwd)
448
- );
424
+ if (!baseline.repositoryId) {
425
+ throw new Error(
426
+ `[apm] \u672A\u5728\u5E73\u53F0\u627E\u5230\u4E0E\u5F53\u524D\u76EE\u5F55\u5339\u914D\u7684\u5DE5\u4F5C\u76EE\u5F55\u767B\u8BB0\uFF1A${workdirPath}
427
+ \u8BF7\u5148\u5728\u5E73\u53F0\u767B\u8BB0\u8BE5\u8DEF\u5F84\u5BF9\u5E94\u7684\u5DE5\u4F5C\u76EE\u5F55\u4E0E\u4ED3\u5E93\u3002`
428
+ );
429
+ }
430
+ const baselineBranch = (baseline.defaultBranch ?? "").trim();
431
+ if (!baselineBranch) {
432
+ throw new Error("[apm] \u5E73\u53F0\u8FD4\u56DE\u7684\u57FA\u7EBF\u5206\u652F\u540D\u4E3A\u7A7A");
433
+ }
434
+ const branch = branchNameForSession(trimmedSessionId);
435
+ return ensureFeatureBranch(branch, baselineBranch, options);
449
436
  }
450
437
 
451
438
  // src/commands/pull.ts
@@ -1795,9 +1782,7 @@ function buildProgram() {
1795
1782
  program.command("update-message-status").description("\u66F4\u65B0\u5E73\u53F0\u4F1A\u8BDD\u6D88\u606F\u72B6\u6001").requiredOption("--id <messageId>", "\u6D88\u606F ID").requiredOption("--status <status>", "CREATED | TYPING | SUCCESS | FAILED").action(async (opts) => {
1796
1783
  await runUpdateMessageStatus(opts);
1797
1784
  });
1798
- program.command("branch").description(
1799
- "\u5207\u6362\u6216\u521B\u5EFA\u9700\u6C42\u5206\u652F feat/req-<requirementId>\uFF08\u7531 sessionId \u89E3\u6790\u9700\u6C42\uFF09"
1800
- ).argument("<sessionId>", "\u6C9F\u901A\u7FA4 ID").option(
1785
+ program.command("branch").description("\u5207\u6362\u6216\u521B\u5EFA\u4F1A\u8BDD\u5206\u652F feat/session-<sessionId>").argument("<sessionId>", "\u6C9F\u901A\u7FA4 ID").option(
1801
1786
  "-m, --message <text>",
1802
1787
  "\u5DF2\u5728\u76EE\u6807\u5206\u652F\u4E14\u9700\u63D0\u4EA4\u672C\u5730\u6539\u52A8\u65F6\u4F7F\u7528\u7684\u63D0\u4EA4\u8BF4\u660E"
1803
1788
  ).action(async (sessionId, opts) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai-project-manage-cli",
3
- "version": "5.0.2",
3
+ "version": "5.0.4",
4
4
  "description": "命令行工具:后续用于调用平台后端 API 完成运维与自动化操作",
5
5
  "type": "module",
6
6
  "private": false,