@app-vitals/agent-admin 1.60.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.
- package/openapi.platform.json +75 -0
- package/package.json +2 -2
- package/src/admin-ui-github-enterprise-pages.ts +88 -16
- package/src/admin-ui-github-enterprise.smoke.test.ts +135 -12
- package/src/admin-ui-github-enterprise.ts +59 -18
- package/src/admin-ui.ts +2 -1
- package/src/agents-api.ts +4 -1
- package/src/github-access-preview.ts +2 -0
- package/src/github-enterprise-api.smoke.test.ts +171 -27
- package/src/github-enterprise-api.ts +63 -25
- package/src/github-installation-wiring.ts +15 -13
- package/src/github-lookup-repositories.integration.test.ts +136 -6
- package/src/github-lookup-repositories.ts +4 -1
- package/src/github-sync.ts +1 -1
- package/src/installation-ports.ts +16 -3
- package/src/installation-repos-service.ts +9 -3
|
@@ -111,8 +111,23 @@ const OrgParamSchema = z.object({
|
|
|
111
111
|
.openapi({ param: { name: "org", in: "path" }, example: "example-org" }),
|
|
112
112
|
});
|
|
113
113
|
|
|
114
|
+
const AccountQuerySchema = z.object({
|
|
115
|
+
accountId: z
|
|
116
|
+
.string()
|
|
117
|
+
.min(1)
|
|
118
|
+
.optional()
|
|
119
|
+
.openapi({
|
|
120
|
+
param: { name: "accountId", in: "query" },
|
|
121
|
+
description:
|
|
122
|
+
"Account whose App pair to read/write. Omit for the deployment-wide pair.",
|
|
123
|
+
}),
|
|
124
|
+
});
|
|
125
|
+
|
|
114
126
|
const AppMetadataSchema = z
|
|
115
127
|
.object({
|
|
128
|
+
accountId: z.string().nullable().optional().openapi({
|
|
129
|
+
description: "Null for the deployment-wide App.",
|
|
130
|
+
}),
|
|
116
131
|
role: z.string().openapi({ example: "installer" }),
|
|
117
132
|
appId: z.string().openapi({ example: "12345" }),
|
|
118
133
|
slug: z.string().openapi({ example: "example-installer" }),
|
|
@@ -168,7 +183,7 @@ const ValidateResultSchema = z
|
|
|
168
183
|
})
|
|
169
184
|
.openapi("GithubEnterpriseValidateResult");
|
|
170
185
|
|
|
171
|
-
const OrgsQuerySchema =
|
|
186
|
+
const OrgsQuerySchema = AccountQuerySchema.extend({
|
|
172
187
|
agentId: z
|
|
173
188
|
.string()
|
|
174
189
|
.min(1)
|
|
@@ -253,6 +268,7 @@ const statusRoute = createRoute({
|
|
|
253
268
|
summary: "GitHub Enterprise App status",
|
|
254
269
|
description:
|
|
255
270
|
"Admin-only. Metadata and key-set flag per role; never the key. Returns configured=false with null roles when nothing is set.",
|
|
271
|
+
request: { query: AccountQuerySchema },
|
|
256
272
|
responses: {
|
|
257
273
|
200: {
|
|
258
274
|
description: "Status",
|
|
@@ -271,6 +287,7 @@ const putRoute = createRoute({
|
|
|
271
287
|
"Admin-only. Stores the App id, slug, enterprise slug (installer) and the encrypted private key, and resets validation state. 422 when SHIPWRIGHT_ENCRYPTION_KEY is not configured.",
|
|
272
288
|
request: {
|
|
273
289
|
params: RoleParamSchema,
|
|
290
|
+
query: AccountQuerySchema,
|
|
274
291
|
body: { content: { "application/json": { schema: PutAppBodySchema } } },
|
|
275
292
|
},
|
|
276
293
|
responses: {
|
|
@@ -293,7 +310,7 @@ const deleteRoute = createRoute({
|
|
|
293
310
|
tags,
|
|
294
311
|
summary: "Remove the App for a role",
|
|
295
312
|
description: "Admin-only. No-op when the role is not configured.",
|
|
296
|
-
request: { params: RoleParamSchema },
|
|
313
|
+
request: { params: RoleParamSchema, query: AccountQuerySchema },
|
|
297
314
|
responses: {
|
|
298
315
|
204: { description: "Removed" },
|
|
299
316
|
...errorResponses,
|
|
@@ -307,7 +324,7 @@ const validateRoute = createRoute({
|
|
|
307
324
|
summary: "Validate the App for a role",
|
|
308
325
|
description:
|
|
309
326
|
"Admin-only. Authenticates as the App and checks the required permission (installer: enterprise_organization_installations write; lookup: metadata read). The outcome is persisted and returned; GitHub failures are reported as status=error with a generic message.",
|
|
310
|
-
request: { params: RoleParamSchema },
|
|
327
|
+
request: { params: RoleParamSchema, query: AccountQuerySchema },
|
|
311
328
|
responses: {
|
|
312
329
|
200: {
|
|
313
330
|
description: "Validation outcome",
|
|
@@ -345,6 +362,7 @@ const installRoute = createRoute({
|
|
|
345
362
|
"Admin-only. Uses the agent's stored GH_APP_CLIENT_ID when set; otherwise resolves the stored GH_APP_SLUG and looks the App up through the public GET /apps/{slug} to get its client id. Installs it on the org with the installer App. 422 for an agent with neither a stored client id nor a slug, and for a private App (not visible to the public lookup) with no stored client id; 409 when the installer App is not configured.",
|
|
346
363
|
request: {
|
|
347
364
|
params: OrgParamSchema,
|
|
365
|
+
query: AccountQuerySchema,
|
|
348
366
|
body: { content: { "application/json": { schema: InstallBodySchema } } },
|
|
349
367
|
},
|
|
350
368
|
responses: {
|
|
@@ -417,6 +435,7 @@ export function mapEnterpriseFailure(kind: GithubEnterpriseErrorKind): Mapped {
|
|
|
417
435
|
|
|
418
436
|
function serializeApp(m: GitHubEnterpriseAppMetadata) {
|
|
419
437
|
return {
|
|
438
|
+
accountId: m.accountId ?? null,
|
|
420
439
|
role: m.role,
|
|
421
440
|
appId: m.appId,
|
|
422
441
|
slug: m.slug,
|
|
@@ -444,17 +463,17 @@ export function createGithubAccessPreview(
|
|
|
444
463
|
"appService" | "fetchFn" | "apiBase" | "now"
|
|
445
464
|
>,
|
|
446
465
|
): GithubAccessPreviewFn {
|
|
447
|
-
return async (username) => {
|
|
466
|
+
return async (username, accountId) => {
|
|
448
467
|
const [installer, lookup] = await Promise.all([
|
|
449
|
-
deps.appService.get("installer"),
|
|
450
|
-
deps.appService.get("lookup"),
|
|
468
|
+
deps.appService.get("installer", accountId),
|
|
469
|
+
deps.appService.get("lookup", accountId),
|
|
451
470
|
]);
|
|
452
471
|
if (!installer || !lookup || !installer.enterpriseSlug) {
|
|
453
472
|
return { status: "unconfigured" };
|
|
454
473
|
}
|
|
455
474
|
const [installerKey, lookupKey] = await Promise.all([
|
|
456
|
-
deps.appService.getPrivateKey("installer"),
|
|
457
|
-
deps.appService.getPrivateKey("lookup"),
|
|
475
|
+
deps.appService.getPrivateKey("installer", accountId),
|
|
476
|
+
deps.appService.getPrivateKey("lookup", accountId),
|
|
458
477
|
]);
|
|
459
478
|
if (!installerKey || !lookupKey) return { status: "unconfigured" };
|
|
460
479
|
|
|
@@ -504,15 +523,18 @@ export function createGithubLookupService(
|
|
|
504
523
|
},
|
|
505
524
|
): GithubLookupService {
|
|
506
525
|
return {
|
|
507
|
-
async listRepositories() {
|
|
526
|
+
async listRepositories(accountId) {
|
|
508
527
|
let clients = deps.clients;
|
|
509
528
|
if (!clients) {
|
|
510
529
|
const [installer, lookup] = await Promise.all([
|
|
511
|
-
deps.appService.get("installer"),
|
|
512
|
-
deps.appService.get("lookup"),
|
|
530
|
+
deps.appService.get("installer", accountId),
|
|
531
|
+
deps.appService.get("lookup", accountId),
|
|
513
532
|
]);
|
|
514
533
|
if (!lookup) return { configured: false, repos: [] };
|
|
515
|
-
const lookupKey = await deps.appService.getPrivateKey(
|
|
534
|
+
const lookupKey = await deps.appService.getPrivateKey(
|
|
535
|
+
"lookup",
|
|
536
|
+
accountId,
|
|
537
|
+
);
|
|
516
538
|
if (!lookupKey) return { configured: false, repos: [] };
|
|
517
539
|
const common = {
|
|
518
540
|
apiBase: deps.apiBase,
|
|
@@ -522,7 +544,7 @@ export function createGithubLookupService(
|
|
|
522
544
|
// An installer with an enterprise slug supplies the org list; without
|
|
523
545
|
// one the lookup App lists the orgs it is installed on.
|
|
524
546
|
const installerKey = installer?.enterpriseSlug
|
|
525
|
-
? await deps.appService.getPrivateKey("installer")
|
|
547
|
+
? await deps.appService.getPrivateKey("installer", accountId)
|
|
526
548
|
: null;
|
|
527
549
|
if (installer?.enterpriseSlug && !installerKey) {
|
|
528
550
|
return { configured: false, repos: [] };
|
|
@@ -613,9 +635,11 @@ export function createGithubEnterpriseApp(
|
|
|
613
635
|
|
|
614
636
|
const clientFor = async (
|
|
615
637
|
meta: GitHubEnterpriseAppMetadata,
|
|
638
|
+
accountId: string | undefined,
|
|
616
639
|
): Promise<GithubEnterpriseClient> => {
|
|
617
640
|
const privateKey = await appService.getPrivateKey(
|
|
618
641
|
meta.role as GitHubEnterpriseAppRole,
|
|
642
|
+
accountId,
|
|
619
643
|
);
|
|
620
644
|
if (!privateKey || !meta.enterpriseSlug) {
|
|
621
645
|
throw new UnprocessableEntityError(
|
|
@@ -682,7 +706,8 @@ export function createGithubEnterpriseApp(
|
|
|
682
706
|
|
|
683
707
|
app.openapi(statusRoute, async (c) => {
|
|
684
708
|
requireAdmin(c);
|
|
685
|
-
const
|
|
709
|
+
const { accountId } = c.req.valid("query");
|
|
710
|
+
const rows = await appService.list(accountId);
|
|
686
711
|
const byRole = (role: string) => {
|
|
687
712
|
const r = rows.find((x) => x.role === role);
|
|
688
713
|
return r ? serializeApp(r) : null;
|
|
@@ -700,6 +725,7 @@ export function createGithubEnterpriseApp(
|
|
|
700
725
|
app.openapi(putRoute, async (c) => {
|
|
701
726
|
requireAdmin(c);
|
|
702
727
|
const { role } = c.req.valid("param");
|
|
728
|
+
const { accountId } = c.req.valid("query");
|
|
703
729
|
const body = c.req.valid("json");
|
|
704
730
|
if (role === "installer" && !body.enterpriseSlug) {
|
|
705
731
|
throw new UnprocessableEntityError(
|
|
@@ -715,6 +741,7 @@ export function createGithubEnterpriseApp(
|
|
|
715
741
|
);
|
|
716
742
|
}
|
|
717
743
|
const saved = await appService.save({
|
|
744
|
+
accountId,
|
|
718
745
|
role,
|
|
719
746
|
appId: body.appId,
|
|
720
747
|
slug: body.slug,
|
|
@@ -726,16 +753,20 @@ export function createGithubEnterpriseApp(
|
|
|
726
753
|
|
|
727
754
|
app.openapi(deleteRoute, async (c) => {
|
|
728
755
|
requireAdmin(c);
|
|
729
|
-
await appService.delete(
|
|
756
|
+
await appService.delete(
|
|
757
|
+
c.req.valid("param").role,
|
|
758
|
+
c.req.valid("query").accountId,
|
|
759
|
+
);
|
|
730
760
|
return c.body(null, 204);
|
|
731
761
|
});
|
|
732
762
|
|
|
733
763
|
app.openapi(validateRoute, async (c) => {
|
|
734
764
|
requireAdmin(c);
|
|
735
765
|
const { role } = c.req.valid("param");
|
|
736
|
-
const
|
|
766
|
+
const { accountId } = c.req.valid("query");
|
|
767
|
+
const meta = await appService.get(role, accountId);
|
|
737
768
|
if (!meta) throw new NotFoundError(`no '${role}' App is configured`);
|
|
738
|
-
const privateKey = await appService.getPrivateKey(role);
|
|
769
|
+
const privateKey = await appService.getPrivateKey(role, accountId);
|
|
739
770
|
if (!privateKey) throw new NotFoundError(`no '${role}' App is configured`);
|
|
740
771
|
|
|
741
772
|
const required = REQUIRED_PERMISSION[role];
|
|
@@ -792,8 +823,14 @@ export function createGithubEnterpriseApp(
|
|
|
792
823
|
}
|
|
793
824
|
}
|
|
794
825
|
|
|
795
|
-
|
|
796
|
-
|
|
826
|
+
// Record against the row that was validated (the deployment-wide row when
|
|
827
|
+
// the account has none of its own).
|
|
828
|
+
await appService.recordValidation(
|
|
829
|
+
role,
|
|
830
|
+
{ status, error },
|
|
831
|
+
meta.accountId ?? null,
|
|
832
|
+
);
|
|
833
|
+
const updated = await appService.get(role, accountId);
|
|
797
834
|
return c.json(
|
|
798
835
|
{
|
|
799
836
|
role,
|
|
@@ -811,8 +848,8 @@ export function createGithubEnterpriseApp(
|
|
|
811
848
|
|
|
812
849
|
app.openapi(orgsRoute, async (c) => {
|
|
813
850
|
requireAdmin(c);
|
|
814
|
-
const { agentId } = c.req.valid("query");
|
|
815
|
-
const installer = await appService.get("installer");
|
|
851
|
+
const { agentId, accountId } = c.req.valid("query");
|
|
852
|
+
const installer = await appService.get("installer", accountId);
|
|
816
853
|
if (!installer) return c.json({ configured: false, orgs: [] }, 200);
|
|
817
854
|
|
|
818
855
|
let agentAppSlug: string | null = null;
|
|
@@ -835,9 +872,9 @@ export function createGithubEnterpriseApp(
|
|
|
835
872
|
}
|
|
836
873
|
}
|
|
837
874
|
}
|
|
838
|
-
const lookup = await appService.get("lookup");
|
|
875
|
+
const lookup = await appService.get("lookup", accountId);
|
|
839
876
|
|
|
840
|
-
const client = await clientFor(installer);
|
|
877
|
+
const client = await clientFor(installer, accountId);
|
|
841
878
|
const orgs = await client.listInstallableOrganizations();
|
|
842
879
|
const same = (a: string, b: string) => a.toLowerCase() === b.toLowerCase();
|
|
843
880
|
const needInstallations = lookup !== null || agentAppSlug !== null;
|
|
@@ -872,8 +909,9 @@ export function createGithubEnterpriseApp(
|
|
|
872
909
|
requireAdmin(c);
|
|
873
910
|
const { org } = c.req.valid("param");
|
|
874
911
|
const body = c.req.valid("json");
|
|
912
|
+
const { accountId } = c.req.valid("query");
|
|
875
913
|
|
|
876
|
-
const installer = await appService.get("installer");
|
|
914
|
+
const installer = await appService.get("installer", accountId);
|
|
877
915
|
if (!installer) {
|
|
878
916
|
throw new ConflictError("the installer App is not configured");
|
|
879
917
|
}
|
|
@@ -902,7 +940,7 @@ export function createGithubEnterpriseApp(
|
|
|
902
940
|
}
|
|
903
941
|
}
|
|
904
942
|
|
|
905
|
-
const client = await clientFor(installer);
|
|
943
|
+
const client = await clientFor(installer, accountId);
|
|
906
944
|
const res = await client.installOnOrg(org, {
|
|
907
945
|
clientId,
|
|
908
946
|
repositorySelection: body.repositorySelection,
|
|
@@ -46,10 +46,12 @@ function lazyInstallerClient(
|
|
|
46
46
|
"appService" | "fetchFn" | "apiBase" | "now"
|
|
47
47
|
>,
|
|
48
48
|
): InstallationRepoClient & InstalledOrgsClient {
|
|
49
|
-
const client = async (
|
|
49
|
+
const client = async (
|
|
50
|
+
accountId?: string | null,
|
|
51
|
+
): Promise<GithubEnterpriseClient> => {
|
|
50
52
|
const [installer, privateKey] = await Promise.all([
|
|
51
|
-
deps.appService.get("installer"),
|
|
52
|
-
deps.appService.getPrivateKey("installer"),
|
|
53
|
+
deps.appService.get("installer", accountId),
|
|
54
|
+
deps.appService.getPrivateKey("installer", accountId),
|
|
53
55
|
]);
|
|
54
56
|
if (!installer?.enterpriseSlug || !privateKey) {
|
|
55
57
|
throw new Error("installer App is not configured");
|
|
@@ -64,16 +66,16 @@ function lazyInstallerClient(
|
|
|
64
66
|
});
|
|
65
67
|
};
|
|
66
68
|
return {
|
|
67
|
-
listInstallableOrganizations: async () =>
|
|
68
|
-
(await client()).listInstallableOrganizations(),
|
|
69
|
-
listOrgInstallations: async (org) =>
|
|
70
|
-
(await client()).listOrgInstallations(org),
|
|
71
|
-
listInstallationRepositories: async (org, id) =>
|
|
72
|
-
(await client()).listInstallationRepositories(org, id),
|
|
73
|
-
addRepositories: async (org, id, names) =>
|
|
74
|
-
(await client()).addRepositories(org, id, names),
|
|
75
|
-
removeRepositories: async (org, id, names) =>
|
|
76
|
-
(await client()).removeRepositories(org, id, names),
|
|
69
|
+
listInstallableOrganizations: async (accountId) =>
|
|
70
|
+
(await client(accountId)).listInstallableOrganizations(),
|
|
71
|
+
listOrgInstallations: async (org, accountId) =>
|
|
72
|
+
(await client(accountId)).listOrgInstallations(org),
|
|
73
|
+
listInstallationRepositories: async (org, id, accountId) =>
|
|
74
|
+
(await client(accountId)).listInstallationRepositories(org, id),
|
|
75
|
+
addRepositories: async (org, id, names, accountId) =>
|
|
76
|
+
(await client(accountId)).addRepositories(org, id, names),
|
|
77
|
+
removeRepositories: async (org, id, names, accountId) =>
|
|
78
|
+
(await client(accountId)).removeRepositories(org, id, names),
|
|
77
79
|
};
|
|
78
80
|
}
|
|
79
81
|
|
|
@@ -200,7 +200,9 @@ describe("githubLookup.listRepositories", () => {
|
|
|
200
200
|
fetchFn: replay((_u, id) =>
|
|
201
201
|
Response.json({
|
|
202
202
|
repositories:
|
|
203
|
-
id === 1
|
|
203
|
+
id === 1
|
|
204
|
+
? [repo("acme", "api", false, 10)]
|
|
205
|
+
: [repo("beta", "web", false, 20)],
|
|
204
206
|
}),
|
|
205
207
|
),
|
|
206
208
|
now: () => NOW,
|
|
@@ -233,29 +235,157 @@ describe("githubLookup.listRepositories", () => {
|
|
|
233
235
|
const svc = createGithubLookupService({
|
|
234
236
|
appService: stored(null),
|
|
235
237
|
fetchFn: (async () =>
|
|
236
|
-
Response.json(
|
|
238
|
+
Response.json(
|
|
239
|
+
{ message: "boom" },
|
|
240
|
+
{ status: 500 },
|
|
241
|
+
)) as unknown as typeof fetch,
|
|
237
242
|
now: () => NOW,
|
|
238
243
|
});
|
|
239
|
-
await expect(svc.listRepositories()).rejects.toBeInstanceOf(
|
|
244
|
+
await expect(svc.listRepositories()).rejects.toBeInstanceOf(
|
|
245
|
+
GithubEnterpriseError,
|
|
246
|
+
);
|
|
240
247
|
});
|
|
241
248
|
|
|
242
249
|
it("an installer with a slug but no key stays unconfigured", async () => {
|
|
243
250
|
const svc = createGithubLookupService({
|
|
244
251
|
appService: {
|
|
245
252
|
get: async (kind: string) =>
|
|
246
|
-
kind === "lookup"
|
|
253
|
+
kind === "lookup"
|
|
254
|
+
? lookupApp
|
|
255
|
+
: { appId: "1", enterpriseSlug: "ent" },
|
|
247
256
|
getPrivateKey: async (kind: string) =>
|
|
248
257
|
kind === "lookup" ? privateKey : null,
|
|
249
258
|
} as unknown as GitHubEnterpriseAppService,
|
|
250
259
|
});
|
|
251
|
-
expect(await svc.listRepositories()).toEqual({
|
|
260
|
+
expect(await svc.listRepositories()).toEqual({
|
|
261
|
+
configured: false,
|
|
262
|
+
repos: [],
|
|
263
|
+
});
|
|
252
264
|
});
|
|
253
265
|
|
|
254
266
|
it("with an installer configured the org list still comes from it", async () => {
|
|
255
|
-
const svc = service(
|
|
267
|
+
const svc = service(
|
|
268
|
+
["acme"],
|
|
269
|
+
replay(() => Response.json({ repositories: [repo("acme", "api")] })),
|
|
270
|
+
);
|
|
256
271
|
expect((await svc.listRepositories()).repos).toEqual([
|
|
257
272
|
{ fullName: "acme/api", archived: false },
|
|
258
273
|
]);
|
|
259
274
|
});
|
|
260
275
|
});
|
|
261
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
|
+
});
|
|
391
|
+
});
|
|
@@ -25,7 +25,10 @@ export interface GithubLookupRepositories {
|
|
|
25
25
|
|
|
26
26
|
/** The extension-facing lister (optional member of AdminPlatformServices). */
|
|
27
27
|
export interface GithubLookupService {
|
|
28
|
-
|
|
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>;
|
|
29
32
|
}
|
|
30
33
|
|
|
31
34
|
/** The installer-side client surface the lister needs. */
|
package/src/github-sync.ts
CHANGED
|
@@ -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(
|
|
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(
|
|
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] ?? [] });
|