@ferronhooi/aihub 0.1.0-next.7 → 0.1.0-next.8

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.
@@ -2,7 +2,7 @@ import { readFileSync } from "node:fs";
2
2
  import { parseArgs } from "node:util";
3
3
  import { changedFiles } from "../lib/changes.js";
4
4
  import { AihubError } from "../lib/errors.js";
5
- import { buildPlan, chooseWorkspace, executionDataFromParams, fabricWorkspaces, FabricClient, formatJob, formatPlan, guardEnv, makeFabricAuth, waitForJob, } from "../lib/fabric.js";
5
+ import { buildPlan, executionDataFromParams, fabricWorkspaces, FabricClient, formatJob, formatPlan, guardEnv, makeFabricAuth, selectWorkspaces, workspaceGroups, waitForJob, } from "../lib/fabric.js";
6
6
  import { git, repoRoot } from "../lib/git.js";
7
7
  import { loadTeam } from "../lib/team.js";
8
8
  const USAGE = "usage: aihub fabric plan|run|status [flags]";
@@ -15,7 +15,8 @@ export async function run(argv, ctx) {
15
15
  options: {
16
16
  branch: { type: "string" },
17
17
  target: { type: "string" },
18
- workspace: { type: "string" },
18
+ workspace: { type: "string", multiple: true },
19
+ workspaces: { type: "string" },
19
20
  item: { type: "string" },
20
21
  job: { type: "string" },
21
22
  "params-file": { type: "string" },
@@ -35,22 +36,28 @@ export async function run(argv, ctx) {
35
36
  const given = v.branch;
36
37
  if (sub === "plan") {
37
38
  const c = changedFiles(root, team, { branch: given, target: v.target });
38
- ctx.out(formatPlan(buildPlan(c.files, list, { branch: c.branch, workspace: v.workspace }), c.branch, c.target));
39
+ ctx.out(formatPlan(buildPlan(c.files, list, { branch: c.branch, workspace: v.workspace?.[0] }), c.branch, c.target));
39
40
  return;
40
41
  }
41
42
  const branch = given ?? git(root, ["rev-parse", "--abbrev-ref", "HEAD"], { allowFail: true }).trim();
42
- const ws = chooseWorkspace(list, { workspace: v.workspace, branch });
43
- if (!ws) {
44
- if (v.workspace)
45
- throw new AihubError(`workspace '${v.workspace}' is not in team.yaml fabric.workspaces; the CLI only runs workspaces the team listed`);
43
+ const chosen = selectWorkspaces(list, workspaceGroups(team), { workspaces: v.workspace, group: v.workspaces, branch });
44
+ if (!chosen.length)
46
45
  throw new AihubError("no dev/feature workspace in team.yaml fabric.workspaces; pass --workspace <name|id>");
47
- }
48
- guardEnv(ws, v["allow-env"]);
46
+ // refuse before any Fabric call when one of them is not allowed
47
+ for (const w of chosen)
48
+ guardEnv(w, v["allow-env"]);
49
+ const ws = chosen[0];
49
50
  if (!v.item)
50
51
  throw new AihubError(`aihub fabric ${sub} needs --item <name|id>`);
51
52
  if (sub === "status" && !v.job)
52
53
  throw new AihubError("aihub fabric status needs --job <id> and --item <name|id>");
53
54
  const client = new FabricClient(makeFabricAuth({ env: ctx.env, exec: ctx.exec, cacheFile: ctx.tokenCache && `${ctx.tokenCache}.fabric` }), ctx.fetch);
55
+ if (chosen.length > 1) {
56
+ if (sub === "status")
57
+ throw new AihubError("aihub fabric status takes one --workspace");
58
+ await runMany(ctx, client, chosen, v);
59
+ return;
60
+ }
54
61
  const wsId = await client.workspaceId(ws);
55
62
  const item = await client.resolveItem(wsId, v.item);
56
63
  if (sub === "status") {
@@ -69,3 +76,46 @@ export async function run(argv, ctx) {
69
76
  if (j.status !== "Completed")
70
77
  throw new AihubError(`${item.displayName} ended as ${j.status}`);
71
78
  }
79
+ /** Same item in several workspaces: start in each, wait for all, one table, keep going on errors. */
80
+ async function runMany(ctx, client, chosen, v) {
81
+ const rows = chosen.map((ws) => ({ ws }));
82
+ for (const r of rows) {
83
+ try {
84
+ r.wsId = await client.workspaceId(r.ws);
85
+ const item = await client.resolveItem(r.wsId, v.item);
86
+ r.itemId = item.id;
87
+ const exec = v["params-file"] ? executionDataFromParams(readFileSync(v["params-file"], "utf8"), item) : undefined;
88
+ r.jobId = await client.startJob(r.wsId, item, exec);
89
+ ctx.out(`started ${item.displayName} in ${r.ws.name} (${r.ws.environment}): job ${r.jobId}`);
90
+ }
91
+ catch (e) {
92
+ r.error = e.message;
93
+ }
94
+ }
95
+ if (v.wait) {
96
+ const pollMs = Number(ctx.env.AIHUB_POLL_MS ?? 5000);
97
+ for (const r of rows) {
98
+ if (!r.jobId)
99
+ continue;
100
+ try {
101
+ r.result = await waitForJob(client, r.wsId, r.itemId, r.jobId, { pollMs });
102
+ }
103
+ catch (e) {
104
+ r.error = e.message;
105
+ }
106
+ }
107
+ }
108
+ const lines = ["workspace | environment | job | result"];
109
+ for (const r of rows) {
110
+ const res = r.error
111
+ ? `error: ${r.error}`
112
+ : r.result
113
+ ? `${r.result.status}${r.result.status === "Failed" ? `: ${(r.result.failureReason?.message ?? "no details").replace(/\s+/g, " ").trim()}` : ""}`
114
+ : "started";
115
+ lines.push(`${r.ws.name} | ${r.ws.environment} | ${r.jobId ?? "-"} | ${res}`);
116
+ }
117
+ ctx.out(lines.join(String.fromCharCode(10)));
118
+ const bad = rows.filter((r) => r.error || (v.wait && r.result?.status !== "Completed"));
119
+ if (bad.length)
120
+ throw new AihubError(`${bad.length} of ${rows.length} workspaces did not complete: ${bad.map((r) => r.ws.name).join(", ")}`);
121
+ }
@@ -45,21 +45,61 @@ export function chooseWorkspace(list, opts) {
45
45
  const byBranch = opts.branch ? list.find((x) => x.branches && anyGlob(x.branches, opts.branch)) : undefined;
46
46
  return byBranch ?? list.find((x) => x.environment === "feature") ?? list.find((x) => x.environment === "dev");
47
47
  }
48
+ export function workspaceGroups(team) {
49
+ const g = team.fabric?.workspaceGroups;
50
+ return g && typeof g === "object" ? g : {};
51
+ }
52
+ /** All workspaces whose branch rules match the branch. */
53
+ export function matchingWorkspaces(list, branch) {
54
+ return branch ? list.filter((x) => x.branches && anyGlob(x.branches, branch)) : [];
55
+ }
56
+ /**
57
+ * Workspaces for a run: explicit names/ids and/or a group from team.yaml, else the default choice.
58
+ * Unknown names and groups are refused. Duplicates are dropped.
59
+ */
60
+ export function selectWorkspaces(list, groups, opts) {
61
+ const names = [...(opts.workspaces ?? [])];
62
+ if (opts.group) {
63
+ const key = Object.keys(groups).find((k) => k.toLowerCase() === opts.group.toLowerCase());
64
+ if (!key)
65
+ throw new AihubError(`workspace group '${opts.group}' is not in team.yaml fabric.workspaceGroups (${Object.keys(groups).join(", ") || "none"})`);
66
+ names.push(...groups[key]);
67
+ }
68
+ if (!names.length) {
69
+ const w = chooseWorkspace(list, { branch: opts.branch });
70
+ return w ? [w] : [];
71
+ }
72
+ const out = [];
73
+ for (const n of names) {
74
+ const w = chooseWorkspace(list, { workspace: n });
75
+ if (!w)
76
+ throw new AihubError(`workspace '${n}' is not in team.yaml fabric.workspaces; the CLI only runs workspaces the team listed`);
77
+ if (!out.includes(w))
78
+ out.push(w);
79
+ }
80
+ return out;
81
+ }
48
82
  export function buildPlan(files, list, opts) {
49
- return { workspace: chooseWorkspace(list, opts), items: mapItems(files) };
83
+ const first = chooseWorkspace(list, opts);
84
+ const matched = opts.workspace ? [] : matchingWorkspaces(list, opts.branch);
85
+ return { workspace: first, workspaces: matched.length > 1 ? matched : first ? [first] : [], items: mapItems(files) };
50
86
  }
51
87
  export function formatPlan(p, branch, target) {
52
88
  const lines = [`Fabric plan for ${branch} (vs ${target}):`];
53
- lines.push(p.workspace
54
- ? `workspace: ${p.workspace.name} (${p.workspace.environment})`
55
- : "workspace: none found in team.yaml fabric.workspaces; pass --workspace <name|id>");
89
+ if (p.workspaces.length > 1)
90
+ lines.push(`workspaces (branch rules): ${p.workspaces.map((w) => `${w.name} (${w.environment})`).join(", ")}`);
91
+ else
92
+ lines.push(p.workspace
93
+ ? `workspace: ${p.workspace.name} (${p.workspace.environment})`
94
+ : "workspace: none found in team.yaml fabric.workspaces; pass --workspace <name|id>");
56
95
  if (!p.items.length) {
57
96
  lines.push("No Fabric items changed: nothing to run in Fabric.");
58
97
  return lines.join("\n");
59
98
  }
60
99
  let n = 0;
61
100
  for (const it of p.items) {
62
- const how = it.jobType ? `run: aihub fabric run --item ${it.name} --wait` : "deploy only (nothing to run)";
101
+ const all = p.workspaces.length > 1 ? p.workspaces.map((w) => ` --workspace ${w.name}`).join("") : "";
102
+ const how = it.jobType ? `run: aihub fabric run --item ${it.name}${all} --wait` : "deploy only (nothing to run)";
63
103
  lines.push(`${++n}. ${it.type} ${it.name} (${it.files} file${it.files === 1 ? "" : "s"}): ${how}`);
64
104
  }
65
105
  return lines.join("\n");
@@ -48,7 +48,9 @@ fabric: # Fabric workspaces for `aihub fabric` (optio
48
48
  - name: <workspace> # workspace display name, e.g. DEV-test-project (optional)
49
49
  id: <guid> # workspace id (optional; looked up by name when absent)
50
50
  environment: dev # dev | feature | test | acc | prd (optional); acc and prd are never run by the CLI
51
- branches: [] # branch patterns tested in this workspace, e.g. ["feature/*"] (optional)
51
+ branches: [] # branch patterns tested in this workspace, e.g. ["feature/*"]; several matches run in each (optional)
52
+ workspaceGroups: # named sets of workspaces for `aihub fabric run --workspaces <name>` (optional)
53
+ <group>: [] # group name and the workspace names in it, e.g. [DEV-anna, DEV-shared] (optional)
52
54
 
53
55
  robot: # Robot integration tests for `aihub robot` (optional)
54
56
  pipeline: <pipeline> # Azure DevOps pipeline that runs the Robot suites (optional; needed for robot run)
@@ -15,6 +15,8 @@ aihub fabric plan
15
15
  aihub fabric run --item nb_load_orders --wait
16
16
  ```
17
17
 
18
+ Several workspaces ("in beide DEV's"): repeat `--workspace` or use `--workspaces <group>`.
19
+
18
20
  2. Robot: check first, then run only when needed. The target comes from the sentence: "test op staging" is `--on staging`, "draai Robot op feature" is `--on feature`. No target named: use the team default, do not ask. Run merges your branch into a merge target first and stops on a conflict:
19
21
 
20
22
  ```sh
@@ -5,7 +5,7 @@
5
5
  | Command | Does |
6
6
  |---|---|
7
7
  | `aihub fabric plan [--branch B] [--target T] [--workspace W]` | Maps changed files (`<name>.Notebook/`, `<name>.DataPipeline/`, lakehouse items) to Fabric items, ordered: dependencies, notebooks, pipelines. |
8
- | `aihub fabric run --item N [--workspace W] [--params-file F] [--wait]` | Starts a notebook or pipeline job; with `--wait` polls and prints status and the failure reason. |
8
+ | `aihub fabric run --item N [--workspace W]... [--workspaces GROUP] [--params-file F] [--wait]` | Starts a notebook or pipeline job; with `--wait` polls and prints status and the failure reason. Several `--workspace` flags or a team.yaml group run it in each workspace: one results table, errors in one workspace do not stop the others. |
9
9
  | `aihub fabric status --item N --job ID [--workspace W]` | Status of one job. |
10
10
  | `aihub robot needed [--branch B]` | Which suites cover a changed flow, and whether a test changed with it. |
11
11
  | `aihub robot run [--on target\|branch] [--suite S] [--include tag] [--pipeline P] [--no-merge] [--no-wait]` | With a merge target: merges the current branch (and `mustAlreadyContain` branches that are behind) into the target branch in a temporary worktree, pushes it and lists the commits that went in; a conflict stops with the files and pushes nothing. Then queues the team Robot pipeline on that branch and prints the results summary. |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ferronhooi/aihub",
3
- "version": "0.1.0-next.7",
3
+ "version": "0.1.0-next.8",
4
4
  "description": "One small CLI for Azure DevOps team workflows used by coding agents (stories, pull requests, reviews, cherry-picks).",
5
5
  "license": "MIT",
6
6
  "type": "module",