@app-vitals/agent-admin 1.59.0 → 1.60.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.59.0",
3
+ "version": "1.60.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/app-vitals/agent.git",
@@ -42,7 +42,7 @@
42
42
  },
43
43
  "dependencies": {
44
44
  "@anthropic-ai/sdk": "0.137.0",
45
- "@app-vitals/agent-lib": "1.59.0",
45
+ "@app-vitals/agent-lib": "1.60.0",
46
46
  "@hono/zod-openapi": "^1.0.0",
47
47
  "@prisma/adapter-pg": "^7.10.0",
48
48
  "@prisma/client": "^7.10.0",
@@ -498,7 +498,7 @@ export function createGithubLookupService(
498
498
  > & {
499
499
  /** Test seam: replaces the clients built from the stored Apps. */
500
500
  clients?: {
501
- installer: LookupInstallerPort;
501
+ installer?: LookupInstallerPort;
502
502
  lookup: LookupRepositoriesPort;
503
503
  };
504
504
  },
@@ -511,27 +511,32 @@ export function createGithubLookupService(
511
511
  deps.appService.get("installer"),
512
512
  deps.appService.get("lookup"),
513
513
  ]);
514
- if (!installer || !lookup || !installer.enterpriseSlug) {
515
- return { configured: false, repos: [] };
516
- }
517
- const [installerKey, lookupKey] = await Promise.all([
518
- deps.appService.getPrivateKey("installer"),
519
- deps.appService.getPrivateKey("lookup"),
520
- ]);
521
- if (!installerKey || !lookupKey)
522
- return { configured: false, repos: [] };
514
+ if (!lookup) return { configured: false, repos: [] };
515
+ const lookupKey = await deps.appService.getPrivateKey("lookup");
516
+ if (!lookupKey) return { configured: false, repos: [] };
523
517
  const common = {
524
518
  apiBase: deps.apiBase,
525
519
  fetchFn: deps.fetchFn,
526
520
  now: deps.now,
527
521
  };
522
+ // An installer with an enterprise slug supplies the org list; without
523
+ // one the lookup App lists the orgs it is installed on.
524
+ const installerKey = installer?.enterpriseSlug
525
+ ? await deps.appService.getPrivateKey("installer")
526
+ : null;
527
+ if (installer?.enterpriseSlug && !installerKey) {
528
+ return { configured: false, repos: [] };
529
+ }
528
530
  clients = {
529
- installer: new GithubEnterpriseClient({
530
- appId: installer.appId,
531
- enterprise: installer.enterpriseSlug,
532
- privateKey: installerKey,
533
- ...common,
534
- }),
531
+ installer:
532
+ installer?.enterpriseSlug && installerKey
533
+ ? new GithubEnterpriseClient({
534
+ appId: installer.appId,
535
+ enterprise: installer.enterpriseSlug,
536
+ privateKey: installerKey,
537
+ ...common,
538
+ })
539
+ : undefined,
535
540
  lookup: new GithubLookupClient({
536
541
  appId: lookup.appId,
537
542
  privateKey: lookupKey,
@@ -81,6 +81,12 @@ export interface LookupToken {
81
81
  repositorySelection?: "all" | "selected";
82
82
  }
83
83
 
84
+ export interface LookupInstallation {
85
+ id: number;
86
+ /** The account (organization) login the App is installed on. */
87
+ login: string;
88
+ }
89
+
84
90
  export interface GithubLookupClientOptions {
85
91
  appId: string;
86
92
  privateKey: string;
@@ -165,6 +171,49 @@ export class GithubLookupClient {
165
171
  return token;
166
172
  }
167
173
 
174
+ /**
175
+ * List the organizations the lookup App is installed on (GET
176
+ * /app/installations, App JWT, paginated). User-account installations are
177
+ * dropped: the per-org read path only works for organizations.
178
+ */
179
+ async listInstallations(): Promise<LookupInstallation[]> {
180
+ const jwt = signGithubAppJwt({
181
+ appId: this.appId,
182
+ privateKey: this.privateKey,
183
+ now: this.now(),
184
+ });
185
+ const all: LookupInstallation[] = [];
186
+ for (let page = 1; page <= MAX_PAGES; page++) {
187
+ const res = await this.request<RawInstallation[]>(
188
+ "GET",
189
+ `/app/installations?per_page=${PER_PAGE}&page=${page}`,
190
+ jwt,
191
+ "GitHub App installations",
192
+ );
193
+ const rows = res.data;
194
+ if (!Array.isArray(rows)) {
195
+ throw new GithubEnterpriseError(
196
+ "unexpected",
197
+ "GitHub App installations: response is not an installation list",
198
+ );
199
+ }
200
+ for (const r of rows) {
201
+ if (
202
+ typeof r.id === "number" &&
203
+ r.account?.type === "Organization" &&
204
+ typeof r.account.login === "string"
205
+ ) {
206
+ all.push({ id: r.id, login: r.account.login });
207
+ }
208
+ }
209
+ const link = res.headers.get("link");
210
+ const hasNext =
211
+ link !== null ? /rel="next"/.test(link) : rows.length >= PER_PAGE;
212
+ if (!hasNext) break;
213
+ }
214
+ return all;
215
+ }
216
+
168
217
  /** List every repository the lookup App can see on `org` (paginated). */
169
218
  async listInstallationRepositories(org: string): Promise<LookupRepository[]> {
170
219
  const { token } = await this.getOrgToken(org);
@@ -302,6 +351,11 @@ export class GithubLookupClient {
302
351
  }
303
352
  }
304
353
 
354
+ interface RawInstallation {
355
+ id?: number;
356
+ account?: { login?: string; type?: string } | null;
357
+ }
358
+
305
359
  interface RawRepository {
306
360
  id: number;
307
361
  name: string;
@@ -91,3 +91,62 @@ describe("GithubLookupClient errors", () => {
91
91
  throw new Error("expected rejection");
92
92
  });
93
93
  });
94
+
95
+ describe("GithubLookupClient.listInstallations", () => {
96
+ const { privateKey } = generateKeyPairSync("rsa", {
97
+ modulusLength: 2048,
98
+ privateKeyEncoding: { type: "pkcs8", format: "pem" },
99
+ publicKeyEncoding: { type: "spki", format: "pem" },
100
+ });
101
+ const inst = (id: number, login: string, type = "Organization") => ({
102
+ id,
103
+ account: { login, type },
104
+ });
105
+
106
+ it("paginates with the App JWT and keeps only organization installs", async () => {
107
+ const page1 = Array.from({ length: 100 }, (_, i) => inst(i, `org${i}`));
108
+ const seen: string[] = [];
109
+ const client = new GithubLookupClient({
110
+ appId: "555",
111
+ privateKey,
112
+ fetchFn: (async (input: RequestInfo | URL, init?: RequestInit) => {
113
+ const u = new URL(input.toString());
114
+ expect(u.pathname).toBe("/app/installations");
115
+ expect(
116
+ String(new Headers(init?.headers).get("authorization")),
117
+ ).toStartWith("Bearer ey");
118
+ seen.push(u.searchParams.get("page") ?? "");
119
+ return u.searchParams.get("page") === "1"
120
+ ? Response.json(page1, {
121
+ headers: { link: '<https://x/?page=2>; rel="next"' },
122
+ })
123
+ : Response.json([inst(900, "last"), inst(901, "someone", "User")]);
124
+ }) as unknown as typeof fetch,
125
+ });
126
+ const out = await client.listInstallations();
127
+ expect(seen).toEqual(["1", "2"]);
128
+ expect(out).toHaveLength(101);
129
+ expect(out.at(-1)).toEqual({ id: 900, login: "last" });
130
+ });
131
+
132
+ it("rejects on a GitHub error", async () => {
133
+ const client = new GithubLookupClient({
134
+ appId: "555",
135
+ privateKey,
136
+ fetchFn: (async () =>
137
+ Response.json({ message: "boom" }, { status: 500 })) as unknown as typeof fetch,
138
+ });
139
+ await expect(client.listInstallations()).rejects.toBeInstanceOf(
140
+ GithubEnterpriseError,
141
+ );
142
+ });
143
+
144
+ it("rejects a non-list response", async () => {
145
+ const client = new GithubLookupClient({
146
+ appId: "555",
147
+ privateKey,
148
+ fetchFn: (async () => Response.json({})) as unknown as typeof fetch,
149
+ });
150
+ await expect(client.listInstallations()).rejects.toThrow("not an installation list");
151
+ });
152
+ });
@@ -53,6 +53,14 @@ function replay(repos: Handler) {
53
53
  expires_at: "2026-11-09T13:00:00Z",
54
54
  });
55
55
  }
56
+ if (u.pathname === "/app/installations") {
57
+ return Response.json(
58
+ Object.entries(INSTALLATIONS).map(([login, id]) => ({
59
+ id,
60
+ account: { login, type: "Organization" },
61
+ })),
62
+ );
63
+ }
56
64
  if (u.pathname === "/installation/repositories") {
57
65
  const bearer = String(new Headers(init?.headers).get("authorization"));
58
66
  return repos(u, Number(bearer.replace(/\D/g, "")));
@@ -175,4 +183,79 @@ describe("githubLookup.listRepositories", () => {
175
183
  });
176
184
  await expect(svc.listRepositories()).rejects.toThrow("limited");
177
185
  });
186
+
187
+ describe("without an installer", () => {
188
+ const lookupApp = { appId: "555", enterpriseSlug: null };
189
+ const stored = (installer: unknown) =>
190
+ ({
191
+ get: async (kind: string) =>
192
+ kind === "lookup" ? lookupApp : installer,
193
+ getPrivateKey: async (kind: string) =>
194
+ kind === "lookup" || installer ? privateKey : null,
195
+ }) as unknown as GitHubEnterpriseAppService;
196
+
197
+ it("lists repos from every org the lookup App is installed on", async () => {
198
+ const svc = createGithubLookupService({
199
+ appService: stored(null),
200
+ fetchFn: replay((_u, id) =>
201
+ Response.json({
202
+ repositories:
203
+ id === 1 ? [repo("acme", "api", false, 10)] : [repo("beta", "web", false, 20)],
204
+ }),
205
+ ),
206
+ now: () => NOW,
207
+ });
208
+ expect(await svc.listRepositories()).toEqual({
209
+ configured: true,
210
+ repos: [
211
+ { fullName: "acme/api", archived: false },
212
+ { fullName: "beta/web", archived: false },
213
+ ],
214
+ });
215
+ });
216
+
217
+ it("lists only the selected repos of a selected-scope installation", async () => {
218
+ const svc = createGithubLookupService({
219
+ appService: stored(null),
220
+ fetchFn: replay((_u, id) =>
221
+ Response.json({
222
+ repositories: id === 1 ? [repo("acme", "only-this")] : [],
223
+ }),
224
+ ),
225
+ now: () => NOW,
226
+ });
227
+ expect((await svc.listRepositories()).repos).toEqual([
228
+ { fullName: "acme/only-this", archived: false },
229
+ ]);
230
+ });
231
+
232
+ it("rejects on a GitHub failure instead of returning an empty list", async () => {
233
+ const svc = createGithubLookupService({
234
+ appService: stored(null),
235
+ fetchFn: (async () =>
236
+ Response.json({ message: "boom" }, { status: 500 })) as unknown as typeof fetch,
237
+ now: () => NOW,
238
+ });
239
+ await expect(svc.listRepositories()).rejects.toBeInstanceOf(GithubEnterpriseError);
240
+ });
241
+
242
+ it("an installer with a slug but no key stays unconfigured", async () => {
243
+ const svc = createGithubLookupService({
244
+ appService: {
245
+ get: async (kind: string) =>
246
+ kind === "lookup" ? lookupApp : { appId: "1", enterpriseSlug: "ent" },
247
+ getPrivateKey: async (kind: string) =>
248
+ kind === "lookup" ? privateKey : null,
249
+ } as unknown as GitHubEnterpriseAppService,
250
+ });
251
+ expect(await svc.listRepositories()).toEqual({ configured: false, repos: [] });
252
+ });
253
+
254
+ it("with an installer configured the org list still comes from it", async () => {
255
+ const svc = service(["acme"], replay(() => Response.json({ repositories: [repo("acme", "api")] })));
256
+ expect((await svc.listRepositories()).repos).toEqual([
257
+ { fullName: "acme/api", archived: false },
258
+ ]);
259
+ });
260
+ });
178
261
  });
@@ -2,7 +2,8 @@
2
2
  * admin/src/github-lookup-repositories.ts
3
3
  * The lookup-App repository lister exposed to extensions as
4
4
  * AdminPlatformServices.githubLookup: every repository the lookup App can see,
5
- * across the organizations the installer App can reach.
5
+ * across the organizations the installer App can reach, or, with no
6
+ * installer configured, the organizations the lookup App is installed on.
6
7
  *
7
8
  * A GitHub failure rejects; it is never reported as an empty list. An org
8
9
  * without the lookup App installed (not_found) is skipped. The stored-App
@@ -35,14 +36,19 @@ export interface LookupInstallerPort {
35
36
  /** The lookup-side client surface the lister needs. */
36
37
  export interface LookupRepositoriesPort {
37
38
  listInstallationRepositories(org: string): Promise<LookupRepository[]>;
39
+ /** Required only when no installer is configured (the App lists its own orgs). */
40
+ listInstallations?(): Promise<{ login: string }[]>;
38
41
  }
39
42
 
40
43
  export async function listLookupRepositories(
41
- installer: LookupInstallerPort,
44
+ installer: LookupInstallerPort | undefined,
42
45
  lookup: LookupRepositoriesPort,
43
46
  ): Promise<GithubLookupRepository[]> {
44
47
  const repos = new Map<string, GithubLookupRepository>();
45
- for (const { login } of await installer.listInstallableOrganizations()) {
48
+ const orgs = installer
49
+ ? await installer.listInstallableOrganizations()
50
+ : await listOwnInstallations(lookup);
51
+ for (const { login } of orgs) {
46
52
  let rows: LookupRepository[];
47
53
  try {
48
54
  rows = await lookup.listInstallationRepositories(login);
@@ -58,3 +64,12 @@ export async function listLookupRepositories(
58
64
  }
59
65
  return [...repos.values()];
60
66
  }
67
+
68
+ async function listOwnInstallations(
69
+ lookup: LookupRepositoriesPort,
70
+ ): Promise<{ login: string }[]> {
71
+ if (!lookup.listInstallations) {
72
+ throw new Error("lookup client cannot list its own installations");
73
+ }
74
+ return lookup.listInstallations();
75
+ }