@app-vitals/agent-admin 1.78.0 → 1.80.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/package.json +2 -2
- package/src/admin-boot.ts +2 -0
- package/src/github-access-backfill.integration.test.ts +208 -0
- package/src/github-access-backfill.ts +356 -0
- package/src/github-access-backfill.unit.test.ts +351 -0
- package/src/github-enterprise-api.ts +31 -58
- package/src/github-lookup-client.ts +15 -1
- package/src/github-lookup-repositories.integration.test.ts +63 -368
- package/src/github-lookup-repositories.ts +74 -46
- package/src/github-lookup-repositories.unit.test.ts +205 -0
- package/src/github-repo-cache.ts +39 -0
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/github-lookup-repositories.unit.test.ts
|
|
3
|
+
* Cache-backed githubLookup: syncState derivation, the unconfigured cases and
|
|
4
|
+
* the guarantee that no GitHub call is made.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { describe, expect, it } from "bun:test";
|
|
8
|
+
import { createGithubLookupService } from "./github-enterprise-api.ts";
|
|
9
|
+
import type { GitHubEnterpriseAppService } from "./github-enterprise-apps.ts";
|
|
10
|
+
import {
|
|
11
|
+
type LookupSnapshotView,
|
|
12
|
+
summarizeLookupSnapshots,
|
|
13
|
+
} from "./github-lookup-repositories.ts";
|
|
14
|
+
|
|
15
|
+
const NOW = new Date("2026-11-20T12:00:00Z");
|
|
16
|
+
const ago = (ms: number) => new Date(NOW.getTime() - ms);
|
|
17
|
+
const MIN = 60_000;
|
|
18
|
+
|
|
19
|
+
const snap = (o: Partial<LookupSnapshotView> = {}): LookupSnapshotView => ({
|
|
20
|
+
completedAt: ago(10 * MIN),
|
|
21
|
+
totalCount: 2,
|
|
22
|
+
lastError: null,
|
|
23
|
+
repos: [
|
|
24
|
+
{ fullName: "acme/a", archived: false },
|
|
25
|
+
{ fullName: "acme/b", archived: true },
|
|
26
|
+
],
|
|
27
|
+
...o,
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
describe("summarizeLookupSnapshots", () => {
|
|
31
|
+
it("no snapshot yet: configured, empty, syncing", () => {
|
|
32
|
+
expect(summarizeLookupSnapshots([], NOW)).toEqual({
|
|
33
|
+
configured: true,
|
|
34
|
+
repos: [],
|
|
35
|
+
syncState: "syncing",
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("complete and fresh: ready with fetchedAt and progress", () => {
|
|
40
|
+
const r = summarizeLookupSnapshots([snap()], NOW);
|
|
41
|
+
expect(r.syncState).toBe("ready");
|
|
42
|
+
expect(r.fetchedAt).toEqual(ago(10 * MIN));
|
|
43
|
+
expect(r.progress).toEqual({ reposSeen: 2, totalCount: 2 });
|
|
44
|
+
expect(r.repos).toEqual([
|
|
45
|
+
{ fullName: "acme/a", archived: false },
|
|
46
|
+
{ fullName: "acme/b", archived: true },
|
|
47
|
+
]);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("complete but older than the TTL: stale, repos still served", () => {
|
|
51
|
+
const r = summarizeLookupSnapshots(
|
|
52
|
+
[snap({ completedAt: ago(61 * MIN) })],
|
|
53
|
+
NOW,
|
|
54
|
+
);
|
|
55
|
+
expect(r.syncState).toBe("stale");
|
|
56
|
+
expect(r.repos).toHaveLength(2);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("honours a custom TTL", () => {
|
|
60
|
+
const r = summarizeLookupSnapshots([snap()], NOW, 5 * MIN);
|
|
61
|
+
expect(r.syncState).toBe("stale");
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("in-progress crawl: partial repos, syncing, progress, no fetchedAt", () => {
|
|
65
|
+
const r = summarizeLookupSnapshots(
|
|
66
|
+
[
|
|
67
|
+
snap({
|
|
68
|
+
completedAt: null,
|
|
69
|
+
totalCount: 10,
|
|
70
|
+
repos: [{ fullName: "acme/a", archived: false }],
|
|
71
|
+
}),
|
|
72
|
+
],
|
|
73
|
+
NOW,
|
|
74
|
+
);
|
|
75
|
+
expect(r.syncState).toBe("syncing");
|
|
76
|
+
expect(r.repos).toEqual([{ fullName: "acme/a", archived: false }]);
|
|
77
|
+
expect(r.progress).toEqual({ reposSeen: 1, totalCount: 10 });
|
|
78
|
+
expect(r.fetchedAt).toBeUndefined();
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("incomplete crawl with an error: failed", () => {
|
|
82
|
+
const r = summarizeLookupSnapshots(
|
|
83
|
+
[snap({ completedAt: null, lastError: "boom" })],
|
|
84
|
+
NOW,
|
|
85
|
+
);
|
|
86
|
+
expect(r.syncState).toBe("failed");
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("omits totalCount while any org has not reported one", () => {
|
|
90
|
+
const r = summarizeLookupSnapshots(
|
|
91
|
+
[snap(), snap({ completedAt: null, totalCount: null, repos: [] })],
|
|
92
|
+
NOW,
|
|
93
|
+
);
|
|
94
|
+
expect(r.progress).toEqual({ reposSeen: 2 });
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("merges orgs: syncing beats stale, failed beats syncing, dedupes repos", () => {
|
|
98
|
+
const orgA = snap({ completedAt: ago(90 * MIN) });
|
|
99
|
+
const orgB = snap({
|
|
100
|
+
completedAt: null,
|
|
101
|
+
repos: [{ fullName: "acme/a", archived: false }],
|
|
102
|
+
});
|
|
103
|
+
const mixed = summarizeLookupSnapshots([orgA, orgB], NOW);
|
|
104
|
+
expect(mixed.syncState).toBe("syncing");
|
|
105
|
+
expect(mixed.repos.map((r) => r.fullName).sort()).toEqual([
|
|
106
|
+
"acme/a",
|
|
107
|
+
"acme/b",
|
|
108
|
+
]);
|
|
109
|
+
const failed = summarizeLookupSnapshots(
|
|
110
|
+
[orgA, { ...orgB, lastError: "x" }],
|
|
111
|
+
NOW,
|
|
112
|
+
);
|
|
113
|
+
expect(failed.syncState).toBe("failed");
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("fetchedAt is the oldest completion across orgs", () => {
|
|
117
|
+
const r = summarizeLookupSnapshots(
|
|
118
|
+
[
|
|
119
|
+
snap({ completedAt: ago(5 * MIN) }),
|
|
120
|
+
snap({ completedAt: ago(30 * MIN) }),
|
|
121
|
+
],
|
|
122
|
+
NOW,
|
|
123
|
+
);
|
|
124
|
+
expect(r.fetchedAt).toEqual(ago(30 * MIN));
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
describe("createGithubLookupService", () => {
|
|
129
|
+
type Row = { appId: string; enterpriseSlug: string | null };
|
|
130
|
+
function apps(rows: Record<string, Row | undefined>, withKey = true) {
|
|
131
|
+
return {
|
|
132
|
+
get: async (role: string) => rows[role] ?? null,
|
|
133
|
+
getPrivateKey: async (role: string) =>
|
|
134
|
+
rows[role] && withKey ? "pem" : null,
|
|
135
|
+
} as unknown as GitHubEnterpriseAppService;
|
|
136
|
+
}
|
|
137
|
+
const lookupRow = { appId: "555", enterpriseSlug: null };
|
|
138
|
+
|
|
139
|
+
function build(
|
|
140
|
+
rows: Record<string, Row | undefined>,
|
|
141
|
+
snapshots: LookupSnapshotView[] = [],
|
|
142
|
+
withKey = true,
|
|
143
|
+
) {
|
|
144
|
+
const calls: unknown[][] = [];
|
|
145
|
+
const svc = createGithubLookupService({
|
|
146
|
+
appService: apps(rows, withKey),
|
|
147
|
+
now: () => NOW,
|
|
148
|
+
cache: {
|
|
149
|
+
listLookupSnapshots: async (...args: unknown[]) => {
|
|
150
|
+
calls.push(args);
|
|
151
|
+
return snapshots;
|
|
152
|
+
},
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
return { svc, calls };
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
it("unconfigured when no lookup App: configured:false, cache untouched", async () => {
|
|
159
|
+
const { svc, calls } = build({});
|
|
160
|
+
expect(await svc.listRepositories("acct")).toEqual({
|
|
161
|
+
configured: false,
|
|
162
|
+
repos: [],
|
|
163
|
+
});
|
|
164
|
+
expect(calls).toHaveLength(0);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("unconfigured when the lookup App has no key", async () => {
|
|
168
|
+
const { svc } = build({ lookup: lookupRow }, [], false);
|
|
169
|
+
expect((await svc.listRepositories()).configured).toBe(false);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it("unconfigured when an installer with a slug has no key", async () => {
|
|
173
|
+
const rows = {
|
|
174
|
+
lookup: lookupRow,
|
|
175
|
+
installer: { appId: "1", enterpriseSlug: "ent" },
|
|
176
|
+
};
|
|
177
|
+
const svc = createGithubLookupService({
|
|
178
|
+
appService: {
|
|
179
|
+
get: async (role: string) => (rows as Record<string, Row>)[role],
|
|
180
|
+
getPrivateKey: async (role: string) =>
|
|
181
|
+
role === "lookup" ? "pem" : null,
|
|
182
|
+
} as unknown as GitHubEnterpriseAppService,
|
|
183
|
+
cache: { listLookupSnapshots: async () => [] },
|
|
184
|
+
});
|
|
185
|
+
expect((await svc.listRepositories()).configured).toBe(false);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it("cold start: configured, empty, syncing", async () => {
|
|
189
|
+
const { svc } = build({ lookup: lookupRow });
|
|
190
|
+
expect(await svc.listRepositories("acct")).toEqual({
|
|
191
|
+
configured: true,
|
|
192
|
+
repos: [],
|
|
193
|
+
syncState: "syncing",
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it("queries the account and deployment-wide scopes by lookup App id", async () => {
|
|
198
|
+
const { svc, calls } = build({ lookup: lookupRow }, [snap()]);
|
|
199
|
+
const r = await svc.listRepositories("acct");
|
|
200
|
+
expect(r.syncState).toBe("ready");
|
|
201
|
+
expect(calls[0]).toEqual([["acct", ""], "555"]);
|
|
202
|
+
await svc.listRepositories();
|
|
203
|
+
expect(calls[1]).toEqual([[""], "555"]);
|
|
204
|
+
});
|
|
205
|
+
});
|
package/src/github-repo-cache.ts
CHANGED
|
@@ -128,6 +128,24 @@ export class GitHubRepoCacheService {
|
|
|
128
128
|
});
|
|
129
129
|
}
|
|
130
130
|
|
|
131
|
+
/** Lookup-App snapshots (with their repos) for the account scopes and App id. Reads only the cache. */
|
|
132
|
+
async listLookupSnapshots(accountIds: string[], appRef: string) {
|
|
133
|
+
requireNonEmpty("appRef", appRef);
|
|
134
|
+
const rows = await this.prisma.gitHubRepoSnapshot.findMany({
|
|
135
|
+
where: { accountId: { in: accountIds }, appKind: "lookup", appRef },
|
|
136
|
+
include: {
|
|
137
|
+
repos: { select: { fullName: true, archived: true } },
|
|
138
|
+
},
|
|
139
|
+
orderBy: { org: "asc" },
|
|
140
|
+
});
|
|
141
|
+
return rows.map((r) => ({
|
|
142
|
+
completedAt: r.completedAt,
|
|
143
|
+
totalCount: r.totalCount,
|
|
144
|
+
lastError: r.lastError,
|
|
145
|
+
repos: r.repos,
|
|
146
|
+
}));
|
|
147
|
+
}
|
|
148
|
+
|
|
131
149
|
/** Upsert repos for a snapshot in batches, stamping each with `seenAt`. */
|
|
132
150
|
async upsertRepos(
|
|
133
151
|
snapshotId: string,
|
|
@@ -301,4 +319,25 @@ export class GitHubRepoCacheService {
|
|
|
301
319
|
}
|
|
302
320
|
return query.repos.filter((r) => !settled.has(r));
|
|
303
321
|
}
|
|
322
|
+
|
|
323
|
+
/** Existing access rows for the user over `repos` (any status). */
|
|
324
|
+
async getAccessRows(query: {
|
|
325
|
+
accountId: string;
|
|
326
|
+
org: string;
|
|
327
|
+
username: string;
|
|
328
|
+
repos: string[];
|
|
329
|
+
}) {
|
|
330
|
+
requireNonEmpty("accountId", query.accountId);
|
|
331
|
+
requireNonEmpty("org", query.org);
|
|
332
|
+
requireNonEmpty("username", query.username);
|
|
333
|
+
if (query.repos.length === 0) return [];
|
|
334
|
+
return this.prisma.gitHubRepoAccess.findMany({
|
|
335
|
+
where: {
|
|
336
|
+
accountId: query.accountId,
|
|
337
|
+
org: query.org,
|
|
338
|
+
username: query.username,
|
|
339
|
+
repo: { in: query.repos },
|
|
340
|
+
},
|
|
341
|
+
});
|
|
342
|
+
}
|
|
304
343
|
}
|