@app-vitals/agent-admin 1.34.0 → 1.35.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/src/admin-ui.ts CHANGED
@@ -63,6 +63,21 @@ import {
63
63
  renderGithubReconcileApplyResult,
64
64
  renderGithubReconcilePlanResult,
65
65
  } from "./admin-ui-github-reconcile-pages.ts";
66
+ import {
67
+ actuallyBlockedWork,
68
+ type InstallationReposView,
69
+ type LostAccessPreviewView,
70
+ type LostAccessRemoveView,
71
+ type OpenWorkView,
72
+ type RepoRemovalPreviewView,
73
+ renderInstallationChangeResult,
74
+ renderInstallationReposBody,
75
+ renderInstallationReposError,
76
+ renderLostAccessPreview,
77
+ renderLostAccessResult,
78
+ renderRepoRemovalConfirm,
79
+ renderRepoRemovalResult,
80
+ } from "./admin-ui-installation-repos-pages.ts";
66
81
  import {
67
82
  type AgentDetail,
68
83
  type AgentOption,
@@ -1985,6 +2000,233 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
1985
2000
  },
1986
2001
  );
1987
2002
 
2003
+ // IRM-1.4: thin proxies over the installation repos (IRM-1.2) and
2004
+ // lost-access (IRM-1.3) routes, plus the repo-removal preview (IRM-1.7).
2005
+ // They return HTML fragments for the Installation repos card and the agent's
2006
+ // repos list; the agents API enforces platform-admin too.
2007
+ const apiErrorFragment = async (res: Response | null, fallback: string) => {
2008
+ let message = fallback;
2009
+ try {
2010
+ const j = (await res?.json()) as { error?: unknown };
2011
+ if (typeof j.error === "string" && j.error) message = j.error;
2012
+ } catch {
2013
+ // keep the fallback
2014
+ }
2015
+ const status = res && res.status >= 400 ? res.status : 502;
2016
+ return html(renderInstallationReposError(status, message), { status });
2017
+ };
2018
+ const readBody = async (c: Context<AdminUIEnv>) => {
2019
+ try {
2020
+ const parsed = await c.req.json();
2021
+ if (parsed && typeof parsed === "object") {
2022
+ return parsed as Record<string, unknown>;
2023
+ }
2024
+ } catch {
2025
+ // empty body
2026
+ }
2027
+ return {} as Record<string, unknown>;
2028
+ };
2029
+ const stringList = (v: unknown): string[] =>
2030
+ Array.isArray(v) ? v.filter((r): r is string => typeof r === "string") : [];
2031
+ const postJson = (c: Context<AdminUIEnv>, path: string, body: unknown) =>
2032
+ callApi(c, agentsApi as NonNullable<typeof agentsApi>, path, {
2033
+ method: "POST",
2034
+ headers: { "content-type": "application/json" },
2035
+ body: JSON.stringify(body),
2036
+ });
2037
+ const apiId = (c: Context<AdminUIEnv>) =>
2038
+ encodeURIComponent(c.req.param("id") ?? "");
2039
+
2040
+ app.get("/admin/agents/:id/github-installation", requireAuth, async (c) => {
2041
+ if (!c.var.isAdmin || !agentsApi) {
2042
+ return new Response("Forbidden", { status: 403 });
2043
+ }
2044
+ const agentId = c.req.param("id") ?? "";
2045
+ const res = await callApi(
2046
+ c,
2047
+ agentsApi,
2048
+ `/agents/${apiId(c)}/github-installation/repos`,
2049
+ );
2050
+ if (!res?.ok) {
2051
+ return apiErrorFragment(res, "Could not load the installation repos.");
2052
+ }
2053
+ const agent = await agentService.getDetail(agentId);
2054
+ return html(
2055
+ renderInstallationReposBody(
2056
+ (await res.json()) as InstallationReposView,
2057
+ agent?.githubUsername,
2058
+ ),
2059
+ );
2060
+ });
2061
+
2062
+ for (const kind of ["add", "remove"] as const) {
2063
+ app.post(
2064
+ `/admin/agents/:id/github-installation/repos/${kind}`,
2065
+ requireAuth,
2066
+ async (c) => {
2067
+ if (!c.var.isAdmin || !agentsApi) {
2068
+ return new Response("Forbidden", { status: 403 });
2069
+ }
2070
+ const repos = stringList((await readBody(c)).repos);
2071
+ const res = await postJson(
2072
+ c,
2073
+ `/agents/${apiId(c)}/github-installation/repos${kind === "add" ? "" : "/remove"}`,
2074
+ { repos },
2075
+ );
2076
+ if (!res?.ok) {
2077
+ return apiErrorFragment(res, `Could not ${kind} the repos.`);
2078
+ }
2079
+ return html(
2080
+ renderInstallationChangeResult(
2081
+ kind,
2082
+ (await res.json()) as { auditId?: string; repos?: string[] },
2083
+ ),
2084
+ );
2085
+ },
2086
+ );
2087
+ }
2088
+
2089
+ app.get(
2090
+ "/admin/agents/:id/github-lost-access/preview",
2091
+ requireAuth,
2092
+ async (c) => {
2093
+ if (!c.var.isAdmin || !agentsApi) {
2094
+ return new Response("Forbidden", { status: 403 });
2095
+ }
2096
+ const res = await callApi(
2097
+ c,
2098
+ agentsApi,
2099
+ `/agents/${apiId(c)}/github-lost-access/preview`,
2100
+ );
2101
+ if (!res?.ok) {
2102
+ return apiErrorFragment(res, "Could not preview the removal.");
2103
+ }
2104
+ return html(
2105
+ renderLostAccessPreview((await res.json()) as LostAccessPreviewView),
2106
+ );
2107
+ },
2108
+ );
2109
+
2110
+ app.post(
2111
+ "/admin/agents/:id/github-lost-access/remove",
2112
+ requireAuth,
2113
+ async (c) => {
2114
+ if (!c.var.isAdmin || !agentsApi) {
2115
+ return new Response("Forbidden", { status: 403 });
2116
+ }
2117
+ const body = await readBody(c);
2118
+ const repos = stringList(body.repos);
2119
+ // The open work the removal will block, read just before it happens so
2120
+ // the result can show what was actually blocked.
2121
+ let seen: OpenWorkView[] = [];
2122
+ const pre = await callApi(
2123
+ c,
2124
+ agentsApi,
2125
+ `/agents/${apiId(c)}/github-lost-access/preview`,
2126
+ );
2127
+ if (pre?.ok) {
2128
+ try {
2129
+ seen = ((await pre.json()) as LostAccessPreviewView).openWork ?? [];
2130
+ } catch {
2131
+ // no preview: nothing is claimed as blocked
2132
+ }
2133
+ }
2134
+ const res = await postJson(
2135
+ c,
2136
+ `/agents/${apiId(c)}/github-lost-access/remove`,
2137
+ { repos, confirmRemovals: body.confirmRemovals === true },
2138
+ );
2139
+ if (!res?.ok) {
2140
+ return apiErrorFragment(res, "Could not remove the repos.");
2141
+ }
2142
+ const result = (await res.json()) as LostAccessRemoveView;
2143
+ const blocked =
2144
+ result.status === "applied"
2145
+ ? actuallyBlockedWork(seen, result.repos ?? [], result.warnings ?? [])
2146
+ : [];
2147
+ return html(renderLostAccessResult(result, blocked));
2148
+ },
2149
+ );
2150
+
2151
+ const splitRepo = (repo: string): [string, string] | null =>
2152
+ isOrgRepo(repo) ? (repo.split("/") as [string, string]) : null;
2153
+ const repoApiPath = (c: Context<AdminUIEnv>, repo: [string, string]) =>
2154
+ `/agents/${apiId(c)}/repos/${encodeURIComponent(repo[0])}/${encodeURIComponent(repo[1])}`;
2155
+
2156
+ app.get("/admin/agents/:id/repos/removal-confirm", requireAuth, async (c) => {
2157
+ if (!c.var.isAdmin || !agentsApi) {
2158
+ return new Response("Forbidden", { status: 403 });
2159
+ }
2160
+ const repo = splitRepo(c.req.query("repo") ?? "");
2161
+ if (!repo) {
2162
+ return html(renderInstallationReposError(400, "Invalid repo."), {
2163
+ status: 400,
2164
+ });
2165
+ }
2166
+ const res = await callApi(
2167
+ c,
2168
+ agentsApi,
2169
+ `${repoApiPath(c, repo)}/removal-preview`,
2170
+ );
2171
+ if (!res?.ok) {
2172
+ return apiErrorFragment(res, "Could not preview the removal.");
2173
+ }
2174
+ return html(
2175
+ renderRepoRemovalConfirm(
2176
+ c.req.param("id") ?? "",
2177
+ (await res.json()) as RepoRemovalPreviewView,
2178
+ ),
2179
+ );
2180
+ });
2181
+
2182
+ app.post(
2183
+ "/admin/agents/:id/repos/remove-confirmed",
2184
+ requireAuth,
2185
+ async (c) => {
2186
+ if (!c.var.isAdmin || !agentsApi) {
2187
+ return new Response("Forbidden", { status: 403 });
2188
+ }
2189
+ const name = String((await readBody(c)).repo ?? "");
2190
+ const repo = splitRepo(name);
2191
+ if (!repo) {
2192
+ return html(renderInstallationReposError(400, "Invalid repo."), {
2193
+ status: 400,
2194
+ });
2195
+ }
2196
+ let seen: OpenWorkView[] = [];
2197
+ const pre = await callApi(
2198
+ c,
2199
+ agentsApi,
2200
+ `${repoApiPath(c, repo)}/removal-preview`,
2201
+ );
2202
+ if (pre?.ok) {
2203
+ try {
2204
+ seen = ((await pre.json()) as RepoRemovalPreviewView).openWork ?? [];
2205
+ } catch {
2206
+ // no preview: nothing is claimed as blocked
2207
+ }
2208
+ }
2209
+ const res = await callApi(c, agentsApi, repoApiPath(c, repo), {
2210
+ method: "DELETE",
2211
+ });
2212
+ if (!res?.ok) return apiErrorFragment(res, "Could not remove the repo.");
2213
+ let warning = "";
2214
+ try {
2215
+ warning = ((await res.json()) as { warning?: string }).warning ?? "";
2216
+ } catch {
2217
+ // no warning
2218
+ }
2219
+ const warnings = warning ? warning.split("; ") : [];
2220
+ return html(
2221
+ renderRepoRemovalResult(
2222
+ name,
2223
+ actuallyBlockedWork(seen, [name], warnings),
2224
+ warnings,
2225
+ ),
2226
+ );
2227
+ },
2228
+ );
2229
+
1988
2230
  app.get("/admin/agents/:id", requireAuth, async (c) => {
1989
2231
  const agentId = c.req.param("id");
1990
2232
  const agent = await agentService.getDetail(agentId);
@@ -2073,6 +2315,31 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
2073
2315
  },
2074
2316
  );
2075
2317
 
2318
+ // IRM-1.4: the repos the agent App's installation covers, for the repos
2319
+ // list's coverage warning. Best effort: unknown means no warning.
2320
+ const enterpriseConfigured = await githubEnterpriseConfigured(c);
2321
+ let installationRepos: string[] | undefined;
2322
+ if (
2323
+ enterpriseConfigured &&
2324
+ agentsApi &&
2325
+ "GH_APP_ID" in (envResult.env ?? {})
2326
+ ) {
2327
+ const res = await callApi(
2328
+ c,
2329
+ agentsApi,
2330
+ `/agents/${encodeURIComponent(agentId)}/github-installation/repos`,
2331
+ );
2332
+ if (res?.ok) {
2333
+ try {
2334
+ installationRepos = (
2335
+ (await res.json()) as InstallationReposView
2336
+ ).orgs.flatMap((o) => o.repos.map((r) => r.fullName));
2337
+ } catch {
2338
+ // unknown: no coverage warning
2339
+ }
2340
+ }
2341
+ }
2342
+
2076
2343
  return html(
2077
2344
  renderAgentDetailPage(
2078
2345
  agentDetail,
@@ -2095,7 +2362,8 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
2095
2362
  configurationCardsHtml: cards.configuration,
2096
2363
  queueActivityLinks: deps.queueActivityLinks,
2097
2364
  marketplaces,
2098
- githubEnterpriseConfigured: await githubEnterpriseConfigured(c),
2365
+ githubEnterpriseConfigured: enterpriseConfigured,
2366
+ ...(installationRepos ? { installationRepos } : {}),
2099
2367
  },
2100
2368
  ),
2101
2369
  );