@app-vitals/agent-admin 1.90.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.90.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.90.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(
@@ -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"