@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
|
@@ -456,31 +456,6 @@ export class GithubEnterpriseClient {
|
|
|
456
456
|
);
|
|
457
457
|
}
|
|
458
458
|
|
|
459
|
-
/** Toggle an installation between "all" and "selected" repositories. */
|
|
460
|
-
async setRepositorySelection(
|
|
461
|
-
org: string,
|
|
462
|
-
installationId: number,
|
|
463
|
-
input: SetRepositorySelectionInput,
|
|
464
|
-
): Promise<AppInstallation> {
|
|
465
|
-
const context = "GitHub set installation repository selection";
|
|
466
|
-
const path = this.repositoriesPath(org, installationId);
|
|
467
|
-
const repos = input.repositories ?? [];
|
|
468
|
-
if (input.repositorySelection === "selected" && (repos.length === 0 || repos.length > MAX_REPOSITORIES)) {
|
|
469
|
-
throw new GithubEnterpriseError(
|
|
470
|
-
"validation",
|
|
471
|
-
`setRepositorySelection: repositories must list 1-${MAX_REPOSITORIES} names when repositorySelection is "selected"`,
|
|
472
|
-
);
|
|
473
|
-
}
|
|
474
|
-
const body: Record<string, unknown> = { repository_selection: input.repositorySelection };
|
|
475
|
-
if (input.repositorySelection === "selected") body.repositories = repos;
|
|
476
|
-
const { token } = await this.getInstallationToken();
|
|
477
|
-
const res = await this.request<RawInstallation>("PATCH", path, token, context, body);
|
|
478
|
-
if (typeof res.data?.id !== "number") {
|
|
479
|
-
throw new GithubEnterpriseError("unexpected", `${context}: response missing id`);
|
|
480
|
-
}
|
|
481
|
-
return toInstallation(res.data);
|
|
482
|
-
}
|
|
483
|
-
|
|
484
459
|
// ─── internals ──────────────────────────────────────────────────────────────
|
|
485
460
|
|
|
486
461
|
private repositoriesPath(org: string, installationId: number): string {
|
|
@@ -136,24 +136,6 @@ describe("repository client validation", () => {
|
|
|
136
136
|
.catch((e) => e);
|
|
137
137
|
expect(err.kind).toBe("validation");
|
|
138
138
|
});
|
|
139
|
-
|
|
140
|
-
it("rejects selected without repositories before any network call", async () => {
|
|
141
|
-
const err = await client
|
|
142
|
-
.setRepositorySelection("o", 1, { repositorySelection: "selected" })
|
|
143
|
-
.catch((e) => e);
|
|
144
|
-
expect(err.kind).toBe("validation");
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
it("rejects selected with more than 50 repositories", async () => {
|
|
148
|
-
const repositories = Array.from({ length: 51 }, (_, i) => `r${i}`);
|
|
149
|
-
const err = await client
|
|
150
|
-
.setRepositorySelection("o", 1, {
|
|
151
|
-
repositorySelection: "selected",
|
|
152
|
-
repositories,
|
|
153
|
-
})
|
|
154
|
-
.catch((e) => e);
|
|
155
|
-
expect(err.kind).toBe("validation");
|
|
156
|
-
});
|
|
157
139
|
});
|
|
158
140
|
|
|
159
141
|
describe("GithubEnterpriseClient.uninstallFromOrg", () => {
|
|
@@ -9,21 +9,24 @@
|
|
|
9
9
|
|
|
10
10
|
import type { PrismaClient } from "../prisma/client/client.ts";
|
|
11
11
|
import type { AgentEnvService } from "./agent-envs.ts";
|
|
12
|
+
import type { Clock } from "./clock.ts";
|
|
13
|
+
import type { GithubAccessPreviewFn } from "./github-access-preview.ts";
|
|
14
|
+
import {
|
|
15
|
+
crawlAgentInstallation,
|
|
16
|
+
DEPLOYMENT_WIDE_ACCOUNT_KEY,
|
|
17
|
+
lookupKeysForApp,
|
|
18
|
+
} from "./github-agent-installation-crawler.ts";
|
|
12
19
|
import type {
|
|
13
20
|
GithubAppsState,
|
|
14
21
|
GithubAppsStateFn,
|
|
15
22
|
} from "./github-apps-state.ts";
|
|
16
|
-
import type { GithubAccessPreviewFn } from "./github-access-preview.ts";
|
|
17
|
-
import type { Clock } from "./clock.ts";
|
|
18
23
|
import type { GitHubEnterpriseAppService } from "./github-enterprise-apps.ts";
|
|
19
24
|
import { GithubEnterpriseClient } from "./github-enterprise-client.ts";
|
|
20
|
-
import { lookupAccountKeys } from "./github-agent-installation-crawler.ts";
|
|
21
25
|
import { GithubLookupClient } from "./github-lookup-client.ts";
|
|
22
26
|
import { GitHubRepoCacheService } from "./github-repo-cache.ts";
|
|
23
27
|
import type { CrawlStore } from "./github-repo-crawler.ts";
|
|
24
28
|
import { createRefreshDiscovery } from "./github-repo-refresh-discovery.ts";
|
|
25
29
|
import type { RefreshDiscovery } from "./github-repo-refresher.ts";
|
|
26
|
-
import { canPushForRole } from "./user-repo-access-resolver.ts";
|
|
27
30
|
import { type GithubSyncDeps, GithubSyncService } from "./github-sync.ts";
|
|
28
31
|
import {
|
|
29
32
|
createInstallationPort,
|
|
@@ -39,6 +42,7 @@ import {
|
|
|
39
42
|
type InstalledOrgsClient,
|
|
40
43
|
} from "./installation-repos-service.ts";
|
|
41
44
|
import type { OpenWorkPort } from "./reconcile-open-work.ts";
|
|
45
|
+
import { canPushForRole } from "./user-repo-access-resolver.ts";
|
|
42
46
|
|
|
43
47
|
export interface GithubInstallationWiringDeps {
|
|
44
48
|
prisma: PrismaClient;
|
|
@@ -94,11 +98,6 @@ export function lazyInstallerClient(
|
|
|
94
98
|
(await client(accountId)).addRepositories(org, id, names),
|
|
95
99
|
removeRepositories: async (org, id, names, accountId) =>
|
|
96
100
|
(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
|
-
}),
|
|
102
101
|
};
|
|
103
102
|
}
|
|
104
103
|
|
|
@@ -131,6 +130,24 @@ function appsStateDep(
|
|
|
131
130
|
: { appsState: githubAppsStateProbe(deps) };
|
|
132
131
|
}
|
|
133
132
|
|
|
133
|
+
/**
|
|
134
|
+
* The repo cache for the access-row reads. Rows filed under the deployment key
|
|
135
|
+
* belong to the deployment-wide lookup App; once that App is gone they are
|
|
136
|
+
* stale and must not reach any account.
|
|
137
|
+
*/
|
|
138
|
+
export function accessCache(
|
|
139
|
+
deps: Pick<InstallationReposWiringDeps, "prisma" | "appService">,
|
|
140
|
+
): GitHubRepoCacheService {
|
|
141
|
+
const cache = new GitHubRepoCacheService(deps.prisma);
|
|
142
|
+
const read = cache.getAccessRows.bind(cache);
|
|
143
|
+
cache.getAccessRows = async (query) =>
|
|
144
|
+
query.accountId === DEPLOYMENT_WIDE_ACCOUNT_KEY &&
|
|
145
|
+
(await deps.appService.get("lookup", null)) === null
|
|
146
|
+
? []
|
|
147
|
+
: read(query);
|
|
148
|
+
return cache;
|
|
149
|
+
}
|
|
150
|
+
|
|
134
151
|
export interface InstallationReposWiringDeps
|
|
135
152
|
extends GithubInstallationWiringDeps {
|
|
136
153
|
/** Test seam: replaces the installer-App-backed port. */
|
|
@@ -153,7 +170,7 @@ export function createGithubInstallationRepos(
|
|
|
153
170
|
installation:
|
|
154
171
|
deps.installationRepos ??
|
|
155
172
|
createInstallationReposPort(createInstallationPort(client), client),
|
|
156
|
-
cache:
|
|
173
|
+
cache: accessCache(deps),
|
|
157
174
|
...appsStateDep(deps),
|
|
158
175
|
});
|
|
159
176
|
}
|
|
@@ -168,7 +185,7 @@ export function createGithubSync(
|
|
|
168
185
|
deps: InstallationReposWiringDeps,
|
|
169
186
|
): GithubSyncService {
|
|
170
187
|
const client = lazyInstallerClient(deps);
|
|
171
|
-
const cache =
|
|
188
|
+
const cache = accessCache(deps);
|
|
172
189
|
const lookupClients = new Map<string, GithubLookupClient>();
|
|
173
190
|
// The lookup client per App (and key version), reused with its installation tokens.
|
|
174
191
|
const lookupClient = async (accountId?: string | null) => {
|
|
@@ -188,7 +205,15 @@ export function createGithubSync(
|
|
|
188
205
|
});
|
|
189
206
|
lookupClients.set(cacheKey, c);
|
|
190
207
|
}
|
|
191
|
-
return {
|
|
208
|
+
return {
|
|
209
|
+
client: c,
|
|
210
|
+
appId: meta.appId,
|
|
211
|
+
keys: await lookupKeysForApp(
|
|
212
|
+
meta,
|
|
213
|
+
accountId,
|
|
214
|
+
async () => (await deps.appService.get("lookup", null)) !== null,
|
|
215
|
+
),
|
|
216
|
+
};
|
|
192
217
|
};
|
|
193
218
|
return new GithubSyncService({
|
|
194
219
|
agents: new PrismaInstallationAgents(deps.prisma, deps.agentEnv),
|
|
@@ -199,7 +224,7 @@ export function createGithubSync(
|
|
|
199
224
|
const lookup = await lookupClient(accountId);
|
|
200
225
|
if (!lookup) return [];
|
|
201
226
|
const snapshots = await cache.listLookupOrgSnapshots(
|
|
202
|
-
|
|
227
|
+
lookup.keys,
|
|
203
228
|
lookup.appId,
|
|
204
229
|
);
|
|
205
230
|
return snapshots.map((s) => ({
|
|
@@ -231,6 +256,16 @@ export function createGithubSync(
|
|
|
231
256
|
return undefined;
|
|
232
257
|
}
|
|
233
258
|
},
|
|
259
|
+
// Re-crawl just the changed org so the UI list is current after apply.
|
|
260
|
+
refreshAgentSnapshot: async (agent, org) => {
|
|
261
|
+
await crawlAgentInstallation({
|
|
262
|
+
agent,
|
|
263
|
+
org,
|
|
264
|
+
client,
|
|
265
|
+
store: cache,
|
|
266
|
+
clock: { now: deps.now ?? (() => new Date()) },
|
|
267
|
+
});
|
|
268
|
+
},
|
|
234
269
|
...deps.syncOverrides,
|
|
235
270
|
installation:
|
|
236
271
|
deps.installationRepos ??
|
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
withLookupSnapshots,
|
|
9
9
|
} from "./github-installation.fixtures.ts";
|
|
10
10
|
import {
|
|
11
|
+
accessCache,
|
|
11
12
|
createGithubInstallationRepos,
|
|
12
13
|
createGithubSync,
|
|
13
14
|
} from "./github-installation-wiring.ts";
|
|
@@ -15,6 +16,14 @@ import type { GitHubEnterpriseAppMetadata } from "./github-enterprise-apps.ts";
|
|
|
15
16
|
import type { InstallationReposPort } from "./installation-repos-service.ts";
|
|
16
17
|
|
|
17
18
|
const noApp = { get: async () => null, getPrivateKey: async () => null };
|
|
19
|
+
// A deployment-wide lookup App exists: its cached access rows apply.
|
|
20
|
+
const withDeploymentLookup = {
|
|
21
|
+
get: async (role: string, accountId?: string | null) =>
|
|
22
|
+
role === "lookup" && !accountId
|
|
23
|
+
? ({ accountId: null, appId: 1 } as never)
|
|
24
|
+
: null,
|
|
25
|
+
getPrivateKey: async () => null,
|
|
26
|
+
};
|
|
18
27
|
|
|
19
28
|
const portWith = (
|
|
20
29
|
seen: number[],
|
|
@@ -88,7 +97,7 @@ describe("createGithubSync", () => {
|
|
|
88
97
|
const verified: string[] = [];
|
|
89
98
|
const svc = createGithubSync({
|
|
90
99
|
prisma,
|
|
91
|
-
appService:
|
|
100
|
+
appService: withDeploymentLookup,
|
|
92
101
|
access: accessFor(["acme/api"]),
|
|
93
102
|
agentEnv: envWith({ GH_APP_ID: "42" }),
|
|
94
103
|
installationRepos: portWith([]),
|
|
@@ -259,3 +268,31 @@ describe("createGithubSync lookup snapshot read", () => {
|
|
|
259
268
|
]);
|
|
260
269
|
});
|
|
261
270
|
});
|
|
271
|
+
|
|
272
|
+
describe("accessCache", () => {
|
|
273
|
+
const query = (accountId: string) => ({
|
|
274
|
+
accountId,
|
|
275
|
+
org: "acme",
|
|
276
|
+
username: "dev",
|
|
277
|
+
repos: ["api"],
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
it("ignores stale deployment-key access rows once no deployment-wide lookup App exists", async () => {
|
|
281
|
+
const { prisma } = fakePrisma(
|
|
282
|
+
["acme/api"],
|
|
283
|
+
freshCache("acme", ["acme/api"], ["acme/api"], new Date()),
|
|
284
|
+
);
|
|
285
|
+
const cache = accessCache({ prisma, appService: noApp });
|
|
286
|
+
expect(await cache.getAccessRows(query("deployment"))).toEqual([]);
|
|
287
|
+
expect((await cache.getAccessRows(query("acct-1"))).length).toBe(1);
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
it("keeps deployment-key rows while the deployment-wide lookup App exists", async () => {
|
|
291
|
+
const { prisma } = fakePrisma(
|
|
292
|
+
["acme/api"],
|
|
293
|
+
freshCache("acme", ["acme/api"], ["acme/api"], new Date()),
|
|
294
|
+
);
|
|
295
|
+
const cache = accessCache({ prisma, appService: withDeploymentLookup });
|
|
296
|
+
expect((await cache.getAccessRows(query("deployment"))).length).toBe(1);
|
|
297
|
+
});
|
|
298
|
+
});
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/github-sync-all-repos.unit.test.ts
|
|
3
|
+
* GIS-1.2: an org whose agent installation is on All repositories is skipped
|
|
4
|
+
* by Sync (never switched), with the uninstall-and-reinstall message.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { describe, expect, it } from "bun:test";
|
|
8
|
+
import {
|
|
9
|
+
ALL_REPOS_INSTALLATION_REASON,
|
|
10
|
+
NOT_INSTALLED_ORG_REASON,
|
|
11
|
+
planGithubSync,
|
|
12
|
+
} from "./github-sync.ts";
|
|
13
|
+
import type { UserRepoAccessResult } from "./user-repo-access-resolver.ts";
|
|
14
|
+
|
|
15
|
+
type Sel = "all" | "selected" | null;
|
|
16
|
+
|
|
17
|
+
const access = (pushable: string[], all: string[]): UserRepoAccessResult => ({
|
|
18
|
+
username: "octo",
|
|
19
|
+
outcome: "ok",
|
|
20
|
+
userVerified: true,
|
|
21
|
+
orgs: [
|
|
22
|
+
{
|
|
23
|
+
org: "acme",
|
|
24
|
+
status: "ok",
|
|
25
|
+
unresolved: 0,
|
|
26
|
+
repos: all.map((fullName) => ({
|
|
27
|
+
fullName,
|
|
28
|
+
canPush: pushable.includes(fullName),
|
|
29
|
+
level: pushable.includes(fullName)
|
|
30
|
+
? ("write" as const)
|
|
31
|
+
: ("read" as const),
|
|
32
|
+
})),
|
|
33
|
+
},
|
|
34
|
+
],
|
|
35
|
+
unresolved: 0,
|
|
36
|
+
complete: true,
|
|
37
|
+
checkedAt: "2026-11-25T00:00:00.000Z",
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const INSTALLED = ["acme/a", "acme/b"];
|
|
41
|
+
const plan = (
|
|
42
|
+
selection: Sel | undefined,
|
|
43
|
+
requested?: {
|
|
44
|
+
appAdds: string[];
|
|
45
|
+
appRemoves: string[];
|
|
46
|
+
agentRemoves: string[];
|
|
47
|
+
},
|
|
48
|
+
selectionKey = "acme",
|
|
49
|
+
) =>
|
|
50
|
+
planGithubSync({
|
|
51
|
+
username: "octo",
|
|
52
|
+
access: access(["acme/a", "acme/new"], [...INSTALLED, "acme/new"]),
|
|
53
|
+
agentRepos: [],
|
|
54
|
+
installationRepos: { acme: INSTALLED },
|
|
55
|
+
...(selection === undefined
|
|
56
|
+
? {}
|
|
57
|
+
: { selections: { [selectionKey]: selection } }),
|
|
58
|
+
...(requested ? { requested } : {}),
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
describe("planGithubSync on an All repositories installation", () => {
|
|
62
|
+
it("skips the org with the reinstall message and plans nothing for it", () => {
|
|
63
|
+
const p = plan("all");
|
|
64
|
+
expect(ALL_REPOS_INSTALLATION_REASON).toBe(
|
|
65
|
+
"Sync does not work when the app has access to all repositories; uninstall the app and install again with selected repositories.",
|
|
66
|
+
);
|
|
67
|
+
expect(p.skippedOrgs).toEqual([
|
|
68
|
+
{ org: "acme", reason: ALL_REPOS_INSTALLATION_REASON },
|
|
69
|
+
]);
|
|
70
|
+
expect(p.appAdds).toEqual([]);
|
|
71
|
+
expect(p.appRemoves).toEqual([]);
|
|
72
|
+
expect(p.removedRepos).toEqual([]);
|
|
73
|
+
expect(p.warnings[0]).toContain("uninstall the app and install again");
|
|
74
|
+
expect("selectionSwitches" in p).toBe(false);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("reports confirmed lists for the org as not applied", () => {
|
|
78
|
+
const p = plan("all", {
|
|
79
|
+
appAdds: ["acme/new"],
|
|
80
|
+
appRemoves: ["acme/b"],
|
|
81
|
+
agentRemoves: [],
|
|
82
|
+
});
|
|
83
|
+
expect(p.appAdds).toEqual([]);
|
|
84
|
+
expect(p.appRemoves).toEqual([]);
|
|
85
|
+
expect(p.notApplied.map((n) => n.repo)).toEqual(["acme/b", "acme/new"]);
|
|
86
|
+
expect(p.skippedOrgs).toHaveLength(1);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("matches the selection key case-insensitively", () => {
|
|
90
|
+
expect(plan("all", undefined, "ACME").skippedOrgs).toHaveLength(1);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("never treats selected, null or a missing selection as all", () => {
|
|
94
|
+
for (const sel of ["selected", null, undefined] as const) {
|
|
95
|
+
const p = plan(sel);
|
|
96
|
+
expect(p.skippedOrgs).toEqual([]);
|
|
97
|
+
expect(p.appAdds).toEqual(["acme/new"]);
|
|
98
|
+
expect(p.appRemoves).toEqual(["acme/b"]);
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it("still reports an add for an org with no installation as not applied", () => {
|
|
103
|
+
const p = planGithubSync({
|
|
104
|
+
username: "octo",
|
|
105
|
+
access: access(["acme/a", "acme/new"], [...INSTALLED, "acme/new"]),
|
|
106
|
+
agentRepos: [],
|
|
107
|
+
installationRepos: { other: ["other/x"] },
|
|
108
|
+
requested: { appAdds: ["acme/new"], appRemoves: [], agentRemoves: [] },
|
|
109
|
+
});
|
|
110
|
+
expect(p.appAdds).toEqual([]);
|
|
111
|
+
expect(p.notApplied).toContainEqual({
|
|
112
|
+
repo: "acme/new",
|
|
113
|
+
reason: NOT_INSTALLED_ORG_REASON,
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
});
|
|
@@ -29,11 +29,18 @@ const port: InstallationReposPort = {
|
|
|
29
29
|
removeRepos: async () => {},
|
|
30
30
|
};
|
|
31
31
|
|
|
32
|
+
// A deployment-wide lookup App exists, so cached rows under the deployment key apply.
|
|
33
|
+
const deploymentLookupApp = {
|
|
34
|
+
get: async () =>
|
|
35
|
+
({ accountId: null }) as unknown as GitHubEnterpriseAppMetadata,
|
|
36
|
+
getPrivateKey: async () => null,
|
|
37
|
+
};
|
|
38
|
+
|
|
32
39
|
function preview(cache: ReturnType<typeof freshCache>, now = new Date()) {
|
|
33
40
|
const { prisma } = fakePrisma(REPOS, cache);
|
|
34
41
|
const svc = createGithubSync({
|
|
35
42
|
prisma,
|
|
36
|
-
appService:
|
|
43
|
+
appService: deploymentLookupApp,
|
|
37
44
|
access: accessFor([]), // the live resolver must not be consulted
|
|
38
45
|
agentEnv: envWith({ GH_APP_ID: "42" }),
|
|
39
46
|
installationRepos: port,
|
|
@@ -118,7 +125,7 @@ describe("installation list access detail", () => {
|
|
|
118
125
|
const { prisma } = fakePrisma(REPOS, cache);
|
|
119
126
|
const svc = createGithubInstallationRepos({
|
|
120
127
|
prisma,
|
|
121
|
-
appService:
|
|
128
|
+
appService: deploymentLookupApp,
|
|
122
129
|
access: accessFor([]),
|
|
123
130
|
agentEnv: envWith({ GH_APP_ID: "42" }),
|
|
124
131
|
installationRepos: port,
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/github-sync-snapshot-refresh.integration.test.ts
|
|
3
|
+
* Real-database test: after Sync apply, the cached agent-App snapshot (what the
|
|
4
|
+
* admin UI lists) for the changed org matches the installation right away,
|
|
5
|
+
* using the same targeted crawl the wiring runs (crawlAgentInstallation).
|
|
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 type { PrismaClient } from "../prisma/client/client.ts";
|
|
12
|
+
import {
|
|
13
|
+
type AgentInstallationClient,
|
|
14
|
+
crawlAgentInstallation,
|
|
15
|
+
DEPLOYMENT_WIDE_ACCOUNT_KEY,
|
|
16
|
+
} from "./github-agent-installation-crawler.ts";
|
|
17
|
+
import { GitHubRepoCacheService } from "./github-repo-cache.ts";
|
|
18
|
+
import { GithubSyncService, type GithubSyncDeps } from "./github-sync.ts";
|
|
19
|
+
import type { InstallationReposPort } from "./installation-repos-service.ts";
|
|
20
|
+
import { createAdminPrismaClient } from "./prisma-client.ts";
|
|
21
|
+
|
|
22
|
+
const TEST_DB = process.env.DATABASE_URL_ADMIN_TEST;
|
|
23
|
+
const describeOrSkip = TEST_DB ? describe : describe.skip;
|
|
24
|
+
|
|
25
|
+
const T0 = new Date("2026-11-25T12:00:00Z");
|
|
26
|
+
// Advances 1s per read so a later crawl starts after the earlier one's rows.
|
|
27
|
+
let tick = 0;
|
|
28
|
+
const clock = { now: () => new Date(T0.getTime() + tick++ * 1000) };
|
|
29
|
+
const APP_ID = 4243;
|
|
30
|
+
|
|
31
|
+
describeOrSkip("Sync apply refreshes the agent snapshot (integration)", () => {
|
|
32
|
+
let prisma: PrismaClient;
|
|
33
|
+
let cache: GitHubRepoCacheService;
|
|
34
|
+
let live: string[];
|
|
35
|
+
|
|
36
|
+
const clean = async () => {
|
|
37
|
+
await prisma.gitHubRepoAccess.deleteMany();
|
|
38
|
+
await prisma.gitHubCachedRepo.deleteMany();
|
|
39
|
+
await prisma.gitHubRepoSnapshot.deleteMany();
|
|
40
|
+
};
|
|
41
|
+
const installer = (): AgentInstallationClient => ({
|
|
42
|
+
async listOrgInstallations() {
|
|
43
|
+
return [{ id: 900, appId: APP_ID, repositorySelection: "selected" }];
|
|
44
|
+
},
|
|
45
|
+
async listInstallationRepositoriesPage() {
|
|
46
|
+
return {
|
|
47
|
+
repos: live.map((fullName, i) => ({ id: i + 1, fullName })),
|
|
48
|
+
hasNext: false,
|
|
49
|
+
perPage: 10,
|
|
50
|
+
};
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
const crawl = () =>
|
|
54
|
+
crawlAgentInstallation({
|
|
55
|
+
agent: { accountId: null, appId: APP_ID },
|
|
56
|
+
org: "acme",
|
|
57
|
+
client: installer(),
|
|
58
|
+
store: cache,
|
|
59
|
+
clock,
|
|
60
|
+
});
|
|
61
|
+
const listed = async () =>
|
|
62
|
+
(
|
|
63
|
+
await cache.listAgentSnapshots(
|
|
64
|
+
DEPLOYMENT_WIDE_ACCOUNT_KEY,
|
|
65
|
+
String(APP_ID),
|
|
66
|
+
)
|
|
67
|
+
)[0]?.repos
|
|
68
|
+
.map((r) => r.fullName)
|
|
69
|
+
.sort();
|
|
70
|
+
|
|
71
|
+
function service(withRefresh: boolean) {
|
|
72
|
+
const port: InstallationReposPort = {
|
|
73
|
+
listRepos: async () => ({ acme: live }),
|
|
74
|
+
listInstalled: async () => ({
|
|
75
|
+
status: "found",
|
|
76
|
+
orgs: [{ org: "acme", repos: live }],
|
|
77
|
+
}),
|
|
78
|
+
addRepos: async (_a, _o, repos) => {
|
|
79
|
+
live = [...live, ...repos];
|
|
80
|
+
},
|
|
81
|
+
removeRepos: async (_a, _o, repos) => {
|
|
82
|
+
live = live.filter((r) => !repos.includes(r));
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
const deps = {
|
|
86
|
+
agents: {
|
|
87
|
+
get: async () => ({
|
|
88
|
+
id: "ag",
|
|
89
|
+
githubUsername: "octo",
|
|
90
|
+
appId: APP_ID,
|
|
91
|
+
repos: [] as string[],
|
|
92
|
+
}),
|
|
93
|
+
setRepos: async () => {},
|
|
94
|
+
},
|
|
95
|
+
access: async () => {
|
|
96
|
+
throw new Error("live resolver must not be used");
|
|
97
|
+
},
|
|
98
|
+
cache,
|
|
99
|
+
lookupRepos: async () => [
|
|
100
|
+
{
|
|
101
|
+
org: "acme",
|
|
102
|
+
completedAt: new Date(T0.getTime() - 1000),
|
|
103
|
+
repos: ["acme/a", "acme/b", "acme/c"],
|
|
104
|
+
},
|
|
105
|
+
],
|
|
106
|
+
now: () => T0,
|
|
107
|
+
verifyPush: async (_u: string, repo: string) => repo === "acme/a",
|
|
108
|
+
installation: port,
|
|
109
|
+
audit: { write: async () => "audit-1" },
|
|
110
|
+
...(withRefresh
|
|
111
|
+
? { refreshAgentSnapshot: async () => void (await crawl()) }
|
|
112
|
+
: {}),
|
|
113
|
+
};
|
|
114
|
+
return new GithubSyncService(deps as unknown as GithubSyncDeps);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const lists = {
|
|
118
|
+
agentId: "ag",
|
|
119
|
+
caller: "root",
|
|
120
|
+
appAdds: [] as string[],
|
|
121
|
+
appRemoves: ["acme/b", "acme/c"],
|
|
122
|
+
agentRemoves: [] as string[],
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
beforeEach(async () => {
|
|
126
|
+
prisma = createAdminPrismaClient(TEST_DB as string);
|
|
127
|
+
tick = 0;
|
|
128
|
+
cache = new GitHubRepoCacheService(prisma, clock);
|
|
129
|
+
await clean();
|
|
130
|
+
live = ["acme/a", "acme/b", "acme/c"];
|
|
131
|
+
await crawl();
|
|
132
|
+
// Access rows so the plan can propose removals (a pushable, b/c not).
|
|
133
|
+
for (const repo of ["a", "b", "c"]) {
|
|
134
|
+
await cache.upsertAccess({
|
|
135
|
+
accountId: DEPLOYMENT_WIDE_ACCOUNT_KEY,
|
|
136
|
+
org: "acme",
|
|
137
|
+
repo,
|
|
138
|
+
username: "octo",
|
|
139
|
+
canPush: repo === "a",
|
|
140
|
+
level: repo === "a" ? "write" : "read",
|
|
141
|
+
status: "ok",
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
const snap = (
|
|
145
|
+
await cache.listAgentSnapshots(
|
|
146
|
+
DEPLOYMENT_WIDE_ACCOUNT_KEY,
|
|
147
|
+
String(APP_ID),
|
|
148
|
+
)
|
|
149
|
+
)[0];
|
|
150
|
+
expect(snap?.repos).toHaveLength(3);
|
|
151
|
+
});
|
|
152
|
+
afterEach(async () => {
|
|
153
|
+
await clean();
|
|
154
|
+
await prisma.$disconnect();
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it("the snapshot lists the removed repos' absence right after apply", async () => {
|
|
158
|
+
const r = await service(true).apply(lists);
|
|
159
|
+
expect(r.status).toBe("applied");
|
|
160
|
+
expect(live).toEqual(["acme/a"]);
|
|
161
|
+
expect(await listed()).toEqual(["acme/a"]);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it("without the refresh the snapshot stays stale (the bug)", async () => {
|
|
165
|
+
await service(false).apply(lists);
|
|
166
|
+
expect(await listed()).toEqual(["acme/a", "acme/b", "acme/c"]);
|
|
167
|
+
});
|
|
168
|
+
});
|