@app-vitals/agent-admin 1.87.0 → 1.89.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/prisma/client/internal/class.ts +4 -4
- package/prisma/client/internal/prismaNamespace.ts +1 -0
- package/prisma/client/internal/prismaNamespaceBrowser.ts +1 -0
- package/prisma/client/models/GitHubRepoSnapshot.ts +33 -1
- package/prisma/migrations/20261125000000_add_github_snapshot_repository_selection/migration.sql +2 -0
- package/prisma/schema/schema.prisma +2 -0
- package/src/admin-boot.ts +11 -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-agent-installation-crawler.ts +27 -8
- package/src/github-agent-installation-crawler.unit.test.ts +76 -1
- 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-selection.integration.test.ts +91 -0
- package/src/github-installation-wiring.ts +5 -0
- package/src/github-repo-cache.ts +8 -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 +43 -0
- package/src/installation-repos-service.ts +2 -0
|
@@ -150,3 +150,68 @@ describe("repository client validation", () => {
|
|
|
150
150
|
expect(err.kind).toBe("validation");
|
|
151
151
|
});
|
|
152
152
|
});
|
|
153
|
+
|
|
154
|
+
describe("GithubEnterpriseClient.uninstallFromOrg", () => {
|
|
155
|
+
const { privateKey } = require("node:crypto").generateKeyPairSync("rsa", {
|
|
156
|
+
modulusLength: 2048,
|
|
157
|
+
privateKeyEncoding: { type: "pkcs8", format: "pem" },
|
|
158
|
+
publicKeyEncoding: { type: "spki", format: "pem" },
|
|
159
|
+
});
|
|
160
|
+
const make = (deleteStatus: number) => {
|
|
161
|
+
const calls: { method: string; path: string; auth: string }[] = [];
|
|
162
|
+
const fetchFn = (async (input: RequestInfo | URL, init?: RequestInit) => {
|
|
163
|
+
const u = new URL(input.toString());
|
|
164
|
+
const method = init?.method ?? "GET";
|
|
165
|
+
calls.push({
|
|
166
|
+
method,
|
|
167
|
+
path: u.pathname,
|
|
168
|
+
auth: ((init?.headers ?? {}) as Record<string, string>)
|
|
169
|
+
.Authorization as string,
|
|
170
|
+
});
|
|
171
|
+
if (u.pathname === "/enterprises/acme/installation")
|
|
172
|
+
return Response.json({ id: 4242 });
|
|
173
|
+
if (u.pathname === "/app/installations/4242/access_tokens")
|
|
174
|
+
return Response.json(
|
|
175
|
+
{ token: "ghs_t", expires_at: "2099-01-01T00:00:00Z" },
|
|
176
|
+
{ status: 201 },
|
|
177
|
+
);
|
|
178
|
+
if (method === "DELETE") {
|
|
179
|
+
return deleteStatus === 204
|
|
180
|
+
? new Response(null, { status: 204 })
|
|
181
|
+
: Response.json({ message: "nope" }, { status: deleteStatus });
|
|
182
|
+
}
|
|
183
|
+
throw new Error(`unexpected ${method} ${u.pathname}`);
|
|
184
|
+
}) as typeof fetch;
|
|
185
|
+
const client = new GithubEnterpriseClient({
|
|
186
|
+
appId: "1",
|
|
187
|
+
enterprise: "acme",
|
|
188
|
+
privateKey,
|
|
189
|
+
fetchFn,
|
|
190
|
+
});
|
|
191
|
+
return { client, calls };
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
it("DELETEs the org installation with the installation token (204)", async () => {
|
|
195
|
+
const { client, calls } = make(204);
|
|
196
|
+
await client.uninstallFromOrg("org-1", 601);
|
|
197
|
+
const del = calls.find((c) => c.method === "DELETE");
|
|
198
|
+
expect(del?.path).toBe(
|
|
199
|
+
"/enterprises/acme/apps/organizations/org-1/installations/601",
|
|
200
|
+
);
|
|
201
|
+
expect(del?.auth).toBe("Bearer ghs_t");
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it("maps a refusal to a typed error", async () => {
|
|
205
|
+
const { client } = make(403);
|
|
206
|
+
const err = await client.uninstallFromOrg("org-1", 601).catch((e) => e);
|
|
207
|
+
expect(err).toBeInstanceOf(GithubEnterpriseError);
|
|
208
|
+
expect(err.kind).toBe("permission");
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it("rejects a bad installation id before any network call", async () => {
|
|
212
|
+
const { client, calls } = make(204);
|
|
213
|
+
const err = await client.uninstallFromOrg("org-1", 0).catch((e) => e);
|
|
214
|
+
expect(err.kind).toBe("validation");
|
|
215
|
+
expect(calls).toHaveLength(0);
|
|
216
|
+
});
|
|
217
|
+
});
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/github-installation-selection.integration.test.ts
|
|
3
|
+
* Real-database test (GIS-1.1): the agent installation crawl records whether
|
|
4
|
+
* the agent App's installation is on "all" or "selected" repositories on the
|
|
5
|
+
* cached snapshot, and listAgentSnapshots exposes it. A missing value stays
|
|
6
|
+
* null (unknown), never "all".
|
|
7
|
+
*
|
|
8
|
+
* Requires DATABASE_URL_ADMIN_TEST to be set; skips otherwise.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
|
|
12
|
+
import type { PrismaClient } from "../prisma/client/client.ts";
|
|
13
|
+
import {
|
|
14
|
+
type AgentInstallationClient,
|
|
15
|
+
crawlAgentInstallation,
|
|
16
|
+
DEPLOYMENT_WIDE_ACCOUNT_KEY,
|
|
17
|
+
} from "./github-agent-installation-crawler.ts";
|
|
18
|
+
import { GitHubRepoCacheService } from "./github-repo-cache.ts";
|
|
19
|
+
import { createAdminPrismaClient } from "./prisma-client.ts";
|
|
20
|
+
|
|
21
|
+
const TEST_DB = process.env.DATABASE_URL_ADMIN_TEST;
|
|
22
|
+
const describeOrSkip = TEST_DB ? describe : describe.skip;
|
|
23
|
+
|
|
24
|
+
const T0 = new Date("2026-11-25T12:00:00Z");
|
|
25
|
+
const clock = { now: () => new Date(T0) };
|
|
26
|
+
const APP_ID = 4242;
|
|
27
|
+
|
|
28
|
+
type Selection = "all" | "selected" | "none" | undefined;
|
|
29
|
+
|
|
30
|
+
function client(selection: Selection): AgentInstallationClient {
|
|
31
|
+
return {
|
|
32
|
+
async listOrgInstallations() {
|
|
33
|
+
return [{ id: 900, appId: APP_ID, repositorySelection: selection }];
|
|
34
|
+
},
|
|
35
|
+
async listInstallationRepositoriesPage() {
|
|
36
|
+
return {
|
|
37
|
+
repos: [{ id: 1, fullName: "acme/a" }],
|
|
38
|
+
hasNext: false,
|
|
39
|
+
perPage: 10,
|
|
40
|
+
};
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
describeOrSkip("agent installation repository selection (integration)", () => {
|
|
46
|
+
let prisma: PrismaClient;
|
|
47
|
+
let cache: GitHubRepoCacheService;
|
|
48
|
+
|
|
49
|
+
const clean = async () => {
|
|
50
|
+
await prisma.gitHubCachedRepo.deleteMany();
|
|
51
|
+
await prisma.gitHubRepoSnapshot.deleteMany();
|
|
52
|
+
};
|
|
53
|
+
const crawl = (selection: Selection) =>
|
|
54
|
+
crawlAgentInstallation({
|
|
55
|
+
agent: { accountId: null, appId: APP_ID },
|
|
56
|
+
org: "acme",
|
|
57
|
+
client: client(selection),
|
|
58
|
+
store: cache,
|
|
59
|
+
clock,
|
|
60
|
+
});
|
|
61
|
+
const stored = async () =>
|
|
62
|
+
(
|
|
63
|
+
await cache.listAgentSnapshots(DEPLOYMENT_WIDE_ACCOUNT_KEY, String(APP_ID))
|
|
64
|
+
)[0]?.repositorySelection;
|
|
65
|
+
|
|
66
|
+
beforeEach(async () => {
|
|
67
|
+
prisma = createAdminPrismaClient(TEST_DB as string);
|
|
68
|
+
cache = new GitHubRepoCacheService(prisma, clock);
|
|
69
|
+
await clean();
|
|
70
|
+
});
|
|
71
|
+
afterEach(async () => {
|
|
72
|
+
await clean();
|
|
73
|
+
await prisma.$disconnect();
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("stores 'all', then 'selected' on a later crawl", async () => {
|
|
77
|
+
expect((await crawl("all")).outcome).toBe("complete");
|
|
78
|
+
expect(await stored()).toBe("all");
|
|
79
|
+
await crawl("selected");
|
|
80
|
+
expect(await stored()).toBe("selected");
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("leaves a missing value null, and resets a previous value to null", async () => {
|
|
84
|
+
await crawl(undefined);
|
|
85
|
+
expect(await stored()).toBeNull();
|
|
86
|
+
await crawl("all");
|
|
87
|
+
expect(await stored()).toBe("all");
|
|
88
|
+
await crawl(undefined);
|
|
89
|
+
expect(await stored()).toBeNull();
|
|
90
|
+
});
|
|
91
|
+
});
|
|
@@ -94,6 +94,11 @@ export function lazyInstallerClient(
|
|
|
94
94
|
(await client(accountId)).addRepositories(org, id, names),
|
|
95
95
|
removeRepositories: async (org, id, names, accountId) =>
|
|
96
96
|
(await client(accountId)).removeRepositories(org, id, names),
|
|
97
|
+
setRepositorySelection: async (org, id, input, accountId) =>
|
|
98
|
+
(await client(accountId)).setRepositorySelection(org, id, {
|
|
99
|
+
repositorySelection: input.repositorySelection,
|
|
100
|
+
repositories: [...input.repositories],
|
|
101
|
+
}),
|
|
97
102
|
};
|
|
98
103
|
}
|
|
99
104
|
|
package/src/github-repo-cache.ts
CHANGED
|
@@ -33,6 +33,8 @@ export type SnapshotProgress = Partial<{
|
|
|
33
33
|
crawlStartedAt: Date | null;
|
|
34
34
|
completedAt: Date | null;
|
|
35
35
|
totalCount: number | null;
|
|
36
|
+
/** Agent snapshots: the App installation's repository selection; null = unknown. */
|
|
37
|
+
repositorySelection: "all" | "selected" | null;
|
|
36
38
|
lastError: string | null;
|
|
37
39
|
attempts: number;
|
|
38
40
|
nextAttemptAt: Date | null;
|
|
@@ -71,6 +73,11 @@ export interface AccessQuery {
|
|
|
71
73
|
|
|
72
74
|
const REPO_BATCH_SIZE = 500;
|
|
73
75
|
|
|
76
|
+
/** A stored selection narrowed to the known values; anything else is unknown (null). */
|
|
77
|
+
function asSelection(raw: string | null): "all" | "selected" | null {
|
|
78
|
+
return raw === "all" || raw === "selected" ? raw : null;
|
|
79
|
+
}
|
|
80
|
+
|
|
74
81
|
function requireNonEmpty(name: string, value: unknown): void {
|
|
75
82
|
if (typeof value !== "string" || value.trim() === "") {
|
|
76
83
|
throw new BadRequestError(`${name} is required`);
|
|
@@ -178,6 +185,7 @@ export class GitHubRepoCacheService {
|
|
|
178
185
|
totalCount: r.totalCount,
|
|
179
186
|
lastError: r.lastError,
|
|
180
187
|
rateLimitResetAt: r.rateLimitResetAt,
|
|
188
|
+
repositorySelection: asSelection(r.repositorySelection),
|
|
181
189
|
attempts: r.attempts,
|
|
182
190
|
nextAttemptAt: r.nextAttemptAt,
|
|
183
191
|
leaseHolder: r.leaseHolder,
|
|
@@ -0,0 +1,297 @@
|
|
|
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
|
+
});
|