@app-vitals/agent-admin 1.35.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.
- package/openapi.platform.json +2 -392
- package/package.json +2 -2
- package/src/admin-boot.ts +1 -12
- package/src/admin-extension.ts +3 -3
- package/src/admin-ui-github-identity.smoke.test.ts +0 -114
- package/src/admin-ui-installation-repos-pages.ts +2 -2
- package/src/admin-ui-installation-repos.smoke.test.ts +0 -1
- package/src/admin-ui.ts +0 -82
- package/src/agents-api.smoke.test.ts +2 -352
- package/src/agents-api.ts +5 -269
- package/src/github-enterprise-isolation.unit.test.ts +4 -4
- package/src/{github-reconcile-wiring.ts → github-installation-wiring.ts} +18 -38
- package/src/github-installation-wiring.unit.test.ts +111 -0
- package/src/github-installation.fixtures.ts +76 -0
- package/src/installation-ports.integration.test.ts +89 -0
- package/src/installation-ports.ts +207 -0
- package/src/installation-ports.unit.test.ts +51 -0
- package/src/installation-repos-service.ts +12 -12
- package/src/installation-repos-service.unit.test.ts +3 -3
- package/src/lost-access-removal.ts +9 -9
- package/src/lost-access-removal.unit.test.ts +7 -7
- package/src/reconcile-open-work.ts +5 -7
- package/src/repo-access-rules.ts +58 -0
- package/src/repo-access-rules.unit.test.ts +98 -0
- package/src/admin-ui-github-reconcile-pages.ts +0 -208
- package/src/admin-ui-github-reconcile-pages.unit.test.ts +0 -226
- package/src/github-reconcile-routes.integration.test.ts +0 -206
- package/src/github-reconcile-wiring.unit.test.ts +0 -170
- package/src/reconcile-apply-service.integration.test.ts +0 -271
- package/src/reconcile-apply-service.ts +0 -439
- package/src/reconcile-apply-service.unit.test.ts +0 -279
- package/src/reconcile-planner.ts +0 -264
- package/src/reconcile-planner.unit.test.ts +0 -274
package/src/agents-api.ts
CHANGED
|
@@ -167,7 +167,6 @@ import {
|
|
|
167
167
|
} from "./openapi-schemas.ts";
|
|
168
168
|
import type { InstallationReposService } from "./installation-repos-service.ts";
|
|
169
169
|
import type { LostAccessRemovalService } from "./lost-access-removal.ts";
|
|
170
|
-
import type { ReconcileApplyService } from "./reconcile-apply-service.ts";
|
|
171
170
|
import type { OpenWorkPort } from "./reconcile-open-work.ts";
|
|
172
171
|
import {
|
|
173
172
|
blockRepoRemovalWork,
|
|
@@ -270,12 +269,6 @@ export interface AdminDeps {
|
|
|
270
269
|
* means the enterprise Apps are not wired, reported as `unconfigured`.
|
|
271
270
|
*/
|
|
272
271
|
githubAccessPreview?: GithubAccessPreviewFn;
|
|
273
|
-
/**
|
|
274
|
-
* GHE-3.4: backs POST /agents/:id/github-reconcile/{plan,apply}. Optional
|
|
275
|
-
* (additive); absent means the enterprise Apps are not wired, reported as
|
|
276
|
-
* `skipped`.
|
|
277
|
-
*/
|
|
278
|
-
githubReconcile?: Pick<ReconcileApplyService, "planWithOpenWork" | "apply">;
|
|
279
272
|
/**
|
|
280
273
|
* IRM-1.7: optional host-supplied open-work port. When an agent's own repos
|
|
281
274
|
* lose a repo (DELETE /agents/{id}/repos/{owner}/{name} or a PATCH that
|
|
@@ -631,87 +624,12 @@ const GithubAccessPreviewSchema = z
|
|
|
631
624
|
})
|
|
632
625
|
.openapi("GithubAccessPreview");
|
|
633
626
|
|
|
634
|
-
const
|
|
627
|
+
const GITHUB_APPS_UNCONFIGURED = "GitHub Apps are not configured";
|
|
635
628
|
|
|
636
629
|
const RepoFullNameSchema = z
|
|
637
630
|
.string()
|
|
638
631
|
.regex(/^[^/\s]+\/[^/\s]+$/, "must be owner/repo");
|
|
639
632
|
|
|
640
|
-
const GithubReconcilePlanBodySchema = z
|
|
641
|
-
.object({
|
|
642
|
-
addRepos: z.array(RepoFullNameSchema).optional().openapi({
|
|
643
|
-
description:
|
|
644
|
-
"Repos (owner/repo) to explicitly add to the agent's config and installation.",
|
|
645
|
-
}),
|
|
646
|
-
})
|
|
647
|
-
.openapi("GithubReconcilePlanBody");
|
|
648
|
-
|
|
649
|
-
const GithubReconcileApplyBodySchema = z
|
|
650
|
-
.object({
|
|
651
|
-
planHash: z.string().min(1).openapi({
|
|
652
|
-
description: "planHash of the plan the admin reviewed.",
|
|
653
|
-
}),
|
|
654
|
-
addRepos: z.array(RepoFullNameSchema).optional().openapi({
|
|
655
|
-
description: "Must match the addRepos used when the plan was made.",
|
|
656
|
-
}),
|
|
657
|
-
confirmRemovals: z.boolean().optional().openapi({
|
|
658
|
-
description: "Required when the plan removes more than 10 repos.",
|
|
659
|
-
}),
|
|
660
|
-
})
|
|
661
|
-
.openapi("GithubReconcileApplyBody");
|
|
662
|
-
|
|
663
|
-
const GithubReconcileDiffSchema = z.object({
|
|
664
|
-
installationAdds: z.array(z.string()),
|
|
665
|
-
installationRemoves: z.array(z.string()),
|
|
666
|
-
configAdds: z.array(z.string()),
|
|
667
|
-
configRemoves: z.array(z.string()),
|
|
668
|
-
});
|
|
669
|
-
|
|
670
|
-
const GithubReconcilePlanResultSchema = z
|
|
671
|
-
.object({
|
|
672
|
-
status: z.enum(["planned", "skipped"]).openapi({
|
|
673
|
-
description:
|
|
674
|
-
"`skipped` (not an error) when no GitHub username is bound or the Apps are not configured; `reason` says why.",
|
|
675
|
-
}),
|
|
676
|
-
reason: z.string().optional(),
|
|
677
|
-
planHash: z.string().optional(),
|
|
678
|
-
empty: z.boolean().optional(),
|
|
679
|
-
diff: GithubReconcileDiffSchema.optional(),
|
|
680
|
-
skippedOrgs: z
|
|
681
|
-
.array(z.object({ org: z.string(), reason: z.string() }))
|
|
682
|
-
.optional(),
|
|
683
|
-
rejectedAdds: z
|
|
684
|
-
.array(z.object({ repo: z.string(), reason: z.string() }))
|
|
685
|
-
.optional(),
|
|
686
|
-
warnings: z.array(z.string()).optional(),
|
|
687
|
-
affectedOpenWork: z
|
|
688
|
-
.array(
|
|
689
|
-
z.object({
|
|
690
|
-
id: z.string(),
|
|
691
|
-
repo: z.string(),
|
|
692
|
-
title: z.string().optional(),
|
|
693
|
-
}),
|
|
694
|
-
)
|
|
695
|
-
.optional()
|
|
696
|
-
.openapi({
|
|
697
|
-
description:
|
|
698
|
-
"Open work the removals would block; empty when no open-work port is wired.",
|
|
699
|
-
}),
|
|
700
|
-
})
|
|
701
|
-
.openapi("GithubReconcilePlanResult");
|
|
702
|
-
|
|
703
|
-
const GithubReconcileApplyResultSchema = z
|
|
704
|
-
.object({
|
|
705
|
-
status: z.enum(["applied", "failed", "noop", "skipped"]),
|
|
706
|
-
reason: z.string().optional(),
|
|
707
|
-
auditId: z.string().optional(),
|
|
708
|
-
planHash: z.string().optional(),
|
|
709
|
-
diff: GithubReconcileDiffSchema.optional(),
|
|
710
|
-
warnings: z.array(z.string()).optional(),
|
|
711
|
-
error: z.string().optional(),
|
|
712
|
-
})
|
|
713
|
-
.openapi("GithubReconcileApplyResult");
|
|
714
|
-
|
|
715
633
|
const InstallationReposBodySchema = z
|
|
716
634
|
.object({
|
|
717
635
|
repos: z.array(RepoFullNameSchema).min(1).max(100).openapi({
|
|
@@ -1740,64 +1658,6 @@ const getGithubAccessRoute = createRoute({
|
|
|
1740
1658
|
},
|
|
1741
1659
|
});
|
|
1742
1660
|
|
|
1743
|
-
const githubReconcilePlanRoute = createRoute({
|
|
1744
|
-
method: "post",
|
|
1745
|
-
path: "/agents/{id}/github-reconcile/plan",
|
|
1746
|
-
summary: "Plan a GitHub repo reconcile for the agent",
|
|
1747
|
-
description:
|
|
1748
|
-
"Platform-admin only (session members, ops keys and agent keys get 403). Read-only: nothing is persisted or changed. Returns the diff, warnings, `planHash` and the open work the removals would affect. 200 with `status: skipped` and a `reason` when no githubUsername is bound or the enterprise Apps are not configured. 404 for an unknown agent.",
|
|
1749
|
-
request: {
|
|
1750
|
-
params: AgentIdParamSchema,
|
|
1751
|
-
body: {
|
|
1752
|
-
required: false,
|
|
1753
|
-
content: {
|
|
1754
|
-
"application/json": { schema: GithubReconcilePlanBodySchema },
|
|
1755
|
-
},
|
|
1756
|
-
},
|
|
1757
|
-
},
|
|
1758
|
-
responses: {
|
|
1759
|
-
200: {
|
|
1760
|
-
description: "Reconcile plan",
|
|
1761
|
-
content: {
|
|
1762
|
-
"application/json": { schema: GithubReconcilePlanResultSchema },
|
|
1763
|
-
},
|
|
1764
|
-
},
|
|
1765
|
-
401: { description: "Unauthorized", ...jsonError },
|
|
1766
|
-
403: { description: "Platform admin required", ...jsonError },
|
|
1767
|
-
404: { description: "Agent not found", ...jsonError },
|
|
1768
|
-
},
|
|
1769
|
-
});
|
|
1770
|
-
|
|
1771
|
-
const githubReconcileApplyRoute = createRoute({
|
|
1772
|
-
method: "post",
|
|
1773
|
-
path: "/agents/{id}/github-reconcile/apply",
|
|
1774
|
-
summary: "Apply a reviewed GitHub repo reconcile plan",
|
|
1775
|
-
description:
|
|
1776
|
-
"Platform-admin only (session members, ops keys and agent keys get 403). Re-plans server-side and applies only when the fresh plan still matches `planHash` (409 otherwise). More than 10 removals need `confirmRemovals: true` (422 otherwise). Returns the audit id and result. 200 with `status: skipped` (unbound user or unconfigured Apps) or `status: noop` (nothing to change) is not an error. Open work on removed repos is blocked through the optional open-work port; a failure there is a warning.",
|
|
1777
|
-
request: {
|
|
1778
|
-
params: AgentIdParamSchema,
|
|
1779
|
-
body: {
|
|
1780
|
-
required: true,
|
|
1781
|
-
content: {
|
|
1782
|
-
"application/json": { schema: GithubReconcileApplyBodySchema },
|
|
1783
|
-
},
|
|
1784
|
-
},
|
|
1785
|
-
},
|
|
1786
|
-
responses: {
|
|
1787
|
-
200: {
|
|
1788
|
-
description: "Apply result",
|
|
1789
|
-
content: {
|
|
1790
|
-
"application/json": { schema: GithubReconcileApplyResultSchema },
|
|
1791
|
-
},
|
|
1792
|
-
},
|
|
1793
|
-
401: { description: "Unauthorized", ...jsonError },
|
|
1794
|
-
403: { description: "Platform admin required", ...jsonError },
|
|
1795
|
-
404: { description: "Agent not found", ...jsonError },
|
|
1796
|
-
409: { description: "Plan hash is stale; re-plan", ...jsonError },
|
|
1797
|
-
422: { description: "Removal cap needs confirmRemovals", ...jsonError },
|
|
1798
|
-
},
|
|
1799
|
-
});
|
|
1800
|
-
|
|
1801
1661
|
const installationReposErrors = {
|
|
1802
1662
|
401: { description: "Unauthorized", ...jsonError },
|
|
1803
1663
|
403: { description: "Platform admin required", ...jsonError },
|
|
@@ -1833,7 +1693,7 @@ const addInstallationReposRoute = createRoute({
|
|
|
1833
1693
|
path: "/agents/{id}/github-installation/repos",
|
|
1834
1694
|
summary: "Add repos to the agent App's installation",
|
|
1835
1695
|
description:
|
|
1836
|
-
"Platform-admin only. Grants the agent's own GitHub App installation access to the named repos
|
|
1696
|
+
"Platform-admin only. Grants the agent's own GitHub App installation access to the named repos; the agent's configured repos are not changed. Writes one audit entry (GitHubReconcileAudit, planHash `direct:add`) recording the caller and repos. 404 unknown agent; 422 no GH_APP_ID; 409 when the App is not installed on a repo's org (nothing is changed or audited); 502 when GitHub refuses (a failed audit entry is written).",
|
|
1837
1697
|
request: {
|
|
1838
1698
|
params: AgentIdParamSchema,
|
|
1839
1699
|
body: {
|
|
@@ -1859,7 +1719,7 @@ const removeInstallationReposRoute = createRoute({
|
|
|
1859
1719
|
path: "/agents/{id}/github-installation/repos/remove",
|
|
1860
1720
|
summary: "Remove repos from the agent App's installation",
|
|
1861
1721
|
description:
|
|
1862
|
-
"Platform-admin only. Revokes the agent's own GitHub App installation access to the named repos
|
|
1722
|
+
"Platform-admin only. Revokes the agent's own GitHub App installation access to the named repos; the agent's configured repos are not changed. Writes one audit entry (GitHubReconcileAudit, planHash `direct:remove`) recording the caller and repos. Same errors as the add route.",
|
|
1863
1723
|
request: {
|
|
1864
1724
|
params: AgentIdParamSchema,
|
|
1865
1725
|
body: {
|
|
@@ -3428,130 +3288,6 @@ export function createAdminApp(deps: AdminDeps): OpenAPIHono<AdminAuthEnv> {
|
|
|
3428
3288
|
return c.json(preview, 200);
|
|
3429
3289
|
});
|
|
3430
3290
|
|
|
3431
|
-
// POST /agents/:id/github-reconcile/{plan,apply} — GHE-3.4. Platform admin
|
|
3432
|
-
// only. Responses carry repo names and ids, never tokens; logs carry ids only.
|
|
3433
|
-
app.openapi(githubReconcilePlanRoute, async (c) => {
|
|
3434
|
-
if (c.get("isAdmin") !== true) {
|
|
3435
|
-
throw new ForbiddenError(
|
|
3436
|
-
"Admin access required to plan a GitHub reconcile",
|
|
3437
|
-
);
|
|
3438
|
-
}
|
|
3439
|
-
const { id: agentId } = c.req.valid("param");
|
|
3440
|
-
const body = c.req.valid("json");
|
|
3441
|
-
const reconcile = deps.githubReconcile;
|
|
3442
|
-
if (!reconcile) {
|
|
3443
|
-
return c.json(
|
|
3444
|
-
{ status: "skipped" as const, reason: GITHUB_RECONCILE_UNCONFIGURED },
|
|
3445
|
-
200,
|
|
3446
|
-
);
|
|
3447
|
-
}
|
|
3448
|
-
let result: Awaited<ReturnType<typeof reconcile.planWithOpenWork>>;
|
|
3449
|
-
try {
|
|
3450
|
-
result = await reconcile.planWithOpenWork(agentId, body?.addRepos);
|
|
3451
|
-
} catch (err) {
|
|
3452
|
-
if (
|
|
3453
|
-
err instanceof Error &&
|
|
3454
|
-
err.message === `agent ${agentId} not found`
|
|
3455
|
-
) {
|
|
3456
|
-
throw new NotFoundError(`agent ${agentId} not found`);
|
|
3457
|
-
}
|
|
3458
|
-
throw err;
|
|
3459
|
-
}
|
|
3460
|
-
const { plan, openWork } = result;
|
|
3461
|
-
if (plan.skipped) {
|
|
3462
|
-
return c.json(
|
|
3463
|
-
{
|
|
3464
|
-
status: "skipped" as const,
|
|
3465
|
-
reason: plan.skipReason ?? "reconcile skipped",
|
|
3466
|
-
},
|
|
3467
|
-
200,
|
|
3468
|
-
);
|
|
3469
|
-
}
|
|
3470
|
-
return c.json(
|
|
3471
|
-
{
|
|
3472
|
-
status: "planned" as const,
|
|
3473
|
-
planHash: plan.planHash,
|
|
3474
|
-
empty: plan.empty,
|
|
3475
|
-
diff: plan.diff,
|
|
3476
|
-
skippedOrgs: plan.skippedOrgs,
|
|
3477
|
-
rejectedAdds: plan.rejectedAdds,
|
|
3478
|
-
warnings: plan.warnings,
|
|
3479
|
-
affectedOpenWork: openWork,
|
|
3480
|
-
},
|
|
3481
|
-
200,
|
|
3482
|
-
);
|
|
3483
|
-
});
|
|
3484
|
-
|
|
3485
|
-
app.openapi(githubReconcileApplyRoute, async (c) => {
|
|
3486
|
-
if (c.get("isAdmin") !== true) {
|
|
3487
|
-
throw new ForbiddenError(
|
|
3488
|
-
"Admin access required to apply a GitHub reconcile",
|
|
3489
|
-
);
|
|
3490
|
-
}
|
|
3491
|
-
const { id: agentId } = c.req.valid("param");
|
|
3492
|
-
const body = c.req.valid("json");
|
|
3493
|
-
const reconcile = deps.githubReconcile;
|
|
3494
|
-
if (!reconcile) {
|
|
3495
|
-
return c.json(
|
|
3496
|
-
{ status: "skipped" as const, reason: GITHUB_RECONCILE_UNCONFIGURED },
|
|
3497
|
-
200,
|
|
3498
|
-
);
|
|
3499
|
-
}
|
|
3500
|
-
const caller = c.get("caller")?.name ?? "anonymous";
|
|
3501
|
-
const result = await reconcile.apply({
|
|
3502
|
-
agentId,
|
|
3503
|
-
caller,
|
|
3504
|
-
planHash: body.planHash,
|
|
3505
|
-
namedAdds: body.addRepos,
|
|
3506
|
-
confirmRemovals: body.confirmRemovals,
|
|
3507
|
-
});
|
|
3508
|
-
switch (result.status) {
|
|
3509
|
-
case "agent_not_found":
|
|
3510
|
-
throw new NotFoundError(`agent ${agentId} not found`);
|
|
3511
|
-
case "stale_plan":
|
|
3512
|
-
throw new ConflictError(
|
|
3513
|
-
"the plan changed since it was reviewed; re-run plan and review it again",
|
|
3514
|
-
);
|
|
3515
|
-
case "removal_cap":
|
|
3516
|
-
throw new UnprocessableEntityError(
|
|
3517
|
-
`plan removes ${result.removals} repos; resend with confirmRemovals: true to apply`,
|
|
3518
|
-
);
|
|
3519
|
-
case "skipped":
|
|
3520
|
-
return c.json(
|
|
3521
|
-
{
|
|
3522
|
-
status: "skipped" as const,
|
|
3523
|
-
reason: result.plan.skipReason ?? "reconcile skipped",
|
|
3524
|
-
},
|
|
3525
|
-
200,
|
|
3526
|
-
);
|
|
3527
|
-
case "noop":
|
|
3528
|
-
return c.json(
|
|
3529
|
-
{
|
|
3530
|
-
status: "noop" as const,
|
|
3531
|
-
planHash: result.plan.planHash,
|
|
3532
|
-
diff: result.plan.diff,
|
|
3533
|
-
},
|
|
3534
|
-
200,
|
|
3535
|
-
);
|
|
3536
|
-
case "applied":
|
|
3537
|
-
case "failed":
|
|
3538
|
-
console.log(
|
|
3539
|
-
`[agents-api] github reconcile ${result.status}: agent=${agentId} audit=${result.auditId} caller=${caller}`,
|
|
3540
|
-
);
|
|
3541
|
-
return c.json(
|
|
3542
|
-
{
|
|
3543
|
-
status: result.status,
|
|
3544
|
-
auditId: result.auditId,
|
|
3545
|
-
planHash: result.plan.planHash,
|
|
3546
|
-
diff: result.plan.diff,
|
|
3547
|
-
warnings: result.status === "applied" ? result.warnings : [],
|
|
3548
|
-
...(result.status === "failed" ? { error: result.error } : {}),
|
|
3549
|
-
},
|
|
3550
|
-
200,
|
|
3551
|
-
);
|
|
3552
|
-
}
|
|
3553
|
-
});
|
|
3554
|
-
|
|
3555
3291
|
// /agents/:id/github-installation/repos — IRM-1.2. Platform admin only;
|
|
3556
3292
|
// responses carry repo names and ids, never tokens.
|
|
3557
3293
|
const installationReposService = (c: {
|
|
@@ -3563,7 +3299,7 @@ export function createAdminApp(deps: AdminDeps): OpenAPIHono<AdminAuthEnv> {
|
|
|
3563
3299
|
);
|
|
3564
3300
|
}
|
|
3565
3301
|
const svc = deps.githubInstallationRepos;
|
|
3566
|
-
if (!svc) throw new ApiError(503,
|
|
3302
|
+
if (!svc) throw new ApiError(503, GITHUB_APPS_UNCONFIGURED);
|
|
3567
3303
|
return svc;
|
|
3568
3304
|
};
|
|
3569
3305
|
const installationFailure = (
|
|
@@ -3655,7 +3391,7 @@ export function createAdminApp(deps: AdminDeps): OpenAPIHono<AdminAuthEnv> {
|
|
|
3655
3391
|
);
|
|
3656
3392
|
}
|
|
3657
3393
|
const svc = deps.githubLostAccessRemoval;
|
|
3658
|
-
if (!svc) throw new ApiError(503,
|
|
3394
|
+
if (!svc) throw new ApiError(503, GITHUB_APPS_UNCONFIGURED);
|
|
3659
3395
|
return svc;
|
|
3660
3396
|
};
|
|
3661
3397
|
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* admin/src/github-enterprise-isolation.unit.test.ts
|
|
3
3
|
* Isolation exception guard (GHE-1.3): the GitHub Enterprise App private key
|
|
4
4
|
* is reachable only from the enterprise service, its admin API, the boot
|
|
5
|
-
* wiring and the
|
|
5
|
+
* wiring and the installation wiring. Agent config bundle, agent-bearer routes and env serialization must
|
|
6
6
|
* never import the service, touch its table or call getPrivateKey().
|
|
7
7
|
*/
|
|
8
8
|
|
|
@@ -17,7 +17,7 @@ const ALLOWED = new Set([
|
|
|
17
17
|
"admin/src/github-enterprise-apps.ts",
|
|
18
18
|
"admin/src/github-enterprise-api.ts",
|
|
19
19
|
"admin/src/admin-boot.ts",
|
|
20
|
-
"admin/src/github-
|
|
20
|
+
"admin/src/github-installation-wiring.ts",
|
|
21
21
|
]);
|
|
22
22
|
|
|
23
23
|
function sources(dir: string, out: string[] = []): string[] {
|
|
@@ -67,7 +67,7 @@ describe("GitHub Enterprise key isolation", () => {
|
|
|
67
67
|
expect(offenders).toEqual([]);
|
|
68
68
|
});
|
|
69
69
|
|
|
70
|
-
it("getPrivateKey() is only called by the enterprise API and
|
|
70
|
+
it("getPrivateKey() is only called by the enterprise API and installation wiring", () => {
|
|
71
71
|
const callers = files
|
|
72
72
|
.map((f) => relative(REPO, f))
|
|
73
73
|
.filter((rel) =>
|
|
@@ -75,7 +75,7 @@ describe("GitHub Enterprise key isolation", () => {
|
|
|
75
75
|
);
|
|
76
76
|
expect(callers.sort()).toEqual([
|
|
77
77
|
"admin/src/github-enterprise-api.ts",
|
|
78
|
-
"admin/src/github-
|
|
78
|
+
"admin/src/github-installation-wiring.ts",
|
|
79
79
|
]);
|
|
80
80
|
});
|
|
81
81
|
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* admin/src/github-
|
|
3
|
-
* Composition of the
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
2
|
+
* admin/src/github-installation-wiring.ts
|
|
3
|
+
* Composition of the installation repos and lost-access removal services for
|
|
4
|
+
* admin boot, from the stored installer App and an optional host-supplied
|
|
5
|
+
* OpenWorkPort. The agent App id comes from each agent's own stored
|
|
6
|
+
* GH_APP_ID; without an open-work port nothing is listed or blocked.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import type { PrismaClient } from "../prisma/client/client.ts";
|
|
@@ -14,11 +14,9 @@ import { GithubEnterpriseClient } from "./github-enterprise-client.ts";
|
|
|
14
14
|
import {
|
|
15
15
|
createInstallationPort,
|
|
16
16
|
type InstallationRepoClient,
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
type ReconcileInstallationPort,
|
|
21
|
-
} from "./reconcile-apply-service.ts";
|
|
17
|
+
PrismaInstallationAgents,
|
|
18
|
+
PrismaInstallationAudit,
|
|
19
|
+
} from "./installation-ports.ts";
|
|
22
20
|
import {
|
|
23
21
|
createInstallationReposPort,
|
|
24
22
|
type InstalledOrgsClient,
|
|
@@ -28,15 +26,13 @@ import {
|
|
|
28
26
|
import { LostAccessRemovalService } from "./lost-access-removal.ts";
|
|
29
27
|
import type { OpenWorkPort } from "./reconcile-open-work.ts";
|
|
30
28
|
|
|
31
|
-
export interface
|
|
29
|
+
export interface GithubInstallationWiringDeps {
|
|
32
30
|
prisma: PrismaClient;
|
|
33
31
|
appService: Pick<GitHubEnterpriseAppService, "get" | "getPrivateKey">;
|
|
34
32
|
access: GithubAccessPreviewFn;
|
|
35
33
|
/** Reads each agent's stored GH_APP_ID (its own GitHub App). */
|
|
36
34
|
agentEnv: Pick<AgentEnvService, "getConfigBundle">;
|
|
37
35
|
openWork?: OpenWorkPort;
|
|
38
|
-
/** Test seam: replaces the installer-App-backed installation port. */
|
|
39
|
-
installation?: ReconcileInstallationPort;
|
|
40
36
|
/** Test seams; default to the real GitHub API. */
|
|
41
37
|
fetchFn?: typeof fetch;
|
|
42
38
|
apiBase?: string;
|
|
@@ -46,7 +42,7 @@ export interface GithubReconcileWiringDeps {
|
|
|
46
42
|
/** Builds the installer client per call so key rotations are picked up. */
|
|
47
43
|
function lazyInstallerClient(
|
|
48
44
|
deps: Pick<
|
|
49
|
-
|
|
45
|
+
GithubInstallationWiringDeps,
|
|
50
46
|
"appService" | "fetchFn" | "apiBase" | "now"
|
|
51
47
|
>,
|
|
52
48
|
): InstallationRepoClient & InstalledOrgsClient {
|
|
@@ -81,44 +77,28 @@ function lazyInstallerClient(
|
|
|
81
77
|
};
|
|
82
78
|
}
|
|
83
79
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
* per agent from its stored GH_APP_ID, so this is always wired.
|
|
87
|
-
*/
|
|
88
|
-
export function createGithubReconcile(
|
|
89
|
-
deps: GithubReconcileWiringDeps,
|
|
90
|
-
): ReconcileApplyService {
|
|
91
|
-
return new ReconcileApplyService({
|
|
92
|
-
agents: new PrismaReconcileAgents(deps.prisma, deps.agentEnv),
|
|
93
|
-
access: deps.access,
|
|
94
|
-
installation:
|
|
95
|
-
deps.installation ?? createInstallationPort(lazyInstallerClient(deps)),
|
|
96
|
-
audit: new PrismaReconcileAudit(deps.prisma),
|
|
97
|
-
...(deps.openWork ? { openWork: deps.openWork } : {}),
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
export interface InstallationReposWiringDeps extends GithubReconcileWiringDeps {
|
|
80
|
+
export interface InstallationReposWiringDeps
|
|
81
|
+
extends GithubInstallationWiringDeps {
|
|
102
82
|
/** Test seam: replaces the installer-App-backed port. */
|
|
103
83
|
installationRepos?: InstallationReposPort;
|
|
104
84
|
}
|
|
105
85
|
|
|
106
86
|
/**
|
|
107
87
|
* The `githubInstallationRepos` dep for createAdminApp (IRM-1.2): direct
|
|
108
|
-
* list/add/remove on the agent App's installation, audited in the
|
|
109
|
-
*
|
|
88
|
+
* list/add/remove on the agent App's installation, audited in the
|
|
89
|
+
* GitHubReconcileAudit table.
|
|
110
90
|
*/
|
|
111
91
|
export function createGithubInstallationRepos(
|
|
112
92
|
deps: InstallationReposWiringDeps,
|
|
113
93
|
): InstallationReposService {
|
|
114
94
|
const client = lazyInstallerClient(deps);
|
|
115
95
|
return new InstallationReposService({
|
|
116
|
-
agents: new
|
|
96
|
+
agents: new PrismaInstallationAgents(deps.prisma, deps.agentEnv),
|
|
117
97
|
access: deps.access,
|
|
118
98
|
installation:
|
|
119
99
|
deps.installationRepos ??
|
|
120
100
|
createInstallationReposPort(createInstallationPort(client), client),
|
|
121
|
-
audit: new
|
|
101
|
+
audit: new PrismaInstallationAudit(deps.prisma),
|
|
122
102
|
});
|
|
123
103
|
}
|
|
124
104
|
|
|
@@ -133,12 +113,12 @@ export function createGithubLostAccessRemoval(
|
|
|
133
113
|
): LostAccessRemovalService {
|
|
134
114
|
const client = lazyInstallerClient(deps);
|
|
135
115
|
return new LostAccessRemovalService({
|
|
136
|
-
agents: new
|
|
116
|
+
agents: new PrismaInstallationAgents(deps.prisma, deps.agentEnv),
|
|
137
117
|
access: deps.access,
|
|
138
118
|
installation:
|
|
139
119
|
deps.installationRepos ??
|
|
140
120
|
createInstallationReposPort(createInstallationPort(client), client),
|
|
141
|
-
audit: new
|
|
121
|
+
audit: new PrismaInstallationAudit(deps.prisma),
|
|
142
122
|
...(deps.openWork ? { openWork: deps.openWork } : {}),
|
|
143
123
|
});
|
|
144
124
|
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { describe, expect, it } from "bun:test";
|
|
2
|
+
import {
|
|
3
|
+
accessFor,
|
|
4
|
+
envWith,
|
|
5
|
+
fakeOpenWork,
|
|
6
|
+
fakePrisma,
|
|
7
|
+
} from "./github-installation.fixtures.ts";
|
|
8
|
+
import {
|
|
9
|
+
createGithubInstallationRepos,
|
|
10
|
+
createGithubLostAccessRemoval,
|
|
11
|
+
} from "./github-installation-wiring.ts";
|
|
12
|
+
import type { InstallationReposPort } from "./installation-repos-service.ts";
|
|
13
|
+
|
|
14
|
+
const noApp = { get: async () => null, getPrivateKey: async () => null };
|
|
15
|
+
|
|
16
|
+
const portWith = (
|
|
17
|
+
seen: number[],
|
|
18
|
+
repos: string[] = ["acme/old", "acme/api"],
|
|
19
|
+
): InstallationReposPort => ({
|
|
20
|
+
listRepos: async (appId) => {
|
|
21
|
+
seen.push(appId);
|
|
22
|
+
return { acme: repos };
|
|
23
|
+
},
|
|
24
|
+
listInstalled: async (appId) => {
|
|
25
|
+
seen.push(appId);
|
|
26
|
+
return [{ org: "acme", repos }];
|
|
27
|
+
},
|
|
28
|
+
addRepos: async () => {},
|
|
29
|
+
removeRepos: async () => {},
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
describe("createGithubInstallationRepos", () => {
|
|
33
|
+
it("lists from the agent's own stored GH_APP_ID", async () => {
|
|
34
|
+
const seen: number[] = [];
|
|
35
|
+
const { prisma } = fakePrisma(["acme/old"]);
|
|
36
|
+
const svc = createGithubInstallationRepos({
|
|
37
|
+
prisma,
|
|
38
|
+
appService: noApp,
|
|
39
|
+
access: accessFor(["acme/api"]),
|
|
40
|
+
agentEnv: envWith({ GH_APP_ID: " 77 " }),
|
|
41
|
+
installationRepos: portWith(seen),
|
|
42
|
+
});
|
|
43
|
+
const res = await svc.list("a1");
|
|
44
|
+
expect(seen).toEqual([77]);
|
|
45
|
+
expect(res.status).toBe("ok");
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("reports an agent with no GH_APP_ID, naming the missing var", async () => {
|
|
49
|
+
const { prisma } = fakePrisma(["acme/old"]);
|
|
50
|
+
const envs: (Record<string, string> | undefined)[] = [
|
|
51
|
+
undefined,
|
|
52
|
+
{ OTHER: "x" },
|
|
53
|
+
{ GH_APP_ID: "abc" },
|
|
54
|
+
];
|
|
55
|
+
for (const env of envs) {
|
|
56
|
+
const svc = createGithubInstallationRepos({
|
|
57
|
+
prisma,
|
|
58
|
+
appService: noApp,
|
|
59
|
+
access: accessFor([]),
|
|
60
|
+
agentEnv: envWith(env),
|
|
61
|
+
installationRepos: portWith([]),
|
|
62
|
+
});
|
|
63
|
+
const res = await svc.list("a1");
|
|
64
|
+
expect(res.status).toBe("no_app_id");
|
|
65
|
+
if (res.status === "no_app_id") expect(res.reason).toContain("GH_APP_ID");
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
describe("createGithubLostAccessRemoval", () => {
|
|
71
|
+
const build = (openWork?: ReturnType<typeof fakeOpenWork>["port"]) => {
|
|
72
|
+
const { prisma, state } = fakePrisma(["acme/old", "acme/api"]);
|
|
73
|
+
const svc = createGithubLostAccessRemoval({
|
|
74
|
+
prisma,
|
|
75
|
+
appService: noApp,
|
|
76
|
+
access: accessFor(["acme/api"]),
|
|
77
|
+
agentEnv: envWith({ GH_APP_ID: "42" }),
|
|
78
|
+
installationRepos: portWith([]),
|
|
79
|
+
openWork,
|
|
80
|
+
});
|
|
81
|
+
return { svc, state };
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
it("with a port: preview lists affected open work and remove blocks it", async () => {
|
|
85
|
+
const { port, calls } = fakeOpenWork();
|
|
86
|
+
const { svc, state } = build(port);
|
|
87
|
+
const preview = await svc.preview("a1");
|
|
88
|
+
expect(preview.status).toBe("previewed");
|
|
89
|
+
const res = await svc.remove({
|
|
90
|
+
agentId: "a1",
|
|
91
|
+
caller: "root",
|
|
92
|
+
repos: ["acme/old"],
|
|
93
|
+
});
|
|
94
|
+
expect(res.status).toBe("applied");
|
|
95
|
+
expect(state.repos).toEqual(["acme/api"]);
|
|
96
|
+
const block = calls.find((c) => c.fn === "block");
|
|
97
|
+
expect(block?.args[0]).toBe("a1");
|
|
98
|
+
expect(block?.args[1]).toEqual(["acme/old"]);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("without a port: nothing is blocked, remove still works", async () => {
|
|
102
|
+
const { svc, state } = build();
|
|
103
|
+
const res = await svc.remove({
|
|
104
|
+
agentId: "a1",
|
|
105
|
+
caller: "root",
|
|
106
|
+
repos: ["acme/old"],
|
|
107
|
+
});
|
|
108
|
+
expect(res.status).toBe("applied");
|
|
109
|
+
expect(state.repos).toEqual(["acme/api"]);
|
|
110
|
+
});
|
|
111
|
+
});
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/github-installation.fixtures.ts
|
|
3
|
+
* Shared fakes for the installation repos / lost-access removal wiring tests.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { PrismaClient } from "../prisma/client/client.ts";
|
|
7
|
+
import type { GithubAccessPreviewFn } from "./github-access-preview.ts";
|
|
8
|
+
import type { OpenWorkPort } from "./reconcile-open-work.ts";
|
|
9
|
+
|
|
10
|
+
export const accessFor = (pushable: string[]): GithubAccessPreviewFn => {
|
|
11
|
+
return async () => ({
|
|
12
|
+
status: "ok",
|
|
13
|
+
access: {
|
|
14
|
+
username: "dev",
|
|
15
|
+
outcome: "ok",
|
|
16
|
+
userVerified: true,
|
|
17
|
+
complete: true,
|
|
18
|
+
unresolved: 0,
|
|
19
|
+
checkedAt: "2026-11-11T00:00:00.000Z",
|
|
20
|
+
orgs: [
|
|
21
|
+
{
|
|
22
|
+
org: "acme",
|
|
23
|
+
status: "ok",
|
|
24
|
+
unresolved: 0,
|
|
25
|
+
repos: pushable.map((fullName) => ({
|
|
26
|
+
fullName,
|
|
27
|
+
level: "write" as const,
|
|
28
|
+
canPush: true,
|
|
29
|
+
})),
|
|
30
|
+
},
|
|
31
|
+
],
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export function fakePrisma(repos: string[]) {
|
|
37
|
+
const state = { repos: [...repos], audits: 0 };
|
|
38
|
+
const prisma = {
|
|
39
|
+
agent: {
|
|
40
|
+
findUnique: async () => ({
|
|
41
|
+
id: "a1",
|
|
42
|
+
repos: state.repos,
|
|
43
|
+
githubUsername: "dev",
|
|
44
|
+
}),
|
|
45
|
+
update: async (args: { data: { repos: string[] } }) => {
|
|
46
|
+
state.repos = args.data.repos;
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
gitHubReconcileAudit: {
|
|
50
|
+
create: async () => {
|
|
51
|
+
state.audits += 1;
|
|
52
|
+
return { id: "audit-1" };
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
} as unknown as PrismaClient;
|
|
56
|
+
return { prisma, state };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function fakeOpenWork() {
|
|
60
|
+
const calls: { fn: string; args: unknown[] }[] = [];
|
|
61
|
+
const port: OpenWorkPort = {
|
|
62
|
+
listOpenWork: async (...args) => {
|
|
63
|
+
calls.push({ fn: "list", args });
|
|
64
|
+
return [{ id: "T-1", repo: "acme/old", title: "fix it" }];
|
|
65
|
+
},
|
|
66
|
+
blockOpenWork: async (...args) => {
|
|
67
|
+
calls.push({ fn: "block", args });
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
return { port, calls };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export const envWith = (env?: Record<string, string>) => ({
|
|
74
|
+
getConfigBundle: async (agentId: string) =>
|
|
75
|
+
env ? { env, agentId, allowedTools: [] } : null,
|
|
76
|
+
});
|