@app-vitals/agent-admin 1.59.0 → 1.61.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.
@@ -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,209 @@ 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
204
+ ? [repo("acme", "api", false, 10)]
205
+ : [repo("beta", "web", false, 20)],
206
+ }),
207
+ ),
208
+ now: () => NOW,
209
+ });
210
+ expect(await svc.listRepositories()).toEqual({
211
+ configured: true,
212
+ repos: [
213
+ { fullName: "acme/api", archived: false },
214
+ { fullName: "beta/web", archived: false },
215
+ ],
216
+ });
217
+ });
218
+
219
+ it("lists only the selected repos of a selected-scope installation", async () => {
220
+ const svc = createGithubLookupService({
221
+ appService: stored(null),
222
+ fetchFn: replay((_u, id) =>
223
+ Response.json({
224
+ repositories: id === 1 ? [repo("acme", "only-this")] : [],
225
+ }),
226
+ ),
227
+ now: () => NOW,
228
+ });
229
+ expect((await svc.listRepositories()).repos).toEqual([
230
+ { fullName: "acme/only-this", archived: false },
231
+ ]);
232
+ });
233
+
234
+ it("rejects on a GitHub failure instead of returning an empty list", async () => {
235
+ const svc = createGithubLookupService({
236
+ appService: stored(null),
237
+ fetchFn: (async () =>
238
+ Response.json(
239
+ { message: "boom" },
240
+ { status: 500 },
241
+ )) as unknown as typeof fetch,
242
+ now: () => NOW,
243
+ });
244
+ await expect(svc.listRepositories()).rejects.toBeInstanceOf(
245
+ GithubEnterpriseError,
246
+ );
247
+ });
248
+
249
+ it("an installer with a slug but no key stays unconfigured", async () => {
250
+ const svc = createGithubLookupService({
251
+ appService: {
252
+ get: async (kind: string) =>
253
+ kind === "lookup"
254
+ ? lookupApp
255
+ : { appId: "1", enterpriseSlug: "ent" },
256
+ getPrivateKey: async (kind: string) =>
257
+ kind === "lookup" ? privateKey : null,
258
+ } as unknown as GitHubEnterpriseAppService,
259
+ });
260
+ expect(await svc.listRepositories()).toEqual({
261
+ configured: false,
262
+ repos: [],
263
+ });
264
+ });
265
+
266
+ it("with an installer configured the org list still comes from it", async () => {
267
+ const svc = service(
268
+ ["acme"],
269
+ replay(() => Response.json({ repositories: [repo("acme", "api")] })),
270
+ );
271
+ expect((await svc.listRepositories()).repos).toEqual([
272
+ { fullName: "acme/api", archived: false },
273
+ ]);
274
+ });
275
+ });
276
+ });
277
+
278
+ describe("githubLookup.listRepositories(accountId)", () => {
279
+ type Row = { appId: string; enterpriseSlug: string | null };
280
+ /** In-memory app service with the real account-then-deployment-wide fallback. */
281
+ function scopedApps(rows: Record<string, Row | undefined>) {
282
+ const key = (role: string, accountId?: string | null) =>
283
+ `${accountId ?? ""}:${role}`;
284
+ const resolve = (role: string, accountId?: string | null) =>
285
+ (accountId ? rows[key(role, accountId)] : undefined) ??
286
+ rows[key(role, null)];
287
+ return {
288
+ get: async (role: string, accountId?: string | null) =>
289
+ resolve(role, accountId) ?? null,
290
+ getPrivateKey: async (role: string, accountId?: string | null) =>
291
+ resolve(role, accountId) ? privateKey : null,
292
+ } as unknown as GitHubEnterpriseAppService;
293
+ }
294
+
295
+ /** The lookup App id (JWT iss) decides which org installation it sees. */
296
+ const byApp: typeof fetch = (async (
297
+ input: RequestInfo | URL,
298
+ init?: RequestInit,
299
+ ) => {
300
+ const u = new URL(input.toString());
301
+ const bearer = String(new Headers(init?.headers).get("authorization"));
302
+ const jwt = bearer.replace(/^Bearer /, "");
303
+ if (u.pathname === "/app/installations") {
304
+ const iss = JSON.parse(
305
+ Buffer.from(jwt.split(".")[1], "base64url").toString(),
306
+ ).iss;
307
+ const org = String(iss) === "777" ? "gamma" : "acme";
308
+ return Response.json([
309
+ {
310
+ id: org === "gamma" ? 3 : 1,
311
+ account: { login: org, type: "Organization" },
312
+ },
313
+ ]);
314
+ }
315
+ const inst = u.pathname.match(/^\/orgs\/([^/]+)\/installation$/);
316
+ if (inst) {
317
+ return Response.json({
318
+ id: inst[1] === "gamma" ? 3 : 1,
319
+ repository_selection: "all",
320
+ });
321
+ }
322
+ if (u.pathname.match(/^\/app\/installations\/(\d+)\/access_tokens$/)) {
323
+ const id = u.pathname.match(/installations\/(\d+)/)?.[1];
324
+ return Response.json({
325
+ token: `ghs_tok${id}`,
326
+ expires_at: "2026-11-09T13:00:00Z",
327
+ });
328
+ }
329
+ if (u.pathname === "/installation/repositories") {
330
+ return Response.json({
331
+ repositories: bearer.endsWith("tok3")
332
+ ? [repo("gamma", "tenant", false, 30)]
333
+ : [repo("acme", "shared", false, 10)],
334
+ });
335
+ }
336
+ return Response.json({ message: "Not Found" }, { status: 404 });
337
+ }) as unknown as typeof fetch;
338
+
339
+ const lookup = (appId: string): Row => ({ appId, enterpriseSlug: null });
340
+
341
+ it("lists through the account's own lookup App", async () => {
342
+ const svc = createGithubLookupService({
343
+ appService: scopedApps({
344
+ ":lookup": lookup("555"),
345
+ "acct-1:lookup": lookup("777"),
346
+ }),
347
+ fetchFn: byApp,
348
+ now: () => NOW,
349
+ });
350
+ expect((await svc.listRepositories("acct-1")).repos).toEqual([
351
+ { fullName: "gamma/tenant", archived: false },
352
+ ]);
353
+ });
354
+
355
+ it("falls back to the deployment-wide pair when the account has none", async () => {
356
+ const svc = createGithubLookupService({
357
+ appService: scopedApps({ ":lookup": lookup("555") }),
358
+ fetchFn: byApp,
359
+ now: () => NOW,
360
+ });
361
+ expect((await svc.listRepositories("acct-2")).repos).toEqual([
362
+ { fullName: "acme/shared", archived: false },
363
+ ]);
364
+ });
365
+
366
+ it("with no argument uses the deployment-wide pair, ignoring account rows", async () => {
367
+ const svc = createGithubLookupService({
368
+ appService: scopedApps({
369
+ ":lookup": lookup("555"),
370
+ "acct-1:lookup": lookup("777"),
371
+ }),
372
+ fetchFn: byApp,
373
+ now: () => NOW,
374
+ });
375
+ expect((await svc.listRepositories()).repos).toEqual([
376
+ { fullName: "acme/shared", archived: false },
377
+ ]);
378
+ });
379
+
380
+ it("is unconfigured when neither the account nor the deployment has a lookup App", async () => {
381
+ const svc = createGithubLookupService({
382
+ appService: scopedApps({}),
383
+ fetchFn: byApp,
384
+ now: () => NOW,
385
+ });
386
+ expect(await svc.listRepositories("acct-1")).toEqual({
387
+ configured: false,
388
+ repos: [],
389
+ });
390
+ });
178
391
  });
@@ -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
@@ -24,7 +25,10 @@ export interface GithubLookupRepositories {
24
25
 
25
26
  /** The extension-facing lister (optional member of AdminPlatformServices). */
26
27
  export interface GithubLookupService {
27
- listRepositories(): Promise<GithubLookupRepositories>;
28
+ /** Lists the account's App pair, else the deployment-wide pair (no argument: deployment-wide). */
29
+ listRepositories(
30
+ accountId?: string | null,
31
+ ): Promise<GithubLookupRepositories>;
28
32
  }
29
33
 
30
34
  /** The installer-side client surface the lister needs. */
@@ -35,14 +39,19 @@ export interface LookupInstallerPort {
35
39
  /** The lookup-side client surface the lister needs. */
36
40
  export interface LookupRepositoriesPort {
37
41
  listInstallationRepositories(org: string): Promise<LookupRepository[]>;
42
+ /** Required only when no installer is configured (the App lists its own orgs). */
43
+ listInstallations?(): Promise<{ login: string }[]>;
38
44
  }
39
45
 
40
46
  export async function listLookupRepositories(
41
- installer: LookupInstallerPort,
47
+ installer: LookupInstallerPort | undefined,
42
48
  lookup: LookupRepositoriesPort,
43
49
  ): Promise<GithubLookupRepository[]> {
44
50
  const repos = new Map<string, GithubLookupRepository>();
45
- for (const { login } of await installer.listInstallableOrganizations()) {
51
+ const orgs = installer
52
+ ? await installer.listInstallableOrganizations()
53
+ : await listOwnInstallations(lookup);
54
+ for (const { login } of orgs) {
46
55
  let rows: LookupRepository[];
47
56
  try {
48
57
  rows = await lookup.listInstallationRepositories(login);
@@ -58,3 +67,12 @@ export async function listLookupRepositories(
58
67
  }
59
68
  return [...repos.values()];
60
69
  }
70
+
71
+ async function listOwnInstallations(
72
+ lookup: LookupRepositoriesPort,
73
+ ): Promise<{ login: string }[]> {
74
+ if (!lookup.listInstallations) {
75
+ throw new Error("lookup client cannot list its own installations");
76
+ }
77
+ return lookup.listInstallations();
78
+ }
@@ -382,7 +382,7 @@ export class GithubSyncService {
382
382
  if (!username) {
383
383
  return skippedPlan(accessSkipReason(username, null) as string);
384
384
  }
385
- const preview = await this.deps.access(username);
385
+ const preview = await this.deps.access(username, agent.accountId);
386
386
  if (preview.status === "unconfigured") {
387
387
  return skippedPlan("GitHub Apps are not configured");
388
388
  }
@@ -31,6 +31,8 @@ export const MAX_UNCONFIRMED_REMOVALS = 10;
31
31
 
32
32
  export interface InstallationAgentRecord {
33
33
  id: string;
34
+ /** The agent's account; selects that account's GitHub App pair (null/absent: deployment-wide). */
35
+ accountId?: string | null;
34
36
  repos: string[];
35
37
  githubUsername: string | null;
36
38
  /** The agent's own GitHub App id (its stored GH_APP_ID); undefined when unset or not numeric. */
@@ -48,6 +50,8 @@ export interface InstallationAgentRecord {
48
50
  export interface AppIdentity {
49
51
  clientId?: string;
50
52
  slug?: string;
53
+ /** Account whose installer App makes the calls (null/absent: deployment-wide pair). */
54
+ accountId?: string | null;
51
55
  }
52
56
 
53
57
  export interface InstallationAgentsPort {
@@ -139,7 +143,7 @@ export class PrismaInstallationAgents implements InstallationAgentsPort {
139
143
  async get(agentId: string): Promise<InstallationAgentRecord | null> {
140
144
  const agent = await this.prisma.agent.findUnique({
141
145
  where: { id: agentId },
142
- select: { id: true, repos: true, githubUsername: true },
146
+ select: { id: true, repos: true, githubUsername: true, accountId: true },
143
147
  });
144
148
  if (!agent) return null;
145
149
  const env = (await this.env.getConfigBundle(agentId))?.env;
@@ -165,7 +169,10 @@ export class PrismaInstallationAgents implements InstallationAgentsPort {
165
169
 
166
170
  /** The slice of GithubEnterpriseClient the installation port needs. */
167
171
  export interface InstallationRepoClient {
168
- listOrgInstallations(org: string): Promise<
172
+ listOrgInstallations(
173
+ org: string,
174
+ accountId?: string | null,
175
+ ): Promise<
169
176
  {
170
177
  id: number;
171
178
  /** Absent on the enterprise-organization-installation schema. */
@@ -177,16 +184,19 @@ export interface InstallationRepoClient {
177
184
  listInstallationRepositories(
178
185
  org: string,
179
186
  installationId: number,
187
+ accountId?: string | null,
180
188
  ): Promise<{ fullName: string }[]>;
181
189
  addRepositories(
182
190
  org: string,
183
191
  installationId: number,
184
192
  names: readonly string[],
193
+ accountId?: string | null,
185
194
  ): Promise<unknown>;
186
195
  removeRepositories(
187
196
  org: string,
188
197
  installationId: number,
189
198
  names: readonly string[],
199
+ accountId?: string | null,
190
200
  ): Promise<unknown>;
191
201
  }
192
202
 
@@ -239,7 +249,7 @@ export function createInstallationPort(
239
249
  identity?: AppIdentity,
240
250
  ): Promise<number> => {
241
251
  const found = matchInstallation(
242
- await client.listOrgInstallations(org),
252
+ await client.listOrgInstallations(org, identity?.accountId),
243
253
  appId,
244
254
  identity,
245
255
  );
@@ -255,6 +265,7 @@ export function createInstallationPort(
255
265
  const repos = await client.listInstallationRepositories(
256
266
  org,
257
267
  await installationId(appId, org, identity),
268
+ identity?.accountId,
258
269
  );
259
270
  out[org] = repos.map((r) => r.fullName);
260
271
  }
@@ -265,6 +276,7 @@ export function createInstallationPort(
265
276
  org,
266
277
  await installationId(appId, org, identity),
267
278
  repos.map(baseName),
279
+ identity?.accountId,
268
280
  );
269
281
  },
270
282
  async removeRepos(appId, org, repos, identity) {
@@ -272,6 +284,7 @@ export function createInstallationPort(
272
284
  org,
273
285
  await installationId(appId, org, identity),
274
286
  repos.map(baseName),
287
+ identity?.accountId,
275
288
  );
276
289
  },
277
290
  };
@@ -64,8 +64,10 @@ export interface InstallationReposDeps {
64
64
  export function identityOf(agent: {
65
65
  clientId?: string;
66
66
  slug?: string;
67
+ accountId?: string | null;
67
68
  }): AppIdentity {
68
69
  return {
70
+ ...(agent.accountId ? { accountId: agent.accountId } : {}),
69
71
  ...(agent.clientId ? { clientId: agent.clientId } : {}),
70
72
  ...(agent.slug ? { slug: agent.slug } : {}),
71
73
  };
@@ -130,7 +132,7 @@ export class InstallationReposService {
130
132
  let access: UserRepoAccessResult | undefined;
131
133
  if (username) {
132
134
  try {
133
- const preview = await this.deps.access(username);
135
+ const preview = await this.deps.access(username, agent.accountId);
134
136
  if (preview.status === "ok") access = preview.access;
135
137
  } catch {
136
138
  // The hint is advisory: a failed lookup omits it.
@@ -149,7 +151,9 @@ export class InstallationReposService {
149
151
 
150
152
  /** The slice of the installer client the installation-repos port needs. */
151
153
  export interface InstalledOrgsClient {
152
- listInstallableOrganizations(): Promise<{ login: string }[]>;
154
+ listInstallableOrganizations(
155
+ accountId?: string | null,
156
+ ): Promise<{ login: string }[]>;
153
157
  }
154
158
 
155
159
  /**
@@ -164,7 +168,9 @@ export function createInstallationReposPort(
164
168
  ...base,
165
169
  async listInstalled(appId, identity) {
166
170
  const out: { org: string; repos: string[] }[] = [];
167
- for (const { login } of await client.listInstallableOrganizations()) {
171
+ for (const { login } of await client.listInstallableOrganizations(
172
+ identity?.accountId,
173
+ )) {
168
174
  try {
169
175
  const repos = await base.listRepos(appId, [login], identity);
170
176
  out.push({ org: login, repos: repos[login] ?? [] });