@app-vitals/agent-admin 1.35.1 → 1.36.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 +55 -295
- package/package.json +2 -2
- package/src/admin-boot.ts +3 -3
- package/src/admin-extension.ts +1 -1
- package/src/admin-ui-github-identity.smoke.test.ts +24 -2
- package/src/admin-ui-installation-repos-pages.ts +198 -231
- package/src/admin-ui-installation-repos-pages.unit.test.ts +199 -0
- package/src/admin-ui-installation-repos.smoke.test.ts +166 -172
- package/src/admin-ui-pages.ts +2 -2
- package/src/admin-ui.ts +103 -89
- package/src/agents-api.smoke.test.ts +193 -290
- package/src/agents-api.ts +83 -210
- package/src/github-installation-wiring.ts +10 -12
- package/src/github-installation-wiring.unit.test.ts +13 -9
- package/src/github-installation.fixtures.ts +1 -1
- package/src/github-sync.ts +407 -0
- package/src/{lost-access-removal.unit.test.ts → github-sync.unit.test.ts} +214 -109
- package/src/installation-ports.ts +1 -1
- package/src/installation-repos-service.ts +3 -98
- package/src/installation-repos-service.unit.test.ts +5 -90
- package/src/reconcile-open-work.ts +1 -1
- package/src/repo-access-rules.ts +1 -1
- package/src/lost-access-removal.ts +0 -333
package/src/agents-api.ts
CHANGED
|
@@ -166,7 +166,7 @@ import {
|
|
|
166
166
|
UpsertChatTokenDailyBodySchema,
|
|
167
167
|
} from "./openapi-schemas.ts";
|
|
168
168
|
import type { InstallationReposService } from "./installation-repos-service.ts";
|
|
169
|
-
import type {
|
|
169
|
+
import type { GithubSyncService } from "./github-sync.ts";
|
|
170
170
|
import type { OpenWorkPort } from "./reconcile-open-work.ts";
|
|
171
171
|
import {
|
|
172
172
|
blockRepoRemovalWork,
|
|
@@ -277,23 +277,17 @@ export interface AdminDeps {
|
|
|
277
277
|
*/
|
|
278
278
|
openWork?: OpenWorkPort;
|
|
279
279
|
/**
|
|
280
|
-
* IRM-1.2: backs the admin-only /agents/:id/github-installation/repos
|
|
281
|
-
*
|
|
282
|
-
* wired and the
|
|
280
|
+
* IRM-1.2: backs the admin-only GET /agents/:id/github-installation/repos
|
|
281
|
+
* route. Optional (additive); absent means the enterprise Apps are not
|
|
282
|
+
* wired and the route answers 503.
|
|
283
283
|
*/
|
|
284
|
-
githubInstallationRepos?: Pick<
|
|
285
|
-
InstallationReposService,
|
|
286
|
-
"list" | "add" | "remove"
|
|
287
|
-
>;
|
|
284
|
+
githubInstallationRepos?: Pick<InstallationReposService, "list">;
|
|
288
285
|
/**
|
|
289
|
-
* IRM-1
|
|
286
|
+
* IRM-3.1: backs the admin-only /agents/:id/github-sync/{preview,apply}
|
|
290
287
|
* routes. Optional (additive); absent means the enterprise Apps are not
|
|
291
288
|
* wired and the routes answer 503.
|
|
292
289
|
*/
|
|
293
|
-
|
|
294
|
-
LostAccessRemovalService,
|
|
295
|
-
"preview" | "remove"
|
|
296
|
-
>;
|
|
290
|
+
githubSync?: Pick<GithubSyncService, "preview" | "apply">;
|
|
297
291
|
/**
|
|
298
292
|
* Retained solely as a pass-through to deleteAgentFully() (DELETE /agents/:id),
|
|
299
293
|
* which has its own internal prisma.agent/prisma.agentEnv access — out of
|
|
@@ -630,15 +624,6 @@ const RepoFullNameSchema = z
|
|
|
630
624
|
.string()
|
|
631
625
|
.regex(/^[^/\s]+\/[^/\s]+$/, "must be owner/repo");
|
|
632
626
|
|
|
633
|
-
const InstallationReposBodySchema = z
|
|
634
|
-
.object({
|
|
635
|
-
repos: z.array(RepoFullNameSchema).min(1).max(100).openapi({
|
|
636
|
-
description:
|
|
637
|
-
"Repos (owner/repo) to change on the agent App's installation.",
|
|
638
|
-
}),
|
|
639
|
-
})
|
|
640
|
-
.openapi("GithubInstallationReposBody");
|
|
641
|
-
|
|
642
627
|
const GithubInstallationReposListSchema = z
|
|
643
628
|
.object({
|
|
644
629
|
appId: z.number().int(),
|
|
@@ -659,14 +644,6 @@ const GithubInstallationReposListSchema = z
|
|
|
659
644
|
})
|
|
660
645
|
.openapi("GithubInstallationReposList");
|
|
661
646
|
|
|
662
|
-
const GithubInstallationReposChangeSchema = z
|
|
663
|
-
.object({
|
|
664
|
-
status: z.enum(["applied"]),
|
|
665
|
-
auditId: z.string(),
|
|
666
|
-
repos: z.array(z.string()),
|
|
667
|
-
})
|
|
668
|
-
.openapi("GithubInstallationReposChange");
|
|
669
|
-
|
|
670
647
|
const OpenWorkItemSchema = z.object({
|
|
671
648
|
id: z.string(),
|
|
672
649
|
repo: z.string(),
|
|
@@ -677,62 +654,76 @@ const SkippedOrgsSchema = z.array(
|
|
|
677
654
|
z.object({ org: z.string(), reason: z.string() }),
|
|
678
655
|
);
|
|
679
656
|
|
|
680
|
-
const
|
|
657
|
+
const GithubSyncPreviewSchema = z
|
|
681
658
|
.object({
|
|
682
659
|
status: z.enum(["previewed", "skipped"]).openapi({
|
|
683
660
|
description:
|
|
684
661
|
"`skipped` (not an error) when no GitHub username is bound, the user is not found or unverified, the agent has no GH_APP_ID, or the Apps are not configured; `reason` says why.",
|
|
685
662
|
}),
|
|
686
663
|
reason: z.string().optional(),
|
|
687
|
-
|
|
664
|
+
appAdds: z.array(z.string()).optional().openapi({
|
|
665
|
+
description:
|
|
666
|
+
"Repos the bound user can push to, in orgs the agent App is installed on with a complete access listing, that are not on the installation. No cap.",
|
|
667
|
+
}),
|
|
668
|
+
appRemoves: z.array(z.string()).optional().openapi({
|
|
669
|
+
description: "Installation repos the bound user can no longer push to.",
|
|
670
|
+
}),
|
|
671
|
+
agentRemoves: z.array(z.string()).optional().openapi({
|
|
688
672
|
description:
|
|
689
|
-
"
|
|
673
|
+
"Configured agent repos the bound user can no longer push to. The agent's repos are never added to.",
|
|
690
674
|
}),
|
|
691
|
-
installationRemoves: z.array(z.string()).optional(),
|
|
692
|
-
configRemoves: z.array(z.string()).optional(),
|
|
693
675
|
skippedOrgs: SkippedOrgsSchema.optional().openapi({
|
|
694
676
|
description:
|
|
695
|
-
"Orgs whose access listing is missing, partial or failed, or whose lookup App is installed on selected repositories only; nothing there is removed.",
|
|
677
|
+
"Orgs whose access listing is missing, partial or failed, or whose lookup App is not installed or installed on selected repositories only; nothing there is added or removed.",
|
|
696
678
|
}),
|
|
697
679
|
openWork: z.array(OpenWorkItemSchema).optional().openapi({
|
|
698
680
|
description:
|
|
699
|
-
"The agent's open tasks and PRs on
|
|
681
|
+
"The agent's open tasks and PRs on the removed repos that the removals would block; empty when no open-work port is wired.",
|
|
700
682
|
}),
|
|
701
683
|
warnings: z.array(z.string()).optional(),
|
|
702
684
|
})
|
|
703
|
-
.openapi("
|
|
685
|
+
.openapi("GithubSyncPreview");
|
|
704
686
|
|
|
705
|
-
const
|
|
687
|
+
const GithubSyncApplyBodySchema = z
|
|
706
688
|
.object({
|
|
707
|
-
|
|
689
|
+
appAdds: z.array(RepoFullNameSchema).max(500).default([]).openapi({
|
|
708
690
|
description:
|
|
709
|
-
"
|
|
691
|
+
"Repos (owner/repo) the admin confirmed to add to the agent App's installation.",
|
|
692
|
+
}),
|
|
693
|
+
appRemoves: z.array(RepoFullNameSchema).max(500).default([]).openapi({
|
|
694
|
+
description:
|
|
695
|
+
"Repos the admin confirmed to remove from the agent App's installation.",
|
|
696
|
+
}),
|
|
697
|
+
agentRemoves: z.array(RepoFullNameSchema).max(500).default([]).openapi({
|
|
698
|
+
description:
|
|
699
|
+
"Repos the admin confirmed to remove from the agent's repos.",
|
|
710
700
|
}),
|
|
711
701
|
confirmRemovals: z.boolean().optional().openapi({
|
|
712
|
-
description:
|
|
702
|
+
description:
|
|
703
|
+
"Required when more than 10 distinct repos would be removed.",
|
|
713
704
|
}),
|
|
714
705
|
})
|
|
715
|
-
.openapi("
|
|
706
|
+
.openapi("GithubSyncApplyBody");
|
|
716
707
|
|
|
717
|
-
const
|
|
708
|
+
const GithubSyncApplyResultSchema = z
|
|
718
709
|
.object({
|
|
719
710
|
status: z.enum(["applied", "noop", "skipped"]),
|
|
720
711
|
reason: z.string().optional(),
|
|
721
712
|
auditId: z.string().optional(),
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
713
|
+
appAdds: z.array(z.string()).optional(),
|
|
714
|
+
appRemoves: z.array(z.string()).optional(),
|
|
715
|
+
agentRemoves: z.array(z.string()).optional(),
|
|
716
|
+
notApplied: z
|
|
726
717
|
.array(z.object({ repo: z.string(), reason: z.string() }))
|
|
727
718
|
.optional()
|
|
728
719
|
.openapi({
|
|
729
720
|
description:
|
|
730
|
-
"
|
|
721
|
+
"Confirmed repos left alone: access changed since the preview, the org was skipped, or the repo is not where the change expected it.",
|
|
731
722
|
}),
|
|
732
723
|
skippedOrgs: SkippedOrgsSchema.optional(),
|
|
733
724
|
warnings: z.array(z.string()).optional(),
|
|
734
725
|
})
|
|
735
|
-
.openapi("
|
|
726
|
+
.openapi("GithubSyncApplyResult");
|
|
736
727
|
|
|
737
728
|
// ─── Route definitions ──────────────────────────────────────────────────────────
|
|
738
729
|
|
|
@@ -1688,59 +1679,7 @@ const listInstallationReposRoute = createRoute({
|
|
|
1688
1679
|
},
|
|
1689
1680
|
});
|
|
1690
1681
|
|
|
1691
|
-
const
|
|
1692
|
-
method: "post",
|
|
1693
|
-
path: "/agents/{id}/github-installation/repos",
|
|
1694
|
-
summary: "Add repos to the agent App's installation",
|
|
1695
|
-
description:
|
|
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).",
|
|
1697
|
-
request: {
|
|
1698
|
-
params: AgentIdParamSchema,
|
|
1699
|
-
body: {
|
|
1700
|
-
required: true,
|
|
1701
|
-
content: { "application/json": { schema: InstallationReposBodySchema } },
|
|
1702
|
-
},
|
|
1703
|
-
},
|
|
1704
|
-
responses: {
|
|
1705
|
-
200: {
|
|
1706
|
-
description: "Repos added",
|
|
1707
|
-
content: {
|
|
1708
|
-
"application/json": { schema: GithubInstallationReposChangeSchema },
|
|
1709
|
-
},
|
|
1710
|
-
},
|
|
1711
|
-
...installationReposErrors,
|
|
1712
|
-
409: { description: "Agent App not installed on the org", ...jsonError },
|
|
1713
|
-
502: { description: "GitHub unavailable or refused", ...jsonError },
|
|
1714
|
-
},
|
|
1715
|
-
});
|
|
1716
|
-
|
|
1717
|
-
const removeInstallationReposRoute = createRoute({
|
|
1718
|
-
method: "post",
|
|
1719
|
-
path: "/agents/{id}/github-installation/repos/remove",
|
|
1720
|
-
summary: "Remove repos from the agent App's installation",
|
|
1721
|
-
description:
|
|
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.",
|
|
1723
|
-
request: {
|
|
1724
|
-
params: AgentIdParamSchema,
|
|
1725
|
-
body: {
|
|
1726
|
-
required: true,
|
|
1727
|
-
content: { "application/json": { schema: InstallationReposBodySchema } },
|
|
1728
|
-
},
|
|
1729
|
-
},
|
|
1730
|
-
responses: {
|
|
1731
|
-
200: {
|
|
1732
|
-
description: "Repos removed",
|
|
1733
|
-
content: {
|
|
1734
|
-
"application/json": { schema: GithubInstallationReposChangeSchema },
|
|
1735
|
-
},
|
|
1736
|
-
},
|
|
1737
|
-
...installationReposErrors,
|
|
1738
|
-
409: { description: "Agent App not installed on the org", ...jsonError },
|
|
1739
|
-
502: { description: "GitHub unavailable or refused", ...jsonError },
|
|
1740
|
-
},
|
|
1741
|
-
});
|
|
1742
|
-
|
|
1743
|
-
const lostAccessErrors = {
|
|
1682
|
+
const syncErrors = {
|
|
1744
1683
|
401: { description: "Unauthorized", ...jsonError },
|
|
1745
1684
|
403: { description: "Platform admin required", ...jsonError },
|
|
1746
1685
|
404: { description: "Agent not found", ...jsonError },
|
|
@@ -1748,47 +1687,41 @@ const lostAccessErrors = {
|
|
|
1748
1687
|
503: { description: "GitHub Apps are not configured", ...jsonError },
|
|
1749
1688
|
};
|
|
1750
1689
|
|
|
1751
|
-
const
|
|
1690
|
+
const githubSyncPreviewRoute = createRoute({
|
|
1752
1691
|
method: "get",
|
|
1753
|
-
path: "/agents/{id}/github-
|
|
1754
|
-
summary: "Preview
|
|
1692
|
+
path: "/agents/{id}/github-sync/preview",
|
|
1693
|
+
summary: "Preview syncing the agent's GitHub access with its bound user",
|
|
1755
1694
|
description:
|
|
1756
|
-
"Platform-admin only (session members, ops keys and agent keys get 403). Read-only: nothing is changed or audited. Lists
|
|
1695
|
+
"Platform-admin only (session members, ops keys and agent keys get 403). Read-only: nothing is changed or audited. Lists (1) repos to add to the agent App's installation, (2) installation repos to remove and (3) agent repos to remove, based on what the bound githubUsername can push to, plus the agent's open tasks and PRs the removals would block (via the optional open-work port; a port failure is a warning). The agent's repos are never added to. Orgs whose access listing is missing, partial or failed, or whose lookup App is not installed or installed on selected repositories only, are skipped (adds included) and reported. 200 with `status: skipped` when the user is unbound, not found or unverified, the agent has no GH_APP_ID, or the Apps are not configured.",
|
|
1757
1696
|
request: { params: AgentIdParamSchema },
|
|
1758
1697
|
responses: {
|
|
1759
1698
|
200: {
|
|
1760
|
-
description: "
|
|
1761
|
-
content: {
|
|
1762
|
-
"application/json": { schema: GithubLostAccessPreviewSchema },
|
|
1763
|
-
},
|
|
1699
|
+
description: "Sync preview",
|
|
1700
|
+
content: { "application/json": { schema: GithubSyncPreviewSchema } },
|
|
1764
1701
|
},
|
|
1765
|
-
...
|
|
1702
|
+
...syncErrors,
|
|
1766
1703
|
},
|
|
1767
1704
|
});
|
|
1768
1705
|
|
|
1769
|
-
const
|
|
1706
|
+
const githubSyncApplyRoute = createRoute({
|
|
1770
1707
|
method: "post",
|
|
1771
|
-
path: "/agents/{id}/github-
|
|
1772
|
-
summary: "
|
|
1708
|
+
path: "/agents/{id}/github-sync/apply",
|
|
1709
|
+
summary: "Apply the confirmed sync changes",
|
|
1773
1710
|
description:
|
|
1774
|
-
"Platform-admin only.
|
|
1711
|
+
"Platform-admin only. Takes the exact lists the admin confirmed from the preview, re-checks each against the bound user's current access, and applies only what still qualifies: installation adds, installation removes, then agent-repo removes. Writes one audit entry (GitHubReconcileAudit, planHash `sync:apply`) and blocks the agent's open work on the removed repos through the optional open-work port (a failure there is a warning). Confirmed repos left alone are reported in `notApplied`. Same skip rules as the preview: a skipped call (200 `status: skipped`) changes and audits nothing; `noop` when nothing confirmed still applies. 422 when more than 10 repos would be removed without `confirmRemovals: true`. 502 when GitHub refuses (a failed audit entry records only what landed).",
|
|
1775
1712
|
request: {
|
|
1776
1713
|
params: AgentIdParamSchema,
|
|
1777
1714
|
body: {
|
|
1778
1715
|
required: true,
|
|
1779
|
-
content: {
|
|
1780
|
-
"application/json": { schema: GithubLostAccessRemoveBodySchema },
|
|
1781
|
-
},
|
|
1716
|
+
content: { "application/json": { schema: GithubSyncApplyBodySchema } },
|
|
1782
1717
|
},
|
|
1783
1718
|
},
|
|
1784
1719
|
responses: {
|
|
1785
1720
|
200: {
|
|
1786
|
-
description: "
|
|
1787
|
-
content: {
|
|
1788
|
-
"application/json": { schema: GithubLostAccessRemoveResultSchema },
|
|
1789
|
-
},
|
|
1721
|
+
description: "Sync result",
|
|
1722
|
+
content: { "application/json": { schema: GithubSyncApplyResultSchema } },
|
|
1790
1723
|
},
|
|
1791
|
-
...
|
|
1724
|
+
...syncErrors,
|
|
1792
1725
|
422: { description: "Removal cap needs confirmRemovals", ...jsonError },
|
|
1793
1726
|
},
|
|
1794
1727
|
});
|
|
@@ -3295,7 +3228,7 @@ export function createAdminApp(deps: AdminDeps): OpenAPIHono<AdminAuthEnv> {
|
|
|
3295
3228
|
}) => {
|
|
3296
3229
|
if (c.get("isAdmin") !== true) {
|
|
3297
3230
|
throw new ForbiddenError(
|
|
3298
|
-
"Admin access required to
|
|
3231
|
+
"Admin access required to list installation repos",
|
|
3299
3232
|
);
|
|
3300
3233
|
}
|
|
3301
3234
|
const svc = deps.githubInstallationRepos;
|
|
@@ -3306,16 +3239,12 @@ export function createAdminApp(deps: AdminDeps): OpenAPIHono<AdminAuthEnv> {
|
|
|
3306
3239
|
agentId: string,
|
|
3307
3240
|
result:
|
|
3308
3241
|
| { status: "agent_not_found" }
|
|
3309
|
-
| { status: "no_app_id"; reason: string }
|
|
3310
|
-
| { status: "not_installed"; reason: string },
|
|
3242
|
+
| { status: "no_app_id"; reason: string },
|
|
3311
3243
|
): never => {
|
|
3312
3244
|
if (result.status === "agent_not_found") {
|
|
3313
3245
|
throw new NotFoundError(`agent ${agentId} not found`);
|
|
3314
3246
|
}
|
|
3315
|
-
|
|
3316
|
-
throw new UnprocessableEntityError(result.reason);
|
|
3317
|
-
}
|
|
3318
|
-
throw new ConflictError(result.reason);
|
|
3247
|
+
throw new UnprocessableEntityError(result.reason);
|
|
3319
3248
|
};
|
|
3320
3249
|
|
|
3321
3250
|
app.openapi(listInstallationReposRoute, async (c) => {
|
|
@@ -3326,77 +3255,19 @@ export function createAdminApp(deps: AdminDeps): OpenAPIHono<AdminAuthEnv> {
|
|
|
3326
3255
|
return c.json({ appId: result.appId, orgs: result.orgs }, 200);
|
|
3327
3256
|
});
|
|
3328
3257
|
|
|
3329
|
-
|
|
3330
|
-
|
|
3331
|
-
|
|
3332
|
-
get(k: "isAdmin"): boolean | undefined;
|
|
3333
|
-
get(k: "caller"): { name: string } | undefined;
|
|
3334
|
-
},
|
|
3335
|
-
agentId: string,
|
|
3336
|
-
repos: string[],
|
|
3337
|
-
) => {
|
|
3338
|
-
const svc = installationReposService(c);
|
|
3339
|
-
const caller = c.get("caller")?.name ?? "anonymous";
|
|
3340
|
-
const result = await svc[kind](agentId, caller, repos);
|
|
3341
|
-
if (
|
|
3342
|
-
result.status === "agent_not_found" ||
|
|
3343
|
-
result.status === "no_app_id" ||
|
|
3344
|
-
result.status === "not_installed"
|
|
3345
|
-
) {
|
|
3346
|
-
return installationFailure(agentId, result);
|
|
3347
|
-
}
|
|
3348
|
-
console.log(
|
|
3349
|
-
`[agents-api] github installation repos ${kind} ${result.status}: agent=${agentId} audit=${result.auditId} caller=${caller}`,
|
|
3350
|
-
);
|
|
3351
|
-
if (result.status === "failed") {
|
|
3352
|
-
throw new BadGatewayError(
|
|
3353
|
-
`GitHub refused the ${kind}; audit ${result.auditId} records the failure: ${result.error}`,
|
|
3354
|
-
);
|
|
3355
|
-
}
|
|
3356
|
-
return {
|
|
3357
|
-
status: "applied" as const,
|
|
3358
|
-
auditId: result.auditId,
|
|
3359
|
-
repos: result.repos,
|
|
3360
|
-
};
|
|
3361
|
-
};
|
|
3362
|
-
app.openapi(addInstallationReposRoute, async (c) =>
|
|
3363
|
-
c.json(
|
|
3364
|
-
await changeInstallationRepos(
|
|
3365
|
-
"add",
|
|
3366
|
-
c,
|
|
3367
|
-
c.req.valid("param").id,
|
|
3368
|
-
c.req.valid("json").repos,
|
|
3369
|
-
),
|
|
3370
|
-
200,
|
|
3371
|
-
),
|
|
3372
|
-
);
|
|
3373
|
-
app.openapi(removeInstallationReposRoute, async (c) =>
|
|
3374
|
-
c.json(
|
|
3375
|
-
await changeInstallationRepos(
|
|
3376
|
-
"remove",
|
|
3377
|
-
c,
|
|
3378
|
-
c.req.valid("param").id,
|
|
3379
|
-
c.req.valid("json").repos,
|
|
3380
|
-
),
|
|
3381
|
-
200,
|
|
3382
|
-
),
|
|
3383
|
-
);
|
|
3384
|
-
|
|
3385
|
-
// /agents/:id/github-lost-access/{preview,remove} — IRM-1.3. Platform admin
|
|
3386
|
-
// only; responses carry repo names and ids, never tokens.
|
|
3387
|
-
const lostAccessService = (c: { get(k: "isAdmin"): boolean | undefined }) => {
|
|
3258
|
+
// /agents/:id/github-sync/{preview,apply} — IRM-3.1. Platform admin only;
|
|
3259
|
+
// responses carry repo names and ids, never tokens.
|
|
3260
|
+
const syncService = (c: { get(k: "isAdmin"): boolean | undefined }) => {
|
|
3388
3261
|
if (c.get("isAdmin") !== true) {
|
|
3389
|
-
throw new ForbiddenError(
|
|
3390
|
-
"Admin access required to remove repos the bound user lost access to",
|
|
3391
|
-
);
|
|
3262
|
+
throw new ForbiddenError("Admin access required to sync GitHub repos");
|
|
3392
3263
|
}
|
|
3393
|
-
const svc = deps.
|
|
3264
|
+
const svc = deps.githubSync;
|
|
3394
3265
|
if (!svc) throw new ApiError(503, GITHUB_APPS_UNCONFIGURED);
|
|
3395
3266
|
return svc;
|
|
3396
3267
|
};
|
|
3397
3268
|
|
|
3398
|
-
app.openapi(
|
|
3399
|
-
const svc =
|
|
3269
|
+
app.openapi(githubSyncPreviewRoute, async (c) => {
|
|
3270
|
+
const svc = syncService(c);
|
|
3400
3271
|
const { id: agentId } = c.req.valid("param");
|
|
3401
3272
|
const result = await svc.preview(agentId);
|
|
3402
3273
|
if (result.status === "agent_not_found") {
|
|
@@ -3409,9 +3280,9 @@ export function createAdminApp(deps: AdminDeps): OpenAPIHono<AdminAuthEnv> {
|
|
|
3409
3280
|
return c.json(
|
|
3410
3281
|
{
|
|
3411
3282
|
status: "previewed" as const,
|
|
3412
|
-
|
|
3413
|
-
|
|
3414
|
-
|
|
3283
|
+
appAdds: plan.appAdds,
|
|
3284
|
+
appRemoves: plan.appRemoves,
|
|
3285
|
+
agentRemoves: plan.agentRemoves,
|
|
3415
3286
|
skippedOrgs: plan.skippedOrgs,
|
|
3416
3287
|
openWork: result.openWork,
|
|
3417
3288
|
warnings: result.warnings,
|
|
@@ -3420,15 +3291,17 @@ export function createAdminApp(deps: AdminDeps): OpenAPIHono<AdminAuthEnv> {
|
|
|
3420
3291
|
);
|
|
3421
3292
|
});
|
|
3422
3293
|
|
|
3423
|
-
app.openapi(
|
|
3424
|
-
const svc =
|
|
3294
|
+
app.openapi(githubSyncApplyRoute, async (c) => {
|
|
3295
|
+
const svc = syncService(c);
|
|
3425
3296
|
const { id: agentId } = c.req.valid("param");
|
|
3426
3297
|
const body = c.req.valid("json");
|
|
3427
3298
|
const caller = c.get("caller")?.name ?? "anonymous";
|
|
3428
|
-
const result = await svc.
|
|
3299
|
+
const result = await svc.apply({
|
|
3429
3300
|
agentId,
|
|
3430
3301
|
caller,
|
|
3431
|
-
|
|
3302
|
+
appAdds: body.appAdds,
|
|
3303
|
+
appRemoves: body.appRemoves,
|
|
3304
|
+
agentRemoves: body.agentRemoves,
|
|
3432
3305
|
confirmRemovals: body.confirmRemovals,
|
|
3433
3306
|
});
|
|
3434
3307
|
switch (result.status) {
|
|
@@ -3445,27 +3318,27 @@ export function createAdminApp(deps: AdminDeps): OpenAPIHono<AdminAuthEnv> {
|
|
|
3445
3318
|
);
|
|
3446
3319
|
case "failed":
|
|
3447
3320
|
console.log(
|
|
3448
|
-
`[agents-api] github
|
|
3321
|
+
`[agents-api] github sync failed: agent=${agentId} audit=${result.auditId} caller=${caller}`,
|
|
3449
3322
|
);
|
|
3450
3323
|
throw new BadGatewayError(
|
|
3451
|
-
`GitHub refused the
|
|
3324
|
+
`GitHub refused the sync; audit ${result.auditId} records the failure: ${result.error}`,
|
|
3452
3325
|
);
|
|
3453
3326
|
case "noop":
|
|
3454
3327
|
case "applied": {
|
|
3455
3328
|
const { plan } = result;
|
|
3456
3329
|
if (result.status === "applied") {
|
|
3457
3330
|
console.log(
|
|
3458
|
-
`[agents-api] github
|
|
3331
|
+
`[agents-api] github sync applied: agent=${agentId} audit=${result.auditId} caller=${caller}`,
|
|
3459
3332
|
);
|
|
3460
3333
|
}
|
|
3461
3334
|
return c.json(
|
|
3462
3335
|
{
|
|
3463
3336
|
status: result.status,
|
|
3464
3337
|
...(result.status === "applied" ? { auditId: result.auditId } : {}),
|
|
3465
|
-
|
|
3466
|
-
|
|
3467
|
-
|
|
3468
|
-
|
|
3338
|
+
appAdds: plan.appAdds,
|
|
3339
|
+
appRemoves: plan.appRemoves,
|
|
3340
|
+
agentRemoves: plan.agentRemoves,
|
|
3341
|
+
notApplied: plan.notApplied,
|
|
3469
3342
|
skippedOrgs: plan.skippedOrgs,
|
|
3470
3343
|
warnings:
|
|
3471
3344
|
result.status === "applied"
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* admin/src/github-installation-wiring.ts
|
|
3
|
-
* Composition of the installation repos and
|
|
3
|
+
* Composition of the installation repos list and GitHub Sync services for
|
|
4
4
|
* admin boot, from the stored installer App and an optional host-supplied
|
|
5
5
|
* OpenWorkPort. The agent App id comes from each agent's own stored
|
|
6
6
|
* GH_APP_ID; without an open-work port nothing is listed or blocked.
|
|
@@ -23,7 +23,7 @@ import {
|
|
|
23
23
|
InstallationReposService,
|
|
24
24
|
type InstallationReposPort,
|
|
25
25
|
} from "./installation-repos-service.ts";
|
|
26
|
-
import {
|
|
26
|
+
import { GithubSyncService } from "./github-sync.ts";
|
|
27
27
|
import type { OpenWorkPort } from "./reconcile-open-work.ts";
|
|
28
28
|
|
|
29
29
|
export interface GithubInstallationWiringDeps {
|
|
@@ -85,8 +85,7 @@ export interface InstallationReposWiringDeps
|
|
|
85
85
|
|
|
86
86
|
/**
|
|
87
87
|
* The `githubInstallationRepos` dep for createAdminApp (IRM-1.2): direct
|
|
88
|
-
* list
|
|
89
|
-
* GitHubReconcileAudit table.
|
|
88
|
+
* list the repos on the agent App's installation.
|
|
90
89
|
*/
|
|
91
90
|
export function createGithubInstallationRepos(
|
|
92
91
|
deps: InstallationReposWiringDeps,
|
|
@@ -98,21 +97,20 @@ export function createGithubInstallationRepos(
|
|
|
98
97
|
installation:
|
|
99
98
|
deps.installationRepos ??
|
|
100
99
|
createInstallationReposPort(createInstallationPort(client), client),
|
|
101
|
-
audit: new PrismaInstallationAudit(deps.prisma),
|
|
102
100
|
});
|
|
103
101
|
}
|
|
104
102
|
|
|
105
103
|
/**
|
|
106
|
-
* The `
|
|
107
|
-
* repos the bound user can
|
|
108
|
-
*
|
|
109
|
-
*
|
|
104
|
+
* The `githubSync` dep for createAdminApp (IRM-3.1): installation adds and
|
|
105
|
+
* removes plus agent-repo removals for the repos the bound user can or can no
|
|
106
|
+
* longer push to, audited once and blocking open work through the optional
|
|
107
|
+
* host OpenWorkPort.
|
|
110
108
|
*/
|
|
111
|
-
export function
|
|
109
|
+
export function createGithubSync(
|
|
112
110
|
deps: InstallationReposWiringDeps,
|
|
113
|
-
):
|
|
111
|
+
): GithubSyncService {
|
|
114
112
|
const client = lazyInstallerClient(deps);
|
|
115
|
-
return new
|
|
113
|
+
return new GithubSyncService({
|
|
116
114
|
agents: new PrismaInstallationAgents(deps.prisma, deps.agentEnv),
|
|
117
115
|
access: deps.access,
|
|
118
116
|
installation:
|
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
} from "./github-installation.fixtures.ts";
|
|
8
8
|
import {
|
|
9
9
|
createGithubInstallationRepos,
|
|
10
|
-
|
|
10
|
+
createGithubSync,
|
|
11
11
|
} from "./github-installation-wiring.ts";
|
|
12
12
|
import type { InstallationReposPort } from "./installation-repos-service.ts";
|
|
13
13
|
|
|
@@ -67,10 +67,10 @@ describe("createGithubInstallationRepos", () => {
|
|
|
67
67
|
});
|
|
68
68
|
});
|
|
69
69
|
|
|
70
|
-
describe("
|
|
70
|
+
describe("createGithubSync", () => {
|
|
71
71
|
const build = (openWork?: ReturnType<typeof fakeOpenWork>["port"]) => {
|
|
72
72
|
const { prisma, state } = fakePrisma(["acme/old", "acme/api"]);
|
|
73
|
-
const svc =
|
|
73
|
+
const svc = createGithubSync({
|
|
74
74
|
prisma,
|
|
75
75
|
appService: noApp,
|
|
76
76
|
access: accessFor(["acme/api"]),
|
|
@@ -81,15 +81,17 @@ describe("createGithubLostAccessRemoval", () => {
|
|
|
81
81
|
return { svc, state };
|
|
82
82
|
};
|
|
83
83
|
|
|
84
|
-
it("with a port: preview lists affected open work and
|
|
84
|
+
it("with a port: preview lists affected open work and apply blocks it", async () => {
|
|
85
85
|
const { port, calls } = fakeOpenWork();
|
|
86
86
|
const { svc, state } = build(port);
|
|
87
87
|
const preview = await svc.preview("a1");
|
|
88
88
|
expect(preview.status).toBe("previewed");
|
|
89
|
-
const res = await svc.
|
|
89
|
+
const res = await svc.apply({
|
|
90
90
|
agentId: "a1",
|
|
91
91
|
caller: "root",
|
|
92
|
-
|
|
92
|
+
appAdds: [],
|
|
93
|
+
appRemoves: ["acme/old"],
|
|
94
|
+
agentRemoves: ["acme/old"],
|
|
93
95
|
});
|
|
94
96
|
expect(res.status).toBe("applied");
|
|
95
97
|
expect(state.repos).toEqual(["acme/api"]);
|
|
@@ -98,12 +100,14 @@ describe("createGithubLostAccessRemoval", () => {
|
|
|
98
100
|
expect(block?.args[1]).toEqual(["acme/old"]);
|
|
99
101
|
});
|
|
100
102
|
|
|
101
|
-
it("without a port: nothing is blocked,
|
|
103
|
+
it("without a port: nothing is blocked, apply still works", async () => {
|
|
102
104
|
const { svc, state } = build();
|
|
103
|
-
const res = await svc.
|
|
105
|
+
const res = await svc.apply({
|
|
104
106
|
agentId: "a1",
|
|
105
107
|
caller: "root",
|
|
106
|
-
|
|
108
|
+
appAdds: [],
|
|
109
|
+
appRemoves: [],
|
|
110
|
+
agentRemoves: ["acme/old"],
|
|
107
111
|
});
|
|
108
112
|
expect(res.status).toBe("applied");
|
|
109
113
|
expect(state.repos).toEqual(["acme/api"]);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* admin/src/github-installation.fixtures.ts
|
|
3
|
-
* Shared fakes for the installation repos /
|
|
3
|
+
* Shared fakes for the installation repos / GitHub Sync wiring tests.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import type { PrismaClient } from "../prisma/client/client.ts";
|