@app-vitals/agent-admin 1.109.0 → 1.111.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 +0 -193
- package/openapi.platform.json +1 -238
- package/package.json +2 -2
- package/src/admin-boot.ts +6 -5
- package/src/admin-ui-github-enterprise-pages.ts +13 -2
- package/src/admin-ui-github-enterprise.smoke.test.ts +20 -1
- package/src/admin-ui-github-enterprise.ts +16 -1
- package/src/admin-ui-github-identity-pages.ts +35 -139
- package/src/admin-ui-github-identity-pages.unit.test.ts +50 -173
- package/src/admin-ui-github-identity.smoke.test.ts +103 -63
- package/src/admin-ui-installation-repos-pages.ts +2 -23
- package/src/admin-ui-installation-repos-pages.unit.test.ts +41 -36
- package/src/admin-ui-installation-repos.smoke.test.ts +9 -8
- package/src/admin-ui-pages.ts +4 -5
- package/src/admin-ui-slack-config-token-pages.ts +11 -2
- package/src/admin-ui-slack-config-token.smoke.test.ts +39 -3
- package/src/admin-ui-slack-config-token.ts +51 -9
- package/src/admin-ui-templates-pages.ts +7 -1
- package/src/admin-ui-templates.smoke.test.ts +31 -5
- package/src/admin-ui-templates.ts +43 -8
- package/src/admin-ui.ts +51 -36
- package/src/agent-templates-api.integration.test.ts +29 -7
- package/src/agent-templates.ts +7 -1
- package/src/agents-api.smoke.test.ts +26 -315
- package/src/agents-api.ts +1 -107
- package/src/deployment-wide-settings.integration.test.ts +309 -0
- package/src/deployment-wide-settings.ts +42 -0
- package/src/github-access-preview.ts +2 -2
- package/src/github-agent-installation-crawler.ts +18 -1
- package/src/github-enterprise-api.integration.test.ts +7 -0
- package/src/github-enterprise-api.smoke.test.ts +26 -0
- package/src/github-enterprise-api.ts +24 -4
- package/src/github-enterprise-apps.integration.test.ts +33 -8
- package/src/github-enterprise-apps.ts +4 -0
- package/src/github-enterprise-client.integration.test.ts +0 -41
- package/src/github-enterprise-client.ts +0 -25
- package/src/github-enterprise-client.unit.test.ts +0 -18
- package/src/github-installation-wiring.ts +48 -13
- package/src/github-installation-wiring.unit.test.ts +38 -1
- package/src/github-sync-all-repos.unit.test.ts +116 -0
- package/src/github-sync-cache.integration.test.ts +9 -2
- package/src/github-sync-snapshot-refresh.integration.test.ts +168 -0
- package/src/github-sync-snapshot-refresh.unit.test.ts +144 -0
- package/src/github-sync.ts +103 -137
- package/src/github-sync.unit.test.ts +82 -1
- package/src/installation-ports.ts +1 -40
- package/src/installation-repos-service.ts +7 -4
- package/src/reconcile-open-work.integration.test.ts +0 -1
- package/src/slack-config-token.integration.test.ts +19 -2
- package/src/slack-config-token.ts +6 -1
- package/src/github-access-preview.integration.test.ts +0 -399
- package/src/github-sync-switch.integration.test.ts +0 -297
- package/src/github-sync-switch.unit.test.ts +0 -467
|
@@ -1,297 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* admin/src/github-sync-switch.integration.test.ts
|
|
3
|
-
* GIS-1.2: apply ordering for an "all" installation against the recorded
|
|
4
|
-
* enterprise cassette (real GithubEnterpriseClient, fetch replaced by the
|
|
5
|
-
* recording) with the real cache and a real GitHubReconcileAudit row.
|
|
6
|
-
*
|
|
7
|
-
* Requires DATABASE_URL_ADMIN_TEST to be set; skips otherwise.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
|
|
11
|
-
import { generateKeyPairSync } from "node:crypto";
|
|
12
|
-
import { readFileSync } from "node:fs";
|
|
13
|
-
import type { PrismaClient } from "../prisma/client/client.ts";
|
|
14
|
-
import { GithubSyncService } from "./github-sync.ts";
|
|
15
|
-
import { lazyInstallerClient } from "./github-installation-wiring.ts";
|
|
16
|
-
import { GitHubRepoCacheService } from "./github-repo-cache.ts";
|
|
17
|
-
import { DEPLOYMENT_WIDE_ACCOUNT_KEY } from "./github-agent-installation-crawler.ts";
|
|
18
|
-
import {
|
|
19
|
-
createInstallationPort,
|
|
20
|
-
PrismaInstallationAudit,
|
|
21
|
-
} from "./installation-ports.ts";
|
|
22
|
-
import { createInstallationReposPort } from "./installation-repos-service.ts";
|
|
23
|
-
import { createAdminPrismaClient } from "./prisma-client.ts";
|
|
24
|
-
|
|
25
|
-
const TEST_DB = process.env.DATABASE_URL_ADMIN_TEST;
|
|
26
|
-
const describeOrSkip = TEST_DB ? describe : describe.skip;
|
|
27
|
-
|
|
28
|
-
const { privateKey: PEM } = generateKeyPairSync("rsa", {
|
|
29
|
-
modulusLength: 2048,
|
|
30
|
-
privateKeyEncoding: { type: "pkcs8", format: "pem" },
|
|
31
|
-
publicKeyEncoding: { type: "spki", format: "pem" },
|
|
32
|
-
});
|
|
33
|
-
const cassette = JSON.parse(
|
|
34
|
-
readFileSync(
|
|
35
|
-
new URL("./fixtures/github-enterprise-cassette.json", import.meta.url)
|
|
36
|
-
.pathname,
|
|
37
|
-
"utf-8",
|
|
38
|
-
),
|
|
39
|
-
) as Record<string, { status: number; body: unknown }>;
|
|
40
|
-
|
|
41
|
-
const INST = "/enterprises/acme/apps/organizations/acme-eng/installations";
|
|
42
|
-
const APP_ID = 777;
|
|
43
|
-
const AGENT_ID = "gis-it-agent";
|
|
44
|
-
const ORG = "acme-eng";
|
|
45
|
-
const REPOS = ["acme-eng/repo-1", "acme-eng/repo-2"];
|
|
46
|
-
|
|
47
|
-
function routeTable(selection: string): Record<string, string> {
|
|
48
|
-
return {
|
|
49
|
-
"GET /enterprises/acme/installation": "enterprise_installation_success",
|
|
50
|
-
"POST /app/installations/4242/access_tokens": "access_token_success",
|
|
51
|
-
[`GET ${INST}`]: selection,
|
|
52
|
-
[`GET ${INST}/501/repositories`]: "install_repos_success",
|
|
53
|
-
[`PATCH ${INST}/501/repositories`]: "install_selection_success",
|
|
54
|
-
[`PATCH ${INST}/501/repositories/add`]: "install_repos_add_success",
|
|
55
|
-
[`PATCH ${INST}/501/repositories/remove`]: "install_repos_remove_success",
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
describeOrSkip("GitHub Sync all-to-selected switch (integration)", () => {
|
|
60
|
-
let prisma: PrismaClient;
|
|
61
|
-
let cache: GitHubRepoCacheService;
|
|
62
|
-
const checkedAt = new Date(Date.now() - 60_000);
|
|
63
|
-
|
|
64
|
-
const clean = async () => {
|
|
65
|
-
await prisma.gitHubReconcileAudit.deleteMany({
|
|
66
|
-
where: { agentId: AGENT_ID },
|
|
67
|
-
});
|
|
68
|
-
await prisma.gitHubRepoAccess.deleteMany({
|
|
69
|
-
where: { accountId: DEPLOYMENT_WIDE_ACCOUNT_KEY, org: ORG },
|
|
70
|
-
});
|
|
71
|
-
await prisma.gitHubCachedRepo.deleteMany();
|
|
72
|
-
await prisma.gitHubRepoSnapshot.deleteMany();
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
async function seed(selection: "all" | "selected", repos: string[]) {
|
|
76
|
-
const key = {
|
|
77
|
-
accountId: DEPLOYMENT_WIDE_ACCOUNT_KEY,
|
|
78
|
-
appKind: "agent" as const,
|
|
79
|
-
appRef: String(APP_ID),
|
|
80
|
-
org: ORG,
|
|
81
|
-
};
|
|
82
|
-
const snap = await cache.upsertSnapshot(key, {
|
|
83
|
-
completedAt: checkedAt,
|
|
84
|
-
totalCount: repos.length,
|
|
85
|
-
repositorySelection: selection,
|
|
86
|
-
lastError: null,
|
|
87
|
-
});
|
|
88
|
-
await cache.upsertRepos(
|
|
89
|
-
snap.id,
|
|
90
|
-
repos.map((fullName, i) => ({ githubId: i + 1, fullName })),
|
|
91
|
-
checkedAt,
|
|
92
|
-
);
|
|
93
|
-
for (const full of REPOS) {
|
|
94
|
-
await prisma.gitHubRepoAccess.upsert({
|
|
95
|
-
where: {
|
|
96
|
-
accountId_org_repo_username: {
|
|
97
|
-
accountId: DEPLOYMENT_WIDE_ACCOUNT_KEY,
|
|
98
|
-
org: ORG,
|
|
99
|
-
repo: full.split("/")[1] as string,
|
|
100
|
-
username: "dev",
|
|
101
|
-
},
|
|
102
|
-
},
|
|
103
|
-
create: {
|
|
104
|
-
accountId: DEPLOYMENT_WIDE_ACCOUNT_KEY,
|
|
105
|
-
org: ORG,
|
|
106
|
-
repo: full.split("/")[1] as string,
|
|
107
|
-
username: "dev",
|
|
108
|
-
canPush: full === "acme-eng/repo-1",
|
|
109
|
-
level: full === "acme-eng/repo-1" ? "write" : "read",
|
|
110
|
-
checkedAt,
|
|
111
|
-
status: "ok",
|
|
112
|
-
},
|
|
113
|
-
update: {},
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
function service(requests: string[], patchBodies: unknown[]) {
|
|
119
|
-
// The installation's repo list follows a landed selection switch.
|
|
120
|
-
let installed = [...REPOS];
|
|
121
|
-
const table = routeTable("org_installations_success");
|
|
122
|
-
const fetchFn = (async (input: RequestInfo | URL, init?: RequestInit) => {
|
|
123
|
-
const u = new URL(input.toString());
|
|
124
|
-
const key = `${init?.method ?? "GET"} ${u.pathname}`;
|
|
125
|
-
if (key === "GET /enterprises/acme/apps/installable_organizations") {
|
|
126
|
-
return new Response(JSON.stringify([{ id: 5010, login: ORG }]), {
|
|
127
|
-
status: 200,
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
const name = table[key];
|
|
131
|
-
if (!name) throw new Error(`no route for ${key}`);
|
|
132
|
-
if (key.startsWith("PATCH") || key.startsWith("DELETE")) {
|
|
133
|
-
requests.push(key);
|
|
134
|
-
patchBodies.push(JSON.parse(String(init?.body ?? "{}")));
|
|
135
|
-
}
|
|
136
|
-
const entry = cassette[name];
|
|
137
|
-
if (!entry) throw new Error(`no cassette entry ${name}`);
|
|
138
|
-
if (key === `GET ${INST}/501/repositories`) {
|
|
139
|
-
return new Response(
|
|
140
|
-
JSON.stringify(
|
|
141
|
-
installed.map((full_name, i) => ({
|
|
142
|
-
id: i + 1,
|
|
143
|
-
name: full_name.split("/")[1],
|
|
144
|
-
full_name,
|
|
145
|
-
})),
|
|
146
|
-
),
|
|
147
|
-
{ status: 200, headers: { "Content-Type": "application/json" } },
|
|
148
|
-
);
|
|
149
|
-
}
|
|
150
|
-
if (key === `PATCH ${INST}/501/repositories` && entry.status === 200) {
|
|
151
|
-
installed = (
|
|
152
|
-
patchBodies.at(-1) as { repositories: string[] }
|
|
153
|
-
).repositories.map((n) => `${ORG}/${n}`);
|
|
154
|
-
}
|
|
155
|
-
return new Response(JSON.stringify(entry.body), {
|
|
156
|
-
status: entry.status,
|
|
157
|
-
headers: { "Content-Type": "application/json" },
|
|
158
|
-
});
|
|
159
|
-
}) as typeof fetch;
|
|
160
|
-
const client = lazyInstallerClient({
|
|
161
|
-
appService: {
|
|
162
|
-
get: async () => ({ appId: 12345, enterpriseSlug: "acme" }) as never,
|
|
163
|
-
getPrivateKey: async () => PEM,
|
|
164
|
-
},
|
|
165
|
-
fetchFn,
|
|
166
|
-
});
|
|
167
|
-
let repos = ["acme-eng/repo-2"];
|
|
168
|
-
return new GithubSyncService({
|
|
169
|
-
agents: {
|
|
170
|
-
get: async () => ({
|
|
171
|
-
id: AGENT_ID,
|
|
172
|
-
githubUsername: "dev",
|
|
173
|
-
appId: APP_ID,
|
|
174
|
-
repos,
|
|
175
|
-
}),
|
|
176
|
-
setRepos: async (_id, next) => {
|
|
177
|
-
requests.push("setRepos");
|
|
178
|
-
repos = next;
|
|
179
|
-
},
|
|
180
|
-
},
|
|
181
|
-
access: async () => {
|
|
182
|
-
throw new Error("live resolver must not be used");
|
|
183
|
-
},
|
|
184
|
-
cache,
|
|
185
|
-
lookupRepos: async () => [
|
|
186
|
-
{ org: ORG, completedAt: checkedAt, repos: REPOS },
|
|
187
|
-
],
|
|
188
|
-
verifyPush: async (_u, repo) => repo === "acme-eng/repo-1",
|
|
189
|
-
installation: createInstallationReposPort(
|
|
190
|
-
createInstallationPort(client),
|
|
191
|
-
client,
|
|
192
|
-
),
|
|
193
|
-
audit: new PrismaInstallationAudit(prisma),
|
|
194
|
-
});
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
beforeEach(async () => {
|
|
198
|
-
prisma = createAdminPrismaClient(TEST_DB as string);
|
|
199
|
-
cache = new GitHubRepoCacheService(prisma);
|
|
200
|
-
await clean();
|
|
201
|
-
});
|
|
202
|
-
afterEach(async () => {
|
|
203
|
-
await clean();
|
|
204
|
-
await prisma.$disconnect();
|
|
205
|
-
});
|
|
206
|
-
|
|
207
|
-
it("switches first, folds the removal, writes the audit row; a later preview has nothing to do", async () => {
|
|
208
|
-
await seed("all", REPOS);
|
|
209
|
-
const requests: string[] = [];
|
|
210
|
-
const bodies: unknown[] = [];
|
|
211
|
-
const svc = service(requests, bodies);
|
|
212
|
-
|
|
213
|
-
const before = await svc.preview(AGENT_ID);
|
|
214
|
-
if (before.status !== "previewed") throw new Error(before.status);
|
|
215
|
-
expect(before.plan.selectionSwitches).toEqual([
|
|
216
|
-
{ org: ORG, repos: ["acme-eng/repo-1"] },
|
|
217
|
-
]);
|
|
218
|
-
expect(before.plan.appRemoves).toEqual(["acme-eng/repo-2"]);
|
|
219
|
-
expect(requests).toEqual([]);
|
|
220
|
-
|
|
221
|
-
const r = await svc.apply({
|
|
222
|
-
agentId: AGENT_ID,
|
|
223
|
-
caller: "root",
|
|
224
|
-
appAdds: [],
|
|
225
|
-
appRemoves: ["acme-eng/repo-2"],
|
|
226
|
-
agentRemoves: ["acme-eng/repo-2"],
|
|
227
|
-
});
|
|
228
|
-
expect(r.status).toBe("applied");
|
|
229
|
-
// PATCH selection (no /add or /remove: the removal is folded), then agent repos.
|
|
230
|
-
expect(requests).toEqual([`PATCH ${INST}/501/repositories`, "setRepos"]);
|
|
231
|
-
expect(bodies[0]).toEqual({
|
|
232
|
-
repository_selection: "selected",
|
|
233
|
-
repositories: ["repo-1"],
|
|
234
|
-
});
|
|
235
|
-
|
|
236
|
-
const rows = await prisma.gitHubReconcileAudit.findMany({
|
|
237
|
-
where: { agentId: AGENT_ID },
|
|
238
|
-
});
|
|
239
|
-
expect(rows).toHaveLength(1);
|
|
240
|
-
expect(rows[0]?.result).toEqual({ status: "applied" });
|
|
241
|
-
expect(rows[0]?.diff).toMatchObject({
|
|
242
|
-
installationRemoves: ["acme-eng/repo-2"],
|
|
243
|
-
configRemoves: ["acme-eng/repo-2"],
|
|
244
|
-
selectionSwitches: [{ org: ORG, repos: ["acme-eng/repo-1"] }],
|
|
245
|
-
});
|
|
246
|
-
|
|
247
|
-
// The next crawl records "selected" and the shrunk repo list.
|
|
248
|
-
await prisma.gitHubCachedRepo.deleteMany();
|
|
249
|
-
await prisma.gitHubRepoSnapshot.deleteMany();
|
|
250
|
-
await seed("selected", ["acme-eng/repo-1"]);
|
|
251
|
-
const after = await svc.preview(AGENT_ID);
|
|
252
|
-
if (after.status !== "previewed") throw new Error(after.status);
|
|
253
|
-
expect(after.plan.selectionSwitches).toEqual([]);
|
|
254
|
-
expect([
|
|
255
|
-
after.plan.appAdds,
|
|
256
|
-
after.plan.appRemoves,
|
|
257
|
-
after.plan.agentRemoves,
|
|
258
|
-
]).toEqual([[], [], []]);
|
|
259
|
-
});
|
|
260
|
-
|
|
261
|
-
it("records a failed audit row with nothing landed when GitHub answers 403", async () => {
|
|
262
|
-
await seed("all", REPOS);
|
|
263
|
-
const requests: string[] = [];
|
|
264
|
-
const svc = service(requests, []);
|
|
265
|
-
// Swap the recorded success for the recorded 403 on the PATCH.
|
|
266
|
-
const original = cassette.install_selection_success;
|
|
267
|
-
cassette.install_selection_success =
|
|
268
|
-
cassette.install_selection_403 as never;
|
|
269
|
-
try {
|
|
270
|
-
const r = await svc.apply({
|
|
271
|
-
agentId: AGENT_ID,
|
|
272
|
-
caller: "root",
|
|
273
|
-
appAdds: [],
|
|
274
|
-
appRemoves: ["acme-eng/repo-2"],
|
|
275
|
-
agentRemoves: [],
|
|
276
|
-
});
|
|
277
|
-
if (r.status !== "failed") throw new Error(r.status);
|
|
278
|
-
expect(r.error).toContain(
|
|
279
|
-
"enterprise_organization_installation_repositories",
|
|
280
|
-
);
|
|
281
|
-
} finally {
|
|
282
|
-
cassette.install_selection_success = original as never;
|
|
283
|
-
}
|
|
284
|
-
expect(requests).toEqual([`PATCH ${INST}/501/repositories`]);
|
|
285
|
-
const rows = await prisma.gitHubReconcileAudit.findMany({
|
|
286
|
-
where: { agentId: AGENT_ID },
|
|
287
|
-
});
|
|
288
|
-
expect(rows).toHaveLength(1);
|
|
289
|
-
expect(rows[0]?.result).toMatchObject({ status: "failed" });
|
|
290
|
-
expect(rows[0]?.diff).toEqual({
|
|
291
|
-
installationAdds: [],
|
|
292
|
-
installationRemoves: [],
|
|
293
|
-
configAdds: [],
|
|
294
|
-
configRemoves: [],
|
|
295
|
-
});
|
|
296
|
-
});
|
|
297
|
-
});
|