@app-vitals/agent-admin 1.88.0 → 1.90.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.json +253 -0
- package/openapi.platform.json +297 -0
- package/package.json +2 -2
- package/src/admin-boot.ts +11 -0
- package/src/admin-ui-installation-repos-pages.ts +25 -4
- package/src/admin-ui-installation-repos-pages.unit.test.ts +41 -0
- package/src/admin-ui-installation-repos.smoke.test.ts +14 -0
- package/src/agents-api.smoke.test.ts +88 -0
- package/src/agents-api.ts +15 -0
- package/src/fixtures/github-enterprise-cassette.json +16 -0
- package/src/github-enterprise-api.integration.test.ts +127 -0
- package/src/github-enterprise-api.smoke.test.ts +188 -5
- package/src/github-enterprise-api.ts +212 -4
- package/src/github-enterprise-client.ts +23 -1
- package/src/github-enterprise-client.unit.test.ts +65 -0
- package/src/github-installation-wiring.ts +5 -0
- package/src/github-sync-switch.integration.test.ts +297 -0
- package/src/github-sync-switch.unit.test.ts +467 -0
- package/src/github-sync.ts +205 -24
- package/src/github-uninstall.ts +70 -0
- package/src/github-uninstall.unit.test.ts +78 -0
- package/src/installation-ports.ts +41 -0
|
@@ -208,6 +208,7 @@ export interface GithubSyncPreviewView {
|
|
|
208
208
|
appAdds?: string[];
|
|
209
209
|
appRemoves?: string[];
|
|
210
210
|
agentRemoves?: string[];
|
|
211
|
+
selectionSwitches?: { org: string; repos: string[] }[];
|
|
211
212
|
skippedOrgs?: { org: string; reason: string; detail?: SkipDetail }[];
|
|
212
213
|
openWork?: OpenWorkView[];
|
|
213
214
|
warnings?: string[];
|
|
@@ -465,6 +466,19 @@ const repoList = (kind: string, title: string, repos: readonly string[]) =>
|
|
|
465
466
|
const plural = (n: number, one: string, many: string) =>
|
|
466
467
|
`${n} ${n === 1 ? one : many}`;
|
|
467
468
|
|
|
469
|
+
/** Orgs the confirmed sync switches from "all repositories" to "selected repositories". */
|
|
470
|
+
const selectionSwitchesHtml = (
|
|
471
|
+
switches: readonly { org: string; repos: readonly string[] }[],
|
|
472
|
+
) =>
|
|
473
|
+
switches.length === 0
|
|
474
|
+
? ""
|
|
475
|
+
: `<div data-list="selection-switches" style="margin-top:8px"><div style="font-weight:600">Switch from all repositories to selected repositories (${switches.length})</div><ul style="margin:4px 0 0 18px">${switches
|
|
476
|
+
.map(
|
|
477
|
+
(s) =>
|
|
478
|
+
`<li data-switch-org="${escapeHtml(s.org)}"><span class="mono">${escapeHtml(s.org)}</span>: keeps ${plural(s.repos.length, "repo", "repos")}</li>`,
|
|
479
|
+
)
|
|
480
|
+
.join("")}</ul></div>`;
|
|
481
|
+
|
|
468
482
|
/** One readable sentence-list describing why an org's cached access was skipped. */
|
|
469
483
|
function skipDetailText(d: SkipDetail, timezone?: string): string {
|
|
470
484
|
const out: string[] = [];
|
|
@@ -528,8 +542,8 @@ const skippedOrgsHtml = (
|
|
|
528
542
|
.join("")}</div>`;
|
|
529
543
|
|
|
530
544
|
/**
|
|
531
|
-
* The single confirm step of Sync: the installation adds,
|
|
532
|
-
* removes, agent-repo removes and the open work the removals would block. One
|
|
545
|
+
* The single confirm step of Sync: the selection switches, installation adds,
|
|
546
|
+
* installation removes, agent-repo removes and the open work the removals would block. One
|
|
533
547
|
* confirmation applies them all; more than the removal cap needs the extra
|
|
534
548
|
* checkbox (confirmRemovals).
|
|
535
549
|
*/
|
|
@@ -544,12 +558,19 @@ export function renderSyncConfirm(
|
|
|
544
558
|
const appAdds = preview.appAdds ?? [];
|
|
545
559
|
const appRemoves = preview.appRemoves ?? [];
|
|
546
560
|
const agentRemoves = preview.agentRemoves ?? [];
|
|
561
|
+
const switches = preview.selectionSwitches ?? [];
|
|
547
562
|
const skippedList = preview.skippedOrgs ?? [];
|
|
548
563
|
const skipped = skippedOrgsHtml(skippedList, timezone);
|
|
549
564
|
const warnings = warningsHtml(
|
|
550
565
|
withoutSkipWarnings(preview.warnings, skippedList),
|
|
551
566
|
);
|
|
552
|
-
if (
|
|
567
|
+
if (
|
|
568
|
+
appAdds.length +
|
|
569
|
+
appRemoves.length +
|
|
570
|
+
agentRemoves.length +
|
|
571
|
+
switches.length ===
|
|
572
|
+
0
|
|
573
|
+
) {
|
|
553
574
|
if (skippedList.length > 0) {
|
|
554
575
|
return `<div class="alert alert-warning" data-sync="nothing-synced">Nothing was synced: ${skippedList.length} org(s) skipped, see below.</div>${skipped}${warnings}`;
|
|
555
576
|
}
|
|
@@ -564,7 +585,7 @@ export function renderSyncConfirm(
|
|
|
564
585
|
const capBox = overCap
|
|
565
586
|
? `<label style="display:block;margin:12px 0"><input type="checkbox" id="github-sync-confirm-removals" /> I confirm removing ${removals} repos (more than ${MAX_UNCONFIRMED_REMOVALS}).</label>`
|
|
566
587
|
: "";
|
|
567
|
-
return `<div class="alert alert-warning" data-sync="confirm">Apply these changes?</div>${repoList("app-adds", "Add to the agent App installation", appAdds)}${repoList("app-removes", "Remove from the agent App installation", appRemoves)}${repoList("agent-removes", "Remove from the agent's repos", agentRemoves)}${renderOpenWorkByRepo(
|
|
588
|
+
return `<div class="alert alert-warning" data-sync="confirm">Apply these changes?</div>${selectionSwitchesHtml(switches)}${repoList("app-adds", "Add to the agent App installation", appAdds)}${repoList("app-removes", "Remove from the agent App installation", appRemoves)}${repoList("agent-removes", "Remove from the agent's repos", agentRemoves)}${renderOpenWorkByRepo(
|
|
568
589
|
preview.openWork ?? [],
|
|
569
590
|
"Open tasks and PRs that will be blocked",
|
|
570
591
|
"No open tasks or PRs would be blocked.",
|
|
@@ -187,6 +187,47 @@ describe("renderSyncConfirm", () => {
|
|
|
187
187
|
});
|
|
188
188
|
});
|
|
189
189
|
|
|
190
|
+
it("lists each selection switch with org and kept repo count in the one confirm", () => {
|
|
191
|
+
const html = renderSyncConfirm("a1", {
|
|
192
|
+
...preview,
|
|
193
|
+
selectionSwitches: [
|
|
194
|
+
{ org: "acme", repos: ["acme/a", "acme/b"] },
|
|
195
|
+
{ org: "solo<x>", repos: ["solo/a"] },
|
|
196
|
+
],
|
|
197
|
+
});
|
|
198
|
+
expect(html).toContain('data-list="selection-switches"');
|
|
199
|
+
expect(html).toContain("all repositories to selected repositories (2)");
|
|
200
|
+
expect(html).toContain('data-switch-org="acme"');
|
|
201
|
+
expect(html).toContain("keeps 2 repos");
|
|
202
|
+
expect(html).toContain("solo<x>");
|
|
203
|
+
expect(html).toContain("keeps 1 repo<");
|
|
204
|
+
expect(html.match(/id="github-sync-confirm-btn"/g)?.length).toBe(1);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it("confirms a switch-only plan instead of saying in sync", () => {
|
|
208
|
+
const html = renderSyncConfirm("a1", {
|
|
209
|
+
status: "previewed",
|
|
210
|
+
selectionSwitches: [{ org: "acme", repos: ["acme/a"] }],
|
|
211
|
+
});
|
|
212
|
+
expect(html).toContain('data-sync="confirm"');
|
|
213
|
+
expect(html).not.toContain("in-sync");
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it("shows the empty-target skip warning with the Uninstall hint", () => {
|
|
217
|
+
const html = renderSyncConfirm("a1", {
|
|
218
|
+
...preview,
|
|
219
|
+
skippedOrgs: [
|
|
220
|
+
{
|
|
221
|
+
org: "empty-org",
|
|
222
|
+
reason:
|
|
223
|
+
"the bound user can push to none of this org's repos, so it stays on All repositories; use Uninstall to remove the agent App from this org",
|
|
224
|
+
},
|
|
225
|
+
],
|
|
226
|
+
});
|
|
227
|
+
expect(html).toContain("empty-org");
|
|
228
|
+
expect(html).toContain("use Uninstall to remove the agent App");
|
|
229
|
+
});
|
|
230
|
+
|
|
190
231
|
it("requires the over-cap checkbox for more than 10 removals", () => {
|
|
191
232
|
const many = Array.from({ length: 11 }, (_, i) => `acme/r${i}`);
|
|
192
233
|
const html = renderSyncConfirm("a1", {
|
|
@@ -397,6 +397,20 @@ describe("admin UI — GitHub sync card (IRM-3.2)", () => {
|
|
|
397
397
|
expect(html).toContain("30 repos have no access answer yet.");
|
|
398
398
|
});
|
|
399
399
|
|
|
400
|
+
it("renders a selection switch row in the Sync confirm fragment", async () => {
|
|
401
|
+
const { app } = setup({
|
|
402
|
+
agentsApi: () =>
|
|
403
|
+
json({
|
|
404
|
+
status: "previewed",
|
|
405
|
+
selectionSwitches: [{ org: "acme", repos: ["acme/a", "acme/b"] }],
|
|
406
|
+
}),
|
|
407
|
+
});
|
|
408
|
+
const html = await (await get(app, "/github-sync/confirm")).text();
|
|
409
|
+
expect(html).toContain('data-list="selection-switches"');
|
|
410
|
+
expect(html).toContain('data-switch-org="acme"');
|
|
411
|
+
expect(html).toContain("keeps 2 repos");
|
|
412
|
+
});
|
|
413
|
+
|
|
400
414
|
it("shows the reason for a skipped preview", async () => {
|
|
401
415
|
const { app } = setup({
|
|
402
416
|
agentsApi: () => json({ status: "skipped", reason: "user not found" }),
|
|
@@ -7418,6 +7418,7 @@ describe("admin API — agent App installation repos and GitHub Sync (IRM-1.2, I
|
|
|
7418
7418
|
"installable_orgs_success",
|
|
7419
7419
|
[`GET ${INST}`]: "org_installations_success",
|
|
7420
7420
|
[`GET ${INST}/501/repositories`]: "install_repos_success",
|
|
7421
|
+
[`PATCH ${INST}/501/repositories`]: "install_selection_success",
|
|
7421
7422
|
[`PATCH ${INST}/501/repositories/add`]: "install_repos_add_success",
|
|
7422
7423
|
[`PATCH ${INST}/501/repositories/remove`]: "install_repos_remove_success",
|
|
7423
7424
|
};
|
|
@@ -7475,6 +7476,24 @@ describe("admin API — agent App installation repos and GitHub Sync (IRM-1.2, I
|
|
|
7475
7476
|
{ status: 200, headers: { "Content-Type": "application/json" } },
|
|
7476
7477
|
);
|
|
7477
7478
|
}
|
|
7479
|
+
if (key === `PATCH ${INST}/501/repositories`) {
|
|
7480
|
+
// GIS-1.2: the "all" to "selected" switch (installation repos become the list).
|
|
7481
|
+
if (entry.status === 200) {
|
|
7482
|
+
const names =
|
|
7483
|
+
(
|
|
7484
|
+
JSON.parse(String(init?.body ?? "{}")) as {
|
|
7485
|
+
repositories?: string[];
|
|
7486
|
+
}
|
|
7487
|
+
).repositories ?? [];
|
|
7488
|
+
patches.push({ key, names });
|
|
7489
|
+
events.push(["switch", names]);
|
|
7490
|
+
state.installed = names.map((n) => `acme-eng/${n}`);
|
|
7491
|
+
}
|
|
7492
|
+
return new Response(JSON.stringify(entry.body), {
|
|
7493
|
+
status: entry.status,
|
|
7494
|
+
headers: { "Content-Type": "application/json" },
|
|
7495
|
+
});
|
|
7496
|
+
}
|
|
7478
7497
|
if (key.startsWith("PATCH") && entry.status === 200) {
|
|
7479
7498
|
const body = JSON.parse(String(init?.body ?? "{}")) as {
|
|
7480
7499
|
repositories?: string[];
|
|
@@ -8117,6 +8136,75 @@ describe("admin API — agent App installation repos and GitHub Sync (IRM-1.2, I
|
|
|
8117
8136
|
expect(state.repos).toEqual([]);
|
|
8118
8137
|
});
|
|
8119
8138
|
|
|
8139
|
+
// GIS-1.2: an agent App installed on All repositories is switched to Selected.
|
|
8140
|
+
const allSnapshot = () => [
|
|
8141
|
+
{
|
|
8142
|
+
org: "acme-eng",
|
|
8143
|
+
completedAt: new Date(),
|
|
8144
|
+
totalCount: 2,
|
|
8145
|
+
lastError: null,
|
|
8146
|
+
rateLimitResetAt: null,
|
|
8147
|
+
repositorySelection: "all",
|
|
8148
|
+
repos: [{ fullName: "acme-eng/repo-1" }, { fullName: "acme-eng/repo-2" }],
|
|
8149
|
+
},
|
|
8150
|
+
];
|
|
8151
|
+
|
|
8152
|
+
it("sync preview lists the selection switch for an 'all' installation", async () => {
|
|
8153
|
+
const { app, events } = setup({ snapshots: allSnapshot() });
|
|
8154
|
+
const res = await app.request(previewUrl, { headers: asAdmin });
|
|
8155
|
+
expect(res.status).toBe(200);
|
|
8156
|
+
const body = (await res.json()) as {
|
|
8157
|
+
selectionSwitches: unknown[];
|
|
8158
|
+
appRemoves: string[];
|
|
8159
|
+
};
|
|
8160
|
+
expect(body.selectionSwitches).toEqual([
|
|
8161
|
+
{ org: "acme-eng", repos: ["acme-eng/repo-1"] },
|
|
8162
|
+
]);
|
|
8163
|
+
expect(body.appRemoves).toEqual(["acme-eng/repo-2"]);
|
|
8164
|
+
// Read-only: only the open-work listing, no GitHub change.
|
|
8165
|
+
expect(events.map((e) => e[0])).toEqual(["list"]);
|
|
8166
|
+
});
|
|
8167
|
+
|
|
8168
|
+
it("sync apply switches before removing, folds the removal and audits once", async () => {
|
|
8169
|
+
const { app, audits, events } = setup({ snapshots: allSnapshot() });
|
|
8170
|
+
const res = await apply(app, {
|
|
8171
|
+
appRemoves: ["acme-eng/repo-2"],
|
|
8172
|
+
agentRemoves: ["acme-eng/repo-2"],
|
|
8173
|
+
});
|
|
8174
|
+
expect(res.status).toBe(200);
|
|
8175
|
+
expect(await res.json()).toMatchObject({
|
|
8176
|
+
status: "applied",
|
|
8177
|
+
appRemoves: ["acme-eng/repo-2"],
|
|
8178
|
+
selectionSwitches: [{ org: "acme-eng", repos: ["acme-eng/repo-1"] }],
|
|
8179
|
+
});
|
|
8180
|
+
expect(events.map((e) => e[0])).toEqual([
|
|
8181
|
+
"switch",
|
|
8182
|
+
"setRepos",
|
|
8183
|
+
"audit",
|
|
8184
|
+
"block",
|
|
8185
|
+
]);
|
|
8186
|
+
expect(events[0]).toEqual(["switch", ["repo-1"]]);
|
|
8187
|
+
expect(audits[0]?.diff).toMatchObject({
|
|
8188
|
+
installationRemoves: ["acme-eng/repo-2"],
|
|
8189
|
+
selectionSwitches: [{ org: "acme-eng", repos: ["acme-eng/repo-1"] }],
|
|
8190
|
+
});
|
|
8191
|
+
});
|
|
8192
|
+
|
|
8193
|
+
it("sync apply: a 403 on the switch is a 502 naming the missing permission, with a failed audit", async () => {
|
|
8194
|
+
const { app, audits } = setup({
|
|
8195
|
+
snapshots: allSnapshot(),
|
|
8196
|
+
override: { [`PATCH ${INST}/501/repositories`]: "install_selection_403" },
|
|
8197
|
+
});
|
|
8198
|
+
const res = await apply(app, { appRemoves: ["acme-eng/repo-2"] });
|
|
8199
|
+
expect(res.status).toBe(502);
|
|
8200
|
+
expect(JSON.stringify(await res.json())).toContain(
|
|
8201
|
+
"enterprise_organization_installation_repositories",
|
|
8202
|
+
);
|
|
8203
|
+
expect(audits).toHaveLength(1);
|
|
8204
|
+
expect(audits[0]).toMatchObject({ result: { status: "failed" } });
|
|
8205
|
+
expect(audits[0]?.diff).toMatchObject({ installationRemoves: [] });
|
|
8206
|
+
});
|
|
8207
|
+
|
|
8120
8208
|
it("sync apply: noop when nothing confirmed still applies; 400 for bad names", async () => {
|
|
8121
8209
|
const { app, audits } = setup();
|
|
8122
8210
|
const res = await apply(app, { appRemoves: ["acme-eng/repo-1"] });
|
package/src/agents-api.ts
CHANGED
|
@@ -766,6 +766,13 @@ const SkippedOrgsSchema = z.array(
|
|
|
766
766
|
}),
|
|
767
767
|
);
|
|
768
768
|
|
|
769
|
+
const SelectionSwitchesSchema = z
|
|
770
|
+
.array(z.object({ org: z.string(), repos: z.array(z.string()) }))
|
|
771
|
+
.openapi({
|
|
772
|
+
description:
|
|
773
|
+
"Orgs whose agent App installation is on All repositories. Sync switches each to Selected repositories (first 50 repos in the switch, the rest added in batches) before any add or remove; `repos` is the resulting list: the installed repos minus the org's confirmed removals. An org the bound user can push to nothing in is skipped (see `skippedOrgs`) and stays on All repositories.",
|
|
774
|
+
});
|
|
775
|
+
|
|
769
776
|
const GithubSyncPreviewSchema = z
|
|
770
777
|
.object({
|
|
771
778
|
status: z.enum(["previewed", "skipped"]).openapi({
|
|
@@ -785,6 +792,7 @@ const GithubSyncPreviewSchema = z
|
|
|
785
792
|
description:
|
|
786
793
|
"Configured agent repos the bound user can no longer push to. The agent's repos are never added to.",
|
|
787
794
|
}),
|
|
795
|
+
selectionSwitches: SelectionSwitchesSchema.optional(),
|
|
788
796
|
skippedOrgs: SkippedOrgsSchema.optional().openapi({
|
|
789
797
|
description:
|
|
790
798
|
"Orgs whose access listing is missing, partial or failed, whose cached access data is incomplete or stale, or whose lookup App is not installed or installed on selected repositories only; nothing there is added or removed. `reason` says why (for stale data, when it was checked).",
|
|
@@ -827,6 +835,7 @@ const GithubSyncApplyResultSchema = z
|
|
|
827
835
|
appAdds: z.array(z.string()).optional(),
|
|
828
836
|
appRemoves: z.array(z.string()).optional(),
|
|
829
837
|
agentRemoves: z.array(z.string()).optional(),
|
|
838
|
+
selectionSwitches: SelectionSwitchesSchema.optional(),
|
|
830
839
|
notApplied: z
|
|
831
840
|
.array(z.object({ repo: z.string(), reason: z.string() }))
|
|
832
841
|
.optional()
|
|
@@ -3589,6 +3598,9 @@ export function createAdminApp(deps: AdminDeps): OpenAPIHono<AdminAuthEnv> {
|
|
|
3589
3598
|
appAdds: plan.appAdds,
|
|
3590
3599
|
appRemoves: plan.appRemoves,
|
|
3591
3600
|
agentRemoves: plan.agentRemoves,
|
|
3601
|
+
...(plan.selectionSwitches?.length
|
|
3602
|
+
? { selectionSwitches: plan.selectionSwitches }
|
|
3603
|
+
: {}),
|
|
3592
3604
|
skippedOrgs: plan.skippedOrgs,
|
|
3593
3605
|
openWork: result.openWork,
|
|
3594
3606
|
warnings: result.warnings,
|
|
@@ -3648,6 +3660,9 @@ export function createAdminApp(deps: AdminDeps): OpenAPIHono<AdminAuthEnv> {
|
|
|
3648
3660
|
appAdds: plan.appAdds,
|
|
3649
3661
|
appRemoves: plan.appRemoves,
|
|
3650
3662
|
agentRemoves: plan.agentRemoves,
|
|
3663
|
+
...(plan.selectionSwitches?.length
|
|
3664
|
+
? { selectionSwitches: plan.selectionSwitches }
|
|
3665
|
+
: {}),
|
|
3651
3666
|
notApplied: plan.notApplied,
|
|
3652
3667
|
skippedOrgs: plan.skippedOrgs,
|
|
3653
3668
|
warnings:
|
|
@@ -1587,5 +1587,21 @@
|
|
|
1587
1587
|
"body": {
|
|
1588
1588
|
"message": "Service Unavailable"
|
|
1589
1589
|
}
|
|
1590
|
+
},
|
|
1591
|
+
"uninstall_success": {
|
|
1592
|
+
"status": 204,
|
|
1593
|
+
"body": null
|
|
1594
|
+
},
|
|
1595
|
+
"uninstall_403": {
|
|
1596
|
+
"status": 403,
|
|
1597
|
+
"body": {
|
|
1598
|
+
"message": "Resource not accessible by integration"
|
|
1599
|
+
}
|
|
1600
|
+
},
|
|
1601
|
+
"uninstall_404": {
|
|
1602
|
+
"status": 404,
|
|
1603
|
+
"body": {
|
|
1604
|
+
"message": "Not Found"
|
|
1605
|
+
}
|
|
1590
1606
|
}
|
|
1591
1607
|
}
|
|
@@ -23,6 +23,10 @@ import { type AdminUIEnv } from "./admin-ui.ts";
|
|
|
23
23
|
import { registerAdminGithubEnterpriseRoutes } from "./admin-ui-github-enterprise.ts";
|
|
24
24
|
import { createGithubEnterpriseApp } from "./github-enterprise-api.ts";
|
|
25
25
|
import { GitHubEnterpriseAppService } from "./github-enterprise-apps.ts";
|
|
26
|
+
import {
|
|
27
|
+
PrismaInstallationAgents,
|
|
28
|
+
PrismaInstallationAudit,
|
|
29
|
+
} from "./installation-ports.ts";
|
|
26
30
|
import { createAdminPrismaClient } from "./prisma-client.ts";
|
|
27
31
|
import { identityCrypto, makeStrictTokenCrypto } from "./token-crypto.ts";
|
|
28
32
|
|
|
@@ -354,3 +358,126 @@ describeOrSkip("github-enterprise API (integration)", () => {
|
|
|
354
358
|
expect(denied.status).toBe(403);
|
|
355
359
|
});
|
|
356
360
|
});
|
|
361
|
+
|
|
362
|
+
describeOrSkip("DELETE /orgs/{org}/install (integration)", () => {
|
|
363
|
+
let prisma: PrismaClient;
|
|
364
|
+
|
|
365
|
+
beforeEach(async () => {
|
|
366
|
+
prisma = createAdminPrismaClient(TEST_DB as string);
|
|
367
|
+
await prisma.gitHubReconcileAudit.deleteMany();
|
|
368
|
+
await prisma.gitHubEnterpriseApp.deleteMany();
|
|
369
|
+
await prisma.agentEnv.deleteMany();
|
|
370
|
+
await prisma.agentToken.deleteMany();
|
|
371
|
+
await prisma.agentCronJob.deleteMany();
|
|
372
|
+
await prisma.agentTool.deleteMany();
|
|
373
|
+
await prisma.agent.deleteMany();
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
afterEach(async () => {
|
|
377
|
+
await prisma.$disconnect();
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
async function setup(deleteStatus: number) {
|
|
381
|
+
const appService = new GitHubEnterpriseAppService(
|
|
382
|
+
prisma,
|
|
383
|
+
makeStrictTokenCrypto(ENC_KEY),
|
|
384
|
+
);
|
|
385
|
+
await appService.save({
|
|
386
|
+
role: "installer",
|
|
387
|
+
appId: "12345",
|
|
388
|
+
slug: "example-installer",
|
|
389
|
+
enterpriseSlug: "acme",
|
|
390
|
+
privateKey: PEM,
|
|
391
|
+
});
|
|
392
|
+
const envService = new AgentEnvService(prisma, identityCrypto);
|
|
393
|
+
const agent = await prisma.agent.create({
|
|
394
|
+
data: {
|
|
395
|
+
name: "Uninstall Agent",
|
|
396
|
+
repos: ["acme-eng/a", "acme-eng/b", "other/c"],
|
|
397
|
+
},
|
|
398
|
+
});
|
|
399
|
+
await envService.upsert(agent.id, { GH_APP_SLUG: "target-app" });
|
|
400
|
+
const fetchFn = (async (input: RequestInfo | URL, init?: RequestInit) => {
|
|
401
|
+
const u = new URL(input.toString());
|
|
402
|
+
const key = `${init?.method ?? "GET"} ${u.pathname}`;
|
|
403
|
+
const json = (k: string) =>
|
|
404
|
+
new Response(JSON.stringify(cassette[k]?.body), {
|
|
405
|
+
status: cassette[k]?.status,
|
|
406
|
+
});
|
|
407
|
+
switch (key) {
|
|
408
|
+
case "GET /enterprises/acme/installation":
|
|
409
|
+
return json("enterprise_installation_success");
|
|
410
|
+
case "POST /app/installations/4242/access_tokens":
|
|
411
|
+
return json("access_token_success");
|
|
412
|
+
case "GET /enterprises/acme/apps/organizations/acme-eng/installations":
|
|
413
|
+
return json("org_installations_success");
|
|
414
|
+
case "DELETE /enterprises/acme/apps/organizations/acme-eng/installations/501":
|
|
415
|
+
return deleteStatus === 204
|
|
416
|
+
? new Response(null, { status: 204 })
|
|
417
|
+
: json("uninstall_403");
|
|
418
|
+
}
|
|
419
|
+
throw new Error(`no route for ${key}`);
|
|
420
|
+
}) as typeof fetch;
|
|
421
|
+
const blocked: string[] = [];
|
|
422
|
+
const app = createGithubEnterpriseApp({
|
|
423
|
+
appService,
|
|
424
|
+
agentService: new AgentService(prisma),
|
|
425
|
+
agentEnvService: envService,
|
|
426
|
+
sessionSecret: "test-secret-32chars-for-ghe-api!!",
|
|
427
|
+
adminApiKeys: parseAdminApiKeys("root:tok-admin:*"),
|
|
428
|
+
agentTokenService: { validate: async () => null },
|
|
429
|
+
uninstall: {
|
|
430
|
+
agents: new PrismaInstallationAgents(prisma, envService),
|
|
431
|
+
audit: new PrismaInstallationAudit(prisma),
|
|
432
|
+
openWork: {
|
|
433
|
+
listOpenWork: async () => [],
|
|
434
|
+
blockOpenWork: async (_a, repos) => {
|
|
435
|
+
blocked.push(...repos);
|
|
436
|
+
},
|
|
437
|
+
},
|
|
438
|
+
},
|
|
439
|
+
fetchFn,
|
|
440
|
+
apiBase: "https://ghe.test",
|
|
441
|
+
});
|
|
442
|
+
const del = () =>
|
|
443
|
+
app.request(
|
|
444
|
+
`/github-enterprise/orgs/acme-eng/install?agentId=${agent.id}`,
|
|
445
|
+
{ method: "DELETE", headers: ADMIN },
|
|
446
|
+
);
|
|
447
|
+
return { agent, del, blocked };
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
it("updates the agent's repos and writes an applied audit row", async () => {
|
|
451
|
+
const { agent, del, blocked } = await setup(204);
|
|
452
|
+
const res = await del();
|
|
453
|
+
expect(res.status).toBe(200);
|
|
454
|
+
const row = await prisma.agent.findUniqueOrThrow({
|
|
455
|
+
where: { id: agent.id },
|
|
456
|
+
});
|
|
457
|
+
expect(row.repos).toEqual(["other/c"]);
|
|
458
|
+
const audits = await prisma.gitHubReconcileAudit.findMany({
|
|
459
|
+
where: { agentId: agent.id },
|
|
460
|
+
});
|
|
461
|
+
expect(audits).toHaveLength(1);
|
|
462
|
+
expect(audits[0]?.planHash).toBe("uninstall:org");
|
|
463
|
+
expect(audits[0]?.result).toEqual({ status: "applied" });
|
|
464
|
+
expect(blocked.sort()).toEqual(["acme-eng/a", "acme-eng/b"]);
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
it("a GitHub refusal writes a failed audit row and keeps the repos", async () => {
|
|
468
|
+
const { agent, del } = await setup(403);
|
|
469
|
+
const res = await del();
|
|
470
|
+
expect(res.status).toBe(502);
|
|
471
|
+
const row = await prisma.agent.findUniqueOrThrow({
|
|
472
|
+
where: { id: agent.id },
|
|
473
|
+
});
|
|
474
|
+
expect(row.repos).toHaveLength(3);
|
|
475
|
+
const audits = await prisma.gitHubReconcileAudit.findMany({
|
|
476
|
+
where: { agentId: agent.id },
|
|
477
|
+
});
|
|
478
|
+
expect(audits).toHaveLength(1);
|
|
479
|
+
expect((audits[0]?.result as { status: string } | undefined)?.status).toBe(
|
|
480
|
+
"failed",
|
|
481
|
+
);
|
|
482
|
+
});
|
|
483
|
+
});
|
|
@@ -9,7 +9,10 @@ import { describe, expect, it } from "bun:test";
|
|
|
9
9
|
import { generateKeyPairSync } from "node:crypto";
|
|
10
10
|
import { readFileSync } from "node:fs";
|
|
11
11
|
import { parseAdminApiKeys } from "./api-auth.ts";
|
|
12
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
createGithubEnterpriseApp,
|
|
14
|
+
type GithubEnterpriseApiDeps,
|
|
15
|
+
} from "./github-enterprise-api.ts";
|
|
13
16
|
import type {
|
|
14
17
|
GitHubEnterpriseAppMetadata,
|
|
15
18
|
GitHubEnterpriseAppRole,
|
|
@@ -100,10 +103,16 @@ function replayFetch(routes: Record<string, string>) {
|
|
|
100
103
|
const key = routes[base];
|
|
101
104
|
if (!key) throw new Error(`no route for ${base}`);
|
|
102
105
|
const entry = cassette[key] as CassetteEntry;
|
|
103
|
-
return new Response(
|
|
104
|
-
status: entry.
|
|
105
|
-
|
|
106
|
-
|
|
106
|
+
return new Response(
|
|
107
|
+
entry.status === 204 ? null : JSON.stringify(entry.body),
|
|
108
|
+
{
|
|
109
|
+
status: entry.status,
|
|
110
|
+
headers: {
|
|
111
|
+
"Content-Type": "application/json",
|
|
112
|
+
...(entry.headers ?? {}),
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
);
|
|
107
116
|
}) as typeof fetch;
|
|
108
117
|
return { fetchFn, calls };
|
|
109
118
|
}
|
|
@@ -119,6 +128,7 @@ function build(
|
|
|
119
128
|
routes?: Record<string, string>;
|
|
120
129
|
agents?: Record<string, Record<string, string> | null>;
|
|
121
130
|
refreshAction?: GithubRefreshAction;
|
|
131
|
+
uninstall?: GithubEnterpriseApiDeps["uninstall"];
|
|
122
132
|
} = {},
|
|
123
133
|
) {
|
|
124
134
|
const appService = fakeAppService();
|
|
@@ -154,6 +164,7 @@ function build(
|
|
|
154
164
|
),
|
|
155
165
|
agentTokenService: { validate: async () => null },
|
|
156
166
|
...(opts.refreshAction ? { refreshAction: opts.refreshAction } : {}),
|
|
167
|
+
...(opts.uninstall ? { uninstall: opts.uninstall } : {}),
|
|
157
168
|
fetchFn,
|
|
158
169
|
apiBase: "https://ghe.test",
|
|
159
170
|
});
|
|
@@ -989,3 +1000,175 @@ describe("refresh action (GLC-2.4)", () => {
|
|
|
989
1000
|
expect(res2.status).toBe(200);
|
|
990
1001
|
});
|
|
991
1002
|
});
|
|
1003
|
+
|
|
1004
|
+
describe("DELETE install (uninstall)", () => {
|
|
1005
|
+
const DELETE_INST =
|
|
1006
|
+
"DELETE /enterprises/acme/apps/organizations/org-1/installations/501";
|
|
1007
|
+
const INSTS_ROUTE =
|
|
1008
|
+
"GET /enterprises/acme/apps/organizations/org-1/installations";
|
|
1009
|
+
|
|
1010
|
+
function harness(
|
|
1011
|
+
opts: {
|
|
1012
|
+
repos?: string[];
|
|
1013
|
+
deleteStatus?: number;
|
|
1014
|
+
openWorkFails?: boolean;
|
|
1015
|
+
noInstall?: boolean;
|
|
1016
|
+
} = {},
|
|
1017
|
+
) {
|
|
1018
|
+
const state = {
|
|
1019
|
+
repos: opts.repos ?? ["org-1/a", "org-1/b", "other/c"],
|
|
1020
|
+
audits: [] as {
|
|
1021
|
+
planHash: string;
|
|
1022
|
+
diff: unknown;
|
|
1023
|
+
result: { status: string };
|
|
1024
|
+
}[],
|
|
1025
|
+
blocked: [] as string[],
|
|
1026
|
+
deleted: 0,
|
|
1027
|
+
};
|
|
1028
|
+
const h = build({
|
|
1029
|
+
agents: {
|
|
1030
|
+
"agent-1": {
|
|
1031
|
+
GH_APP_SLUG: "target-app",
|
|
1032
|
+
GH_APP_CLIENT_ID: "Iv1.targetapp0001",
|
|
1033
|
+
},
|
|
1034
|
+
},
|
|
1035
|
+
routes: {
|
|
1036
|
+
[DELETE_INST]:
|
|
1037
|
+
opts.deleteStatus === 403 ? "uninstall_403" : "uninstall_success",
|
|
1038
|
+
[INSTS_ROUTE]: opts.noInstall
|
|
1039
|
+
? "installable_orgs_empty"
|
|
1040
|
+
: "org_installations_success",
|
|
1041
|
+
},
|
|
1042
|
+
uninstall: {
|
|
1043
|
+
agents: {
|
|
1044
|
+
get: async (id: string) => ({
|
|
1045
|
+
id,
|
|
1046
|
+
repos: [...state.repos],
|
|
1047
|
+
githubUsername: null,
|
|
1048
|
+
}),
|
|
1049
|
+
setRepos: async (_id: string, repos: string[]) => {
|
|
1050
|
+
state.repos = repos;
|
|
1051
|
+
},
|
|
1052
|
+
},
|
|
1053
|
+
audit: {
|
|
1054
|
+
write: async (e: never) => {
|
|
1055
|
+
state.audits.push(e);
|
|
1056
|
+
return `audit-${state.audits.length}`;
|
|
1057
|
+
},
|
|
1058
|
+
},
|
|
1059
|
+
openWork: {
|
|
1060
|
+
listOpenWork: async (_a: string, repos: readonly string[]) => {
|
|
1061
|
+
if (opts.openWorkFails) throw new Error("port down");
|
|
1062
|
+
return repos.map((repo) => ({ id: `t-${repo}`, repo }));
|
|
1063
|
+
},
|
|
1064
|
+
blockOpenWork: async (_a: string, repos: readonly string[]) => {
|
|
1065
|
+
if (opts.openWorkFails) throw new Error("port down");
|
|
1066
|
+
state.blocked.push(...repos);
|
|
1067
|
+
},
|
|
1068
|
+
},
|
|
1069
|
+
},
|
|
1070
|
+
});
|
|
1071
|
+
return { h, state, del: DELETE_INST, status: opts.deleteStatus ?? 204 };
|
|
1072
|
+
}
|
|
1073
|
+
|
|
1074
|
+
async function run(
|
|
1075
|
+
o: ReturnType<typeof harness>,
|
|
1076
|
+
query: string,
|
|
1077
|
+
headers: Record<string, string> = ADMIN,
|
|
1078
|
+
) {
|
|
1079
|
+
return o.h.app.request(
|
|
1080
|
+
`/github-enterprise/orgs/org-1/install?agentId=agent-1${query}`,
|
|
1081
|
+
{
|
|
1082
|
+
method: "DELETE",
|
|
1083
|
+
headers,
|
|
1084
|
+
},
|
|
1085
|
+
);
|
|
1086
|
+
}
|
|
1087
|
+
|
|
1088
|
+
it("uninstalls, removes the org's repos, blocks open work and audits", async () => {
|
|
1089
|
+
const o = harness();
|
|
1090
|
+
await seed(o.h);
|
|
1091
|
+
const res = await run(o, "");
|
|
1092
|
+
expect(res.status).toBe(200);
|
|
1093
|
+
const body = (await res.json()) as Record<string, unknown>;
|
|
1094
|
+
expect(body).toMatchObject({
|
|
1095
|
+
status: "uninstalled",
|
|
1096
|
+
org: "org-1",
|
|
1097
|
+
installationId: 501,
|
|
1098
|
+
repos: ["org-1/a", "org-1/b"],
|
|
1099
|
+
auditId: "audit-1",
|
|
1100
|
+
});
|
|
1101
|
+
expect(o.h.calls).toContain(o.del);
|
|
1102
|
+
expect(o.state.repos).toEqual(["other/c"]);
|
|
1103
|
+
expect(o.state.blocked.sort()).toEqual(["org-1/a", "org-1/b"]);
|
|
1104
|
+
expect(o.state.audits[0]).toMatchObject({
|
|
1105
|
+
planHash: "uninstall:org",
|
|
1106
|
+
result: { status: "applied" },
|
|
1107
|
+
});
|
|
1108
|
+
});
|
|
1109
|
+
|
|
1110
|
+
it("preview lists repos and open work and changes nothing", async () => {
|
|
1111
|
+
const o = harness();
|
|
1112
|
+
await seed(o.h);
|
|
1113
|
+
const res = await run(o, "&preview=true");
|
|
1114
|
+
expect(res.status).toBe(200);
|
|
1115
|
+
const body = (await res.json()) as { status: string; openWork: unknown[] };
|
|
1116
|
+
expect(body.status).toBe("preview");
|
|
1117
|
+
expect(body.openWork).toHaveLength(2);
|
|
1118
|
+
expect(o.h.calls).not.toContain(o.del);
|
|
1119
|
+
expect(o.state.repos).toHaveLength(3);
|
|
1120
|
+
expect(o.state.audits).toHaveLength(0);
|
|
1121
|
+
expect(o.state.blocked).toHaveLength(0);
|
|
1122
|
+
});
|
|
1123
|
+
|
|
1124
|
+
it("a port failure on preview is a warning", async () => {
|
|
1125
|
+
const o = harness({ openWorkFails: true });
|
|
1126
|
+
await seed(o.h);
|
|
1127
|
+
const body = (await (await run(o, "&preview=true")).json()) as {
|
|
1128
|
+
warnings: string[];
|
|
1129
|
+
};
|
|
1130
|
+
expect(body.warnings.join()).toContain("port down");
|
|
1131
|
+
});
|
|
1132
|
+
|
|
1133
|
+
it("not installed is a distinct 404 and changes nothing", async () => {
|
|
1134
|
+
const o = harness({ noInstall: true });
|
|
1135
|
+
await seed(o.h);
|
|
1136
|
+
const res = await run(o, "");
|
|
1137
|
+
expect(res.status).toBe(404);
|
|
1138
|
+
expect(((await res.json()) as { error: string }).error).toContain(
|
|
1139
|
+
"not installed",
|
|
1140
|
+
);
|
|
1141
|
+
expect(o.state.audits).toHaveLength(0);
|
|
1142
|
+
expect(o.state.repos).toHaveLength(3);
|
|
1143
|
+
});
|
|
1144
|
+
|
|
1145
|
+
it("a GitHub refusal is 502 with a failed audit row and repos untouched", async () => {
|
|
1146
|
+
const o = harness({ deleteStatus: 403 });
|
|
1147
|
+
await seed(o.h);
|
|
1148
|
+
const res = await run(o, "");
|
|
1149
|
+
expect(res.status).toBe(502);
|
|
1150
|
+
expect(o.state.audits[0]?.result.status).toBe("failed");
|
|
1151
|
+
expect(o.state.repos).toHaveLength(3);
|
|
1152
|
+
expect(o.state.blocked).toHaveLength(0);
|
|
1153
|
+
});
|
|
1154
|
+
|
|
1155
|
+
it("more than 10 repos need confirmRemovals", async () => {
|
|
1156
|
+
const many = Array.from({ length: 11 }, (_, i) => `org-1/r${i}`);
|
|
1157
|
+
const o = harness({ repos: many });
|
|
1158
|
+
await seed(o.h);
|
|
1159
|
+
const refused = await run(o, "");
|
|
1160
|
+
expect(refused.status).toBe(422);
|
|
1161
|
+
expect(o.h.calls).not.toContain(o.del);
|
|
1162
|
+
const ok = await run(o, "&confirmRemovals=true");
|
|
1163
|
+
expect(ok.status).toBe(200);
|
|
1164
|
+
expect(o.state.repos).toEqual([]);
|
|
1165
|
+
});
|
|
1166
|
+
|
|
1167
|
+
it("non-admin callers get 403 and nothing is called", async () => {
|
|
1168
|
+
const o = harness();
|
|
1169
|
+
await seed(o.h);
|
|
1170
|
+
const res = await run(o, "", { Authorization: "Bearer tok-read" });
|
|
1171
|
+
expect(res.status).toBe(403);
|
|
1172
|
+
expect(o.h.calls).toHaveLength(0);
|
|
1173
|
+
});
|
|
1174
|
+
});
|