@ferronhooi/aihub 0.0.1 → 0.1.0-next.10
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/dist/cli.js +10 -9
- package/dist/commands/batch.js +19 -0
- package/dist/commands/branch.js +10 -0
- package/dist/commands/cherry-pick.js +25 -0
- package/dist/commands/comments.js +32 -0
- package/dist/commands/crossrepo.js +70 -0
- package/dist/commands/doctor.js +21 -0
- package/dist/commands/evidence.js +13 -0
- package/dist/commands/fabric.js +121 -0
- package/dist/commands/init.js +18 -0
- package/dist/commands/pipeline.js +35 -0
- package/dist/commands/port.js +38 -0
- package/dist/commands/pr-changes.js +36 -0
- package/dist/commands/pr.js +78 -0
- package/dist/commands/release.js +41 -0
- package/dist/commands/review.js +54 -0
- package/dist/commands/robot.js +69 -0
- package/dist/commands/skills.js +58 -0
- package/dist/commands/story-flow.js +48 -0
- package/dist/commands/story.js +23 -0
- package/dist/commands/wiki.js +19 -0
- package/dist/commands/work.js +6 -0
- package/dist/commands/workitem.js +23 -0
- package/dist/commands/worktree.js +27 -0
- package/dist/lib/ado.js +172 -0
- package/dist/lib/args.js +18 -0
- package/dist/lib/auth.js +68 -0
- package/dist/lib/batch.js +146 -0
- package/dist/lib/branch.js +72 -0
- package/dist/lib/branches.js +46 -0
- package/dist/lib/changes.js +14 -0
- package/dist/lib/cherryPick.js +182 -0
- package/dist/lib/cliargs.js +26 -0
- package/dist/lib/clients.js +26 -0
- package/dist/lib/comments.js +119 -0
- package/dist/lib/context.js +9 -0
- package/dist/lib/crossRepo.js +134 -0
- package/dist/lib/crossRepoOps.js +199 -0
- package/dist/lib/diff.js +68 -0
- package/dist/lib/doctor.js +126 -0
- package/dist/lib/errors.js +7 -0
- package/dist/lib/evidence.js +41 -0
- package/dist/lib/fabric.js +272 -0
- package/dist/lib/git.js +111 -0
- package/dist/lib/gitx.js +45 -0
- package/dist/lib/glob.js +28 -0
- package/dist/lib/init.js +159 -0
- package/dist/lib/install.js +178 -0
- package/dist/lib/integrate.js +80 -0
- package/dist/lib/pack.js +48 -0
- package/dist/lib/pipeline.js +249 -0
- package/dist/lib/port.js +213 -0
- package/dist/lib/pr.js +131 -0
- package/dist/lib/prChanges.js +85 -0
- package/dist/lib/release.js +202 -0
- package/dist/lib/repos.js +50 -0
- package/dist/lib/review.js +223 -0
- package/dist/lib/robot.js +132 -0
- package/dist/lib/schema.js +56 -0
- package/dist/lib/story-flow.js +202 -0
- package/dist/lib/story.js +248 -0
- package/dist/lib/team.js +37 -0
- package/dist/lib/templates.js +184 -0
- package/dist/lib/wiki.js +46 -0
- package/dist/lib/work.js +67 -0
- package/dist/lib/workitems.js +56 -0
- package/dist/lib/worktree.js +152 -0
- package/dist/router.js +81 -0
- package/dist/tools/lintSkills.js +173 -0
- package/dist/tools/lintSkills.test.js +70 -0
- package/pack/commands/copilot/cherry-pick.prompt.md +6 -0
- package/pack/commands/copilot/explain.prompt.md +6 -0
- package/pack/commands/copilot/finish.prompt.md +6 -0
- package/pack/commands/copilot/pr-update.prompt.md +6 -0
- package/pack/commands/copilot/pr.prompt.md +6 -0
- package/pack/commands/copilot/refine.prompt.md +6 -0
- package/pack/commands/copilot/release.prompt.md +6 -0
- package/pack/commands/copilot/review-pr.prompt.md +6 -0
- package/pack/commands/copilot/review.prompt.md +6 -0
- package/pack/commands/copilot/start.prompt.md +6 -0
- package/pack/commands/copilot/story.prompt.md +6 -0
- package/pack/commands/copilot/test.prompt.md +6 -0
- package/pack/commands/copilot/watch.prompt.md +6 -0
- package/pack/commands/copilot/wiki.prompt.md +6 -0
- package/pack/commands/opencode/cherry-pick.md +5 -0
- package/pack/commands/opencode/explain.md +5 -0
- package/pack/commands/opencode/finish.md +5 -0
- package/pack/commands/opencode/pr-update.md +5 -0
- package/pack/commands/opencode/pr.md +5 -0
- package/pack/commands/opencode/refine.md +5 -0
- package/pack/commands/opencode/release.md +5 -0
- package/pack/commands/opencode/review-pr.md +5 -0
- package/pack/commands/opencode/review.md +5 -0
- package/pack/commands/opencode/start.md +5 -0
- package/pack/commands/opencode/story.md +5 -0
- package/pack/commands/opencode/test.md +5 -0
- package/pack/commands/opencode/watch.md +5 -0
- package/pack/commands/opencode/wiki.md +5 -0
- package/pack/skills/_shared/assets/commit-template.nl.txt +5 -0
- package/pack/skills/_shared/assets/commit-template.txt +5 -0
- package/pack/skills/_shared/assets/pull_request_template.md +16 -0
- package/pack/skills/_shared/assets/pull_request_template.nl.md +16 -0
- package/pack/skills/_shared/assets/story-template.md +12 -0
- package/pack/skills/_shared/assets/story-template.nl.md +12 -0
- package/pack/skills/_shared/references/team.schema.yaml +83 -0
- package/pack/skills/branch-sync/SKILL.md +31 -0
- package/pack/skills/cross-repo/SKILL.md +35 -0
- package/pack/skills/cross-repo/references/cross-repo.md +22 -0
- package/pack/skills/evidence/SKILL.md +28 -0
- package/pack/skills/implement/SKILL.md +25 -0
- package/pack/skills/pipeline-fix/SKILL.md +30 -0
- package/pack/skills/pr-create/SKILL.md +29 -0
- package/pack/skills/pr-explain/SKILL.md +28 -0
- package/pack/skills/pr-review/SKILL.md +35 -0
- package/pack/skills/pr-update/SKILL.md +28 -0
- package/pack/skills/pr-watch/SKILL.md +30 -0
- package/pack/skills/release/SKILL.md +32 -0
- package/pack/skills/review-process/SKILL.md +35 -0
- package/pack/skills/start-here/SKILL.md +46 -0
- package/pack/skills/story-create/SKILL.md +28 -0
- package/pack/skills/story-finish/SKILL.md +29 -0
- package/pack/skills/story-refine/SKILL.md +28 -0
- package/pack/skills/story-run/SKILL.md +27 -0
- package/pack/skills/story-start/SKILL.md +31 -0
- package/pack/skills/team-settings/SKILL.md +28 -0
- package/pack/skills/test-run/SKILL.md +37 -0
- package/pack/skills/test-run/references/fabric-robot.md +24 -0
- package/pack/skills/voice-notes/SKILL.md +30 -0
- package/pack/skills/wiki/SKILL.md +29 -0
- package/pack/skills/worktrees/SKILL.md +29 -0
- package/package.json +13 -3
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { parseArgs } from "node:util";
|
|
2
|
+
import { changedFiles } from "../lib/changes.js";
|
|
3
|
+
import { repoClient } from "../lib/context.js";
|
|
4
|
+
import { AihubError } from "../lib/errors.js";
|
|
5
|
+
import { formatIntegrate, integrate } from "../lib/integrate.js";
|
|
6
|
+
import { currentBranch } from "../lib/git.js";
|
|
7
|
+
import { followBuild, formatNeeded, queueRobot, formatTargets, resolveTarget, robotConfig, robotDefinitionId, robotNeeded, summarize } from "../lib/robot.js";
|
|
8
|
+
import { loadTeam } from "../lib/team.js";
|
|
9
|
+
export async function run(argv, ctx) {
|
|
10
|
+
const [sub, ...rest] = argv;
|
|
11
|
+
let v;
|
|
12
|
+
try {
|
|
13
|
+
v = parseArgs({
|
|
14
|
+
args: rest,
|
|
15
|
+
options: {
|
|
16
|
+
branch: { type: "string" },
|
|
17
|
+
target: { type: "string" },
|
|
18
|
+
suite: { type: "string" },
|
|
19
|
+
include: { type: "string" },
|
|
20
|
+
pipeline: { type: "string" },
|
|
21
|
+
"no-wait": { type: "boolean" },
|
|
22
|
+
"no-merge": { type: "boolean" },
|
|
23
|
+
on: { type: "string" },
|
|
24
|
+
},
|
|
25
|
+
}).values;
|
|
26
|
+
}
|
|
27
|
+
catch (e) {
|
|
28
|
+
throw new AihubError(e.message);
|
|
29
|
+
}
|
|
30
|
+
if (sub !== "needed" && sub !== "run" && sub !== "targets")
|
|
31
|
+
throw new AihubError("usage: aihub robot needed|run|targets [flags]");
|
|
32
|
+
const { root, client } = repoClient(ctx);
|
|
33
|
+
const team = loadTeam(root);
|
|
34
|
+
const cfg = robotConfig(team);
|
|
35
|
+
if (sub === "targets") {
|
|
36
|
+
ctx.out(formatTargets(cfg));
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (sub === "needed") {
|
|
40
|
+
const c = changedFiles(root, team, { branch: v.branch, target: v.target });
|
|
41
|
+
ctx.out(formatNeeded(robotNeeded(c.files, cfg), cfg, c.branch));
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const pipeline = v.pipeline ?? cfg.pipeline;
|
|
45
|
+
if (!pipeline)
|
|
46
|
+
throw new AihubError("no Robot pipeline: set robot.pipeline in team.yaml or pass --pipeline <name>");
|
|
47
|
+
const target = resolveTarget(cfg, client.remote.repo, v.on);
|
|
48
|
+
if (!target)
|
|
49
|
+
throw new AihubError("no Robot target: set robot.targets (and robot.defaultTarget) or robot.integrationBranch in team.yaml, or pass --on <name|branch>");
|
|
50
|
+
const { branch, requires } = target;
|
|
51
|
+
if (v.suite && cfg.suites.length && !cfg.suites.some((s) => s.name === v.suite)) {
|
|
52
|
+
throw new AihubError(`unknown suite '${v.suite}'; team.yaml suites: ${cfg.suites.map((s) => s.name).join(", ")}`);
|
|
53
|
+
}
|
|
54
|
+
const definitionId = await robotDefinitionId(client, pipeline);
|
|
55
|
+
if (target.merge && !v["no-merge"]) {
|
|
56
|
+
const feature = currentBranch(root);
|
|
57
|
+
if (feature === branch)
|
|
58
|
+
throw new AihubError(`you are on the integration branch '${branch}'; check out the feature branch or pass --no-merge`);
|
|
59
|
+
ctx.out(formatIntegrate(feature, integrate(root, { feature, integration: branch, requires })));
|
|
60
|
+
}
|
|
61
|
+
const b = await queueRobot(client, { definitionId, branch, suite: v.suite, include: v.include });
|
|
62
|
+
ctx.out(`queued ${pipeline} build ${b.id} on ${branch}${v.suite ? ` (suite ${v.suite})` : ""}${v.include ? ` (include ${v.include})` : ""}`);
|
|
63
|
+
if (v["no-wait"])
|
|
64
|
+
return;
|
|
65
|
+
const done = await followBuild(client, b.id, Number(ctx.env.AIHUB_POLL_MS ?? 15000));
|
|
66
|
+
ctx.out(await summarize(client, done));
|
|
67
|
+
if (done.result !== "succeeded")
|
|
68
|
+
throw new AihubError(`Robot build ${b.id} ended as ${done.result ?? done.status}`);
|
|
69
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { parseArgs } from "node:util";
|
|
2
|
+
import { AihubError } from "../lib/errors.js";
|
|
3
|
+
import { repoRoot } from "../lib/git.js";
|
|
4
|
+
import { installClients, parseClients, uninstallClients, updateClients } from "../lib/install.js";
|
|
5
|
+
const USAGE = "usage: aihub skills install|update|uninstall [--client opencode|github-copilot|codex|all] [--into-repo]\n" +
|
|
6
|
+
" install copy the bundled skills and slash commands into each client's global folder\n" +
|
|
7
|
+
" update rewrite the copies of every client that has them from the bundled pack (files you edited stay)\n" +
|
|
8
|
+
" uninstall remove only the files aihub wrote and you did not change\n" +
|
|
9
|
+
" --into-repo use the current repository instead of the global folders (for teams that keep skills in the repo)";
|
|
10
|
+
function report(reports, out) {
|
|
11
|
+
for (const r of reports) {
|
|
12
|
+
const count = (a) => r.results.filter((x) => x.action === a).length;
|
|
13
|
+
out(`${r.client}: ${r.dir}`);
|
|
14
|
+
out(` created ${count("created")}, updated ${count("updated")}, unchanged ${count("unchanged")}, removed ${count("removed")}, skipped ${count("skipped")}`);
|
|
15
|
+
for (const s of r.results.filter((x) => x.action === "skipped"))
|
|
16
|
+
out(` skipped ${s.path} (${s.reason})`);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export async function run(argv, ctx) {
|
|
20
|
+
const [sub, ...rest] = argv;
|
|
21
|
+
if (sub !== "install" && sub !== "update" && sub !== "uninstall")
|
|
22
|
+
throw new AihubError(USAGE);
|
|
23
|
+
let v;
|
|
24
|
+
try {
|
|
25
|
+
v = parseArgs({ args: rest, options: { global: { type: "boolean" }, "into-repo": { type: "boolean" }, client: { type: "string" } } }).values;
|
|
26
|
+
}
|
|
27
|
+
catch (e) {
|
|
28
|
+
throw new AihubError(e.message);
|
|
29
|
+
}
|
|
30
|
+
let clients;
|
|
31
|
+
try {
|
|
32
|
+
clients = parseClients(v.client);
|
|
33
|
+
}
|
|
34
|
+
catch (e) {
|
|
35
|
+
throw new AihubError(e.message);
|
|
36
|
+
}
|
|
37
|
+
let opts = { global: true };
|
|
38
|
+
if (v["into-repo"]) {
|
|
39
|
+
try {
|
|
40
|
+
opts = { global: false, root: repoRoot(ctx.cwd) };
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
throw new AihubError("--into-repo needs a git repository: cd into the repository first");
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (sub === "install")
|
|
47
|
+
report(installClients(clients, ctx.env, opts), ctx.out);
|
|
48
|
+
else if (sub === "uninstall")
|
|
49
|
+
report(uninstallClients(clients, ctx.env, opts), ctx.out);
|
|
50
|
+
else {
|
|
51
|
+
if (!opts.global)
|
|
52
|
+
throw new AihubError("'aihub skills update' refreshes the global copies; for the repository run 'aihub skills install --into-repo' again");
|
|
53
|
+
const r = updateClients(clients, ctx.env);
|
|
54
|
+
if (!r.length)
|
|
55
|
+
throw new AihubError("no client has aihub skills yet; run 'aihub skills install'");
|
|
56
|
+
report(r, ctx.out);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { AihubError } from "../lib/errors.js";
|
|
2
|
+
import { list, parseFlags, posInt } from "../lib/cliargs.js";
|
|
3
|
+
import { finishStory, linkStory, startStory } from "../lib/story-flow.js";
|
|
4
|
+
export const STORY_FLOW_SUBS = ["start", "link", "finish"];
|
|
5
|
+
/** aihub story start|link|finish (create|refine|show live in the story command of the stories flow). */
|
|
6
|
+
export async function run(argv, ctx) {
|
|
7
|
+
const [sub, ...rest] = argv;
|
|
8
|
+
const v = parseFlags(rest, {
|
|
9
|
+
id: { type: "string" },
|
|
10
|
+
repos: { type: "string" },
|
|
11
|
+
base: { type: "string" },
|
|
12
|
+
root: { type: "string" },
|
|
13
|
+
branch: { type: "string" },
|
|
14
|
+
checkout: { type: "boolean" },
|
|
15
|
+
"no-checkout": { type: "boolean" },
|
|
16
|
+
"dry-run": { type: "boolean" },
|
|
17
|
+
"no-state": { type: "boolean" },
|
|
18
|
+
json: { type: "boolean" },
|
|
19
|
+
});
|
|
20
|
+
if (!STORY_FLOW_SUBS.includes(sub ?? ""))
|
|
21
|
+
throw new AihubError("usage: aihub story start|link|finish --id <story> [flags]");
|
|
22
|
+
const id = posInt(v.id, "--id");
|
|
23
|
+
const json = !!v.json;
|
|
24
|
+
if (sub === "start") {
|
|
25
|
+
ctx.out(await startStory(ctx, {
|
|
26
|
+
id,
|
|
27
|
+
repos: list(v.repos),
|
|
28
|
+
base: v.base,
|
|
29
|
+
root: v.root,
|
|
30
|
+
checkout: v.checkout,
|
|
31
|
+
noCheckout: v["no-checkout"],
|
|
32
|
+
json,
|
|
33
|
+
}));
|
|
34
|
+
}
|
|
35
|
+
else if (sub === "link") {
|
|
36
|
+
ctx.out(await linkStory(ctx, { id, branch: v.branch, json }));
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
ctx.out(await finishStory(ctx, {
|
|
40
|
+
id,
|
|
41
|
+
repos: list(v.repos),
|
|
42
|
+
root: v.root,
|
|
43
|
+
dryRun: !!v["dry-run"],
|
|
44
|
+
noState: !!v["no-state"],
|
|
45
|
+
json,
|
|
46
|
+
}));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { num, parse } from "../lib/args.js";
|
|
2
|
+
import { AihubError } from "../lib/errors.js";
|
|
3
|
+
import { storyCreate, storyRefine, storyShow } from "../lib/story.js";
|
|
4
|
+
/** Accepts `--id 123` and the positional form `story show 123` used by the skills. */
|
|
5
|
+
function idOf(v, pos) {
|
|
6
|
+
const id = num(v.id ?? pos[0], "--id");
|
|
7
|
+
if (!id)
|
|
8
|
+
throw new AihubError("pass the story id: --id <number>");
|
|
9
|
+
return id;
|
|
10
|
+
}
|
|
11
|
+
export async function run(argv, ctx) {
|
|
12
|
+
const [sub, ...rest] = argv;
|
|
13
|
+
const p = parse(rest, { id: { type: "string" }, title: { type: "string" }, "body-file": { type: "string" }, parent: { type: "string" }, type: { type: "string" }, json: { type: "boolean" } }, true);
|
|
14
|
+
const v = p.values;
|
|
15
|
+
if (sub === "show")
|
|
16
|
+
return void ctx.out(await storyShow(ctx, { id: idOf(v, p.positionals), json: v.json }));
|
|
17
|
+
if (sub === "create") {
|
|
18
|
+
return void ctx.out(await storyCreate(ctx, { title: v.title, bodyFile: v["body-file"], parent: num(v.parent, "--parent"), type: v.type, json: v.json }));
|
|
19
|
+
}
|
|
20
|
+
if (sub === "refine")
|
|
21
|
+
return void ctx.out(await storyRefine(ctx, { id: idOf(v, p.positionals), bodyFile: v["body-file"], json: v.json }));
|
|
22
|
+
throw new AihubError("usage: aihub story show|create|refine [flags]");
|
|
23
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { AihubError } from "../lib/errors.js";
|
|
2
|
+
import { parseFlags } from "../lib/cliargs.js";
|
|
3
|
+
import { wikiUpdate } from "../lib/wiki.js";
|
|
4
|
+
/** aihub wiki update --page path --body-file F [--publish] */
|
|
5
|
+
export async function run(argv, ctx) {
|
|
6
|
+
const [sub, ...rest] = argv;
|
|
7
|
+
const v = parseFlags(rest, {
|
|
8
|
+
page: { type: "string" },
|
|
9
|
+
"body-file": { type: "string" },
|
|
10
|
+
wiki: { type: "string" },
|
|
11
|
+
publish: { type: "boolean" },
|
|
12
|
+
json: { type: "boolean" },
|
|
13
|
+
});
|
|
14
|
+
if (sub !== "update")
|
|
15
|
+
throw new AihubError("usage: aihub wiki update --page <path> --body-file <file> [--publish]");
|
|
16
|
+
if (!v.page || !v["body-file"])
|
|
17
|
+
throw new AihubError("aihub wiki update needs --page <path> and --body-file <file>");
|
|
18
|
+
ctx.out(await wikiUpdate(ctx, { page: v.page, bodyFile: v["body-file"], wiki: v.wiki, publish: !!v.publish, json: !!v.json }));
|
|
19
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { repoClient } from "../lib/context.js";
|
|
2
|
+
import { AihubError } from "../lib/errors.js";
|
|
3
|
+
import { parseFlags, posInt } from "../lib/cliargs.js";
|
|
4
|
+
import { getWorkItem, setWorkItemState } from "../lib/workitems.js";
|
|
5
|
+
/** aihub workitem state --id N --to <state> */
|
|
6
|
+
export async function run(argv, ctx) {
|
|
7
|
+
const [sub, ...rest] = argv;
|
|
8
|
+
const v = parseFlags(rest, { id: { type: "string" }, to: { type: "string" }, json: { type: "boolean" } });
|
|
9
|
+
if (sub !== "state")
|
|
10
|
+
throw new AihubError("usage: aihub workitem state --id <n> --to <state>");
|
|
11
|
+
const id = posInt(v.id, "--id");
|
|
12
|
+
const to = String(v.to ?? "").trim();
|
|
13
|
+
if (!to)
|
|
14
|
+
throw new AihubError("--to <state> is required (for example Active, Resolved, Done)");
|
|
15
|
+
const { client } = repoClient(ctx);
|
|
16
|
+
const wi = await getWorkItem(client, id);
|
|
17
|
+
if (wi.state.toLowerCase() === to.toLowerCase()) {
|
|
18
|
+
ctx.out(v.json ? JSON.stringify({ id, from: wi.state, to: wi.state, changed: false }) : `work item ${id} is already ${wi.state}`);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
await setWorkItemState(client, id, to);
|
|
22
|
+
ctx.out(v.json ? JSON.stringify({ id, from: wi.state, to, changed: true }) : `work item ${id}: ${wi.state} -> ${to}`);
|
|
23
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { AihubError } from "../lib/errors.js";
|
|
2
|
+
import { list, parseFlags, posInt } from "../lib/cliargs.js";
|
|
3
|
+
import { createSet, listSets, removeSet } from "../lib/worktree.js";
|
|
4
|
+
export async function run(argv, ctx) {
|
|
5
|
+
const [sub, ...rest] = argv;
|
|
6
|
+
const v = parseFlags(rest, {
|
|
7
|
+
story: { type: "string" },
|
|
8
|
+
repos: { type: "string" },
|
|
9
|
+
root: { type: "string" },
|
|
10
|
+
branch: { type: "string" },
|
|
11
|
+
base: { type: "string" },
|
|
12
|
+
json: { type: "boolean" },
|
|
13
|
+
});
|
|
14
|
+
const root = v.root;
|
|
15
|
+
const json = !!v.json;
|
|
16
|
+
if (sub === "create") {
|
|
17
|
+
ctx.out(await createSet(ctx, { story: posInt(v.story, "--story"), repos: list(v.repos), root, branch: v.branch, base: v.base, json }));
|
|
18
|
+
}
|
|
19
|
+
else if (sub === "list") {
|
|
20
|
+
ctx.out(listSets(ctx, { root, json }));
|
|
21
|
+
}
|
|
22
|
+
else if (sub === "remove") {
|
|
23
|
+
ctx.out(removeSet(ctx, { story: posInt(v.story, "--story"), root, json }));
|
|
24
|
+
}
|
|
25
|
+
else
|
|
26
|
+
throw new AihubError("usage: aihub worktree create|list|remove --story <id> [--repos a,b] [--root <folder>]");
|
|
27
|
+
}
|
package/dist/lib/ado.js
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { AihubError } from "./errors.js";
|
|
2
|
+
export function parseRemote(url) {
|
|
3
|
+
const u = url.trim();
|
|
4
|
+
const dec = (s) => decodeURIComponent(s);
|
|
5
|
+
const make = (org, project, repo) => ({
|
|
6
|
+
org: dec(org),
|
|
7
|
+
project: dec(project),
|
|
8
|
+
repo: dec(repo),
|
|
9
|
+
orgUrl: `https://dev.azure.com/${encodeURIComponent(dec(org))}`,
|
|
10
|
+
});
|
|
11
|
+
let m;
|
|
12
|
+
// SSH: git@ssh.dev.azure.com:v3/org/project/repo | org@vs-ssh.visualstudio.com:v3/org/project/repo
|
|
13
|
+
if ((m = /^(?:ssh:\/\/)?[^@\s]+@(?:ssh\.dev\.azure\.com|vs-ssh\.visualstudio\.com)[:/](?:v3\/)?([^/]+)\/([^/]+)\/([^/]+?)(?:\.git)?\/?$/.exec(u))) {
|
|
14
|
+
return make(m[1], m[2], m[3]);
|
|
15
|
+
}
|
|
16
|
+
// HTTPS dev.azure.com/org/project/_git/repo (optional user@)
|
|
17
|
+
if ((m = /^https?:\/\/(?:[^@/]+@)?dev\.azure\.com\/([^/]+)\/([^/]+)\/_git\/([^/?#]+?)(?:\.git)?\/?(?:[?#].*)?$/.exec(u))) {
|
|
18
|
+
return make(m[1], m[2], m[3]);
|
|
19
|
+
}
|
|
20
|
+
// HTTPS org.visualstudio.com/[DefaultCollection/]project/_git/repo
|
|
21
|
+
if ((m = /^https?:\/\/(?:[^@/]+@)?([^./]+)\.visualstudio\.com\/(?:DefaultCollection\/)?([^/]+)\/_git\/([^/?#]+?)(?:\.git)?\/?(?:[?#].*)?$/.exec(u))) {
|
|
22
|
+
return make(m[1], m[2], m[3]);
|
|
23
|
+
}
|
|
24
|
+
throw new AihubError(`remote '${u}' is not an Azure DevOps remote (dev.azure.com or visualstudio.com)`);
|
|
25
|
+
}
|
|
26
|
+
export const API_VERSION = "7.1";
|
|
27
|
+
export class AdoClient {
|
|
28
|
+
remote;
|
|
29
|
+
auth;
|
|
30
|
+
fetchFn;
|
|
31
|
+
constructor(remote, auth, fetchFn = fetch) {
|
|
32
|
+
this.remote = remote;
|
|
33
|
+
this.auth = auth;
|
|
34
|
+
this.fetchFn = fetchFn;
|
|
35
|
+
}
|
|
36
|
+
repoPath() {
|
|
37
|
+
return `git/repositories/${encodeURIComponent(this.remote.repo)}`;
|
|
38
|
+
}
|
|
39
|
+
prUrl(id) {
|
|
40
|
+
const r = this.remote;
|
|
41
|
+
return `${r.orgUrl}/${encodeURIComponent(r.project)}/_git/${encodeURIComponent(r.repo)}/pullrequest/${id}`;
|
|
42
|
+
}
|
|
43
|
+
url(path, query = {}) {
|
|
44
|
+
const q = new URLSearchParams();
|
|
45
|
+
for (const [k, v] of Object.entries(query))
|
|
46
|
+
if (v !== undefined)
|
|
47
|
+
q.set(k, String(v));
|
|
48
|
+
q.set("api-version", API_VERSION);
|
|
49
|
+
return `${this.remote.orgUrl}/${encodeURIComponent(this.remote.project)}/_apis/${path}?${q.toString()}`;
|
|
50
|
+
}
|
|
51
|
+
async request(method, path, opts = {}) {
|
|
52
|
+
const res = await this.fetchFn(this.url(path, opts.query), {
|
|
53
|
+
method,
|
|
54
|
+
headers: {
|
|
55
|
+
Authorization: await this.auth(),
|
|
56
|
+
Accept: opts.raw ? "text/plain" : "application/json",
|
|
57
|
+
...(opts.body !== undefined || opts.rawBody !== undefined
|
|
58
|
+
? { "Content-Type": opts.contentType ?? "application/json; charset=utf-8" }
|
|
59
|
+
: {}),
|
|
60
|
+
...(opts.headers ?? {}),
|
|
61
|
+
},
|
|
62
|
+
body: opts.rawBody !== undefined ? opts.rawBody : opts.body !== undefined ? JSON.stringify(opts.body) : undefined,
|
|
63
|
+
});
|
|
64
|
+
const text = await res.text();
|
|
65
|
+
const ctype = res.headers.get("content-type") ?? "";
|
|
66
|
+
// Azure DevOps answers a bad/missing credential with 203 + an HTML sign-in page.
|
|
67
|
+
if (res.status === 401 || res.status === 203 || (res.ok && !opts.raw && ctype.includes("text/html"))) {
|
|
68
|
+
throw new AihubError("not signed in to Azure DevOps: run 'az login' or set AIHUB_ADO_PAT");
|
|
69
|
+
}
|
|
70
|
+
if (!res.ok)
|
|
71
|
+
throw new AihubError(httpMessage(res.status, text, method, path));
|
|
72
|
+
if (opts.raw)
|
|
73
|
+
return { data: text, headers: res.headers };
|
|
74
|
+
return { data: (text ? JSON.parse(text) : {}), headers: res.headers };
|
|
75
|
+
}
|
|
76
|
+
get(path, query) {
|
|
77
|
+
return this.request("GET", path, { query }).then((r) => r.data);
|
|
78
|
+
}
|
|
79
|
+
/** GET a collection, following x-ms-continuationtoken. Returns all `value` items. */
|
|
80
|
+
async list(path, query = {}) {
|
|
81
|
+
const out = [];
|
|
82
|
+
let token = null;
|
|
83
|
+
for (let i = 0; i < 100; i++) {
|
|
84
|
+
const r = await this.request("GET", path, {
|
|
85
|
+
query: { ...query, ...(token ? { continuationToken: token } : {}) },
|
|
86
|
+
});
|
|
87
|
+
out.push(...(r.data.value ?? []));
|
|
88
|
+
token = r.headers.get("x-ms-continuationtoken");
|
|
89
|
+
if (!token)
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
return out;
|
|
93
|
+
}
|
|
94
|
+
post(path, body, query) {
|
|
95
|
+
return this.request("POST", path, { body, query }).then((r) => r.data);
|
|
96
|
+
}
|
|
97
|
+
patch(path, body, query) {
|
|
98
|
+
return this.request("PATCH", path, { body, query }).then((r) => r.data);
|
|
99
|
+
}
|
|
100
|
+
/** Work item writes need the JSON Patch media type. */
|
|
101
|
+
jsonPatch(method, path, ops, query) {
|
|
102
|
+
return this.request(method, path, { body: ops, query, contentType: "application/json-patch+json" }).then((r) => r.data);
|
|
103
|
+
}
|
|
104
|
+
workItemUrl(id) {
|
|
105
|
+
return `${this.remote.orgUrl}/${encodeURIComponent(this.remote.project)}/_workitems/edit/${id}`;
|
|
106
|
+
}
|
|
107
|
+
/** PUT/POST raw bytes (attachments). */
|
|
108
|
+
sendBytes(method, path, bytes, query) {
|
|
109
|
+
return this.request(method, path, { rawBody: bytes, contentType: "application/octet-stream", query }).then((r) => r.data);
|
|
110
|
+
}
|
|
111
|
+
getText(path, query) {
|
|
112
|
+
return this.request("GET", path, { query, raw: true }).then((r) => r.data);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
function httpMessage(status, body, method, path) {
|
|
116
|
+
let detail = "";
|
|
117
|
+
try {
|
|
118
|
+
detail = JSON.parse(body).message ?? "";
|
|
119
|
+
}
|
|
120
|
+
catch {
|
|
121
|
+
detail = body.slice(0, 200);
|
|
122
|
+
}
|
|
123
|
+
detail = detail.replace(/\s+/g, " ").trim();
|
|
124
|
+
const p = path.split("?")[0];
|
|
125
|
+
if (status === 403)
|
|
126
|
+
return `Azure DevOps denied access (403): your account needs access to this repository${detail ? ` (${detail})` : ""}`;
|
|
127
|
+
if (status === 404)
|
|
128
|
+
return `Azure DevOps: not found (404) for ${method} ${p}${detail ? `: ${detail}` : ""}`;
|
|
129
|
+
return `Azure DevOps ${method} ${p} failed (${status})${detail ? `: ${detail}` : ""}`;
|
|
130
|
+
}
|
|
131
|
+
export const branchRef = (b) => (b.startsWith("refs/") ? b : `refs/heads/${b}`);
|
|
132
|
+
export const shortBranch = (r) => r.replace(/^refs\/heads\//, "");
|
|
133
|
+
export async function findActivePr(c, source, target) {
|
|
134
|
+
const prs = await c.list(`${c.repoPath()}/pullrequests`, {
|
|
135
|
+
"searchCriteria.sourceRefName": branchRef(source),
|
|
136
|
+
"searchCriteria.status": "active",
|
|
137
|
+
...(target ? { "searchCriteria.targetRefName": branchRef(target) } : {}),
|
|
138
|
+
});
|
|
139
|
+
return prs[0];
|
|
140
|
+
}
|
|
141
|
+
export const getPr = (c, id) => c.get(`${c.repoPath()}/pullrequests/${id}`);
|
|
142
|
+
export async function repoDefaultBranch(c) {
|
|
143
|
+
const r = await c.get(c.repoPath());
|
|
144
|
+
return r.defaultBranch ? shortBranch(r.defaultBranch) : undefined;
|
|
145
|
+
}
|
|
146
|
+
export async function getWorkItemTitle(c, id) {
|
|
147
|
+
try {
|
|
148
|
+
const r = await c.get(`wit/workitems/${id}`, { fields: "System.Title" });
|
|
149
|
+
return r.fields?.["System.Title"];
|
|
150
|
+
}
|
|
151
|
+
catch {
|
|
152
|
+
return undefined;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
/** PR ids linked to a work item through artifact links (vstfs:///Git/PullRequestId/<project>%2F<repo>%2F<id>). */
|
|
156
|
+
export async function getWorkItemPrIds(c, id) {
|
|
157
|
+
try {
|
|
158
|
+
const r = await c.get(`wit/workitems/${id}`, { $expand: "relations" });
|
|
159
|
+
const ids = [];
|
|
160
|
+
for (const rel of r.relations ?? []) {
|
|
161
|
+
if (rel.rel !== "ArtifactLink")
|
|
162
|
+
continue;
|
|
163
|
+
const m = /PullRequestId\/.*?(\d+)$/i.exec(decodeURIComponent(rel.url));
|
|
164
|
+
if (m)
|
|
165
|
+
ids.push(Number(m[1]));
|
|
166
|
+
}
|
|
167
|
+
return ids;
|
|
168
|
+
}
|
|
169
|
+
catch {
|
|
170
|
+
return [];
|
|
171
|
+
}
|
|
172
|
+
}
|
package/dist/lib/args.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { parseArgs } from "node:util";
|
|
2
|
+
import { AihubError } from "./errors.js";
|
|
3
|
+
export function parse(argv, options, positionals = false) {
|
|
4
|
+
try {
|
|
5
|
+
return parseArgs({ args: argv, options, allowPositionals: positionals });
|
|
6
|
+
}
|
|
7
|
+
catch (e) {
|
|
8
|
+
throw new AihubError(e.message);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export function num(v, flag) {
|
|
12
|
+
if (v === undefined)
|
|
13
|
+
return undefined;
|
|
14
|
+
const n = Number(String(v).replace(/^AB#/i, ""));
|
|
15
|
+
if (!Number.isInteger(n) || n <= 0)
|
|
16
|
+
throw new AihubError(`${flag} must be a number`);
|
|
17
|
+
return n;
|
|
18
|
+
}
|
package/dist/lib/auth.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { AihubError } from "./errors.js";
|
|
6
|
+
export const ADO_RESOURCE = "499b84ac-1321-427f-aa17-267ca6975798";
|
|
7
|
+
export const defaultExec = (cmd, args, input) => {
|
|
8
|
+
const r = spawnSync(cmd, args, {
|
|
9
|
+
encoding: "utf8",
|
|
10
|
+
input,
|
|
11
|
+
shell: process.platform === "win32" && cmd === "az",
|
|
12
|
+
timeout: 30000,
|
|
13
|
+
});
|
|
14
|
+
if (r.error || r.status !== 0)
|
|
15
|
+
throw new Error(r.error?.message ?? (r.stderr || "").trim());
|
|
16
|
+
return r.stdout;
|
|
17
|
+
};
|
|
18
|
+
const basic = (pat) => `Basic ${Buffer.from(`:${pat}`).toString("base64")}`;
|
|
19
|
+
/** Order: PAT env -> az token (cached until expiry) -> git credential fill. */
|
|
20
|
+
export function makeAuth(deps = {}) {
|
|
21
|
+
const env = deps.env ?? process.env;
|
|
22
|
+
const exec = deps.exec ?? defaultExec;
|
|
23
|
+
const now = deps.now ?? Date.now;
|
|
24
|
+
const cacheFile = deps.cacheFile ?? join(tmpdir(), "aihub-az-token.json");
|
|
25
|
+
let cached;
|
|
26
|
+
return async () => {
|
|
27
|
+
if (cached)
|
|
28
|
+
return cached;
|
|
29
|
+
const pat = env.AIHUB_ADO_PAT || env.AZURE_DEVOPS_EXT_PAT;
|
|
30
|
+
if (pat)
|
|
31
|
+
return (cached = basic(pat));
|
|
32
|
+
try {
|
|
33
|
+
if (existsSync(cacheFile)) {
|
|
34
|
+
const c = JSON.parse(readFileSync(cacheFile, "utf8"));
|
|
35
|
+
if (c.token && c.expiresAt - 120_000 > now())
|
|
36
|
+
return (cached = `Bearer ${c.token}`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
/* ignore a broken cache */
|
|
41
|
+
}
|
|
42
|
+
try {
|
|
43
|
+
const out = exec("az", ["account", "get-access-token", "--resource", ADO_RESOURCE, "--output", "json"]);
|
|
44
|
+
const j = JSON.parse(out);
|
|
45
|
+
const expiresAt = j.expires_on ? Number(j.expires_on) * 1000 : Date.parse(j.expiresOn ?? "") || now() + 30 * 60_000;
|
|
46
|
+
try {
|
|
47
|
+
writeFileSync(cacheFile, JSON.stringify({ token: j.accessToken, expiresAt }), { mode: 0o600 });
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
/* cache is optional */
|
|
51
|
+
}
|
|
52
|
+
return (cached = `Bearer ${j.accessToken}`);
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
/* fall through to git credential */
|
|
56
|
+
}
|
|
57
|
+
try {
|
|
58
|
+
const out = exec("git", ["credential", "fill"], "protocol=https\nhost=dev.azure.com\n\n");
|
|
59
|
+
const pw = /^password=(.*)$/m.exec(out)?.[1]?.trim();
|
|
60
|
+
if (pw)
|
|
61
|
+
return (cached = basic(pw));
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
/* fall through */
|
|
65
|
+
}
|
|
66
|
+
throw new AihubError("not signed in to Azure DevOps: run 'az login' or set AIHUB_ADO_PAT");
|
|
67
|
+
};
|
|
68
|
+
}
|