@app-vitals/agent-admin 1.80.0 → 1.82.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 +48 -2
- package/package.json +2 -2
- package/src/admin-boot.boot-order.unit.test.ts +11 -0
- package/src/admin-boot.ts +42 -2
- package/src/admin-ui-installation-repos-pages.ts +89 -25
- package/src/admin-ui-installation-repos.smoke.test.ts +71 -0
- package/src/admin-ui-pages.ts +13 -1
- package/src/admin-ui.ts +16 -4
- package/src/agents-api.smoke.test.ts +85 -5
- package/src/agents-api.ts +32 -3
- package/src/github-access-backfill.ts +9 -1
- package/src/github-access-backfill.unit.test.ts +16 -0
- package/src/github-agent-installation-crawler.ts +4 -1
- package/src/github-agent-installation-crawler.unit.test.ts +14 -0
- package/src/github-enterprise-apps.integration.test.ts +11 -0
- package/src/github-enterprise-apps.ts +10 -0
- package/src/github-installation-wiring.ts +86 -4
- package/src/github-installation-wiring.unit.test.ts +8 -4
- package/src/github-installation.fixtures.ts +12 -1
- package/src/github-repo-cache.integration.test.ts +14 -0
- package/src/github-repo-cache.ts +30 -0
- package/src/github-repo-crawler.ts +6 -0
- package/src/github-repo-crawler.unit.test.ts +36 -0
- package/src/github-repo-refresh-discovery.ts +241 -0
- package/src/github-repo-refresh-discovery.unit.test.ts +237 -0
- package/src/github-repo-refresher.integration.test.ts +179 -0
- package/src/github-repo-refresher.ts +640 -0
- package/src/github-repo-refresher.unit.test.ts +735 -0
- package/src/installation-repos-service.ts +174 -50
- package/src/installation-repos-service.unit.test.ts +157 -52
package/src/agents-api.ts
CHANGED
|
@@ -641,18 +641,46 @@ const RepoFullNameSchema = z
|
|
|
641
641
|
.string()
|
|
642
642
|
.regex(/^[^/\s]+\/[^/\s]+$/, "must be owner/repo");
|
|
643
643
|
|
|
644
|
+
const InstallationSyncDetailShape = {
|
|
645
|
+
complete: z.boolean().optional().openapi({
|
|
646
|
+
description:
|
|
647
|
+
"Whether the cached crawl is complete. While false the list may be partial.",
|
|
648
|
+
}),
|
|
649
|
+
syncedAt: z
|
|
650
|
+
.string()
|
|
651
|
+
.optional()
|
|
652
|
+
.openapi({ description: "When the completed crawl finished (ISO 8601)." }),
|
|
653
|
+
reposSeen: z
|
|
654
|
+
.number()
|
|
655
|
+
.int()
|
|
656
|
+
.optional()
|
|
657
|
+
.openapi({ description: "Repos in the cache (the N of 'N of M')." }),
|
|
658
|
+
totalCount: z
|
|
659
|
+
.number()
|
|
660
|
+
.int()
|
|
661
|
+
.optional()
|
|
662
|
+
.openapi({ description: "GitHub's repo count, when known (the M)." }),
|
|
663
|
+
lastError: z.string().optional(),
|
|
664
|
+
rateLimitResetAt: z
|
|
665
|
+
.string()
|
|
666
|
+
.optional()
|
|
667
|
+
.openapi({ description: "When the GitHub rate limit resets (ISO 8601)." }),
|
|
668
|
+
};
|
|
669
|
+
|
|
644
670
|
const GithubInstallationReposListSchema = z
|
|
645
671
|
.object({
|
|
646
672
|
appId: z.number().int(),
|
|
673
|
+
...InstallationSyncDetailShape,
|
|
647
674
|
orgs: z.array(
|
|
648
675
|
z.object({
|
|
649
676
|
org: z.string(),
|
|
677
|
+
...InstallationSyncDetailShape,
|
|
650
678
|
repos: z.array(
|
|
651
679
|
z.object({
|
|
652
680
|
fullName: z.string(),
|
|
653
681
|
userCanPush: z.boolean().optional().openapi({
|
|
654
682
|
description:
|
|
655
|
-
"Whether the agent's bound GitHub user can push. Omitted when no username is bound or the org's access
|
|
683
|
+
"Whether the agent's bound GitHub user can push, from cached access rows. Omitted when no username is bound or the org's access data is incomplete (a missing or errored pair is never reported as false).",
|
|
656
684
|
}),
|
|
657
685
|
}),
|
|
658
686
|
),
|
|
@@ -1793,7 +1821,7 @@ const listInstallationReposRoute = createRoute({
|
|
|
1793
1821
|
path: "/agents/{id}/github-installation/repos",
|
|
1794
1822
|
summary: "List the repos on the agent App's installation",
|
|
1795
1823
|
description:
|
|
1796
|
-
"Platform-admin only (session members, ops keys and agent keys get 403). Per org the agent's own GitHub App (its GH_APP_ID) is installed on, lists the installation's repos. When the agent has a bound githubUsername, each repo carries `userCanPush` from
|
|
1824
|
+
"Platform-admin only (session members, ops keys and agent keys get 403). Per org the agent's own GitHub App (its GH_APP_ID) is installed on, lists the installation's repos from the background-synced cache; it makes no GitHub call. Sync detail (`complete`, `syncedAt`, `reposSeen`, `totalCount`, `lastError`, `rateLimitResetAt`) is reported per org and overall. When the agent has a bound githubUsername, each repo carries `userCanPush` from cached access rows only when that org's snapshot is complete and every repo has an answer; otherwise the field is omitted (never false). 404 for an unknown agent; 422 when the agent has no GH_APP_ID.",
|
|
1797
1825
|
request: { params: AgentIdParamSchema },
|
|
1798
1826
|
responses: {
|
|
1799
1827
|
200: {
|
|
@@ -3453,7 +3481,8 @@ export function createAdminApp(deps: AdminDeps): OpenAPIHono<AdminAuthEnv> {
|
|
|
3453
3481
|
return c.json({ appId: result.appId, orgs: [] }, 200);
|
|
3454
3482
|
}
|
|
3455
3483
|
if (result.status !== "ok") return installationFailure(agentId, result);
|
|
3456
|
-
|
|
3484
|
+
const { status: _status, ...body } = result;
|
|
3485
|
+
return c.json(body, 200);
|
|
3457
3486
|
});
|
|
3458
3487
|
|
|
3459
3488
|
// /agents/:id/github-sync/{preview,apply} — IRM-3.1. Platform admin only;
|
|
@@ -98,6 +98,11 @@ export interface BackfillOptions {
|
|
|
98
98
|
lowRemaining?: number;
|
|
99
99
|
/** Most probes in this run. Default 500. */
|
|
100
100
|
maxProbes?: number;
|
|
101
|
+
/**
|
|
102
|
+
* Checked before every probe after the first; true ends the run as
|
|
103
|
+
* in_progress. Lets a caller bound a run by time.
|
|
104
|
+
*/
|
|
105
|
+
shouldStop?: () => boolean;
|
|
101
106
|
}
|
|
102
107
|
|
|
103
108
|
export type BackfillOutcome = "complete" | "in_progress" | "rate_limited";
|
|
@@ -240,7 +245,10 @@ export async function backfillAccess(
|
|
|
240
245
|
);
|
|
241
246
|
|
|
242
247
|
for (const item of queue) {
|
|
243
|
-
if (
|
|
248
|
+
if (
|
|
249
|
+
result.probed >= maxProbes ||
|
|
250
|
+
(result.probed > 0 && opts.shouldStop?.())
|
|
251
|
+
) {
|
|
244
252
|
result.outcome = "in_progress";
|
|
245
253
|
return result;
|
|
246
254
|
}
|
|
@@ -349,3 +349,19 @@ describe("backfillAccess", () => {
|
|
|
349
349
|
});
|
|
350
350
|
});
|
|
351
351
|
});
|
|
352
|
+
|
|
353
|
+
describe("backfillAccess: shouldStop (GLC-1.4 time budget)", () => {
|
|
354
|
+
it("makes at least one probe, then stops in_progress when asked", async () => {
|
|
355
|
+
const h = makeStore();
|
|
356
|
+
const calls: string[] = [];
|
|
357
|
+
let n = 0;
|
|
358
|
+
const out = await run(
|
|
359
|
+
h,
|
|
360
|
+
probeOf(() => "write", calls),
|
|
361
|
+
{ shouldStop: () => ++n > 1 },
|
|
362
|
+
);
|
|
363
|
+
expect(out.outcome).toBe("in_progress");
|
|
364
|
+
expect(calls).toEqual(["octocat/a", "octocat/b"]);
|
|
365
|
+
expect(out.missing).toBe(1);
|
|
366
|
+
});
|
|
367
|
+
});
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* A PageSource over the installer client's single-page installation listing,
|
|
6
6
|
* fed through the same resumable engine (crawlRepos) and completeness check as
|
|
7
7
|
* the lookup crawl, under appKind "agent" and appRef = the agent's GH_APP_ID.
|
|
8
|
-
* Scheduling
|
|
8
|
+
* Scheduling lives in github-repo-refresher.ts (GLC-1.4).
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
import type { Clock } from "./clock.ts";
|
|
@@ -121,6 +121,8 @@ export interface CrawlAgentInstallationOptions {
|
|
|
121
121
|
clock: Clock;
|
|
122
122
|
maxPages?: number;
|
|
123
123
|
backoffMs?: (attempts: number) => number;
|
|
124
|
+
/** Passed to the engine: checked before every page after the first. */
|
|
125
|
+
shouldStop?: () => boolean;
|
|
124
126
|
}
|
|
125
127
|
|
|
126
128
|
/** Crawl one agent App's installation repos on one org into the cache. */
|
|
@@ -174,6 +176,7 @@ export async function crawlAgentInstallation(
|
|
|
174
176
|
clock,
|
|
175
177
|
...(opts.maxPages === undefined ? {} : { maxPages: opts.maxPages }),
|
|
176
178
|
...(opts.backoffMs === undefined ? {} : { backoffMs: opts.backoffMs }),
|
|
179
|
+
...(opts.shouldStop === undefined ? {} : { shouldStop: opts.shouldStop }),
|
|
177
180
|
};
|
|
178
181
|
}
|
|
179
182
|
}
|
|
@@ -268,6 +268,20 @@ describe("crawlAgentInstallation", () => {
|
|
|
268
268
|
expect(calls.pages).toEqual([1, 1, 2, 3]);
|
|
269
269
|
});
|
|
270
270
|
|
|
271
|
+
it("passes shouldStop through to the engine", async () => {
|
|
272
|
+
const { client, calls } = fakeClient({ total: 5 });
|
|
273
|
+
const res = await crawlAgentInstallation({
|
|
274
|
+
agent: agent(),
|
|
275
|
+
org: "acme",
|
|
276
|
+
client,
|
|
277
|
+
store,
|
|
278
|
+
clock,
|
|
279
|
+
shouldStop: () => true,
|
|
280
|
+
});
|
|
281
|
+
expect(res.outcome).toBe("in_progress");
|
|
282
|
+
expect(calls.pages).toEqual([1]);
|
|
283
|
+
});
|
|
284
|
+
|
|
271
285
|
it("records not-installed as a completed empty snapshot, not an error", async () => {
|
|
272
286
|
store.rows.set(1, { fullName: "acme/stale", seenAt: new Date(0) });
|
|
273
287
|
const { client, calls } = fakeClient({ installs: [] });
|
|
@@ -181,6 +181,17 @@ describeOrSkip("GitHubEnterpriseAppService (integration)", () => {
|
|
|
181
181
|
expect((await service.get("lookup"))?.appId).toBe("900");
|
|
182
182
|
});
|
|
183
183
|
|
|
184
|
+
it("listAccountIds returns each account with Apps of its own once, never the deployment scope", async () => {
|
|
185
|
+
await service.save(lookup);
|
|
186
|
+
expect(await service.listAccountIds()).toEqual([]);
|
|
187
|
+
await service.save({ ...lookup, accountId: acctA });
|
|
188
|
+
await service.save({ ...installer, accountId: acctA });
|
|
189
|
+
await service.save({ ...installer, accountId: acctB });
|
|
190
|
+
expect((await service.listAccountIds()).sort()).toEqual(
|
|
191
|
+
[acctA, acctB].sort(),
|
|
192
|
+
);
|
|
193
|
+
});
|
|
194
|
+
|
|
184
195
|
it("get(role) without accountId never sees an account row", async () => {
|
|
185
196
|
await service.save({ ...lookup, accountId: acctA });
|
|
186
197
|
expect(await service.get("lookup")).toBeNull();
|
|
@@ -197,6 +197,16 @@ export class GitHubEnterpriseAppService {
|
|
|
197
197
|
return rows.map(toGitHubEnterpriseAppMetadata);
|
|
198
198
|
}
|
|
199
199
|
|
|
200
|
+
/** Ids of the accounts with at least one App of their own (no key material). */
|
|
201
|
+
async listAccountIds(): Promise<string[]> {
|
|
202
|
+
const rows = await this.prisma.gitHubEnterpriseApp.findMany({
|
|
203
|
+
where: { accountId: { not: null } },
|
|
204
|
+
select: { accountId: true },
|
|
205
|
+
distinct: ["accountId"],
|
|
206
|
+
});
|
|
207
|
+
return rows.flatMap((r) => (r.accountId ? [r.accountId] : []));
|
|
208
|
+
}
|
|
209
|
+
|
|
200
210
|
/** Delete the app at exactly this (accountId, role). No-ops if absent. */
|
|
201
211
|
async delete(
|
|
202
212
|
role: GitHubEnterpriseAppRole,
|
|
@@ -2,28 +2,36 @@
|
|
|
2
2
|
* admin/src/github-installation-wiring.ts
|
|
3
3
|
* Composition of the installation repos list and GitHub Sync services for
|
|
4
4
|
* admin boot, from the stored installer App and an optional host-supplied
|
|
5
|
-
* OpenWorkPort
|
|
5
|
+
* OpenWorkPort, plus the GitHub cache refresher's target discovery over the
|
|
6
|
+
* stored Apps (GLC-1.4). The agent App id comes from each agent's own stored
|
|
6
7
|
* GH_APP_ID; without an open-work port nothing is listed or blocked.
|
|
7
8
|
*/
|
|
8
9
|
|
|
9
10
|
import type { PrismaClient } from "../prisma/client/client.ts";
|
|
10
11
|
import type { AgentEnvService } from "./agent-envs.ts";
|
|
11
12
|
import type { GithubAccessPreviewFn } from "./github-access-preview.ts";
|
|
13
|
+
import type { Clock } from "./clock.ts";
|
|
12
14
|
import type { GitHubEnterpriseAppService } from "./github-enterprise-apps.ts";
|
|
13
15
|
import { GithubEnterpriseClient } from "./github-enterprise-client.ts";
|
|
16
|
+
import { GithubLookupClient } from "./github-lookup-client.ts";
|
|
17
|
+
import { GitHubRepoCacheService } from "./github-repo-cache.ts";
|
|
18
|
+
import type { CrawlStore } from "./github-repo-crawler.ts";
|
|
19
|
+
import { createRefreshDiscovery } from "./github-repo-refresh-discovery.ts";
|
|
20
|
+
import type { RefreshDiscovery } from "./github-repo-refresher.ts";
|
|
21
|
+
import { GithubSyncService } from "./github-sync.ts";
|
|
14
22
|
import {
|
|
15
23
|
createInstallationPort,
|
|
24
|
+
type InstallationAgentRecord,
|
|
16
25
|
type InstallationRepoClient,
|
|
17
26
|
PrismaInstallationAgents,
|
|
18
27
|
PrismaInstallationAudit,
|
|
19
28
|
} from "./installation-ports.ts";
|
|
20
29
|
import {
|
|
21
30
|
createInstallationReposPort,
|
|
22
|
-
type InstalledOrgsClient,
|
|
23
|
-
InstallationReposService,
|
|
24
31
|
type InstallationReposPort,
|
|
32
|
+
InstallationReposService,
|
|
33
|
+
type InstalledOrgsClient,
|
|
25
34
|
} from "./installation-repos-service.ts";
|
|
26
|
-
import { GithubSyncService } from "./github-sync.ts";
|
|
27
35
|
import type { OpenWorkPort } from "./reconcile-open-work.ts";
|
|
28
36
|
|
|
29
37
|
export interface GithubInstallationWiringDeps {
|
|
@@ -103,6 +111,7 @@ export function createGithubInstallationRepos(
|
|
|
103
111
|
installation:
|
|
104
112
|
deps.installationRepos ??
|
|
105
113
|
createInstallationReposPort(createInstallationPort(client), client),
|
|
114
|
+
cache: new GitHubRepoCacheService(deps.prisma),
|
|
106
115
|
});
|
|
107
116
|
}
|
|
108
117
|
|
|
@@ -126,3 +135,76 @@ export function createGithubSync(
|
|
|
126
135
|
...(deps.openWork ? { openWork: deps.openWork } : {}),
|
|
127
136
|
});
|
|
128
137
|
}
|
|
138
|
+
|
|
139
|
+
export interface StoredAppDiscoveryDeps {
|
|
140
|
+
prisma: PrismaClient;
|
|
141
|
+
appService: Pick<
|
|
142
|
+
GitHubEnterpriseAppService,
|
|
143
|
+
"get" | "getPrivateKey" | "listAccountIds"
|
|
144
|
+
>;
|
|
145
|
+
/** Reads each agent's stored GH_APP_ID (its own GitHub App). */
|
|
146
|
+
agentEnv: Pick<AgentEnvService, "getConfigBundle">;
|
|
147
|
+
store: CrawlStore;
|
|
148
|
+
clock: Clock;
|
|
149
|
+
/** Test seams; default to the real GitHub API. */
|
|
150
|
+
fetchFn?: typeof fetch;
|
|
151
|
+
apiBase?: string;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* The refresher's target discovery (GLC-1.4) over the stored Apps, the
|
|
156
|
+
* agents table and real GitHub clients. Lives here, beside the other
|
|
157
|
+
* key-bearing installation wiring, so the private key stays in the
|
|
158
|
+
* allowlisted modules.
|
|
159
|
+
*/
|
|
160
|
+
export function createStoredAppRefreshDiscovery(
|
|
161
|
+
deps: StoredAppDiscoveryDeps,
|
|
162
|
+
): RefreshDiscovery {
|
|
163
|
+
const { prisma, appService, fetchFn, apiBase, clock } = deps;
|
|
164
|
+
const now = () => clock.now();
|
|
165
|
+
const installer = lazyInstallerClient({ appService, fetchFn, apiBase, now });
|
|
166
|
+
const agentsPort = new PrismaInstallationAgents(prisma, deps.agentEnv);
|
|
167
|
+
// Reuse a client (and its cached installation tokens) until the App changes.
|
|
168
|
+
const lookupClients = new Map<string, GithubLookupClient>();
|
|
169
|
+
return createRefreshDiscovery({
|
|
170
|
+
listAppScopes: () => appService.listAccountIds(),
|
|
171
|
+
async listAgents() {
|
|
172
|
+
const rows = await prisma.agent.findMany({ select: { id: true } });
|
|
173
|
+
const out: InstallationAgentRecord[] = [];
|
|
174
|
+
for (const { id } of rows) {
|
|
175
|
+
const rec = await agentsPort.get(id);
|
|
176
|
+
if (rec) out.push(rec);
|
|
177
|
+
}
|
|
178
|
+
return out;
|
|
179
|
+
},
|
|
180
|
+
async lookupApp(scope) {
|
|
181
|
+
const meta = await appService.get("lookup", scope);
|
|
182
|
+
if (!meta) return null;
|
|
183
|
+
const privateKey = await appService.getPrivateKey("lookup", scope);
|
|
184
|
+
if (!privateKey) return null;
|
|
185
|
+
const cacheKey = `${meta.accountId ?? ""}|${meta.appId}|${meta.updatedAt.getTime()}`;
|
|
186
|
+
let client = lookupClients.get(cacheKey);
|
|
187
|
+
if (!client) {
|
|
188
|
+
client = new GithubLookupClient({
|
|
189
|
+
appId: meta.appId,
|
|
190
|
+
privateKey,
|
|
191
|
+
apiBase,
|
|
192
|
+
fetchFn,
|
|
193
|
+
now,
|
|
194
|
+
});
|
|
195
|
+
lookupClients.set(cacheKey, client);
|
|
196
|
+
}
|
|
197
|
+
return { accountId: meta.accountId ?? null, appId: meta.appId, client };
|
|
198
|
+
},
|
|
199
|
+
async installerOrgs(scope) {
|
|
200
|
+
const meta = await appService.get("installer", scope);
|
|
201
|
+
if (!meta?.enterpriseSlug) return null;
|
|
202
|
+
return (await installer.listInstallableOrganizations(scope)).map(
|
|
203
|
+
(o) => o.login,
|
|
204
|
+
);
|
|
205
|
+
},
|
|
206
|
+
installer,
|
|
207
|
+
store: deps.store,
|
|
208
|
+
clock,
|
|
209
|
+
});
|
|
210
|
+
}
|
|
@@ -30,9 +30,9 @@ const portWith = (
|
|
|
30
30
|
});
|
|
31
31
|
|
|
32
32
|
describe("createGithubInstallationRepos", () => {
|
|
33
|
-
it("lists from the agent's own stored GH_APP_ID", async () => {
|
|
33
|
+
it("lists from the cache under the agent's own stored GH_APP_ID, without GitHub", async () => {
|
|
34
34
|
const seen: number[] = [];
|
|
35
|
-
const { prisma } = fakePrisma(["acme/old"]);
|
|
35
|
+
const { prisma, state } = fakePrisma(["acme/old"]);
|
|
36
36
|
const svc = createGithubInstallationRepos({
|
|
37
37
|
prisma,
|
|
38
38
|
appService: noApp,
|
|
@@ -41,8 +41,12 @@ describe("createGithubInstallationRepos", () => {
|
|
|
41
41
|
installationRepos: portWith(seen),
|
|
42
42
|
});
|
|
43
43
|
const res = await svc.list("a1");
|
|
44
|
-
|
|
45
|
-
expect(
|
|
44
|
+
// The port (live GitHub) is never used; the cache is read for App 77.
|
|
45
|
+
expect(seen).toEqual([]);
|
|
46
|
+
expect(state.snapshotQueries).toMatchObject([
|
|
47
|
+
{ where: { appKind: "agent", appRef: "77" } },
|
|
48
|
+
]);
|
|
49
|
+
expect(res).toMatchObject({ status: "ok", appId: 77, complete: false });
|
|
46
50
|
});
|
|
47
51
|
|
|
48
52
|
it("reports an agent with no GH_APP_ID, naming the missing var", async () => {
|
|
@@ -34,7 +34,11 @@ export const accessFor = (pushable: string[]): GithubAccessPreviewFn => {
|
|
|
34
34
|
};
|
|
35
35
|
|
|
36
36
|
export function fakePrisma(repos: string[]) {
|
|
37
|
-
const state = {
|
|
37
|
+
const state = {
|
|
38
|
+
repos: [...repos],
|
|
39
|
+
audits: 0,
|
|
40
|
+
snapshotQueries: [] as unknown[],
|
|
41
|
+
};
|
|
38
42
|
const prisma = {
|
|
39
43
|
agent: {
|
|
40
44
|
findUnique: async () => ({
|
|
@@ -46,6 +50,13 @@ export function fakePrisma(repos: string[]) {
|
|
|
46
50
|
state.repos = args.data.repos;
|
|
47
51
|
},
|
|
48
52
|
},
|
|
53
|
+
gitHubRepoSnapshot: {
|
|
54
|
+
findMany: async (args: unknown) => {
|
|
55
|
+
state.snapshotQueries.push(args);
|
|
56
|
+
return [];
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
gitHubRepoAccess: { findMany: async () => [] },
|
|
49
60
|
gitHubReconcileAudit: {
|
|
50
61
|
create: async () => {
|
|
51
62
|
state.audits += 1;
|
|
@@ -68,6 +68,20 @@ describeOrSkip("GitHubRepoCacheService (integration)", () => {
|
|
|
68
68
|
expect(r.archived).toBe(true);
|
|
69
69
|
});
|
|
70
70
|
|
|
71
|
+
it("lists a snapshot's repo full names, sorted, and no other snapshot's", async () => {
|
|
72
|
+
const snap = await svc.upsertSnapshot(key);
|
|
73
|
+
const other = await svc.upsertSnapshot({ ...key, org: "other" });
|
|
74
|
+
await svc.upsertRepos(snap.id, [
|
|
75
|
+
{ githubId: 2, fullName: "acme/web" },
|
|
76
|
+
{ githubId: 1, fullName: "acme/api" },
|
|
77
|
+
]);
|
|
78
|
+
await svc.upsertRepos(other.id, [{ githubId: 3, fullName: "other/x" }]);
|
|
79
|
+
expect(await svc.listRepoFullNames(snap.id)).toEqual([
|
|
80
|
+
"acme/api",
|
|
81
|
+
"acme/web",
|
|
82
|
+
]);
|
|
83
|
+
});
|
|
84
|
+
|
|
71
85
|
it("sweep deletes only rows seen before crawlStartedAt", async () => {
|
|
72
86
|
const snap = await svc.upsertSnapshot(key, { crawlStartedAt: min(10) });
|
|
73
87
|
await svc.upsertRepos(
|
package/src/github-repo-cache.ts
CHANGED
|
@@ -146,6 +146,25 @@ export class GitHubRepoCacheService {
|
|
|
146
146
|
}));
|
|
147
147
|
}
|
|
148
148
|
|
|
149
|
+
/** Agent-App installation snapshots (with their repos) for one account scope and App id. Reads only the cache. */
|
|
150
|
+
async listAgentSnapshots(accountId: string, appRef: string) {
|
|
151
|
+
requireNonEmpty("accountId", accountId);
|
|
152
|
+
requireNonEmpty("appRef", appRef);
|
|
153
|
+
const rows = await this.prisma.gitHubRepoSnapshot.findMany({
|
|
154
|
+
where: { accountId, appKind: "agent", appRef },
|
|
155
|
+
include: { repos: { select: { fullName: true } } },
|
|
156
|
+
orderBy: { org: "asc" },
|
|
157
|
+
});
|
|
158
|
+
return rows.map((r) => ({
|
|
159
|
+
org: r.org,
|
|
160
|
+
completedAt: r.completedAt,
|
|
161
|
+
totalCount: r.totalCount,
|
|
162
|
+
lastError: r.lastError,
|
|
163
|
+
rateLimitResetAt: r.rateLimitResetAt,
|
|
164
|
+
repos: r.repos,
|
|
165
|
+
}));
|
|
166
|
+
}
|
|
167
|
+
|
|
149
168
|
/** Upsert repos for a snapshot in batches, stamping each with `seenAt`. */
|
|
150
169
|
async upsertRepos(
|
|
151
170
|
snapshotId: string,
|
|
@@ -194,6 +213,17 @@ export class GitHubRepoCacheService {
|
|
|
194
213
|
});
|
|
195
214
|
}
|
|
196
215
|
|
|
216
|
+
/** Full names (owner/repo) of every repo cached for the snapshot, sorted. */
|
|
217
|
+
async listRepoFullNames(snapshotId: string): Promise<string[]> {
|
|
218
|
+
requireNonEmpty("snapshotId", snapshotId);
|
|
219
|
+
const rows = await this.prisma.gitHubCachedRepo.findMany({
|
|
220
|
+
where: { snapshotId },
|
|
221
|
+
select: { fullName: true },
|
|
222
|
+
orderBy: { fullName: "asc" },
|
|
223
|
+
});
|
|
224
|
+
return rows.map((r) => r.fullName);
|
|
225
|
+
}
|
|
226
|
+
|
|
197
227
|
/**
|
|
198
228
|
* Delete repos last seen before the snapshot's crawlStartedAt. Returns the
|
|
199
229
|
* number deleted; 0 (and deletes nothing) when no crawl has started.
|
|
@@ -114,6 +114,11 @@ export interface CrawlOptions {
|
|
|
114
114
|
maxPages?: number;
|
|
115
115
|
/** Delay before retrying after the n-th consecutive failure. */
|
|
116
116
|
backoffMs?: (attempts: number) => number;
|
|
117
|
+
/**
|
|
118
|
+
* Checked before every page after the first; true ends the run as
|
|
119
|
+
* in_progress (the next run resumes). Lets a caller bound a run by time.
|
|
120
|
+
*/
|
|
121
|
+
shouldStop?: () => boolean;
|
|
117
122
|
}
|
|
118
123
|
|
|
119
124
|
const DEFAULT_MAX_PAGES = 1000;
|
|
@@ -158,6 +163,7 @@ export async function crawlRepos(opts: CrawlOptions): Promise<CrawlResult> {
|
|
|
158
163
|
|
|
159
164
|
let pagesFetched = 0;
|
|
160
165
|
for (let page = startPage; pagesFetched < maxPages; page++) {
|
|
166
|
+
if (pagesFetched > 0 && opts.shouldStop?.()) break;
|
|
161
167
|
let result: RepoPage;
|
|
162
168
|
try {
|
|
163
169
|
result = await source.fetchPage(page);
|
|
@@ -450,3 +450,39 @@ describe("crawlRepos: page budget", () => {
|
|
|
450
450
|
expect(done.outcome).toBe("complete");
|
|
451
451
|
});
|
|
452
452
|
});
|
|
453
|
+
|
|
454
|
+
describe("crawlRepos: shouldStop (GLC-1.4 time budget)", () => {
|
|
455
|
+
it("fetches at least one page, then stops in_progress when asked and resumes later", async () => {
|
|
456
|
+
const source = new FakeSource([1, 2, 3, 4, 5, 6]);
|
|
457
|
+
let calls = 0;
|
|
458
|
+
const out = await crawlRepos({
|
|
459
|
+
key: KEY,
|
|
460
|
+
source,
|
|
461
|
+
store,
|
|
462
|
+
clock,
|
|
463
|
+
shouldStop: () => ++calls > 1,
|
|
464
|
+
});
|
|
465
|
+
expect(out.outcome).toBe("in_progress");
|
|
466
|
+
expect(source.fetched).toEqual([1, 2]);
|
|
467
|
+
expect(store.snap?.crawlPage).toBe(2);
|
|
468
|
+
expect(store.snap?.completedAt).toBeNull();
|
|
469
|
+
|
|
470
|
+
source.fetched = [];
|
|
471
|
+
const done = await run(source);
|
|
472
|
+
expect(done.outcome).toBe("complete");
|
|
473
|
+
expect(source.fetched).toEqual([1, 2, 3]);
|
|
474
|
+
});
|
|
475
|
+
|
|
476
|
+
it("is not consulted before the first page", async () => {
|
|
477
|
+
const source = new FakeSource([1, 2]);
|
|
478
|
+
const out = await crawlRepos({
|
|
479
|
+
key: KEY,
|
|
480
|
+
source,
|
|
481
|
+
store,
|
|
482
|
+
clock,
|
|
483
|
+
shouldStop: () => true,
|
|
484
|
+
});
|
|
485
|
+
expect(out.outcome).toBe("complete");
|
|
486
|
+
expect(source.fetched).toEqual([1]);
|
|
487
|
+
});
|
|
488
|
+
});
|