@app-vitals/agent-admin 1.35.0 → 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 +136 -766
- package/package.json +2 -2
- package/src/admin-boot.ts +4 -15
- package/src/admin-extension.ts +3 -3
- package/src/admin-ui-github-identity.smoke.test.ts +24 -116
- 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 -173
- package/src/admin-ui-pages.ts +2 -2
- package/src/admin-ui.ts +103 -171
- package/src/agents-api.smoke.test.ts +195 -642
- package/src/agents-api.ts +86 -477
- package/src/github-enterprise-isolation.unit.test.ts +4 -4
- package/src/{github-reconcile-wiring.ts → github-installation-wiring.ts} +24 -46
- package/src/github-installation-wiring.unit.test.ts +115 -0
- package/src/github-installation.fixtures.ts +76 -0
- package/src/github-sync.ts +407 -0
- package/src/{lost-access-removal.unit.test.ts → github-sync.unit.test.ts} +220 -115
- 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 +10 -105
- package/src/installation-repos-service.unit.test.ts +5 -90
- 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/lost-access-removal.ts +0 -333
- 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
|
@@ -26,15 +26,9 @@ import { createAdminApp, parseAdminApiKeys } from "./agents-api.ts";
|
|
|
26
26
|
import { PLATFORM_OPS_ROUTES } from "./api-auth.ts";
|
|
27
27
|
import {
|
|
28
28
|
createGithubInstallationRepos,
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
} from "./github-
|
|
32
|
-
import {
|
|
33
|
-
accessFor,
|
|
34
|
-
envWith,
|
|
35
|
-
fakeOpenWork,
|
|
36
|
-
fakePrisma,
|
|
37
|
-
} from "./github-reconcile-wiring.unit.test.ts";
|
|
29
|
+
createGithubSync,
|
|
30
|
+
} from "./github-installation-wiring.ts";
|
|
31
|
+
import { accessFor, envWith } from "./github-installation.fixtures.ts";
|
|
38
32
|
import type { OpenWorkPort } from "./reconcile-open-work.ts";
|
|
39
33
|
import {
|
|
40
34
|
repoLostAccessBlockReason,
|
|
@@ -7137,353 +7131,9 @@ describe("admin API — GET /agents/:id/github-access (GHE-2.4)", () => {
|
|
|
7137
7131
|
});
|
|
7138
7132
|
});
|
|
7139
7133
|
|
|
7140
|
-
// ───
|
|
7141
|
-
|
|
7142
|
-
describe("admin API — GitHub reconcile routes (GHE-3.4)", () => {
|
|
7143
|
-
const OPS = "ghr-ops-token";
|
|
7144
|
-
const READER = "ghr-read-token";
|
|
7145
|
-
const AGENT_KEY = "ghr-agent-token";
|
|
7146
|
-
const DIFF = {
|
|
7147
|
-
installationAdds: ["acme/api"],
|
|
7148
|
-
installationRemoves: [],
|
|
7149
|
-
configAdds: ["acme/api"],
|
|
7150
|
-
configRemoves: ["acme/old"],
|
|
7151
|
-
};
|
|
7152
|
-
const PLAN = {
|
|
7153
|
-
skipped: false,
|
|
7154
|
-
diff: DIFF,
|
|
7155
|
-
skippedOrgs: [],
|
|
7156
|
-
rejectedAdds: [],
|
|
7157
|
-
warnings: ["heads up"],
|
|
7158
|
-
empty: false,
|
|
7159
|
-
planHash: "hash-1",
|
|
7160
|
-
};
|
|
7161
|
-
const SKIPPED_PLAN = {
|
|
7162
|
-
...PLAN,
|
|
7163
|
-
skipped: true,
|
|
7164
|
-
skipReason: "agent has no bound GitHub username",
|
|
7165
|
-
diff: {
|
|
7166
|
-
installationAdds: [],
|
|
7167
|
-
installationRemoves: [],
|
|
7168
|
-
configAdds: [],
|
|
7169
|
-
configRemoves: [],
|
|
7170
|
-
},
|
|
7171
|
-
warnings: [],
|
|
7172
|
-
empty: true,
|
|
7173
|
-
planHash: "hash-skip",
|
|
7174
|
-
};
|
|
7175
|
-
type Reconcile = NonNullable<AdminDeps["githubReconcile"]>;
|
|
7176
|
-
|
|
7177
|
-
function setup(reconcile?: Partial<Reconcile>) {
|
|
7178
|
-
const base = makeMockDeps();
|
|
7179
|
-
const calls: { fn: string; args: unknown[] }[] = [];
|
|
7180
|
-
const port: Reconcile = {
|
|
7181
|
-
planWithOpenWork: async (...args) => {
|
|
7182
|
-
calls.push({ fn: "plan", args });
|
|
7183
|
-
return {
|
|
7184
|
-
plan: PLAN,
|
|
7185
|
-
openWork: [{ id: "T-1", repo: "acme/old", title: "fix it" }],
|
|
7186
|
-
};
|
|
7187
|
-
},
|
|
7188
|
-
apply: async (input) => {
|
|
7189
|
-
calls.push({ fn: "apply", args: [input] });
|
|
7190
|
-
return {
|
|
7191
|
-
status: "applied",
|
|
7192
|
-
plan: PLAN,
|
|
7193
|
-
auditId: "audit-1",
|
|
7194
|
-
warnings: [],
|
|
7195
|
-
};
|
|
7196
|
-
},
|
|
7197
|
-
...reconcile,
|
|
7198
|
-
};
|
|
7199
|
-
const deps: AdminDeps = {
|
|
7200
|
-
...base,
|
|
7201
|
-
adminApiKeys: parseAdminApiKeys(
|
|
7202
|
-
`root:${ADMIN_API_KEY}:*,operator:${OPS}:platform.ops,reader:${READER}:platform.read`,
|
|
7203
|
-
),
|
|
7204
|
-
agentTokenService: {
|
|
7205
|
-
...base.agentTokenService,
|
|
7206
|
-
validate: async (t: string) =>
|
|
7207
|
-
t === AGENT_KEY ? { agentId: AGENT_ID } : null,
|
|
7208
|
-
},
|
|
7209
|
-
githubReconcile: reconcile === undefined ? undefined : port,
|
|
7210
|
-
};
|
|
7211
|
-
return { app: createAdminApp(deps), calls, port };
|
|
7212
|
-
}
|
|
7213
|
-
const withPort = () => setup({});
|
|
7214
|
-
|
|
7215
|
-
const post = (
|
|
7216
|
-
app: ReturnType<typeof createAdminApp>,
|
|
7217
|
-
action: "plan" | "apply",
|
|
7218
|
-
headers: Record<string, string>,
|
|
7219
|
-
body: unknown = {},
|
|
7220
|
-
id = AGENT_ID,
|
|
7221
|
-
) =>
|
|
7222
|
-
app.request(`/agents/${id}/github-reconcile/${action}`, {
|
|
7223
|
-
method: "POST",
|
|
7224
|
-
headers: { "Content-Type": "application/json", ...headers },
|
|
7225
|
-
body: JSON.stringify(body),
|
|
7226
|
-
});
|
|
7227
|
-
const asAdmin = { Authorization: `Bearer ${ADMIN_API_KEY}` };
|
|
7228
|
-
|
|
7229
|
-
it("403 for a session member, ops key, read key and agent key", async () => {
|
|
7230
|
-
const { app, calls } = withPort();
|
|
7231
|
-
const memberApp = createAdminApp({
|
|
7232
|
-
...makeMockDeps(),
|
|
7233
|
-
callerScopeResolver: async () => ({
|
|
7234
|
-
kind: "scoped",
|
|
7235
|
-
accountId: "acct-a",
|
|
7236
|
-
agentIds: [AGENT_ID],
|
|
7237
|
-
}),
|
|
7238
|
-
});
|
|
7239
|
-
const cookie = await sign(
|
|
7240
|
-
{
|
|
7241
|
-
isAdmin: false,
|
|
7242
|
-
userId: "user-acct",
|
|
7243
|
-
email: "owner@acct-a.example.com",
|
|
7244
|
-
iat: Math.floor(Date.now() / 1000),
|
|
7245
|
-
exp: Math.floor(Date.now() / 1000) + 3600,
|
|
7246
|
-
},
|
|
7247
|
-
SESSION_SECRET,
|
|
7248
|
-
"HS256",
|
|
7249
|
-
);
|
|
7250
|
-
for (const action of ["plan", "apply"] as const) {
|
|
7251
|
-
const res = await post(
|
|
7252
|
-
memberApp,
|
|
7253
|
-
action,
|
|
7254
|
-
{ Cookie: `admin_session=${cookie}` },
|
|
7255
|
-
{ planHash: "x" },
|
|
7256
|
-
);
|
|
7257
|
-
expect(res.status).toBe(403);
|
|
7258
|
-
for (const key of [OPS, READER, AGENT_KEY]) {
|
|
7259
|
-
const r = await post(
|
|
7260
|
-
app,
|
|
7261
|
-
action,
|
|
7262
|
-
{ Authorization: `Bearer ${key}` },
|
|
7263
|
-
{ planHash: "x" },
|
|
7264
|
-
);
|
|
7265
|
-
expect(r.status).toBe(403);
|
|
7266
|
-
}
|
|
7267
|
-
}
|
|
7268
|
-
expect(calls).toEqual([]);
|
|
7269
|
-
});
|
|
7270
|
-
|
|
7271
|
-
it("plan returns the diff, warnings, hash and affected open work", async () => {
|
|
7272
|
-
const { app, calls } = withPort();
|
|
7273
|
-
const res = await post(app, "plan", asAdmin, { addRepos: ["acme/api"] });
|
|
7274
|
-
expect(res.status).toBe(200);
|
|
7275
|
-
const body = (await res.json()) as Record<string, unknown>;
|
|
7276
|
-
expect(body).toMatchObject({
|
|
7277
|
-
status: "planned",
|
|
7278
|
-
planHash: "hash-1",
|
|
7279
|
-
warnings: ["heads up"],
|
|
7280
|
-
diff: DIFF,
|
|
7281
|
-
empty: false,
|
|
7282
|
-
affectedOpenWork: [{ id: "T-1", repo: "acme/old", title: "fix it" }],
|
|
7283
|
-
});
|
|
7284
|
-
expect(calls).toEqual([{ fn: "plan", args: [AGENT_ID, ["acme/api"]] }]);
|
|
7285
|
-
});
|
|
7286
|
-
|
|
7287
|
-
it("plan with no body works", async () => {
|
|
7288
|
-
const { app } = withPort();
|
|
7289
|
-
const res = await app.request(`/agents/${AGENT_ID}/github-reconcile/plan`, {
|
|
7290
|
-
method: "POST",
|
|
7291
|
-
headers: asAdmin,
|
|
7292
|
-
});
|
|
7293
|
-
expect(res.status).toBe(200);
|
|
7294
|
-
});
|
|
7134
|
+
// ─── IRM-1.2 / IRM-3.1: installation repos list and GitHub Sync ───────────────
|
|
7295
7135
|
|
|
7296
|
-
|
|
7297
|
-
const { app } = setup({
|
|
7298
|
-
planWithOpenWork: async () => ({ plan: SKIPPED_PLAN, openWork: [] }),
|
|
7299
|
-
});
|
|
7300
|
-
const res = await post(app, "plan", asAdmin);
|
|
7301
|
-
expect(res.status).toBe(200);
|
|
7302
|
-
expect(await res.json()).toMatchObject({
|
|
7303
|
-
status: "skipped",
|
|
7304
|
-
reason: "agent has no bound GitHub username",
|
|
7305
|
-
});
|
|
7306
|
-
});
|
|
7307
|
-
|
|
7308
|
-
it("with no reconcile port wired, plan and apply are skipped, not errors", async () => {
|
|
7309
|
-
const { app } = setup();
|
|
7310
|
-
for (const action of ["plan", "apply"] as const) {
|
|
7311
|
-
const res = await post(app, action, asAdmin, { planHash: "x" });
|
|
7312
|
-
expect(res.status).toBe(200);
|
|
7313
|
-
const body = (await res.json()) as { status: string; reason: string };
|
|
7314
|
-
expect(body.status).toBe("skipped");
|
|
7315
|
-
expect(body.reason).toContain("not configured");
|
|
7316
|
-
}
|
|
7317
|
-
});
|
|
7318
|
-
|
|
7319
|
-
it("404 for an unknown agent", async () => {
|
|
7320
|
-
const { app } = setup({
|
|
7321
|
-
planWithOpenWork: async () => {
|
|
7322
|
-
throw new Error("agent no-such-agent not found");
|
|
7323
|
-
},
|
|
7324
|
-
apply: async () => ({ status: "agent_not_found" }),
|
|
7325
|
-
});
|
|
7326
|
-
expect((await post(app, "plan", asAdmin, {}, "no-such-agent")).status).toBe(
|
|
7327
|
-
404,
|
|
7328
|
-
);
|
|
7329
|
-
expect(
|
|
7330
|
-
(await post(app, "apply", asAdmin, { planHash: "x" }, "no-such-agent"))
|
|
7331
|
-
.status,
|
|
7332
|
-
).toBe(404);
|
|
7333
|
-
});
|
|
7334
|
-
|
|
7335
|
-
it("apply returns the audit id and result and passes the caller", async () => {
|
|
7336
|
-
const { app, calls } = withPort();
|
|
7337
|
-
const res = await post(app, "apply", asAdmin, {
|
|
7338
|
-
planHash: "hash-1",
|
|
7339
|
-
addRepos: ["acme/api"],
|
|
7340
|
-
confirmRemovals: true,
|
|
7341
|
-
});
|
|
7342
|
-
expect(res.status).toBe(200);
|
|
7343
|
-
expect(await res.json()).toMatchObject({
|
|
7344
|
-
status: "applied",
|
|
7345
|
-
auditId: "audit-1",
|
|
7346
|
-
planHash: "hash-1",
|
|
7347
|
-
diff: DIFF,
|
|
7348
|
-
warnings: [],
|
|
7349
|
-
});
|
|
7350
|
-
const input = calls[0]?.args[0] as Record<string, unknown>;
|
|
7351
|
-
expect(input).toMatchObject({
|
|
7352
|
-
agentId: AGENT_ID,
|
|
7353
|
-
planHash: "hash-1",
|
|
7354
|
-
namedAdds: ["acme/api"],
|
|
7355
|
-
confirmRemovals: true,
|
|
7356
|
-
});
|
|
7357
|
-
expect(typeof input.caller).toBe("string");
|
|
7358
|
-
});
|
|
7359
|
-
|
|
7360
|
-
it("apply on a skipped or noop plan is 200 with that status", async () => {
|
|
7361
|
-
const skipped = setup({
|
|
7362
|
-
apply: async () => ({ status: "skipped", plan: SKIPPED_PLAN }),
|
|
7363
|
-
});
|
|
7364
|
-
const r1 = await post(skipped.app, "apply", asAdmin, { planHash: "x" });
|
|
7365
|
-
expect(r1.status).toBe(200);
|
|
7366
|
-
expect(await r1.json()).toMatchObject({
|
|
7367
|
-
status: "skipped",
|
|
7368
|
-
reason: "agent has no bound GitHub username",
|
|
7369
|
-
});
|
|
7370
|
-
const noop = setup({
|
|
7371
|
-
apply: async () => ({ status: "noop", plan: { ...PLAN, empty: true } }),
|
|
7372
|
-
});
|
|
7373
|
-
const r2 = await post(noop.app, "apply", asAdmin, { planHash: "x" });
|
|
7374
|
-
expect(r2.status).toBe(200);
|
|
7375
|
-
expect(((await r2.json()) as { status: string }).status).toBe("noop");
|
|
7376
|
-
});
|
|
7377
|
-
|
|
7378
|
-
it("apply 409 on a stale plan hash, 422 over the removal cap", async () => {
|
|
7379
|
-
const stale = setup({
|
|
7380
|
-
apply: async () => ({ status: "stale_plan", plan: PLAN }),
|
|
7381
|
-
});
|
|
7382
|
-
expect(
|
|
7383
|
-
(await post(stale.app, "apply", asAdmin, { planHash: "old" })).status,
|
|
7384
|
-
).toBe(409);
|
|
7385
|
-
const cap = setup({
|
|
7386
|
-
apply: async () => ({ status: "removal_cap", plan: PLAN, removals: 11 }),
|
|
7387
|
-
});
|
|
7388
|
-
const res = await post(cap.app, "apply", asAdmin, { planHash: "hash-1" });
|
|
7389
|
-
expect(res.status).toBe(422);
|
|
7390
|
-
expect(((await res.json()) as { error: string }).error).toContain(
|
|
7391
|
-
"confirmRemovals",
|
|
7392
|
-
);
|
|
7393
|
-
});
|
|
7394
|
-
|
|
7395
|
-
it("apply failure is reported with the audit id", async () => {
|
|
7396
|
-
const { app } = setup({
|
|
7397
|
-
apply: async () => ({
|
|
7398
|
-
status: "failed",
|
|
7399
|
-
plan: PLAN,
|
|
7400
|
-
auditId: "audit-2",
|
|
7401
|
-
error: "boom",
|
|
7402
|
-
}),
|
|
7403
|
-
});
|
|
7404
|
-
const res = await post(app, "apply", asAdmin, { planHash: "hash-1" });
|
|
7405
|
-
expect(res.status).toBe(200);
|
|
7406
|
-
expect(await res.json()).toMatchObject({
|
|
7407
|
-
status: "failed",
|
|
7408
|
-
auditId: "audit-2",
|
|
7409
|
-
});
|
|
7410
|
-
});
|
|
7411
|
-
|
|
7412
|
-
it("400 when apply has no planHash", async () => {
|
|
7413
|
-
const { app } = withPort();
|
|
7414
|
-
expect((await post(app, "apply", asAdmin, {})).status).toBe(400);
|
|
7415
|
-
});
|
|
7416
|
-
});
|
|
7417
|
-
|
|
7418
|
-
describe("admin API — reconcile open-work seam, in-process composition (GHE-3.9)", () => {
|
|
7419
|
-
const compose = (
|
|
7420
|
-
openWork?: ReturnType<typeof fakeOpenWork>["port"],
|
|
7421
|
-
env: Record<string, string> | undefined = { GH_APP_ID: "42" },
|
|
7422
|
-
) => {
|
|
7423
|
-
const { prisma, state } = fakePrisma(["acme/old"]);
|
|
7424
|
-
const githubReconcile = createGithubReconcile({
|
|
7425
|
-
prisma,
|
|
7426
|
-
appService: { get: async () => null, getPrivateKey: async () => null },
|
|
7427
|
-
access: accessFor(["acme/api"]),
|
|
7428
|
-
agentEnv: envWith(env),
|
|
7429
|
-
installation: {
|
|
7430
|
-
listRepos: async () => ({ acme: ["acme/old", "acme/api"] }),
|
|
7431
|
-
addRepos: async () => {},
|
|
7432
|
-
removeRepos: async () => {},
|
|
7433
|
-
},
|
|
7434
|
-
openWork,
|
|
7435
|
-
});
|
|
7436
|
-
const app = createAdminApp({
|
|
7437
|
-
...makeMockDeps(),
|
|
7438
|
-
adminApiKeys: parseAdminApiKeys(`root:${ADMIN_API_KEY}:*`),
|
|
7439
|
-
githubReconcile,
|
|
7440
|
-
});
|
|
7441
|
-
const plan = () =>
|
|
7442
|
-
app.request(`/agents/${AGENT_ID}/github-reconcile/plan`, {
|
|
7443
|
-
method: "POST",
|
|
7444
|
-
headers: { Authorization: `Bearer ${ADMIN_API_KEY}` },
|
|
7445
|
-
});
|
|
7446
|
-
return { plan, state };
|
|
7447
|
-
};
|
|
7448
|
-
|
|
7449
|
-
it("plan returns the port's items and retires nothing", async () => {
|
|
7450
|
-
const { port, calls } = fakeOpenWork();
|
|
7451
|
-
const { plan, state } = compose(port);
|
|
7452
|
-
const res = await plan();
|
|
7453
|
-
expect(res.status).toBe(200);
|
|
7454
|
-
const body = (await res.json()) as { affectedOpenWork: unknown[] };
|
|
7455
|
-
expect(body.affectedOpenWork).toEqual([
|
|
7456
|
-
{ id: "T-1", repo: "acme/old", title: "fix it" },
|
|
7457
|
-
]);
|
|
7458
|
-
expect(calls.map((c) => c.fn)).toEqual(["list"]);
|
|
7459
|
-
expect(state.repos).toEqual(["acme/old"]);
|
|
7460
|
-
});
|
|
7461
|
-
|
|
7462
|
-
it("plans for an agent whose GH_APP_ID is in its stored env", async () => {
|
|
7463
|
-
const res = await compose(undefined, { GH_APP_ID: "42" }).plan();
|
|
7464
|
-
expect(res.status).toBe(200);
|
|
7465
|
-
expect(await res.json()).toMatchObject({ status: "planned" });
|
|
7466
|
-
});
|
|
7467
|
-
|
|
7468
|
-
it("reports skipped naming GH_APP_ID for an agent without one", async () => {
|
|
7469
|
-
const res = await compose(undefined, {}).plan();
|
|
7470
|
-
expect(res.status).toBe(200);
|
|
7471
|
-
const body = (await res.json()) as { status: string; reason?: string };
|
|
7472
|
-
expect(body.status).toBe("skipped");
|
|
7473
|
-
expect(body.reason).toContain("GH_APP_ID");
|
|
7474
|
-
});
|
|
7475
|
-
|
|
7476
|
-
it("without a port plan lists nothing", async () => {
|
|
7477
|
-
const res = await compose().plan();
|
|
7478
|
-
expect(res.status).toBe(200);
|
|
7479
|
-
const body = (await res.json()) as { affectedOpenWork: unknown[] };
|
|
7480
|
-
expect(body.affectedOpenWork).toEqual([]);
|
|
7481
|
-
});
|
|
7482
|
-
});
|
|
7483
|
-
|
|
7484
|
-
// ─── IRM-1.2: /agents/:id/github-installation/repos ───────────────────────────
|
|
7485
|
-
|
|
7486
|
-
describe("admin API — agent App installation repos (IRM-1.2)", () => {
|
|
7136
|
+
describe("admin API — agent App installation repos and GitHub Sync (IRM-1.2, IRM-3.1)", () => {
|
|
7487
7137
|
const OPS = "irm-ops-token";
|
|
7488
7138
|
const READER = "irm-read-token";
|
|
7489
7139
|
const AGENT_KEY = "irm-agent-token";
|
|
@@ -7500,7 +7150,8 @@ describe("admin API — agent App installation repos (IRM-1.2)", () => {
|
|
|
7500
7150
|
"utf-8",
|
|
7501
7151
|
),
|
|
7502
7152
|
);
|
|
7503
|
-
// The recorded org installation belongs to App 777 (installation 501)
|
|
7153
|
+
// The recorded org installation belongs to App 777 (installation 501) and
|
|
7154
|
+
// covers acme-eng/repo-1 and repo-2.
|
|
7504
7155
|
const INST = "/enterprises/acme/apps/organizations/acme-eng/installations";
|
|
7505
7156
|
const routes: Record<string, string | unknown> = {
|
|
7506
7157
|
"GET /enterprises/acme/installation": "enterprise_installation_success",
|
|
@@ -7512,6 +7163,7 @@ describe("admin API — agent App installation repos (IRM-1.2)", () => {
|
|
|
7512
7163
|
[`PATCH ${INST}/501/repositories/add`]: "install_repos_add_success",
|
|
7513
7164
|
[`PATCH ${INST}/501/repositories/remove`]: "install_repos_remove_success",
|
|
7514
7165
|
};
|
|
7166
|
+
const LIST = `GET ${INST}/501/repositories`;
|
|
7515
7167
|
|
|
7516
7168
|
function setup(
|
|
7517
7169
|
opts: {
|
|
@@ -7519,10 +7171,19 @@ describe("admin API — agent App installation repos (IRM-1.2)", () => {
|
|
|
7519
7171
|
username?: string | null;
|
|
7520
7172
|
known?: boolean;
|
|
7521
7173
|
override?: Record<string, string>;
|
|
7174
|
+
agentRepos?: string[];
|
|
7175
|
+
pushable?: string[];
|
|
7176
|
+
openWork?: Partial<OpenWorkPort> | null;
|
|
7522
7177
|
} = {},
|
|
7523
7178
|
) {
|
|
7524
7179
|
const audits: Array<Record<string, unknown>> = [];
|
|
7525
7180
|
const gh: string[] = [];
|
|
7181
|
+
const patches: Array<{ key: string; names: string[] }> = [];
|
|
7182
|
+
const events: unknown[][] = [];
|
|
7183
|
+
const state = {
|
|
7184
|
+
repos: [...(opts.agentRepos ?? ["acme-eng/repo-2"])],
|
|
7185
|
+
installed: ["acme-eng/repo-1", "acme-eng/repo-2"],
|
|
7186
|
+
};
|
|
7526
7187
|
const table = { ...routes, ...(opts.override ?? {}) };
|
|
7527
7188
|
const fetchFn = (async (input: RequestInfo | URL, init?: RequestInit) => {
|
|
7528
7189
|
const u = new URL(input.toString());
|
|
@@ -7538,6 +7199,33 @@ describe("admin API — agent App installation repos (IRM-1.2)", () => {
|
|
|
7538
7199
|
if (!name) throw new Error(`no route for ${key}`);
|
|
7539
7200
|
const entry = cassette[name];
|
|
7540
7201
|
if (!entry) throw new Error(`no cassette entry ${name}`);
|
|
7202
|
+
// The installation's repo list follows the changes applied to it.
|
|
7203
|
+
if (key === LIST && name === "install_repos_success") {
|
|
7204
|
+
return new Response(
|
|
7205
|
+
JSON.stringify(
|
|
7206
|
+
state.installed.map((full_name, i) => ({
|
|
7207
|
+
id: i + 1,
|
|
7208
|
+
name: full_name.split("/")[1],
|
|
7209
|
+
full_name,
|
|
7210
|
+
})),
|
|
7211
|
+
),
|
|
7212
|
+
{ status: 200, headers: { "Content-Type": "application/json" } },
|
|
7213
|
+
);
|
|
7214
|
+
}
|
|
7215
|
+
if (key.startsWith("PATCH") && entry.status === 200) {
|
|
7216
|
+
const body = JSON.parse(String(init?.body ?? "{}")) as {
|
|
7217
|
+
repositories?: string[];
|
|
7218
|
+
};
|
|
7219
|
+
const names = body.repositories ?? [];
|
|
7220
|
+
patches.push({ key, names });
|
|
7221
|
+
events.push([key.endsWith("/add") ? "addRepos" : "removeRepos", names]);
|
|
7222
|
+
const full = names.map((n) =>
|
|
7223
|
+
`acme-eng/${n}`.replace("acme-eng/acme-eng/", "acme-eng/"),
|
|
7224
|
+
);
|
|
7225
|
+
state.installed = key.endsWith("/add")
|
|
7226
|
+
? [...state.installed, ...full]
|
|
7227
|
+
: state.installed.filter((r) => !full.includes(r));
|
|
7228
|
+
}
|
|
7541
7229
|
return new Response(JSON.stringify(entry.body), {
|
|
7542
7230
|
status: entry.status,
|
|
7543
7231
|
headers: { "Content-Type": "application/json" },
|
|
@@ -7550,13 +7238,18 @@ describe("admin API — agent App installation repos (IRM-1.2)", () => {
|
|
|
7550
7238
|
? null
|
|
7551
7239
|
: {
|
|
7552
7240
|
id: AGENT_ID,
|
|
7553
|
-
repos: [],
|
|
7241
|
+
repos: [...state.repos],
|
|
7554
7242
|
githubUsername:
|
|
7555
7243
|
opts.username === undefined ? "dev" : opts.username,
|
|
7556
7244
|
},
|
|
7245
|
+
update: async (args: { data: { repos: string[] } }) => {
|
|
7246
|
+
events.push(["setRepos", args.data.repos]);
|
|
7247
|
+
state.repos = args.data.repos;
|
|
7248
|
+
},
|
|
7557
7249
|
},
|
|
7558
7250
|
gitHubReconcileAudit: {
|
|
7559
7251
|
create: async (args: { data: Record<string, unknown> }) => {
|
|
7252
|
+
events.push(["audit"]);
|
|
7560
7253
|
audits.push(args.data);
|
|
7561
7254
|
return { id: `audit-${audits.length}` };
|
|
7562
7255
|
},
|
|
@@ -7564,14 +7257,29 @@ describe("admin API — agent App installation repos (IRM-1.2)", () => {
|
|
|
7564
7257
|
} as unknown as Parameters<
|
|
7565
7258
|
typeof createGithubInstallationRepos
|
|
7566
7259
|
>[0]["prisma"];
|
|
7567
|
-
const
|
|
7260
|
+
const openWork: OpenWorkPort | undefined =
|
|
7261
|
+
opts.openWork === null
|
|
7262
|
+
? undefined
|
|
7263
|
+
: {
|
|
7264
|
+
listOpenWork: async (...a) => {
|
|
7265
|
+
events.push(["list", ...a]);
|
|
7266
|
+
return [{ id: "T-1", repo: "acme-eng/repo-2", title: "fix it" }];
|
|
7267
|
+
},
|
|
7268
|
+
blockOpenWork: async (...a) => {
|
|
7269
|
+
events.push(["block", ...a]);
|
|
7270
|
+
},
|
|
7271
|
+
...opts.openWork,
|
|
7272
|
+
};
|
|
7273
|
+
const wiring = {
|
|
7568
7274
|
prisma,
|
|
7569
7275
|
appService: {
|
|
7570
7276
|
get: async () => ({ appId: 12345, enterpriseSlug: "acme" }) as never,
|
|
7571
7277
|
getPrivateKey: async () => PEM,
|
|
7572
7278
|
},
|
|
7573
|
-
access: async (username) => {
|
|
7574
|
-
const base = await accessFor(
|
|
7279
|
+
access: async (username: string) => {
|
|
7280
|
+
const base = await accessFor(
|
|
7281
|
+
opts.pushable ?? ["acme-eng/repo-1", "acme-eng/repo-3"],
|
|
7282
|
+
)(username);
|
|
7575
7283
|
if (base.status === "unconfigured") return base;
|
|
7576
7284
|
const [org] = base.access.orgs;
|
|
7577
7285
|
return {
|
|
@@ -7584,6 +7292,11 @@ describe("admin API — agent App installation repos (IRM-1.2)", () => {
|
|
|
7584
7292
|
},
|
|
7585
7293
|
agentEnv: envWith(opts.env ?? { GH_APP_ID: "777" }),
|
|
7586
7294
|
fetchFn,
|
|
7295
|
+
};
|
|
7296
|
+
const githubInstallationRepos = createGithubInstallationRepos(wiring);
|
|
7297
|
+
const githubSync = createGithubSync({
|
|
7298
|
+
...wiring,
|
|
7299
|
+
...(openWork ? { openWork } : {}),
|
|
7587
7300
|
});
|
|
7588
7301
|
const base = makeMockDeps();
|
|
7589
7302
|
const app = createAdminApp({
|
|
@@ -7597,20 +7310,21 @@ describe("admin API — agent App installation repos (IRM-1.2)", () => {
|
|
|
7597
7310
|
t === AGENT_KEY ? { agentId: AGENT_ID } : null,
|
|
7598
7311
|
},
|
|
7599
7312
|
githubInstallationRepos,
|
|
7313
|
+
githubSync,
|
|
7600
7314
|
});
|
|
7601
|
-
return { app, audits, gh };
|
|
7315
|
+
return { app, audits, gh, patches, events, state };
|
|
7602
7316
|
}
|
|
7603
7317
|
|
|
7604
7318
|
const asAdmin = { Authorization: `Bearer ${ADMIN_API_KEY}` };
|
|
7605
|
-
const url = (
|
|
7606
|
-
|
|
7607
|
-
const
|
|
7319
|
+
const url = (id = AGENT_ID) => `/agents/${id}/github-installation/repos`;
|
|
7320
|
+
const previewUrl = `/agents/${AGENT_ID}/github-sync/preview`;
|
|
7321
|
+
const applyUrl = `/agents/${AGENT_ID}/github-sync/apply`;
|
|
7322
|
+
const apply = (
|
|
7608
7323
|
app: ReturnType<typeof createAdminApp>,
|
|
7609
|
-
|
|
7610
|
-
headers: Record<string, string
|
|
7611
|
-
body: unknown = { repos: ["acme-eng/repo-3"] },
|
|
7324
|
+
body: unknown,
|
|
7325
|
+
headers: Record<string, string> = asAdmin,
|
|
7612
7326
|
) =>
|
|
7613
|
-
app.request(
|
|
7327
|
+
app.request(applyUrl, {
|
|
7614
7328
|
method: "POST",
|
|
7615
7329
|
headers: { "Content-Type": "application/json", ...headers },
|
|
7616
7330
|
body: JSON.stringify(body),
|
|
@@ -7647,20 +7361,41 @@ describe("admin API — agent App installation repos (IRM-1.2)", () => {
|
|
|
7647
7361
|
]);
|
|
7648
7362
|
});
|
|
7649
7363
|
|
|
7364
|
+
it("the direct add and remove routes are gone", async () => {
|
|
7365
|
+
const { app, audits, gh } = setup();
|
|
7366
|
+
for (const [method, path] of [
|
|
7367
|
+
["POST", url()],
|
|
7368
|
+
["POST", `${url()}/remove`],
|
|
7369
|
+
["GET", `/agents/${AGENT_ID}/github-lost-access/preview`],
|
|
7370
|
+
["POST", `/agents/${AGENT_ID}/github-lost-access/remove`],
|
|
7371
|
+
] as const) {
|
|
7372
|
+
const res = await app.request(path, {
|
|
7373
|
+
method,
|
|
7374
|
+
headers: { "Content-Type": "application/json", ...asAdmin },
|
|
7375
|
+
body:
|
|
7376
|
+
method === "POST" ? JSON.stringify({ repos: ["a/b"] }) : undefined,
|
|
7377
|
+
});
|
|
7378
|
+
expect([404, 405]).toContain(res.status);
|
|
7379
|
+
}
|
|
7380
|
+
expect(audits).toEqual([]);
|
|
7381
|
+
expect(gh).toEqual([]);
|
|
7382
|
+
});
|
|
7383
|
+
|
|
7650
7384
|
it("403 for ops, read and agent keys on every route; no GitHub call, no audit", async () => {
|
|
7651
7385
|
const { app, audits, gh } = setup();
|
|
7652
7386
|
for (const key of [OPS, READER, AGENT_KEY]) {
|
|
7653
7387
|
const h = { Authorization: `Bearer ${key}` };
|
|
7654
7388
|
expect((await app.request(url(), { headers: h })).status).toBe(403);
|
|
7655
|
-
expect((await
|
|
7656
|
-
expect(
|
|
7389
|
+
expect((await app.request(previewUrl, { headers: h })).status).toBe(403);
|
|
7390
|
+
expect(
|
|
7391
|
+
(await apply(app, { appAdds: ["acme-eng/repo-3"] }, h)).status,
|
|
7392
|
+
).toBe(403);
|
|
7657
7393
|
}
|
|
7658
7394
|
expect(audits).toEqual([]);
|
|
7659
7395
|
expect(gh).toEqual([]);
|
|
7660
7396
|
});
|
|
7661
7397
|
|
|
7662
7398
|
it("403 for a non-admin session member", async () => {
|
|
7663
|
-
const { app } = setup();
|
|
7664
7399
|
const cookie = await sign(
|
|
7665
7400
|
{
|
|
7666
7401
|
isAdmin: false,
|
|
@@ -7684,91 +7419,27 @@ describe("admin API — agent App installation repos (IRM-1.2)", () => {
|
|
|
7684
7419
|
headers: { Cookie: `admin_session=${cookie}` },
|
|
7685
7420
|
});
|
|
7686
7421
|
expect(res.status).toBe(403);
|
|
7687
|
-
void app;
|
|
7688
7422
|
});
|
|
7689
7423
|
|
|
7690
7424
|
it("404 for an unknown agent on every route", async () => {
|
|
7691
7425
|
const { app, audits } = setup({ known: false });
|
|
7692
7426
|
expect((await app.request(url(), { headers: asAdmin })).status).toBe(404);
|
|
7693
|
-
expect((await
|
|
7694
|
-
|
|
7427
|
+
expect((await app.request(previewUrl, { headers: asAdmin })).status).toBe(
|
|
7428
|
+
404,
|
|
7429
|
+
);
|
|
7430
|
+
expect((await apply(app, { appAdds: ["acme-eng/repo-3"] })).status).toBe(
|
|
7431
|
+
404,
|
|
7432
|
+
);
|
|
7695
7433
|
expect(audits).toEqual([]);
|
|
7696
7434
|
});
|
|
7697
7435
|
|
|
7698
|
-
it("422 naming GH_APP_ID when the agent has none", async () => {
|
|
7436
|
+
it("422 naming GH_APP_ID when the agent has none on the list route; sync is skipped", async () => {
|
|
7699
7437
|
const { app, audits } = setup({ env: {} });
|
|
7700
7438
|
const res = await app.request(url(), { headers: asAdmin });
|
|
7701
7439
|
expect(res.status).toBe(422);
|
|
7702
7440
|
expect(JSON.stringify(await res.json())).toContain("GH_APP_ID");
|
|
7703
|
-
|
|
7704
|
-
expect(
|
|
7705
|
-
});
|
|
7706
|
-
|
|
7707
|
-
it("409 when the agent's App is not installed on the org; nothing audited", async () => {
|
|
7708
|
-
// App 999 has no recorded installation on acme-eng.
|
|
7709
|
-
const { app, audits, gh } = setup({ env: { GH_APP_ID: "999" } });
|
|
7710
|
-
const res = await send(app, url(), asAdmin);
|
|
7711
|
-
expect(res.status).toBe(409);
|
|
7712
|
-
expect(JSON.stringify(await res.json())).toContain("not installed");
|
|
7713
|
-
expect(audits).toEqual([]);
|
|
7714
|
-
expect(gh.some((k) => k.startsWith("PATCH"))).toBe(false);
|
|
7715
|
-
});
|
|
7716
|
-
|
|
7717
|
-
it("POST add changes the installation and writes one audit entry with the caller", async () => {
|
|
7718
|
-
const { app, audits, gh } = setup();
|
|
7719
|
-
const res = await send(app, url(), asAdmin, {
|
|
7720
|
-
repos: ["acme-eng/repo-3"],
|
|
7721
|
-
});
|
|
7722
|
-
expect(res.status).toBe(200);
|
|
7723
|
-
expect(await res.json()).toEqual({
|
|
7724
|
-
status: "applied",
|
|
7725
|
-
auditId: "audit-1",
|
|
7726
|
-
repos: ["acme-eng/repo-3"],
|
|
7727
|
-
});
|
|
7728
|
-
expect(gh).toContain(`PATCH ${INST}/501/repositories/add`);
|
|
7729
|
-
expect(audits).toHaveLength(1);
|
|
7730
|
-
expect(audits[0]).toMatchObject({
|
|
7731
|
-
agentId: AGENT_ID,
|
|
7732
|
-
caller: "root",
|
|
7733
|
-
planHash: "direct:add",
|
|
7734
|
-
diff: { installationAdds: ["acme-eng/repo-3"], installationRemoves: [] },
|
|
7735
|
-
result: { status: "applied" },
|
|
7736
|
-
});
|
|
7737
|
-
});
|
|
7738
|
-
|
|
7739
|
-
it("POST remove changes the installation and writes one audit entry", async () => {
|
|
7740
|
-
const { app, audits, gh } = setup();
|
|
7741
|
-
const res = await send(app, url("/remove"), asAdmin, {
|
|
7742
|
-
repos: ["acme-eng/repo-2"],
|
|
7743
|
-
});
|
|
7744
|
-
expect(res.status).toBe(200);
|
|
7745
|
-
expect(gh).toContain(`PATCH ${INST}/501/repositories/remove`);
|
|
7746
|
-
expect(audits).toHaveLength(1);
|
|
7747
|
-
expect(audits[0]).toMatchObject({
|
|
7748
|
-
caller: "root",
|
|
7749
|
-
planHash: "direct:remove",
|
|
7750
|
-
diff: { installationRemoves: ["acme-eng/repo-2"], installationAdds: [] },
|
|
7751
|
-
});
|
|
7752
|
-
});
|
|
7753
|
-
|
|
7754
|
-
it("502 and a failed audit entry when GitHub refuses the change", async () => {
|
|
7755
|
-
const { app, audits } = setup({
|
|
7756
|
-
override: {
|
|
7757
|
-
[`PATCH ${INST}/501/repositories/add`]: "install_repos_add_403",
|
|
7758
|
-
},
|
|
7759
|
-
});
|
|
7760
|
-
const res = await send(app, url(), asAdmin);
|
|
7761
|
-
expect(res.status).toBe(502);
|
|
7762
|
-
expect(audits).toHaveLength(1);
|
|
7763
|
-
expect(audits[0]?.result).toMatchObject({ status: "failed" });
|
|
7764
|
-
});
|
|
7765
|
-
|
|
7766
|
-
it("400 for a body that is not owner/repo names", async () => {
|
|
7767
|
-
const { app, audits } = setup();
|
|
7768
|
-
expect((await send(app, url(), asAdmin, { repos: ["nope"] })).status).toBe(
|
|
7769
|
-
400,
|
|
7770
|
-
);
|
|
7771
|
-
expect((await send(app, url(), asAdmin, { repos: [] })).status).toBe(400);
|
|
7441
|
+
const pre = await app.request(previewUrl, { headers: asAdmin });
|
|
7442
|
+
expect(await pre.json()).toMatchObject({ status: "skipped" });
|
|
7772
7443
|
expect(audits).toEqual([]);
|
|
7773
7444
|
});
|
|
7774
7445
|
|
|
@@ -7778,137 +7449,34 @@ describe("admin API — agent App installation repos (IRM-1.2)", () => {
|
|
|
7778
7449
|
adminApiKeys: parseAdminApiKeys(`root:${ADMIN_API_KEY}:*`),
|
|
7779
7450
|
});
|
|
7780
7451
|
expect((await app.request(url(), { headers: asAdmin })).status).toBe(503);
|
|
7452
|
+
expect((await app.request(previewUrl, { headers: asAdmin })).status).toBe(
|
|
7453
|
+
503,
|
|
7454
|
+
);
|
|
7455
|
+
expect((await apply(app, { appAdds: ["acme-eng/repo-3"] })).status).toBe(
|
|
7456
|
+
503,
|
|
7457
|
+
);
|
|
7781
7458
|
});
|
|
7782
|
-
});
|
|
7783
|
-
|
|
7784
|
-
// ─── IRM-1.3: /agents/:id/github-lost-access ──────────────────────────────────
|
|
7785
|
-
|
|
7786
|
-
describe("admin API — remove repos the bound user lost access to (IRM-1.3)", () => {
|
|
7787
|
-
const OPS = "irm13-ops-token";
|
|
7788
|
-
const AGENT_KEY = "irm13-agent-token";
|
|
7789
|
-
|
|
7790
|
-
function setup(
|
|
7791
|
-
opts: {
|
|
7792
|
-
repos?: string[];
|
|
7793
|
-
installed?: string[];
|
|
7794
|
-
username?: string | null;
|
|
7795
|
-
known?: boolean;
|
|
7796
|
-
openWork?: Partial<OpenWorkPort> | null;
|
|
7797
|
-
access?: AdminDeps["githubAccessPreview"];
|
|
7798
|
-
} = {},
|
|
7799
|
-
) {
|
|
7800
|
-
const state = {
|
|
7801
|
-
repos: [...(opts.repos ?? ["acme/api", "acme/old"])],
|
|
7802
|
-
installed: [...(opts.installed ?? ["acme/api", "acme/old", "acme/gone"])],
|
|
7803
|
-
};
|
|
7804
|
-
const audits: Array<Record<string, unknown>> = [];
|
|
7805
|
-
const calls: unknown[][] = [];
|
|
7806
|
-
const prisma = {
|
|
7807
|
-
agent: {
|
|
7808
|
-
findUnique: async () =>
|
|
7809
|
-
opts.known === false
|
|
7810
|
-
? null
|
|
7811
|
-
: {
|
|
7812
|
-
id: AGENT_ID,
|
|
7813
|
-
repos: [...state.repos],
|
|
7814
|
-
githubUsername:
|
|
7815
|
-
opts.username === undefined ? "dev" : opts.username,
|
|
7816
|
-
},
|
|
7817
|
-
update: async (args: { data: { repos: string[] } }) => {
|
|
7818
|
-
calls.push(["setRepos", args.data.repos]);
|
|
7819
|
-
state.repos = args.data.repos;
|
|
7820
|
-
},
|
|
7821
|
-
},
|
|
7822
|
-
gitHubReconcileAudit: {
|
|
7823
|
-
create: async (args: { data: Record<string, unknown> }) => {
|
|
7824
|
-
calls.push(["audit"]);
|
|
7825
|
-
audits.push(args.data);
|
|
7826
|
-
return { id: `audit-${audits.length}` };
|
|
7827
|
-
},
|
|
7828
|
-
},
|
|
7829
|
-
} as unknown as Parameters<
|
|
7830
|
-
typeof createGithubLostAccessRemoval
|
|
7831
|
-
>[0]["prisma"];
|
|
7832
|
-
const openWork: OpenWorkPort | undefined =
|
|
7833
|
-
opts.openWork === null
|
|
7834
|
-
? undefined
|
|
7835
|
-
: {
|
|
7836
|
-
listOpenWork: async (...a) => {
|
|
7837
|
-
calls.push(["list", ...a]);
|
|
7838
|
-
return [{ id: "T-1", repo: "acme/old", title: "fix it" }];
|
|
7839
|
-
},
|
|
7840
|
-
blockOpenWork: async (...a) => {
|
|
7841
|
-
calls.push(["block", ...a]);
|
|
7842
|
-
},
|
|
7843
|
-
...opts.openWork,
|
|
7844
|
-
};
|
|
7845
|
-
const githubLostAccessRemoval = createGithubLostAccessRemoval({
|
|
7846
|
-
prisma,
|
|
7847
|
-
appService: { get: async () => null, getPrivateKey: async () => null },
|
|
7848
|
-
access: opts.access ?? accessFor(["acme/api"]),
|
|
7849
|
-
agentEnv: envWith({ GH_APP_ID: "42" }),
|
|
7850
|
-
installationRepos: {
|
|
7851
|
-
listInstalled: async () => [
|
|
7852
|
-
{ org: "acme", repos: [...state.installed] },
|
|
7853
|
-
],
|
|
7854
|
-
listRepos: async () => ({ acme: [...state.installed] }),
|
|
7855
|
-
addRepos: async () => {},
|
|
7856
|
-
removeRepos: async (_appId, org, repos) => {
|
|
7857
|
-
calls.push(["removeRepos", org, [...repos]]);
|
|
7858
|
-
state.installed = state.installed.filter((r) => !repos.includes(r));
|
|
7859
|
-
},
|
|
7860
|
-
},
|
|
7861
|
-
...(openWork ? { openWork } : {}),
|
|
7862
|
-
});
|
|
7863
|
-
const base = makeMockDeps();
|
|
7864
|
-
const app = createAdminApp({
|
|
7865
|
-
...base,
|
|
7866
|
-
adminApiKeys: parseAdminApiKeys(
|
|
7867
|
-
`root:${ADMIN_API_KEY}:*,operator:${OPS}:platform.ops`,
|
|
7868
|
-
),
|
|
7869
|
-
agentTokenService: {
|
|
7870
|
-
...base.agentTokenService,
|
|
7871
|
-
validate: async (t: string) =>
|
|
7872
|
-
t === AGENT_KEY ? { agentId: AGENT_ID } : null,
|
|
7873
|
-
},
|
|
7874
|
-
githubLostAccessRemoval,
|
|
7875
|
-
});
|
|
7876
|
-
return { app, audits, calls, state };
|
|
7877
|
-
}
|
|
7878
7459
|
|
|
7879
|
-
|
|
7880
|
-
|
|
7881
|
-
const removeUrl = `/agents/${AGENT_ID}/github-lost-access/remove`;
|
|
7882
|
-
const remove = (
|
|
7883
|
-
app: ReturnType<typeof createAdminApp>,
|
|
7884
|
-
body: unknown,
|
|
7885
|
-
headers: Record<string, string> = asAdmin,
|
|
7886
|
-
) =>
|
|
7887
|
-
app.request(removeUrl, {
|
|
7888
|
-
method: "POST",
|
|
7889
|
-
headers: { "Content-Type": "application/json", ...headers },
|
|
7890
|
-
body: JSON.stringify(body),
|
|
7891
|
-
});
|
|
7892
|
-
|
|
7893
|
-
it("preview lists the repos that would be removed and their open work, changing nothing", async () => {
|
|
7894
|
-
const { app, audits, calls, state } = setup();
|
|
7460
|
+
it("sync preview lists app adds, app removes, agent removes and the open work, changing nothing", async () => {
|
|
7461
|
+
const { app, audits, patches, events, state } = setup();
|
|
7895
7462
|
const res = await app.request(previewUrl, { headers: asAdmin });
|
|
7896
7463
|
expect(res.status).toBe(200);
|
|
7897
7464
|
expect(await res.json()).toEqual({
|
|
7898
7465
|
status: "previewed",
|
|
7899
|
-
|
|
7900
|
-
|
|
7901
|
-
|
|
7466
|
+
appAdds: ["acme-eng/repo-3"],
|
|
7467
|
+
appRemoves: ["acme-eng/repo-2"],
|
|
7468
|
+
agentRemoves: ["acme-eng/repo-2"],
|
|
7902
7469
|
skippedOrgs: [],
|
|
7903
|
-
openWork: [{ id: "T-1", repo: "acme/
|
|
7470
|
+
openWork: [{ id: "T-1", repo: "acme-eng/repo-2", title: "fix it" }],
|
|
7904
7471
|
warnings: [],
|
|
7905
7472
|
});
|
|
7906
7473
|
expect(audits).toEqual([]);
|
|
7907
|
-
expect(
|
|
7908
|
-
expect(
|
|
7474
|
+
expect(patches).toEqual([]);
|
|
7475
|
+
expect(events.map((e) => e[0])).toEqual(["list"]);
|
|
7476
|
+
expect(state.repos).toEqual(["acme-eng/repo-2"]);
|
|
7909
7477
|
});
|
|
7910
7478
|
|
|
7911
|
-
it("preview reports a failing open-work port as a warning", async () => {
|
|
7479
|
+
it("sync preview reports a failing open-work port as a warning", async () => {
|
|
7912
7480
|
const { app } = setup({
|
|
7913
7481
|
openWork: {
|
|
7914
7482
|
listOpenWork: async () => {
|
|
@@ -7923,132 +7491,117 @@ describe("admin API — remove repos the bound user lost access to (IRM-1.3)", (
|
|
|
7923
7491
|
expect(body.warnings.join()).toContain("store down");
|
|
7924
7492
|
});
|
|
7925
7493
|
|
|
7926
|
-
it("
|
|
7927
|
-
const { app, audits,
|
|
7928
|
-
const res = await
|
|
7494
|
+
it("sync apply changes the confirmed repos, audits once, blocks open work; a second preview has nothing to do", async () => {
|
|
7495
|
+
const { app, audits, events, state } = setup();
|
|
7496
|
+
const res = await apply(app, {
|
|
7497
|
+
appAdds: ["acme-eng/repo-3", "acme-eng/repo-1"],
|
|
7498
|
+
appRemoves: ["acme-eng/repo-2"],
|
|
7499
|
+
agentRemoves: ["acme-eng/repo-2"],
|
|
7500
|
+
});
|
|
7929
7501
|
expect(res.status).toBe(200);
|
|
7930
7502
|
expect(await res.json()).toEqual({
|
|
7931
7503
|
status: "applied",
|
|
7932
7504
|
auditId: "audit-1",
|
|
7933
|
-
|
|
7934
|
-
|
|
7935
|
-
|
|
7936
|
-
|
|
7505
|
+
appAdds: ["acme-eng/repo-3"],
|
|
7506
|
+
appRemoves: ["acme-eng/repo-2"],
|
|
7507
|
+
agentRemoves: ["acme-eng/repo-2"],
|
|
7508
|
+
notApplied: [
|
|
7937
7509
|
{
|
|
7938
|
-
repo: "acme/
|
|
7939
|
-
reason: "
|
|
7510
|
+
repo: "acme-eng/repo-1",
|
|
7511
|
+
reason: "already on the agent installation",
|
|
7940
7512
|
},
|
|
7941
7513
|
],
|
|
7942
7514
|
skippedOrgs: [],
|
|
7943
7515
|
warnings: [],
|
|
7944
7516
|
});
|
|
7945
|
-
expect(
|
|
7946
|
-
["
|
|
7947
|
-
["
|
|
7517
|
+
expect(events).toEqual([
|
|
7518
|
+
["addRepos", ["repo-3"]],
|
|
7519
|
+
["removeRepos", ["repo-2"]],
|
|
7520
|
+
["setRepos", []],
|
|
7948
7521
|
["audit"],
|
|
7949
|
-
[
|
|
7522
|
+
[
|
|
7523
|
+
"block",
|
|
7524
|
+
AGENT_ID,
|
|
7525
|
+
["acme-eng/repo-2"],
|
|
7526
|
+
repoLostAccessBlockReason("acme-eng/repo-2"),
|
|
7527
|
+
],
|
|
7950
7528
|
]);
|
|
7951
|
-
expect(state.repos).toEqual(["acme/api"]);
|
|
7952
|
-
expect(state.installed).toEqual(["acme/api", "acme/gone"]);
|
|
7953
7529
|
expect(audits).toHaveLength(1);
|
|
7954
7530
|
expect(audits[0]).toMatchObject({
|
|
7955
7531
|
agentId: AGENT_ID,
|
|
7956
7532
|
caller: "root",
|
|
7957
|
-
planHash: "
|
|
7533
|
+
planHash: "sync:apply",
|
|
7958
7534
|
result: { status: "applied" },
|
|
7959
7535
|
});
|
|
7536
|
+
expect(state.repos).toEqual([]);
|
|
7537
|
+
const again = await app.request(previewUrl, { headers: asAdmin });
|
|
7538
|
+
expect(await again.json()).toMatchObject({
|
|
7539
|
+
status: "previewed",
|
|
7540
|
+
appAdds: [],
|
|
7541
|
+
appRemoves: [],
|
|
7542
|
+
agentRemoves: [],
|
|
7543
|
+
openWork: [],
|
|
7544
|
+
});
|
|
7960
7545
|
});
|
|
7961
7546
|
|
|
7962
|
-
it("a failing open-work port is a warning, not a failed call", async () => {
|
|
7963
|
-
const { app,
|
|
7547
|
+
it("sync apply: a failing open-work port is a warning, not a failed call", async () => {
|
|
7548
|
+
const { app, audits } = setup({
|
|
7964
7549
|
openWork: {
|
|
7965
7550
|
blockOpenWork: async () => {
|
|
7966
7551
|
throw new Error("store down");
|
|
7967
7552
|
},
|
|
7968
7553
|
},
|
|
7969
7554
|
});
|
|
7970
|
-
const res = await
|
|
7555
|
+
const res = await apply(app, { agentRemoves: ["acme-eng/repo-2"] });
|
|
7971
7556
|
expect(res.status).toBe(200);
|
|
7972
7557
|
const body = (await res.json()) as { status: string; warnings: string[] };
|
|
7973
7558
|
expect(body.status).toBe("applied");
|
|
7974
|
-
expect(body.warnings[0]).toContain("acme/old");
|
|
7975
7559
|
expect(body.warnings[0]).toContain("store down");
|
|
7976
|
-
expect(state.repos).toEqual(["acme/api"]);
|
|
7977
7560
|
expect(audits).toHaveLength(1);
|
|
7978
7561
|
});
|
|
7979
7562
|
|
|
7980
|
-
it("
|
|
7981
|
-
const { app, audits,
|
|
7982
|
-
|
|
7983
|
-
|
|
7984
|
-
expect(await res.json()).toMatchObject({ status: "skipped" });
|
|
7985
|
-
expect((await app.request(previewUrl, { headers: asAdmin })).status).toBe(
|
|
7986
|
-
200,
|
|
7987
|
-
);
|
|
7988
|
-
expect(audits).toEqual([]);
|
|
7989
|
-
expect(calls).toEqual([]);
|
|
7990
|
-
});
|
|
7991
|
-
|
|
7992
|
-
it("skipped for an unverified user: nothing changed, no audit", async () => {
|
|
7993
|
-
const verified = accessFor(["acme/api"]);
|
|
7994
|
-
const { app, audits, calls } = setup({
|
|
7995
|
-
access: async (u) => {
|
|
7996
|
-
const r = await verified(u);
|
|
7997
|
-
return r.status === "ok"
|
|
7998
|
-
? { ...r, access: { ...r.access, userVerified: false } }
|
|
7999
|
-
: r;
|
|
7563
|
+
it("sync apply: 502 and a failed audit entry when GitHub refuses a change", async () => {
|
|
7564
|
+
const { app, audits, state } = setup({
|
|
7565
|
+
override: {
|
|
7566
|
+
[`PATCH ${INST}/501/repositories/add`]: "install_repos_add_403",
|
|
8000
7567
|
},
|
|
8001
7568
|
});
|
|
8002
|
-
const res = await
|
|
8003
|
-
|
|
8004
|
-
|
|
8005
|
-
|
|
7569
|
+
const res = await apply(app, {
|
|
7570
|
+
appAdds: ["acme-eng/repo-3"],
|
|
7571
|
+
agentRemoves: ["acme-eng/repo-2"],
|
|
7572
|
+
});
|
|
7573
|
+
expect(res.status).toBe(502);
|
|
7574
|
+
expect(audits).toHaveLength(1);
|
|
7575
|
+
expect(audits[0]?.result).toMatchObject({ status: "failed" });
|
|
7576
|
+
expect(state.repos).toEqual(["acme-eng/repo-2"]);
|
|
7577
|
+
});
|
|
7578
|
+
|
|
7579
|
+
it("sync apply: skipped for an unbound user, nothing changed, no audit", async () => {
|
|
7580
|
+
const { app, audits, gh } = setup({ username: null });
|
|
7581
|
+
const res = await apply(app, { appAdds: ["acme-eng/repo-3"] });
|
|
7582
|
+
expect(await res.json()).toMatchObject({ status: "skipped" });
|
|
8006
7583
|
expect(audits).toEqual([]);
|
|
8007
|
-
expect(
|
|
7584
|
+
expect(gh).toEqual([]);
|
|
8008
7585
|
});
|
|
8009
7586
|
|
|
8010
|
-
it("422 for more than 10 removals without confirmRemovals; nothing changed", async () => {
|
|
8011
|
-
const many = Array.from({ length: 11 }, (_, i) => `acme/r${i}`);
|
|
8012
|
-
const { app, audits, state } = setup({
|
|
8013
|
-
const res = await
|
|
7587
|
+
it("sync apply: 422 for more than 10 removals without confirmRemovals; nothing changed", async () => {
|
|
7588
|
+
const many = Array.from({ length: 11 }, (_, i) => `acme-eng/r${i}`);
|
|
7589
|
+
const { app, audits, state } = setup({ agentRepos: many });
|
|
7590
|
+
const res = await apply(app, { agentRemoves: many });
|
|
8014
7591
|
expect(res.status).toBe(422);
|
|
8015
7592
|
expect(JSON.stringify(await res.json())).toContain("confirmRemovals");
|
|
8016
7593
|
expect(audits).toEqual([]);
|
|
8017
7594
|
expect(state.repos).toEqual(many);
|
|
8018
|
-
const ok = await
|
|
7595
|
+
const ok = await apply(app, { agentRemoves: many, confirmRemovals: true });
|
|
8019
7596
|
expect(ok.status).toBe(200);
|
|
8020
7597
|
expect(state.repos).toEqual([]);
|
|
8021
7598
|
});
|
|
8022
7599
|
|
|
8023
|
-
it("noop when nothing
|
|
7600
|
+
it("sync apply: noop when nothing confirmed still applies; 400 for bad names", async () => {
|
|
8024
7601
|
const { app, audits } = setup();
|
|
8025
|
-
const res = await
|
|
8026
|
-
expect(res.
|
|
8027
|
-
expect(await
|
|
7602
|
+
const res = await apply(app, { appRemoves: ["acme-eng/repo-1"] });
|
|
7603
|
+
expect(await res.json()).toMatchObject({ status: "noop" });
|
|
7604
|
+
expect((await apply(app, { appAdds: ["nope"] })).status).toBe(400);
|
|
8028
7605
|
expect(audits).toEqual([]);
|
|
8029
7606
|
});
|
|
8030
|
-
|
|
8031
|
-
it("403 for ops and agent keys; 404 unknown agent; 400 bad body; 503 unwired", async () => {
|
|
8032
|
-
const { app, audits } = setup();
|
|
8033
|
-
for (const key of [OPS, AGENT_KEY]) {
|
|
8034
|
-
const h = { Authorization: `Bearer ${key}` };
|
|
8035
|
-
expect((await app.request(previewUrl, { headers: h })).status).toBe(403);
|
|
8036
|
-
expect((await remove(app, { repos: ["acme/old"] }, h)).status).toBe(403);
|
|
8037
|
-
}
|
|
8038
|
-
expect((await remove(app, { repos: [] })).status).toBe(400);
|
|
8039
|
-
expect((await remove(app, { repos: ["nope"] })).status).toBe(400);
|
|
8040
|
-
expect(audits).toEqual([]);
|
|
8041
|
-
const unknown = setup({ known: false }).app;
|
|
8042
|
-
expect(
|
|
8043
|
-
(await unknown.request(previewUrl, { headers: asAdmin })).status,
|
|
8044
|
-
).toBe(404);
|
|
8045
|
-
expect((await remove(unknown, { repos: ["acme/old"] })).status).toBe(404);
|
|
8046
|
-
const unwired = createAdminApp({
|
|
8047
|
-
...makeMockDeps(),
|
|
8048
|
-
adminApiKeys: parseAdminApiKeys(`root:${ADMIN_API_KEY}:*`),
|
|
8049
|
-
});
|
|
8050
|
-
expect(
|
|
8051
|
-
(await unwired.request(previewUrl, { headers: asAdmin })).status,
|
|
8052
|
-
).toBe(503);
|
|
8053
|
-
});
|
|
8054
7607
|
});
|