@app-vitals/agent-admin 1.89.0 → 1.91.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@app-vitals/agent-admin",
3
- "version": "1.89.0",
3
+ "version": "1.91.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/app-vitals/agent.git",
@@ -44,7 +44,7 @@
44
44
  },
45
45
  "dependencies": {
46
46
  "@anthropic-ai/sdk": "0.137.0",
47
- "@app-vitals/agent-lib": "1.89.0",
47
+ "@app-vitals/agent-lib": "1.91.0",
48
48
  "@hono/zod-openapi": "^1.0.0",
49
49
  "@prisma/adapter-pg": "^7.10.0",
50
50
  "@prisma/client": "^7.10.0",
@@ -231,10 +231,14 @@ export function renderGithubOrgsFragment(
231
231
  const disabled = data.agentAppReason ? " disabled" : "";
232
232
  const rows = data.orgs
233
233
  .map((o) => {
234
+ const base = `/admin/agents/${escapeHtml(agentId)}/github-orgs/${escapeHtml(encodeURIComponent(o.login))}`;
234
235
  const action = o.agentAppInstalled
235
- ? ""
236
- : `<form method="POST" action="/admin/agents/${escapeHtml(agentId)}/github-orgs/${escapeHtml(encodeURIComponent(o.login))}/install" style="display:inline;margin:0"><button type="submit" class="btn btn-primary" style="font-size:12px;padding:4px 10px"${disabled}>Install</button></form>`;
237
- return `<tr><td>${escapeHtml(o.login)}</td><td>${installedBadge(o.lookupAppInstalled)}</td><td>${installedBadge(o.agentAppInstalled)}</td><td style="text-align:right">${action}</td></tr>`;
236
+ ? `<button type="button" class="btn btn-danger" style="font-size:12px;padding:4px 10px" data-url="${base}/uninstall-confirm" onclick="var b=this,t=document.getElementById('uninstall-confirm-'+b.getAttribute('data-org'));b.disabled=true;fetch(b.getAttribute('data-url'),{credentials:'same-origin'}).then(function(r){return r.text()}).then(function(h){t.innerHTML=h}).catch(function(){t.textContent='The request failed.'}).then(function(){b.disabled=false})" data-org="${escapeHtml(o.login)}"${disabled}>Uninstall</button>`
237
+ : `<form method="POST" action="${base}/install" style="display:inline;margin:0"><button type="submit" class="btn btn-primary" style="font-size:12px;padding:4px 10px"${disabled}>Install</button></form>`;
238
+ const confirmRow = o.agentAppInstalled
239
+ ? `<tr><td colspan="4" id="uninstall-confirm-${escapeHtml(o.login)}" style="padding:0"></td></tr>`
240
+ : "";
241
+ return `<tr><td>${escapeHtml(o.login)}</td><td>${installedBadge(o.lookupAppInstalled)}</td><td>${installedBadge(o.agentAppInstalled)}</td><td style="text-align:right">${action}</td></tr>${confirmRow}`;
238
242
  })
239
243
  .join("");
240
244
  return `${reason}<table class="data-table">
@@ -242,3 +246,41 @@ export function renderGithubOrgsFragment(
242
246
  <tbody>${rows}</tbody>
243
247
  </table>`;
244
248
  }
249
+
250
+ export interface GithubUninstallPreviewView {
251
+ org: string;
252
+ repos: string[];
253
+ openWork: { id: string; repo: string; title?: string }[];
254
+ warnings?: string[];
255
+ }
256
+
257
+ /** The confirm step for uninstalling the agent App from an org (GIS-1.6). */
258
+ export function renderGithubUninstallConfirm(
259
+ agentId: string,
260
+ view: GithubUninstallPreviewView,
261
+ ): string {
262
+ const org = escapeHtml(view.org);
263
+ const action = `/admin/agents/${escapeHtml(agentId)}/github-orgs/${escapeHtml(encodeURIComponent(view.org))}/uninstall`;
264
+ const repos =
265
+ view.repos.length === 0
266
+ ? `<p class="empty-state">No agent repos would be removed.</p>`
267
+ : `<ul>${view.repos.map((r) => `<li class="mono">${escapeHtml(r)}</li>`).join("")}</ul>`;
268
+ const work =
269
+ view.openWork.length === 0
270
+ ? `<p class="empty-state">No open tasks or PRs would be blocked.</p>`
271
+ : `<ul>${view.openWork.map((w) => `<li><span class="mono">${escapeHtml(w.id)}</span> (${escapeHtml(w.repo)})${w.title ? ` ${escapeHtml(w.title)}` : ""}</li>`).join("")}</ul>`;
272
+ const warnings = (view.warnings ?? [])
273
+ .map((w) => `<div class="alert alert-warning">${escapeHtml(w)}</div>`)
274
+ .join("");
275
+ const confirmAll =
276
+ view.repos.length > 10
277
+ ? `<input type="hidden" name="confirmRemovals" value="true" />`
278
+ : "";
279
+ return `<div class="alert alert-warning" data-reason="uninstall-confirm" style="margin:8px 0">
280
+ <div style="font-weight:600">Uninstall the agent App from ${org}?</div>
281
+ ${warnings}
282
+ <div style="margin-top:8px">Agent repos to be removed:</div>${repos}
283
+ <div>Open tasks and PRs to be blocked:</div>${work}
284
+ <form method="POST" action="${action}" style="margin:8px 0 0">${confirmAll}<button type="submit" class="btn btn-danger">Confirm uninstall</button></form>
285
+ </div>`;
286
+ }
@@ -11,6 +11,7 @@ import {
11
11
  renderGithubAccessResult,
12
12
  renderGithubInstallCard,
13
13
  renderGithubOrgsFragment,
14
+ renderGithubUninstallConfirm,
14
15
  renderGithubUsernameRow,
15
16
  } from "./admin-ui-github-identity-pages.ts";
16
17
  import {
@@ -301,10 +302,52 @@ describe("Install on org block (GHC-1.2)", () => {
301
302
  );
302
303
  expect(html).toContain("Installed");
303
304
  expect(html).toContain("Not installed");
304
- expect(html).not.toContain("disabled");
305
+ expect(html).not.toMatch(/ disabled/);
305
306
  expect(html).not.toContain('name="agentId"');
306
307
  });
307
308
 
309
+ test("installed org shows Uninstall, not installed keeps Install", () => {
310
+ const html = renderGithubOrgsFragment("agent-123", orgs());
311
+ expect(html.match(/>Uninstall<\/button>/g)?.length).toBe(1);
312
+ expect(html).toContain(
313
+ "/admin/agents/agent-123/github-orgs/%3Cb%3Ex%3C%2Fb%3E/uninstall-confirm",
314
+ );
315
+ expect(html.match(/>Install<\/button>/g)?.length).toBe(1);
316
+ });
317
+
318
+ test("Uninstall is disabled when the agent App reason is set", () => {
319
+ const html = renderGithubOrgsFragment(
320
+ "agent-123",
321
+ orgs({ agentAppReason: "nope" }),
322
+ );
323
+ expect(html).toMatch(/data-org="[^"]*" disabled>Uninstall</);
324
+ });
325
+
326
+ test("unconfigured installer renders no Uninstall", () => {
327
+ expect(
328
+ renderGithubOrgsFragment("a", { configured: false, orgs: orgs().orgs }),
329
+ ).not.toContain("Uninstall");
330
+ });
331
+
332
+ test("uninstall confirm lists repos and open work, escaped", () => {
333
+ const html = renderGithubUninstallConfirm("agent-123", {
334
+ org: "acme",
335
+ repos: ["acme/<r>"],
336
+ openWork: [{ id: "T-1", repo: "acme/<r>", title: "Do it" }],
337
+ });
338
+ expect(html).toContain("acme/&lt;r&gt;");
339
+ expect(html).toContain("T-1");
340
+ expect(html).toContain('action="/admin/agents/agent-123/github-orgs/acme/uninstall"');
341
+ expect(html).not.toContain("confirmRemovals");
342
+ });
343
+
344
+ test("uninstall confirm carries confirmRemovals for more than 10 repos", () => {
345
+ const repos = Array.from({ length: 11 }, (_, i) => `acme/r${i}`);
346
+ expect(
347
+ renderGithubUninstallConfirm("a", { org: "acme", repos, openWork: [] }),
348
+ ).toContain('name="confirmRemovals"');
349
+ });
350
+
308
351
  test("installed org has no Install button; names are escaped", () => {
309
352
  const html = renderGithubOrgsFragment("agent-123", orgs());
310
353
  expect(html).not.toContain("<b>x</b>");
@@ -322,6 +322,80 @@ describe("admin UI — GitHub identity (GHE-2.5)", () => {
322
322
  expect(calls.length).toBe(0);
323
323
  });
324
324
 
325
+ it("uninstall-confirm proxies the preview and renders the lists", async () => {
326
+ const { app, calls } = setup({
327
+ enterpriseApi: () =>
328
+ json({
329
+ status: "preview",
330
+ org: "acme",
331
+ installationId: 9,
332
+ repos: ["acme/web"],
333
+ openWork: [{ id: "T-7", repo: "acme/web" }],
334
+ warnings: [],
335
+ }),
336
+ });
337
+ const res = await app.request(
338
+ `${orgsUrl}/acme/uninstall-confirm`,
339
+ opts(admin),
340
+ );
341
+ expect(res.status).toBe(200);
342
+ const c = calls.at(-1);
343
+ expect(c?.path).toBe(
344
+ `/github-enterprise/orgs/acme/install?agentId=${AGENT_ID}&preview=true`,
345
+ );
346
+ expect(c?.init?.method).toBe("DELETE");
347
+ const body = await res.text();
348
+ expect(body).toContain("acme/web");
349
+ expect(body).toContain("T-7");
350
+ expect(body).toContain(`${orgsUrl}/acme/uninstall`);
351
+ });
352
+
353
+ it("uninstall POST calls the DELETE route and redirects with a banner", async () => {
354
+ const { app, calls } = setup({
355
+ enterpriseApi: () => json({ status: "uninstalled" }),
356
+ });
357
+ const res = await app.request(`${orgsUrl}/acme/uninstall`, opts(admin, "POST"));
358
+ expect(res.status).toBe(302);
359
+ expect(res.headers.get("location")).toBe(
360
+ `/admin/agents/${AGENT_ID}/access?success=github_app_uninstalled`,
361
+ );
362
+ const c = calls.at(-1);
363
+ expect(c?.path).toBe(
364
+ `/github-enterprise/orgs/acme/install?agentId=${AGENT_ID}`,
365
+ );
366
+ expect(c?.init?.method).toBe("DELETE");
367
+ });
368
+
369
+ it("uninstall POST forwards confirmRemovals and surfaces errors", async () => {
370
+ const { app, calls } = setup({
371
+ enterpriseApi: () => json({ error: "GitHub refused" }, 502),
372
+ });
373
+ const res = await app.request(`${orgsUrl}/acme/uninstall`, {
374
+ ...opts(admin, "POST"),
375
+ headers: {
376
+ Cookie: `admin_session=${admin}`,
377
+ "content-type": "application/x-www-form-urlencoded",
378
+ },
379
+ body: "confirmRemovals=true",
380
+ });
381
+ expect(calls.at(-1)?.path).toContain("&confirmRemovals=true");
382
+ expect(res.status).toBe(302);
383
+ expect(res.headers.get("location")).toContain(
384
+ encodeURIComponent("GitHub refused"),
385
+ );
386
+ });
387
+
388
+ it("uninstall routes are forbidden for a non-admin and make no call", async () => {
389
+ const { app, calls } = setup({});
390
+ expect(
391
+ (await app.request(`${orgsUrl}/acme/uninstall`, opts(member, "POST"))).status,
392
+ ).toBe(403);
393
+ expect(
394
+ (await app.request(`${orgsUrl}/acme/uninstall-confirm`, opts(member))).status,
395
+ ).toBe(403);
396
+ expect(calls.length).toBe(0);
397
+ });
398
+
325
399
  it("banners render on the Access tab", async () => {
326
400
  const { app } = setup({});
327
401
  const res = await app.request(
@@ -208,6 +208,7 @@ export interface GithubSyncPreviewView {
208
208
  appAdds?: string[];
209
209
  appRemoves?: string[];
210
210
  agentRemoves?: string[];
211
+ selectionSwitches?: { org: string; repos: string[] }[];
211
212
  skippedOrgs?: { org: string; reason: string; detail?: SkipDetail }[];
212
213
  openWork?: OpenWorkView[];
213
214
  warnings?: string[];
@@ -465,6 +466,19 @@ const repoList = (kind: string, title: string, repos: readonly string[]) =>
465
466
  const plural = (n: number, one: string, many: string) =>
466
467
  `${n} ${n === 1 ? one : many}`;
467
468
 
469
+ /** Orgs the confirmed sync switches from "all repositories" to "selected repositories". */
470
+ const selectionSwitchesHtml = (
471
+ switches: readonly { org: string; repos: readonly string[] }[],
472
+ ) =>
473
+ switches.length === 0
474
+ ? ""
475
+ : `<div data-list="selection-switches" style="margin-top:8px"><div style="font-weight:600">Switch from all repositories to selected repositories (${switches.length})</div><ul style="margin:4px 0 0 18px">${switches
476
+ .map(
477
+ (s) =>
478
+ `<li data-switch-org="${escapeHtml(s.org)}"><span class="mono">${escapeHtml(s.org)}</span>: keeps ${plural(s.repos.length, "repo", "repos")}</li>`,
479
+ )
480
+ .join("")}</ul></div>`;
481
+
468
482
  /** One readable sentence-list describing why an org's cached access was skipped. */
469
483
  function skipDetailText(d: SkipDetail, timezone?: string): string {
470
484
  const out: string[] = [];
@@ -528,8 +542,8 @@ const skippedOrgsHtml = (
528
542
  .join("")}</div>`;
529
543
 
530
544
  /**
531
- * The single confirm step of Sync: the installation adds, installation
532
- * removes, agent-repo removes and the open work the removals would block. One
545
+ * The single confirm step of Sync: the selection switches, installation adds,
546
+ * installation removes, agent-repo removes and the open work the removals would block. One
533
547
  * confirmation applies them all; more than the removal cap needs the extra
534
548
  * checkbox (confirmRemovals).
535
549
  */
@@ -544,12 +558,19 @@ export function renderSyncConfirm(
544
558
  const appAdds = preview.appAdds ?? [];
545
559
  const appRemoves = preview.appRemoves ?? [];
546
560
  const agentRemoves = preview.agentRemoves ?? [];
561
+ const switches = preview.selectionSwitches ?? [];
547
562
  const skippedList = preview.skippedOrgs ?? [];
548
563
  const skipped = skippedOrgsHtml(skippedList, timezone);
549
564
  const warnings = warningsHtml(
550
565
  withoutSkipWarnings(preview.warnings, skippedList),
551
566
  );
552
- if (appAdds.length + appRemoves.length + agentRemoves.length === 0) {
567
+ if (
568
+ appAdds.length +
569
+ appRemoves.length +
570
+ agentRemoves.length +
571
+ switches.length ===
572
+ 0
573
+ ) {
553
574
  if (skippedList.length > 0) {
554
575
  return `<div class="alert alert-warning" data-sync="nothing-synced">Nothing was synced: ${skippedList.length} org(s) skipped, see below.</div>${skipped}${warnings}`;
555
576
  }
@@ -564,7 +585,7 @@ export function renderSyncConfirm(
564
585
  const capBox = overCap
565
586
  ? `<label style="display:block;margin:12px 0"><input type="checkbox" id="github-sync-confirm-removals" /> I confirm removing ${removals} repos (more than ${MAX_UNCONFIRMED_REMOVALS}).</label>`
566
587
  : "";
567
- return `<div class="alert alert-warning" data-sync="confirm">Apply these changes?</div>${repoList("app-adds", "Add to the agent App installation", appAdds)}${repoList("app-removes", "Remove from the agent App installation", appRemoves)}${repoList("agent-removes", "Remove from the agent's repos", agentRemoves)}${renderOpenWorkByRepo(
588
+ return `<div class="alert alert-warning" data-sync="confirm">Apply these changes?</div>${selectionSwitchesHtml(switches)}${repoList("app-adds", "Add to the agent App installation", appAdds)}${repoList("app-removes", "Remove from the agent App installation", appRemoves)}${repoList("agent-removes", "Remove from the agent's repos", agentRemoves)}${renderOpenWorkByRepo(
568
589
  preview.openWork ?? [],
569
590
  "Open tasks and PRs that will be blocked",
570
591
  "No open tasks or PRs would be blocked.",
@@ -187,6 +187,47 @@ describe("renderSyncConfirm", () => {
187
187
  });
188
188
  });
189
189
 
190
+ it("lists each selection switch with org and kept repo count in the one confirm", () => {
191
+ const html = renderSyncConfirm("a1", {
192
+ ...preview,
193
+ selectionSwitches: [
194
+ { org: "acme", repos: ["acme/a", "acme/b"] },
195
+ { org: "solo<x>", repos: ["solo/a"] },
196
+ ],
197
+ });
198
+ expect(html).toContain('data-list="selection-switches"');
199
+ expect(html).toContain("all repositories to selected repositories (2)");
200
+ expect(html).toContain('data-switch-org="acme"');
201
+ expect(html).toContain("keeps 2 repos");
202
+ expect(html).toContain("solo&lt;x&gt;");
203
+ expect(html).toContain("keeps 1 repo<");
204
+ expect(html.match(/id="github-sync-confirm-btn"/g)?.length).toBe(1);
205
+ });
206
+
207
+ it("confirms a switch-only plan instead of saying in sync", () => {
208
+ const html = renderSyncConfirm("a1", {
209
+ status: "previewed",
210
+ selectionSwitches: [{ org: "acme", repos: ["acme/a"] }],
211
+ });
212
+ expect(html).toContain('data-sync="confirm"');
213
+ expect(html).not.toContain("in-sync");
214
+ });
215
+
216
+ it("shows the empty-target skip warning with the Uninstall hint", () => {
217
+ const html = renderSyncConfirm("a1", {
218
+ ...preview,
219
+ skippedOrgs: [
220
+ {
221
+ org: "empty-org",
222
+ reason:
223
+ "the bound user can push to none of this org's repos, so it stays on All repositories; use Uninstall to remove the agent App from this org",
224
+ },
225
+ ],
226
+ });
227
+ expect(html).toContain("empty-org");
228
+ expect(html).toContain("use Uninstall to remove the agent App");
229
+ });
230
+
190
231
  it("requires the over-cap checkbox for more than 10 removals", () => {
191
232
  const many = Array.from({ length: 11 }, (_, i) => `acme/r${i}`);
192
233
  const html = renderSyncConfirm("a1", {
@@ -397,6 +397,20 @@ describe("admin UI — GitHub sync card (IRM-3.2)", () => {
397
397
  expect(html).toContain("30 repos have no access answer yet.");
398
398
  });
399
399
 
400
+ it("renders a selection switch row in the Sync confirm fragment", async () => {
401
+ const { app } = setup({
402
+ agentsApi: () =>
403
+ json({
404
+ status: "previewed",
405
+ selectionSwitches: [{ org: "acme", repos: ["acme/a", "acme/b"] }],
406
+ }),
407
+ });
408
+ const html = await (await get(app, "/github-sync/confirm")).text();
409
+ expect(html).toContain('data-list="selection-switches"');
410
+ expect(html).toContain('data-switch-org="acme"');
411
+ expect(html).toContain("keeps 2 repos");
412
+ });
413
+
400
414
  it("shows the reason for a skipped preview", async () => {
401
415
  const { app } = setup({
402
416
  agentsApi: () => json({ status: "skipped", reason: "user not found" }),
@@ -1043,6 +1043,7 @@ export const AGENT_POST_SECTIONS: Readonly<Record<string, AgentSection>> = {
1043
1043
  "/github-sync/apply-confirmed": "access",
1044
1044
  "/github-cache/refresh": "access",
1045
1045
  "/github-orgs/:org/install": "access",
1046
+ "/github-orgs/:org/uninstall": "access",
1046
1047
  "/repos/remove-confirmed": "configuration",
1047
1048
  "/envs": "configuration",
1048
1049
  "/template/detach": "configuration",
package/src/admin-ui.ts CHANGED
@@ -56,6 +56,8 @@ import {
56
56
  renderGithubAccessError,
57
57
  renderGithubAccessResult,
58
58
  renderGithubOrgsFragment,
59
+ renderGithubUninstallConfirm,
60
+ type GithubUninstallPreviewView,
59
61
  } from "./admin-ui-github-identity-pages.ts";
60
62
  import {
61
63
  actuallyBlockedWork,
@@ -2211,6 +2213,74 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
2211
2213
  },
2212
2214
  );
2213
2215
 
2216
+ // GIS-1.6: Uninstall confirm (the GIS-1.5 preview) and the uninstall itself.
2217
+ app.get(
2218
+ "/admin/agents/:id/github-orgs/:org/uninstall-confirm",
2219
+ requireAuth,
2220
+ async (c) => {
2221
+ const agentId = c.req.param("id");
2222
+ if (!c.var.isAdmin || !githubEnterpriseApi) {
2223
+ return new Response("Forbidden", { status: 403 });
2224
+ }
2225
+ const res = await callApi(
2226
+ c,
2227
+ githubEnterpriseApi,
2228
+ `/github-enterprise/orgs/${encodeURIComponent(c.req.param("org"))}/install?agentId=${encodeURIComponent(agentId)}&preview=true`,
2229
+ { method: "DELETE" },
2230
+ );
2231
+ if (!res?.ok) {
2232
+ return html(
2233
+ renderGithubAccessError(
2234
+ await gheError(res, "Could not preview the uninstall."),
2235
+ ),
2236
+ { status: res && res.status >= 400 ? res.status : 502 },
2237
+ );
2238
+ }
2239
+ return html(
2240
+ renderGithubUninstallConfirm(
2241
+ agentId,
2242
+ (await res.json()) as GithubUninstallPreviewView,
2243
+ ),
2244
+ );
2245
+ },
2246
+ );
2247
+
2248
+ app.post(
2249
+ "/admin/agents/:id/github-orgs/:org/uninstall",
2250
+ requireAuth,
2251
+ async (c) => {
2252
+ const agentId = c.req.param("id");
2253
+ if (!c.var.isAdmin || !githubEnterpriseApi) {
2254
+ return new Response("Forbidden", { status: 403 });
2255
+ }
2256
+ let confirm = "";
2257
+ try {
2258
+ const form = await c.req.parseBody();
2259
+ if (form.confirmRemovals === "true") confirm = "&confirmRemovals=true";
2260
+ } catch {
2261
+ // no body: no confirmRemovals
2262
+ }
2263
+ const res = await callApi(
2264
+ c,
2265
+ githubEnterpriseApi,
2266
+ `/github-enterprise/orgs/${encodeURIComponent(c.req.param("org"))}/install?agentId=${encodeURIComponent(agentId)}${confirm}`,
2267
+ { method: "DELETE" },
2268
+ );
2269
+ if (!res?.ok) {
2270
+ return c.redirect(
2271
+ agentSectionPath(agentId, "access", {
2272
+ error: await gheError(res, "Could not uninstall the agent app."),
2273
+ }),
2274
+ 302,
2275
+ );
2276
+ }
2277
+ return c.redirect(
2278
+ `${agentSectionPath(agentId, "access")}?success=github_app_uninstalled`,
2279
+ 302,
2280
+ );
2281
+ },
2282
+ );
2283
+
2214
2284
  // IRM-1.4: thin proxies over the installation repos list (IRM-1.2) and
2215
2285
  // the repo-removal preview (IRM-1.7).
2216
2286
  // They return HTML fragments for the Installation repos card and the agent's
@@ -2507,7 +2577,9 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
2507
2577
  ? "Agent app installed."
2508
2578
  : successParam === "github_app_already_installed"
2509
2579
  ? "The agent app was already installed on that organization."
2510
- : undefined;
2580
+ : successParam === "github_app_uninstalled"
2581
+ ? "Agent app uninstalled."
2582
+ : undefined;
2511
2583
  const warningParam = c.req.query("warning");
2512
2584
  const warning =
2513
2585
  warningParam === "restrict_slack_no_members"