@app-vitals/agent-admin 1.86.1 → 1.88.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 +2 -2
- package/openapi.platform.json +30 -2
- 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-ui-github-identity-pages.ts +22 -4
- package/src/admin-ui-github-identity-pages.unit.test.ts +87 -0
- package/src/admin-ui-installation-repos-pages.ts +29 -7
- package/src/admin-ui-installation-repos-pages.unit.test.ts +83 -0
- package/src/admin-ui-installation-repos.smoke.test.ts +77 -9
- package/src/admin-ui-pages.ts +15 -2
- package/src/admin-ui.ts +23 -17
- package/src/agents-api.smoke.test.ts +237 -2
- package/src/agents-api.ts +27 -4
- package/src/github-access-preview.integration.test.ts +38 -3
- package/src/github-access-preview.ts +1 -1
- package/src/github-agent-installation-crawler.ts +27 -8
- package/src/github-agent-installation-crawler.unit.test.ts +76 -1
- package/src/github-apps-state.ts +31 -0
- package/src/github-cache-roundtrip.integration.test.ts +405 -0
- package/src/github-enterprise-api.ts +24 -18
- package/src/github-installation-selection.integration.test.ts +91 -0
- package/src/github-installation-wiring.ts +35 -0
- package/src/github-repo-cache.ts +8 -0
- package/src/github-sync.ts +38 -4
- package/src/installation-ports.ts +2 -0
- package/src/installation-repos-service.ts +24 -1
- package/src/user-repo-access-resolver.ts +26 -5
- package/src/user-repo-access-resolver.unit.test.ts +80 -0
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
import type { UserRepoAccessResult } from "./user-repo-access-resolver.ts";
|
|
10
10
|
|
|
11
11
|
export type GithubAccessPreview =
|
|
12
|
-
/** The
|
|
12
|
+
/** The lookup App (or its key) is not configured; GitHub was not called. */
|
|
13
13
|
| { status: "unconfigured" }
|
|
14
14
|
/** `access` is the resolver output, unchanged. */
|
|
15
15
|
| { status: "ok" | "user_not_found"; access: UserRepoAccessResult };
|
|
@@ -55,6 +55,8 @@ export type AgentInstallationClient = Pick<
|
|
|
55
55
|
export interface AgentInstallationPageSource extends PageSource {
|
|
56
56
|
/** The agent App's installation id on the org; throws AppNotInstalledError. Cached. */
|
|
57
57
|
resolveInstallationId(): Promise<number>;
|
|
58
|
+
/** The installation's repository selection ('all' | 'selected'); null if unknown. Resolves like resolveInstallationId. */
|
|
59
|
+
resolveRepositorySelection?(): Promise<"all" | "selected" | null>;
|
|
58
60
|
}
|
|
59
61
|
|
|
60
62
|
export interface AgentInstallationSourceOptions {
|
|
@@ -78,23 +80,34 @@ export function agentInstallationPageSource(
|
|
|
78
80
|
opts: AgentInstallationSourceOptions,
|
|
79
81
|
): AgentInstallationPageSource {
|
|
80
82
|
const { client, org, appId, identity } = opts;
|
|
81
|
-
let
|
|
82
|
-
|
|
83
|
-
|
|
83
|
+
let installation:
|
|
84
|
+
| Promise<{ id: number; selection: "all" | "selected" | null }>
|
|
85
|
+
| undefined;
|
|
86
|
+
const resolveInstallation = () => {
|
|
87
|
+
installation ??= client
|
|
84
88
|
.listOrgInstallations(org, identity?.accountId)
|
|
85
89
|
.then((installs) => {
|
|
86
90
|
const found = matchInstallation(installs, appId, identity);
|
|
87
91
|
if (!found) throw new AppNotInstalledError(appId, org);
|
|
88
|
-
|
|
92
|
+
const sel = found.repositorySelection;
|
|
93
|
+
return {
|
|
94
|
+
id: found.id,
|
|
95
|
+
// "none" or an absent value is unknown, never "all".
|
|
96
|
+
selection: sel === "all" || sel === "selected" ? sel : null,
|
|
97
|
+
};
|
|
89
98
|
});
|
|
90
99
|
// Do not cache a failure: a later page or run may succeed.
|
|
91
|
-
|
|
92
|
-
|
|
100
|
+
installation.catch(() => {
|
|
101
|
+
installation = undefined;
|
|
93
102
|
});
|
|
94
|
-
return
|
|
103
|
+
return installation;
|
|
95
104
|
};
|
|
105
|
+
const resolveInstallationId = async (): Promise<number> =>
|
|
106
|
+
(await resolveInstallation()).id;
|
|
96
107
|
return {
|
|
97
108
|
resolveInstallationId,
|
|
109
|
+
resolveRepositorySelection: async () =>
|
|
110
|
+
(await resolveInstallation()).selection,
|
|
98
111
|
async fetchPage(page: number): Promise<RepoPage> {
|
|
99
112
|
const id = await resolveInstallationId();
|
|
100
113
|
const res = await client.listInstallationRepositoriesPage(
|
|
@@ -158,8 +171,10 @@ export async function crawlAgentInstallation(
|
|
|
158
171
|
appId: agent.appId,
|
|
159
172
|
identity: identityOf(agent),
|
|
160
173
|
});
|
|
174
|
+
let selection: "all" | "selected" | null;
|
|
161
175
|
try {
|
|
162
|
-
await source.
|
|
176
|
+
selection = (await source.resolveRepositorySelection?.()) ?? null;
|
|
177
|
+
if (!source.resolveRepositorySelection) await source.resolveInstallationId();
|
|
163
178
|
} catch (err) {
|
|
164
179
|
if (!(err instanceof AppNotInstalledError)) {
|
|
165
180
|
// Let the engine record GitHub failures and rate limits on the snapshot.
|
|
@@ -177,10 +192,14 @@ export async function crawlAgentInstallation(
|
|
|
177
192
|
nextAttemptAt: null,
|
|
178
193
|
rateLimitResetAt: null,
|
|
179
194
|
lastTickAt: now,
|
|
195
|
+
repositorySelection: null,
|
|
180
196
|
});
|
|
181
197
|
const swept = await store.sweepGeneration(snap.id);
|
|
182
198
|
return { outcome: "not_installed", pagesFetched: 0, swept };
|
|
183
199
|
}
|
|
200
|
+
// Record the selection on every crawl; null clears a stale value. A GitHub
|
|
201
|
+
// failure above returns early and leaves the previous value untouched.
|
|
202
|
+
await store.upsertSnapshot(key, { repositorySelection: selection });
|
|
184
203
|
return crawlRepos({ ...engineOpts(), key, source });
|
|
185
204
|
|
|
186
205
|
function engineOpts() {
|
|
@@ -77,7 +77,13 @@ class MemoryStore implements CrawlStore {
|
|
|
77
77
|
}
|
|
78
78
|
|
|
79
79
|
interface FakeClientOpts {
|
|
80
|
-
installs?: {
|
|
80
|
+
installs?: {
|
|
81
|
+
id: number;
|
|
82
|
+
appId?: number;
|
|
83
|
+
appSlug?: string;
|
|
84
|
+
clientId?: string;
|
|
85
|
+
repositorySelection?: "all" | "selected" | "none";
|
|
86
|
+
}[];
|
|
81
87
|
total?: number;
|
|
82
88
|
perPage?: number;
|
|
83
89
|
failPage?: (page: number) => Error | undefined;
|
|
@@ -376,3 +382,72 @@ describe("lookupAccountKeys", () => {
|
|
|
376
382
|
expect(lookupAccountKeys("acct")).not.toContain("");
|
|
377
383
|
});
|
|
378
384
|
});
|
|
385
|
+
|
|
386
|
+
describe("crawlAgentInstallation repository selection (GIS-1.1)", () => {
|
|
387
|
+
const run = (store: MemoryStore, client: AgentInstallationClient) =>
|
|
388
|
+
crawlAgentInstallation({
|
|
389
|
+
agent: agent(),
|
|
390
|
+
org: "acme",
|
|
391
|
+
client,
|
|
392
|
+
store,
|
|
393
|
+
clock,
|
|
394
|
+
});
|
|
395
|
+
const selection = (store: MemoryStore) =>
|
|
396
|
+
(store.snap as unknown as Record<string, unknown> | null)
|
|
397
|
+
?.repositorySelection;
|
|
398
|
+
|
|
399
|
+
it.each(["all", "selected"] as const)("records %s", async (value) => {
|
|
400
|
+
const store = new MemoryStore();
|
|
401
|
+
const { client } = fakeClient({
|
|
402
|
+
installs: [{ id: 501, appId: 777, repositorySelection: value }],
|
|
403
|
+
});
|
|
404
|
+
await run(store, client);
|
|
405
|
+
expect(selection(store)).toBe(value);
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
it("records null when the listing omits it or says none", async () => {
|
|
409
|
+
for (const repositorySelection of [undefined, "none" as const]) {
|
|
410
|
+
const store = new MemoryStore();
|
|
411
|
+
const { client } = fakeClient({
|
|
412
|
+
installs: [{ id: 501, appId: 777, repositorySelection }],
|
|
413
|
+
});
|
|
414
|
+
await run(store, client);
|
|
415
|
+
expect(selection(store)).toBeNull();
|
|
416
|
+
}
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
it("clears a previous value when the App is not installed", async () => {
|
|
420
|
+
const store = new MemoryStore();
|
|
421
|
+
await run(
|
|
422
|
+
store,
|
|
423
|
+
fakeClient({
|
|
424
|
+
installs: [{ id: 501, appId: 777, repositorySelection: "all" }],
|
|
425
|
+
}).client,
|
|
426
|
+
);
|
|
427
|
+
expect(selection(store)).toBe("all");
|
|
428
|
+
const res = await run(store, fakeClient({ installs: [] }).client);
|
|
429
|
+
expect(res.outcome).toBe("not_installed");
|
|
430
|
+
expect(selection(store)).toBeNull();
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
it("keeps the previous value when listing installations fails", async () => {
|
|
434
|
+
const store = new MemoryStore();
|
|
435
|
+
await run(
|
|
436
|
+
store,
|
|
437
|
+
fakeClient({
|
|
438
|
+
installs: [{ id: 501, appId: 777, repositorySelection: "selected" }],
|
|
439
|
+
}).client,
|
|
440
|
+
);
|
|
441
|
+
const failing: AgentInstallationClient = {
|
|
442
|
+
async listOrgInstallations() {
|
|
443
|
+
throw new GithubEnterpriseError("server", "boom");
|
|
444
|
+
},
|
|
445
|
+
async listInstallationRepositoriesPage() {
|
|
446
|
+
throw new GithubEnterpriseError("server", "boom");
|
|
447
|
+
},
|
|
448
|
+
};
|
|
449
|
+
const res = await run(store, failing);
|
|
450
|
+
expect(res.outcome).toBe("failed");
|
|
451
|
+
expect(selection(store)).toBe("selected");
|
|
452
|
+
});
|
|
453
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/github-apps-state.ts
|
|
3
|
+
* Which of the two enterprise GitHub Apps are usable. The lookup App backs the
|
|
4
|
+
* repo crawl/cache, the repo picker and the access preview; the installer App
|
|
5
|
+
* (GitHub Enterprise Cloud only) backs listing or changing the agent App's
|
|
6
|
+
* installation repos, Sync adds/removes and install-on-org. Three states, so
|
|
7
|
+
* "lookup missing" is never confused with "installer missing".
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export type GithubAppsState = "both" | "lookup_only" | "lookup_missing";
|
|
11
|
+
|
|
12
|
+
/** Resolves the state for an account; undefined account = deployment-wide. */
|
|
13
|
+
export type GithubAppsStateFn = (
|
|
14
|
+
accountId?: string | null,
|
|
15
|
+
) => Promise<GithubAppsState>;
|
|
16
|
+
|
|
17
|
+
/** Lookup-only: repo access is visible, the installer-dependent surfaces are not. */
|
|
18
|
+
export const LOOKUP_ONLY_REASON =
|
|
19
|
+
"Only the lookup GitHub App is configured, so repo access is visible but Sync and the agent App's installation repos need an enterprise installer App (GitHub Enterprise Cloud).";
|
|
20
|
+
|
|
21
|
+
/** Lookup App missing (with or without the installer App). */
|
|
22
|
+
export const LOOKUP_MISSING_REASON =
|
|
23
|
+
"The lookup GitHub App is not configured, so repo access cannot be checked. Configure the lookup App first.";
|
|
24
|
+
|
|
25
|
+
export const GITHUB_APPS_STATE_REASONS: Record<
|
|
26
|
+
Exclude<GithubAppsState, "both">,
|
|
27
|
+
string
|
|
28
|
+
> = {
|
|
29
|
+
lookup_only: LOOKUP_ONLY_REASON,
|
|
30
|
+
lookup_missing: LOOKUP_MISSING_REASON,
|
|
31
|
+
};
|
|
@@ -0,0 +1,405 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/github-cache-roundtrip.integration.test.ts
|
|
3
|
+
* Real-database round trip for the GitHub repo cache (GFU-1.1). The real
|
|
4
|
+
* refresher, discovery and crawlers write snapshots and access rows through
|
|
5
|
+
* GitHubRepoCacheService into PostgreSQL, and every reader reads them back:
|
|
6
|
+
* githubLookup.listRepositories, GitHub Sync preview (lookupRepos plus
|
|
7
|
+
* cachedAccess) and the installation repos list.
|
|
8
|
+
*
|
|
9
|
+
* Earlier tests stubbed lookupRepos or the cache, which hid readers that
|
|
10
|
+
* looked under the wrong account key (GSF-1.1, GSF-4.1). Only the GitHub
|
|
11
|
+
* ports are faked here (lookup client, installer client); time is an injected
|
|
12
|
+
* Clock. Two scopes: a deployment-wide (null-account) lookup App, filed under
|
|
13
|
+
* DEPLOYMENT_WIDE_ACCOUNT_KEY, and an account-scoped App, read through its own
|
|
14
|
+
* account key plus the deployment key (lookupAccountKeys).
|
|
15
|
+
*
|
|
16
|
+
* Requires DATABASE_URL_ADMIN_TEST to be set; skips otherwise.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
|
|
20
|
+
import type { PrismaClient } from "../prisma/client/client.ts";
|
|
21
|
+
import type { AgentEnvService } from "./agent-envs.ts";
|
|
22
|
+
import type { Clock } from "./clock.ts";
|
|
23
|
+
import type { GithubAccessPreviewFn } from "./github-access-preview.ts";
|
|
24
|
+
import { createGithubLookupService } from "./github-enterprise-api.ts";
|
|
25
|
+
import type { GitHubEnterpriseAppService } from "./github-enterprise-apps.ts";
|
|
26
|
+
import {
|
|
27
|
+
createGithubInstallationRepos,
|
|
28
|
+
createGithubSync,
|
|
29
|
+
} from "./github-installation-wiring.ts";
|
|
30
|
+
import {
|
|
31
|
+
DEPLOYMENT_WIDE_ACCOUNT_KEY,
|
|
32
|
+
lookupAccountKeys,
|
|
33
|
+
} from "./github-agent-installation-crawler.ts";
|
|
34
|
+
import { GitHubRepoCacheService } from "./github-repo-cache.ts";
|
|
35
|
+
import {
|
|
36
|
+
createRefreshDiscovery,
|
|
37
|
+
type LookupAppClient,
|
|
38
|
+
} from "./github-repo-refresh-discovery.ts";
|
|
39
|
+
import { GithubRepoRefresher } from "./github-repo-refresher.ts";
|
|
40
|
+
import {
|
|
41
|
+
createInstallationPort,
|
|
42
|
+
type InstallationRepoClient,
|
|
43
|
+
PrismaInstallationAgents,
|
|
44
|
+
} from "./installation-ports.ts";
|
|
45
|
+
import { createInstallationReposPort } from "./installation-repos-service.ts";
|
|
46
|
+
import { createAdminPrismaClient } from "./prisma-client.ts";
|
|
47
|
+
|
|
48
|
+
const TEST_DB = process.env.DATABASE_URL_ADMIN_TEST;
|
|
49
|
+
const describeOrSkip = TEST_DB ? describe : describe.skip;
|
|
50
|
+
|
|
51
|
+
const T0 = new Date("2026-11-20T12:00:00Z");
|
|
52
|
+
const MINUTE = 60_000;
|
|
53
|
+
const HOUR = 3_600_000;
|
|
54
|
+
const LOOKUP_APP_ID = "555";
|
|
55
|
+
const AGENT_APP_ID = 42;
|
|
56
|
+
const USERNAME = "octocat";
|
|
57
|
+
|
|
58
|
+
/** Repos per org: three, listed over two pages (two per page). */
|
|
59
|
+
const reposOf = (org: string) => ["a", "b", "c"].map((n) => `${org}/${n}`);
|
|
60
|
+
|
|
61
|
+
/** Fake lookup App client: the GitHub port the crawl and access backfill use. */
|
|
62
|
+
function fakeLookupClient(orgs: string[]): LookupAppClient {
|
|
63
|
+
return {
|
|
64
|
+
async listInstallations() {
|
|
65
|
+
return orgs.map((login, i) => ({
|
|
66
|
+
id: i + 1,
|
|
67
|
+
login,
|
|
68
|
+
})) as unknown as Awaited<
|
|
69
|
+
ReturnType<LookupAppClient["listInstallations"]>
|
|
70
|
+
>;
|
|
71
|
+
},
|
|
72
|
+
async listInstallationRepositoriesPage(org, page) {
|
|
73
|
+
const all = reposOf(org);
|
|
74
|
+
const slice = all.slice((page - 1) * 2, page * 2);
|
|
75
|
+
return {
|
|
76
|
+
repositories: slice.map((fullName) => ({
|
|
77
|
+
id: org.length * 1000 + all.indexOf(fullName) + 1,
|
|
78
|
+
fullName,
|
|
79
|
+
archived: false,
|
|
80
|
+
})),
|
|
81
|
+
totalCount: all.length,
|
|
82
|
+
hasNext: page * 2 < all.length,
|
|
83
|
+
rateLimit: { remaining: 4000, resetAt: null, retryAfterSeconds: null },
|
|
84
|
+
} as unknown as Awaited<
|
|
85
|
+
ReturnType<LookupAppClient["listInstallationRepositoriesPage"]>
|
|
86
|
+
>;
|
|
87
|
+
},
|
|
88
|
+
async getCollaboratorPermissionWithRateLimit(_org, repo) {
|
|
89
|
+
return {
|
|
90
|
+
role: repo === "a" ? "write" : "read",
|
|
91
|
+
rateLimit: { remaining: 4000, resetAt: null, retryAfterSeconds: null },
|
|
92
|
+
};
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
type InstallerClient = InstallationRepoClient &
|
|
98
|
+
Required<Pick<InstallationRepoClient, "listInstallationRepositoriesPage">> & {
|
|
99
|
+
listInstallableOrganizations(): Promise<{ login: string }[]>;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
/** Fake installer client: the agent App has installation 900 on org `acme`. */
|
|
103
|
+
function fakeInstaller(): InstallerClient {
|
|
104
|
+
const repos = (org: string) =>
|
|
105
|
+
reposOf(org).map((fullName, i) => ({
|
|
106
|
+
id: 9000 + i,
|
|
107
|
+
fullName,
|
|
108
|
+
archived: false,
|
|
109
|
+
}));
|
|
110
|
+
return {
|
|
111
|
+
async listInstallableOrganizations() {
|
|
112
|
+
return [{ login: "acme" }];
|
|
113
|
+
},
|
|
114
|
+
async listOrgInstallations() {
|
|
115
|
+
return [{ id: 900, appId: AGENT_APP_ID }];
|
|
116
|
+
},
|
|
117
|
+
async listInstallationRepositories(org: string) {
|
|
118
|
+
return repos(org);
|
|
119
|
+
},
|
|
120
|
+
async listInstallationRepositoriesPage(
|
|
121
|
+
org: string,
|
|
122
|
+
_id: number,
|
|
123
|
+
page: number,
|
|
124
|
+
) {
|
|
125
|
+
const all = repos(org);
|
|
126
|
+
const slice = all.slice((page - 1) * 2, page * 2);
|
|
127
|
+
return {
|
|
128
|
+
repos: slice,
|
|
129
|
+
perPage: 2,
|
|
130
|
+
hasNext: page * 2 < all.length,
|
|
131
|
+
};
|
|
132
|
+
},
|
|
133
|
+
async addRepositories() {},
|
|
134
|
+
async removeRepositories() {},
|
|
135
|
+
} as unknown as InstallerClient;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
describeOrSkip("GitHub repo cache round trip (integration)", () => {
|
|
139
|
+
let prisma: PrismaClient;
|
|
140
|
+
let now: Date;
|
|
141
|
+
let cache: GitHubRepoCacheService;
|
|
142
|
+
const accountIds: string[] = [];
|
|
143
|
+
const agentIds: string[] = [];
|
|
144
|
+
|
|
145
|
+
const clock: Clock = { now: () => new Date(now) };
|
|
146
|
+
|
|
147
|
+
const agentEnv = {
|
|
148
|
+
getConfigBundle: async () => ({ env: { GH_APP_ID: String(AGENT_APP_ID) } }),
|
|
149
|
+
} as unknown as Pick<AgentEnvService, "getConfigBundle">;
|
|
150
|
+
|
|
151
|
+
/** Resolves a lookup App per scope, as the stored-App wiring would. */
|
|
152
|
+
interface Apps {
|
|
153
|
+
/** scope (null = deployment-wide) -> the App row that scope resolves to. */
|
|
154
|
+
byScope: Map<
|
|
155
|
+
string | null,
|
|
156
|
+
{ ownerAccountId: string | null; orgs: string[] }
|
|
157
|
+
>;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const appService = (apps: Apps) =>
|
|
161
|
+
({
|
|
162
|
+
get: async (role: string, scope?: string | null) => {
|
|
163
|
+
if (role !== "lookup") return null;
|
|
164
|
+
const found = apps.byScope.get(scope ?? null);
|
|
165
|
+
if (!found) return null;
|
|
166
|
+
return {
|
|
167
|
+
appId: LOOKUP_APP_ID,
|
|
168
|
+
accountId: found.ownerAccountId,
|
|
169
|
+
enterpriseSlug: null,
|
|
170
|
+
updatedAt: T0,
|
|
171
|
+
};
|
|
172
|
+
},
|
|
173
|
+
getPrivateKey: async (role: string, scope?: string | null) =>
|
|
174
|
+
role === "lookup" && apps.byScope.has(scope ?? null) ? "pem" : null,
|
|
175
|
+
}) as unknown as GitHubEnterpriseAppService;
|
|
176
|
+
|
|
177
|
+
/** Run the real refresher until every target is crawled and backfilled. */
|
|
178
|
+
async function refresh(apps: Apps, agentScope: string | null) {
|
|
179
|
+
const installer = fakeInstaller();
|
|
180
|
+
const agentsPort = new PrismaInstallationAgents(prisma, agentEnv);
|
|
181
|
+
const discovery = createRefreshDiscovery({
|
|
182
|
+
listAppScopes: async () =>
|
|
183
|
+
[...apps.byScope.keys()].filter((s): s is string => s !== null),
|
|
184
|
+
async listAgents() {
|
|
185
|
+
const out = [];
|
|
186
|
+
for (const id of agentIds) {
|
|
187
|
+
const rec = await agentsPort.get(id);
|
|
188
|
+
if (rec) out.push(rec);
|
|
189
|
+
}
|
|
190
|
+
return out;
|
|
191
|
+
},
|
|
192
|
+
async lookupApp(scope) {
|
|
193
|
+
const found = apps.byScope.get(scope);
|
|
194
|
+
if (!found) return null;
|
|
195
|
+
return {
|
|
196
|
+
accountId: found.ownerAccountId,
|
|
197
|
+
appId: LOOKUP_APP_ID,
|
|
198
|
+
client: fakeLookupClient(found.orgs),
|
|
199
|
+
};
|
|
200
|
+
},
|
|
201
|
+
// Only the agent's own scope has an installer; the others fall back to
|
|
202
|
+
// the lookup App's own installations for their orgs.
|
|
203
|
+
installerOrgs: async (scope) => (scope === agentScope ? ["acme"] : null),
|
|
204
|
+
installer,
|
|
205
|
+
store: cache,
|
|
206
|
+
clock,
|
|
207
|
+
log: () => {},
|
|
208
|
+
});
|
|
209
|
+
const refresher = new GithubRepoRefresher({
|
|
210
|
+
discovery,
|
|
211
|
+
store: cache,
|
|
212
|
+
clock,
|
|
213
|
+
holder: "roundtrip",
|
|
214
|
+
config: {
|
|
215
|
+
intervalMs: MINUTE,
|
|
216
|
+
ttlMs: HOUR,
|
|
217
|
+
stalenessCapMs: 6 * HOUR,
|
|
218
|
+
timeBudgetMs: 45_000,
|
|
219
|
+
},
|
|
220
|
+
log: () => {},
|
|
221
|
+
});
|
|
222
|
+
const result = await refresher.tick();
|
|
223
|
+
expect(result.targets.every((t) => t.state === "worked")).toBe(true);
|
|
224
|
+
// Readers run a little later, still inside the freshness window.
|
|
225
|
+
now = new Date(T0.getTime() + MINUTE);
|
|
226
|
+
return installer;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
async function makeAgent(accountId: string | null) {
|
|
230
|
+
const agent = await prisma.agent.create({
|
|
231
|
+
data: {
|
|
232
|
+
name: `roundtrip-${agentIds.length}`,
|
|
233
|
+
githubUsername: USERNAME,
|
|
234
|
+
repos: ["acme/a"],
|
|
235
|
+
...(accountId ? { accountId } : {}),
|
|
236
|
+
},
|
|
237
|
+
});
|
|
238
|
+
agentIds.push(agent.id);
|
|
239
|
+
return agent.id;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
async function makeAccount(name: string) {
|
|
243
|
+
const acct = await prisma.account.create({ data: { name } });
|
|
244
|
+
accountIds.push(acct.id);
|
|
245
|
+
return acct.id;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const lister = (apps: Apps) =>
|
|
249
|
+
createGithubLookupService({
|
|
250
|
+
appService: appService(apps),
|
|
251
|
+
cache,
|
|
252
|
+
now: () => clock.now(),
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
const noLiveAccess: GithubAccessPreviewFn = async () => {
|
|
256
|
+
throw new Error("the live access resolver must not be consulted");
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
const wiring = (apps: Apps, installer: InstallerClient) => {
|
|
260
|
+
const installationRepos = createInstallationReposPort(
|
|
261
|
+
createInstallationPort(installer),
|
|
262
|
+
installer,
|
|
263
|
+
);
|
|
264
|
+
return {
|
|
265
|
+
prisma,
|
|
266
|
+
appService: appService(apps),
|
|
267
|
+
access: noLiveAccess,
|
|
268
|
+
agentEnv,
|
|
269
|
+
installationRepos,
|
|
270
|
+
now: () => clock.now(),
|
|
271
|
+
};
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
beforeEach(async () => {
|
|
275
|
+
prisma = createAdminPrismaClient(TEST_DB as string);
|
|
276
|
+
await prisma.gitHubRepoAccess.deleteMany();
|
|
277
|
+
await prisma.gitHubCachedRepo.deleteMany();
|
|
278
|
+
await prisma.gitHubRepoSnapshot.deleteMany();
|
|
279
|
+
now = T0;
|
|
280
|
+
cache = new GitHubRepoCacheService(prisma, clock);
|
|
281
|
+
accountIds.length = 0;
|
|
282
|
+
agentIds.length = 0;
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
afterEach(async () => {
|
|
286
|
+
await prisma.gitHubRepoAccess.deleteMany();
|
|
287
|
+
await prisma.gitHubCachedRepo.deleteMany();
|
|
288
|
+
await prisma.gitHubRepoSnapshot.deleteMany();
|
|
289
|
+
if (agentIds.length > 0) {
|
|
290
|
+
await prisma.agent.deleteMany({ where: { id: { in: agentIds } } });
|
|
291
|
+
}
|
|
292
|
+
if (accountIds.length > 0) {
|
|
293
|
+
await prisma.account.deleteMany({ where: { id: { in: accountIds } } });
|
|
294
|
+
}
|
|
295
|
+
await prisma.$disconnect();
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
it("null-account lookup App: every reader reads the deployment-keyed snapshots", async () => {
|
|
299
|
+
const agentId = await makeAgent(null);
|
|
300
|
+
const apps: Apps = {
|
|
301
|
+
byScope: new Map([[null, { ownerAccountId: null, orgs: ["acme"] }]]),
|
|
302
|
+
};
|
|
303
|
+
const installer = await refresh(apps, null);
|
|
304
|
+
|
|
305
|
+
// The writers filed everything under the deployment key.
|
|
306
|
+
const snaps = await prisma.gitHubRepoSnapshot.findMany();
|
|
307
|
+
expect(snaps.map((s) => [s.appKind, s.accountId, s.org]).sort()).toEqual([
|
|
308
|
+
["agent", DEPLOYMENT_WIDE_ACCOUNT_KEY, "acme"],
|
|
309
|
+
["lookup", DEPLOYMENT_WIDE_ACCOUNT_KEY, "acme"],
|
|
310
|
+
]);
|
|
311
|
+
|
|
312
|
+
// 1. githubLookup.listRepositories
|
|
313
|
+
const listed = await lister(apps).listRepositories(null);
|
|
314
|
+
expect(listed.configured).toBe(true);
|
|
315
|
+
expect(listed.syncState).toBe("ready");
|
|
316
|
+
expect(listed.repos.map((r) => r.fullName).sort()).toEqual(reposOf("acme"));
|
|
317
|
+
|
|
318
|
+
// 2. Sync preview: lookupRepos + cachedAccess over the real cache.
|
|
319
|
+
const sync = createGithubSync(wiring(apps, installer));
|
|
320
|
+
const preview = await sync.preview(agentId);
|
|
321
|
+
if (preview.status !== "previewed") throw new Error(preview.status);
|
|
322
|
+
expect(preview.plan.skippedOrgs).toEqual([]);
|
|
323
|
+
expect(preview.plan.appRemoves).toEqual(["acme/b", "acme/c"]);
|
|
324
|
+
|
|
325
|
+
// 3. Installation list: the agent App snapshot.
|
|
326
|
+
const list = await createGithubInstallationRepos(
|
|
327
|
+
wiring(apps, installer),
|
|
328
|
+
).list(agentId);
|
|
329
|
+
if (list.status !== "ok") throw new Error(list.status);
|
|
330
|
+
expect(list.appId).toBe(AGENT_APP_ID);
|
|
331
|
+
expect(list.orgs).toHaveLength(1);
|
|
332
|
+
expect(list.orgs[0]?.org).toBe("acme");
|
|
333
|
+
expect(list.orgs[0]?.complete).toBe(true);
|
|
334
|
+
expect(list.orgs[0]?.repos.map((r) => r.fullName).sort()).toEqual(
|
|
335
|
+
reposOf("acme"),
|
|
336
|
+
);
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
it("account-scoped App: readers use the own account key plus the deployment key, never another account's", async () => {
|
|
340
|
+
const acct = await makeAccount("roundtrip-own");
|
|
341
|
+
const other = await makeAccount("roundtrip-other");
|
|
342
|
+
const agentId = await makeAgent(acct);
|
|
343
|
+
const apps: Apps = {
|
|
344
|
+
byScope: new Map([
|
|
345
|
+
// The account's own App covers `acme`; the deployment-wide App `beta`.
|
|
346
|
+
[acct, { ownerAccountId: acct, orgs: ["acme"] }],
|
|
347
|
+
[null, { ownerAccountId: null, orgs: ["beta"] }],
|
|
348
|
+
// Another account's App, same App id, must never be read.
|
|
349
|
+
[other, { ownerAccountId: other, orgs: ["evil"] }],
|
|
350
|
+
]),
|
|
351
|
+
};
|
|
352
|
+
const installer = await refresh(apps, acct);
|
|
353
|
+
|
|
354
|
+
expect(lookupAccountKeys(acct)).toEqual([
|
|
355
|
+
acct,
|
|
356
|
+
DEPLOYMENT_WIDE_ACCOUNT_KEY,
|
|
357
|
+
]);
|
|
358
|
+
const lookupSnaps = await prisma.gitHubRepoSnapshot.findMany({
|
|
359
|
+
where: { appKind: "lookup" },
|
|
360
|
+
});
|
|
361
|
+
expect(lookupSnaps.map((s) => [s.accountId, s.org]).sort()).toEqual(
|
|
362
|
+
[
|
|
363
|
+
[acct, "acme"],
|
|
364
|
+
[DEPLOYMENT_WIDE_ACCOUNT_KEY, "beta"],
|
|
365
|
+
[other, "evil"],
|
|
366
|
+
].sort(),
|
|
367
|
+
);
|
|
368
|
+
|
|
369
|
+
// 1. Lister: own account's acme plus the deployment-wide beta, no evil.
|
|
370
|
+
const listed = await lister(apps).listRepositories(acct);
|
|
371
|
+
expect(listed.configured).toBe(true);
|
|
372
|
+
expect(listed.syncState).toBe("ready");
|
|
373
|
+
expect(listed.repos.map((r) => r.fullName).sort()).toEqual(
|
|
374
|
+
[...reposOf("acme"), ...reposOf("beta")].sort(),
|
|
375
|
+
);
|
|
376
|
+
expect(listed.repos.some((r) => r.fullName.startsWith("evil/"))).toBe(
|
|
377
|
+
false,
|
|
378
|
+
);
|
|
379
|
+
|
|
380
|
+
// The other account's lister sees its own org plus the deployment key only.
|
|
381
|
+
const otherListed = await lister(apps).listRepositories(other);
|
|
382
|
+
expect(otherListed.repos.map((r) => r.fullName).sort()).toEqual(
|
|
383
|
+
[...reposOf("evil"), ...reposOf("beta")].sort(),
|
|
384
|
+
);
|
|
385
|
+
|
|
386
|
+
// 2. Sync preview over the account's own access rows.
|
|
387
|
+
const sync = createGithubSync(wiring(apps, installer));
|
|
388
|
+
const preview = await sync.preview(agentId);
|
|
389
|
+
if (preview.status !== "previewed") throw new Error(preview.status);
|
|
390
|
+
expect(preview.plan.skippedOrgs).toEqual([]);
|
|
391
|
+
expect(preview.plan.appRemoves).toEqual(["acme/b", "acme/c"]);
|
|
392
|
+
|
|
393
|
+
// 3. Installation list: the agent App snapshot under the account key.
|
|
394
|
+
const list = await createGithubInstallationRepos(
|
|
395
|
+
wiring(apps, installer),
|
|
396
|
+
).list(agentId);
|
|
397
|
+
if (list.status !== "ok") throw new Error(list.status);
|
|
398
|
+
expect(list.orgs).toHaveLength(1);
|
|
399
|
+
expect(list.orgs[0]?.org).toBe("acme");
|
|
400
|
+
expect(list.orgs[0]?.complete).toBe(true);
|
|
401
|
+
expect(list.orgs[0]?.repos.map((r) => r.fullName).sort()).toEqual(
|
|
402
|
+
reposOf("acme"),
|
|
403
|
+
);
|
|
404
|
+
});
|
|
405
|
+
});
|
|
@@ -483,8 +483,8 @@ function serializeApp(m: GitHubEnterpriseAppMetadata) {
|
|
|
483
483
|
// ─── Access preview (GHE-2.4) ─────────────────────────────────────────────────
|
|
484
484
|
|
|
485
485
|
/**
|
|
486
|
-
* Backs GET /agents/:id/github-access. Unconfigured (
|
|
487
|
-
*
|
|
486
|
+
* Backs GET /agents/:id/github-access. Unconfigured (the lookup App, or its
|
|
487
|
+
* key, missing) returns before any GitHub
|
|
488
488
|
* call. A GitHub failure while listing orgs is rethrown as a 502 carrying only
|
|
489
489
|
* the mapped, non-leaky message.
|
|
490
490
|
*/
|
|
@@ -499,32 +499,38 @@ export function createGithubAccessPreview(
|
|
|
499
499
|
deps.appService.get("installer", accountId),
|
|
500
500
|
deps.appService.get("lookup", accountId),
|
|
501
501
|
]);
|
|
502
|
-
if (!
|
|
503
|
-
return { status: "unconfigured" };
|
|
504
|
-
}
|
|
502
|
+
if (!lookup) return { status: "unconfigured" };
|
|
505
503
|
const [installerKey, lookupKey] = await Promise.all([
|
|
506
|
-
deps.appService.getPrivateKey("installer", accountId),
|
|
504
|
+
installer ? deps.appService.getPrivateKey("installer", accountId) : null,
|
|
507
505
|
deps.appService.getPrivateKey("lookup", accountId),
|
|
508
506
|
]);
|
|
509
|
-
if (!
|
|
507
|
+
if (!lookupKey) return { status: "unconfigured" };
|
|
510
508
|
|
|
511
509
|
const common = {
|
|
512
510
|
apiBase: deps.apiBase,
|
|
513
511
|
fetchFn: deps.fetchFn,
|
|
514
512
|
now: deps.now,
|
|
515
513
|
};
|
|
514
|
+
const lookupClient = new GithubLookupClient({
|
|
515
|
+
appId: lookup.appId,
|
|
516
|
+
privateKey: lookupKey,
|
|
517
|
+
...common,
|
|
518
|
+
});
|
|
519
|
+
// The installer only widens the org list; without a usable one (no key or
|
|
520
|
+
// enterprise slug) the lookup App's own installations are the org source.
|
|
521
|
+
const installerClient =
|
|
522
|
+
installer?.enterpriseSlug && installerKey
|
|
523
|
+
? new GithubEnterpriseClient({
|
|
524
|
+
appId: installer.appId,
|
|
525
|
+
enterprise: installer.enterpriseSlug,
|
|
526
|
+
privateKey: installerKey,
|
|
527
|
+
...common,
|
|
528
|
+
})
|
|
529
|
+
: undefined;
|
|
516
530
|
const resolver = new UserRepoAccessResolver({
|
|
517
|
-
installer:
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
privateKey: installerKey,
|
|
521
|
-
...common,
|
|
522
|
-
}),
|
|
523
|
-
lookup: new GithubLookupClient({
|
|
524
|
-
appId: lookup.appId,
|
|
525
|
-
privateKey: lookupKey,
|
|
526
|
-
...common,
|
|
527
|
-
}),
|
|
531
|
+
installer: installerClient,
|
|
532
|
+
lookupInstallations: lookupClient,
|
|
533
|
+
lookup: lookupClient,
|
|
528
534
|
now: deps.now,
|
|
529
535
|
});
|
|
530
536
|
try {
|