@app-vitals/agent-admin 1.34.0 → 1.35.1

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.
Files changed (35) hide show
  1. package/openapi.platform.json +2 -392
  2. package/package.json +2 -2
  3. package/src/admin-boot.ts +1 -12
  4. package/src/admin-extension.ts +3 -3
  5. package/src/admin-ui-github-identity.smoke.test.ts +0 -114
  6. package/src/admin-ui-installation-repos-pages.ts +404 -0
  7. package/src/admin-ui-installation-repos-pages.unit.test.ts +57 -0
  8. package/src/admin-ui-installation-repos.smoke.test.ts +496 -0
  9. package/src/admin-ui-pages.ts +57 -9
  10. package/src/admin-ui.ts +236 -50
  11. package/src/agents-api.smoke.test.ts +2 -352
  12. package/src/agents-api.ts +5 -269
  13. package/src/github-enterprise-isolation.unit.test.ts +4 -4
  14. package/src/{github-reconcile-wiring.ts → github-installation-wiring.ts} +18 -38
  15. package/src/github-installation-wiring.unit.test.ts +111 -0
  16. package/src/github-installation.fixtures.ts +76 -0
  17. package/src/installation-ports.integration.test.ts +89 -0
  18. package/src/installation-ports.ts +207 -0
  19. package/src/installation-ports.unit.test.ts +51 -0
  20. package/src/installation-repos-service.ts +12 -12
  21. package/src/installation-repos-service.unit.test.ts +3 -3
  22. package/src/lost-access-removal.ts +9 -9
  23. package/src/lost-access-removal.unit.test.ts +7 -7
  24. package/src/reconcile-open-work.ts +5 -7
  25. package/src/repo-access-rules.ts +58 -0
  26. package/src/repo-access-rules.unit.test.ts +98 -0
  27. package/src/admin-ui-github-reconcile-pages.ts +0 -208
  28. package/src/admin-ui-github-reconcile-pages.unit.test.ts +0 -222
  29. package/src/github-reconcile-routes.integration.test.ts +0 -206
  30. package/src/github-reconcile-wiring.unit.test.ts +0 -170
  31. package/src/reconcile-apply-service.integration.test.ts +0 -271
  32. package/src/reconcile-apply-service.ts +0 -439
  33. package/src/reconcile-apply-service.unit.test.ts +0 -279
  34. package/src/reconcile-planner.ts +0 -264
  35. package/src/reconcile-planner.unit.test.ts +0 -274
package/src/admin-ui.ts CHANGED
@@ -58,11 +58,20 @@ import {
58
58
  renderGithubAccessResult,
59
59
  } from "./admin-ui-github-identity-pages.ts";
60
60
  import {
61
- type GithubReconcileApplyView,
62
- type GithubReconcilePlanView,
63
- renderGithubReconcileApplyResult,
64
- renderGithubReconcilePlanResult,
65
- } from "./admin-ui-github-reconcile-pages.ts";
61
+ actuallyBlockedWork,
62
+ type InstallationReposView,
63
+ type LostAccessPreviewView,
64
+ type LostAccessRemoveView,
65
+ type OpenWorkView,
66
+ type RepoRemovalPreviewView,
67
+ renderInstallationChangeResult,
68
+ renderInstallationReposBody,
69
+ renderInstallationReposError,
70
+ renderLostAccessPreview,
71
+ renderLostAccessResult,
72
+ renderRepoRemovalConfirm,
73
+ renderRepoRemovalResult,
74
+ } from "./admin-ui-installation-repos-pages.ts";
66
75
  import {
67
76
  type AgentDetail,
68
77
  type AgentOption,
@@ -1909,77 +1918,228 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
1909
1918
  );
1910
1919
  });
1911
1920
 
1912
- // GHE-3.5: thin proxies over the GHE-3.4 plan/apply routes. They return
1913
- // HTML fragments for the Reconcile card; the agents API enforces admin too.
1914
- const reconcileProxy = async (
1915
- c: Context<AdminUIEnv>,
1916
- step: "plan" | "apply",
1917
- ): Promise<{ res: Response | null; body: Record<string, unknown> }> => {
1918
- let body: Record<string, unknown> = {};
1921
+ // IRM-1.4: thin proxies over the installation repos (IRM-1.2) and
1922
+ // lost-access (IRM-1.3) routes, plus the repo-removal preview (IRM-1.7).
1923
+ // They return HTML fragments for the Installation repos card and the agent's
1924
+ // repos list; the agents API enforces platform-admin too.
1925
+ const apiErrorFragment = async (res: Response | null, fallback: string) => {
1926
+ let message = fallback;
1927
+ try {
1928
+ const j = (await res?.json()) as { error?: unknown };
1929
+ if (typeof j.error === "string" && j.error) message = j.error;
1930
+ } catch {
1931
+ // keep the fallback
1932
+ }
1933
+ const status = res && res.status >= 400 ? res.status : 502;
1934
+ return html(renderInstallationReposError(status, message), { status });
1935
+ };
1936
+ const readBody = async (c: Context<AdminUIEnv>) => {
1919
1937
  try {
1920
1938
  const parsed = await c.req.json();
1921
- if (parsed && typeof parsed === "object") body = parsed;
1939
+ if (parsed && typeof parsed === "object") {
1940
+ return parsed as Record<string, unknown>;
1941
+ }
1922
1942
  } catch {
1923
1943
  // empty body
1924
1944
  }
1945
+ return {} as Record<string, unknown>;
1946
+ };
1947
+ const stringList = (v: unknown): string[] =>
1948
+ Array.isArray(v) ? v.filter((r): r is string => typeof r === "string") : [];
1949
+ const postJson = (c: Context<AdminUIEnv>, path: string, body: unknown) =>
1950
+ callApi(c, agentsApi as NonNullable<typeof agentsApi>, path, {
1951
+ method: "POST",
1952
+ headers: { "content-type": "application/json" },
1953
+ body: JSON.stringify(body),
1954
+ });
1955
+ const apiId = (c: Context<AdminUIEnv>) =>
1956
+ encodeURIComponent(c.req.param("id") ?? "");
1957
+
1958
+ app.get("/admin/agents/:id/github-installation", requireAuth, async (c) => {
1959
+ if (!c.var.isAdmin || !agentsApi) {
1960
+ return new Response("Forbidden", { status: 403 });
1961
+ }
1962
+ const agentId = c.req.param("id") ?? "";
1925
1963
  const res = await callApi(
1926
1964
  c,
1927
- agentsApi as NonNullable<typeof agentsApi>,
1928
- `/agents/${encodeURIComponent(c.req.param("id") ?? "")}/github-reconcile/${step}`,
1929
- {
1930
- method: "POST",
1931
- headers: { "content-type": "application/json" },
1932
- body: JSON.stringify(body),
1933
- },
1965
+ agentsApi,
1966
+ `/agents/${apiId(c)}/github-installation/repos`,
1934
1967
  );
1935
- return { res, body };
1936
- };
1937
- const reconcileError = async (res: Response | null) => {
1938
- let message = "The reconcile request failed.";
1939
- try {
1940
- const j = (await res?.json()) as { error?: unknown };
1941
- if (typeof j.error === "string" && j.error) message = j.error;
1942
- } catch {
1943
- // keep the generic message
1968
+ if (!res?.ok) {
1969
+ return apiErrorFragment(res, "Could not load the installation repos.");
1944
1970
  }
1945
- return html(renderGithubAccessError(message), {
1946
- status: res && res.status >= 400 ? res.status : 502,
1947
- });
1948
- };
1971
+ const agent = await agentService.getDetail(agentId);
1972
+ return html(
1973
+ renderInstallationReposBody(
1974
+ (await res.json()) as InstallationReposView,
1975
+ agent?.githubUsername,
1976
+ ),
1977
+ );
1978
+ });
1949
1979
 
1950
- app.post(
1951
- "/admin/agents/:id/github-reconcile/plan",
1980
+ for (const kind of ["add", "remove"] as const) {
1981
+ app.post(
1982
+ `/admin/agents/:id/github-installation/repos/${kind}`,
1983
+ requireAuth,
1984
+ async (c) => {
1985
+ if (!c.var.isAdmin || !agentsApi) {
1986
+ return new Response("Forbidden", { status: 403 });
1987
+ }
1988
+ const repos = stringList((await readBody(c)).repos);
1989
+ const res = await postJson(
1990
+ c,
1991
+ `/agents/${apiId(c)}/github-installation/repos${kind === "add" ? "" : "/remove"}`,
1992
+ { repos },
1993
+ );
1994
+ if (!res?.ok) {
1995
+ return apiErrorFragment(res, `Could not ${kind} the repos.`);
1996
+ }
1997
+ return html(
1998
+ renderInstallationChangeResult(
1999
+ kind,
2000
+ (await res.json()) as { auditId?: string; repos?: string[] },
2001
+ ),
2002
+ );
2003
+ },
2004
+ );
2005
+ }
2006
+
2007
+ app.get(
2008
+ "/admin/agents/:id/github-lost-access/preview",
1952
2009
  requireAuth,
1953
2010
  async (c) => {
1954
2011
  if (!c.var.isAdmin || !agentsApi) {
1955
2012
  return new Response("Forbidden", { status: 403 });
1956
2013
  }
1957
- const { res, body } = await reconcileProxy(c, "plan");
1958
- if (!res?.ok) return reconcileError(res);
1959
- const adds = Array.isArray(body.addRepos)
1960
- ? body.addRepos.filter((r): r is string => typeof r === "string")
1961
- : [];
2014
+ const res = await callApi(
2015
+ c,
2016
+ agentsApi,
2017
+ `/agents/${apiId(c)}/github-lost-access/preview`,
2018
+ );
2019
+ if (!res?.ok) {
2020
+ return apiErrorFragment(res, "Could not preview the removal.");
2021
+ }
1962
2022
  return html(
1963
- renderGithubReconcilePlanResult(
1964
- (await res.json()) as GithubReconcilePlanView,
1965
- adds,
1966
- ),
2023
+ renderLostAccessPreview((await res.json()) as LostAccessPreviewView),
2024
+ );
2025
+ },
2026
+ );
2027
+
2028
+ app.post(
2029
+ "/admin/agents/:id/github-lost-access/remove",
2030
+ requireAuth,
2031
+ async (c) => {
2032
+ if (!c.var.isAdmin || !agentsApi) {
2033
+ return new Response("Forbidden", { status: 403 });
2034
+ }
2035
+ const body = await readBody(c);
2036
+ const repos = stringList(body.repos);
2037
+ // The open work the removal will block, read just before it happens so
2038
+ // the result can show what was actually blocked.
2039
+ let seen: OpenWorkView[] = [];
2040
+ const pre = await callApi(
2041
+ c,
2042
+ agentsApi,
2043
+ `/agents/${apiId(c)}/github-lost-access/preview`,
2044
+ );
2045
+ if (pre?.ok) {
2046
+ try {
2047
+ seen = ((await pre.json()) as LostAccessPreviewView).openWork ?? [];
2048
+ } catch {
2049
+ // no preview: nothing is claimed as blocked
2050
+ }
2051
+ }
2052
+ const res = await postJson(
2053
+ c,
2054
+ `/agents/${apiId(c)}/github-lost-access/remove`,
2055
+ { repos, confirmRemovals: body.confirmRemovals === true },
1967
2056
  );
2057
+ if (!res?.ok) {
2058
+ return apiErrorFragment(res, "Could not remove the repos.");
2059
+ }
2060
+ const result = (await res.json()) as LostAccessRemoveView;
2061
+ const blocked =
2062
+ result.status === "applied"
2063
+ ? actuallyBlockedWork(seen, result.repos ?? [], result.warnings ?? [])
2064
+ : [];
2065
+ return html(renderLostAccessResult(result, blocked));
1968
2066
  },
1969
2067
  );
1970
2068
 
2069
+ const splitRepo = (repo: string): [string, string] | null =>
2070
+ isOrgRepo(repo) ? (repo.split("/") as [string, string]) : null;
2071
+ const repoApiPath = (c: Context<AdminUIEnv>, repo: [string, string]) =>
2072
+ `/agents/${apiId(c)}/repos/${encodeURIComponent(repo[0])}/${encodeURIComponent(repo[1])}`;
2073
+
2074
+ app.get("/admin/agents/:id/repos/removal-confirm", requireAuth, async (c) => {
2075
+ if (!c.var.isAdmin || !agentsApi) {
2076
+ return new Response("Forbidden", { status: 403 });
2077
+ }
2078
+ const repo = splitRepo(c.req.query("repo") ?? "");
2079
+ if (!repo) {
2080
+ return html(renderInstallationReposError(400, "Invalid repo."), {
2081
+ status: 400,
2082
+ });
2083
+ }
2084
+ const res = await callApi(
2085
+ c,
2086
+ agentsApi,
2087
+ `${repoApiPath(c, repo)}/removal-preview`,
2088
+ );
2089
+ if (!res?.ok) {
2090
+ return apiErrorFragment(res, "Could not preview the removal.");
2091
+ }
2092
+ return html(
2093
+ renderRepoRemovalConfirm(
2094
+ c.req.param("id") ?? "",
2095
+ (await res.json()) as RepoRemovalPreviewView,
2096
+ ),
2097
+ );
2098
+ });
2099
+
1971
2100
  app.post(
1972
- "/admin/agents/:id/github-reconcile/apply",
2101
+ "/admin/agents/:id/repos/remove-confirmed",
1973
2102
  requireAuth,
1974
2103
  async (c) => {
1975
2104
  if (!c.var.isAdmin || !agentsApi) {
1976
2105
  return new Response("Forbidden", { status: 403 });
1977
2106
  }
1978
- const { res } = await reconcileProxy(c, "apply");
1979
- if (!res?.ok) return reconcileError(res);
2107
+ const name = String((await readBody(c)).repo ?? "");
2108
+ const repo = splitRepo(name);
2109
+ if (!repo) {
2110
+ return html(renderInstallationReposError(400, "Invalid repo."), {
2111
+ status: 400,
2112
+ });
2113
+ }
2114
+ let seen: OpenWorkView[] = [];
2115
+ const pre = await callApi(
2116
+ c,
2117
+ agentsApi,
2118
+ `${repoApiPath(c, repo)}/removal-preview`,
2119
+ );
2120
+ if (pre?.ok) {
2121
+ try {
2122
+ seen = ((await pre.json()) as RepoRemovalPreviewView).openWork ?? [];
2123
+ } catch {
2124
+ // no preview: nothing is claimed as blocked
2125
+ }
2126
+ }
2127
+ const res = await callApi(c, agentsApi, repoApiPath(c, repo), {
2128
+ method: "DELETE",
2129
+ });
2130
+ if (!res?.ok) return apiErrorFragment(res, "Could not remove the repo.");
2131
+ let warning = "";
2132
+ try {
2133
+ warning = ((await res.json()) as { warning?: string }).warning ?? "";
2134
+ } catch {
2135
+ // no warning
2136
+ }
2137
+ const warnings = warning ? warning.split("; ") : [];
1980
2138
  return html(
1981
- renderGithubReconcileApplyResult(
1982
- (await res.json()) as GithubReconcileApplyView,
2139
+ renderRepoRemovalResult(
2140
+ name,
2141
+ actuallyBlockedWork(seen, [name], warnings),
2142
+ warnings,
1983
2143
  ),
1984
2144
  );
1985
2145
  },
@@ -2073,6 +2233,31 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
2073
2233
  },
2074
2234
  );
2075
2235
 
2236
+ // IRM-1.4: the repos the agent App's installation covers, for the repos
2237
+ // list's coverage warning. Best effort: unknown means no warning.
2238
+ const enterpriseConfigured = await githubEnterpriseConfigured(c);
2239
+ let installationRepos: string[] | undefined;
2240
+ if (
2241
+ enterpriseConfigured &&
2242
+ agentsApi &&
2243
+ "GH_APP_ID" in (envResult.env ?? {})
2244
+ ) {
2245
+ const res = await callApi(
2246
+ c,
2247
+ agentsApi,
2248
+ `/agents/${encodeURIComponent(agentId)}/github-installation/repos`,
2249
+ );
2250
+ if (res?.ok) {
2251
+ try {
2252
+ installationRepos = (
2253
+ (await res.json()) as InstallationReposView
2254
+ ).orgs.flatMap((o) => o.repos.map((r) => r.fullName));
2255
+ } catch {
2256
+ // unknown: no coverage warning
2257
+ }
2258
+ }
2259
+ }
2260
+
2076
2261
  return html(
2077
2262
  renderAgentDetailPage(
2078
2263
  agentDetail,
@@ -2095,7 +2280,8 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
2095
2280
  configurationCardsHtml: cards.configuration,
2096
2281
  queueActivityLinks: deps.queueActivityLinks,
2097
2282
  marketplaces,
2098
- githubEnterpriseConfigured: await githubEnterpriseConfigured(c),
2283
+ githubEnterpriseConfigured: enterpriseConfigured,
2284
+ ...(installationRepos ? { installationRepos } : {}),
2099
2285
  },
2100
2286
  ),
2101
2287
  );