@app-vitals/agent-admin 1.76.0 → 1.78.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/fixtures/github-agent-installation-cassette.json +29 -0
- package/src/fixtures/github-lookup-cassette.json +65 -0
- package/src/github-agent-installation-crawler.integration.test.ts +262 -0
- package/src/github-agent-installation-crawler.ts +179 -0
- package/src/github-agent-installation-crawler.unit.test.ts +343 -0
- package/src/github-enterprise-client-page.unit.test.ts +100 -0
- package/src/github-enterprise-client.ts +46 -0
- package/src/github-installation-wiring.ts +6 -2
- package/src/github-lookup-client.integration.test.ts +90 -1
- package/src/github-lookup-client.ts +156 -6
- package/src/github-lookup-client.unit.test.ts +147 -2
- package/src/github-repo-cache.ts +8 -0
- package/src/github-repo-crawler.integration.test.ts +244 -0
- package/src/github-repo-crawler.ts +273 -0
- package/src/github-repo-crawler.unit.test.ts +452 -0
- package/src/installation-ports.ts +14 -0
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/github-agent-installation-crawler.unit.test.ts
|
|
3
|
+
* Unit tests for the agent-App installation PageSource and the
|
|
4
|
+
* crawlAgentInstallation orchestrator, against fake pages and an in-memory
|
|
5
|
+
* CrawlStore.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { beforeEach, describe, expect, it } from "bun:test";
|
|
9
|
+
import { GithubEnterpriseError } from "./github-enterprise-client.ts";
|
|
10
|
+
import {
|
|
11
|
+
agentInstallationPageSource,
|
|
12
|
+
crawlAgentInstallation,
|
|
13
|
+
DEPLOYMENT_WIDE_ACCOUNT_KEY,
|
|
14
|
+
type AgentInstallationClient,
|
|
15
|
+
} from "./github-agent-installation-crawler.ts";
|
|
16
|
+
import type {
|
|
17
|
+
CachedRepoInput,
|
|
18
|
+
SnapshotKey,
|
|
19
|
+
SnapshotProgress,
|
|
20
|
+
} from "./github-repo-cache.ts";
|
|
21
|
+
import type { CrawlSnapshot, CrawlStore } from "./github-repo-crawler.ts";
|
|
22
|
+
import { AppNotInstalledError } from "./installation-ports.ts";
|
|
23
|
+
|
|
24
|
+
const T0 = new Date("2026-11-20T12:00:00Z");
|
|
25
|
+
const clock = { now: () => new Date(T0) };
|
|
26
|
+
|
|
27
|
+
class MemoryStore implements CrawlStore {
|
|
28
|
+
keys: SnapshotKey[] = [];
|
|
29
|
+
snap: CrawlSnapshot | null = null;
|
|
30
|
+
rows = new Map<number, { fullName: string; seenAt: Date }>();
|
|
31
|
+
async getSnapshot(key: SnapshotKey) {
|
|
32
|
+
this.keys.push(key);
|
|
33
|
+
return this.snap ? { ...this.snap } : null;
|
|
34
|
+
}
|
|
35
|
+
async upsertSnapshot(key: SnapshotKey, progress: SnapshotProgress = {}) {
|
|
36
|
+
this.keys.push(key);
|
|
37
|
+
const base: CrawlSnapshot = this.snap ?? {
|
|
38
|
+
id: "s1",
|
|
39
|
+
crawlPage: 0,
|
|
40
|
+
crawlStartedAt: null,
|
|
41
|
+
completedAt: null,
|
|
42
|
+
totalCount: null,
|
|
43
|
+
lastError: null,
|
|
44
|
+
attempts: 0,
|
|
45
|
+
nextAttemptAt: null,
|
|
46
|
+
rateLimitResetAt: null,
|
|
47
|
+
lastTickAt: null,
|
|
48
|
+
};
|
|
49
|
+
const next = { ...base } as Record<string, unknown>;
|
|
50
|
+
for (const [k, v] of Object.entries(progress)) {
|
|
51
|
+
if (v !== undefined) next[k] = v;
|
|
52
|
+
}
|
|
53
|
+
this.snap = next as unknown as CrawlSnapshot;
|
|
54
|
+
return { ...this.snap };
|
|
55
|
+
}
|
|
56
|
+
async upsertRepos(_id: string, repos: CachedRepoInput[], seenAt: Date) {
|
|
57
|
+
for (const r of repos) {
|
|
58
|
+
this.rows.set(Number(r.githubId), { fullName: r.fullName, seenAt });
|
|
59
|
+
}
|
|
60
|
+
return repos.length;
|
|
61
|
+
}
|
|
62
|
+
async countReposSeenSince(_id: string, since: Date) {
|
|
63
|
+
return [...this.rows.values()].filter((r) => r.seenAt >= since).length;
|
|
64
|
+
}
|
|
65
|
+
async sweepGeneration(_id: string) {
|
|
66
|
+
const since = this.snap?.crawlStartedAt;
|
|
67
|
+
let n = 0;
|
|
68
|
+
for (const [id, r] of this.rows) {
|
|
69
|
+
if (since && r.seenAt < since) {
|
|
70
|
+
this.rows.delete(id);
|
|
71
|
+
n++;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return n;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
interface FakeClientOpts {
|
|
79
|
+
installs?: { id: number; appId?: number; appSlug?: string; clientId?: string }[];
|
|
80
|
+
total?: number;
|
|
81
|
+
perPage?: number;
|
|
82
|
+
failPage?: (page: number) => Error | undefined;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function fakeClient(o: FakeClientOpts = {}) {
|
|
86
|
+
const perPage = o.perPage ?? 2;
|
|
87
|
+
const total = o.total ?? 5;
|
|
88
|
+
const calls = { installs: 0, pages: [] as number[] };
|
|
89
|
+
const client: AgentInstallationClient = {
|
|
90
|
+
async listOrgInstallations() {
|
|
91
|
+
calls.installs++;
|
|
92
|
+
return o.installs ?? [{ id: 501, appId: 777 }];
|
|
93
|
+
},
|
|
94
|
+
async listInstallationRepositoriesPage(_org, installationId, page) {
|
|
95
|
+
expect(installationId).toBe(501);
|
|
96
|
+
calls.pages.push(page);
|
|
97
|
+
const err = o.failPage?.(page);
|
|
98
|
+
if (err) throw err;
|
|
99
|
+
const start = (page - 1) * perPage;
|
|
100
|
+
const ids = Array.from({ length: total }, (_, i) => i + 1).slice(
|
|
101
|
+
start,
|
|
102
|
+
start + perPage,
|
|
103
|
+
);
|
|
104
|
+
return {
|
|
105
|
+
repos: ids.map((id) => ({ id, fullName: `acme/r${id}` })),
|
|
106
|
+
hasNext: start + perPage < total,
|
|
107
|
+
perPage,
|
|
108
|
+
};
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
return { client, calls };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const agent = (over: Record<string, unknown> = {}) => ({
|
|
115
|
+
id: "a1",
|
|
116
|
+
accountId: "acc",
|
|
117
|
+
appId: 777,
|
|
118
|
+
...over,
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
describe("agentInstallationPageSource", () => {
|
|
122
|
+
it("maps a page and reports totalCount from the page position", async () => {
|
|
123
|
+
const { client } = fakeClient({ total: 5 });
|
|
124
|
+
const src = agentInstallationPageSource({
|
|
125
|
+
client,
|
|
126
|
+
org: "acme",
|
|
127
|
+
appId: 777,
|
|
128
|
+
});
|
|
129
|
+
const p1 = await src.fetchPage(1);
|
|
130
|
+
expect(p1.repos.map((r) => r.fullName)).toEqual(["acme/r1", "acme/r2"]);
|
|
131
|
+
expect(p1.hasNext).toBe(true);
|
|
132
|
+
const p3 = await src.fetchPage(3);
|
|
133
|
+
// (3-1)*2 + 1 row on the last page
|
|
134
|
+
expect(p3.totalCount).toBe(5);
|
|
135
|
+
expect(p3.hasNext).toBe(false);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("carries archived through to the cache input", async () => {
|
|
139
|
+
const src = agentInstallationPageSource({
|
|
140
|
+
client: {
|
|
141
|
+
listOrgInstallations: async () => [{ id: 501, appId: 777 }],
|
|
142
|
+
listInstallationRepositoriesPage: async () => ({
|
|
143
|
+
repos: [{ id: 9, fullName: "acme/x", archived: true }],
|
|
144
|
+
hasNext: false,
|
|
145
|
+
perPage: 100,
|
|
146
|
+
}),
|
|
147
|
+
},
|
|
148
|
+
org: "acme",
|
|
149
|
+
appId: 777,
|
|
150
|
+
});
|
|
151
|
+
expect((await src.fetchPage(1)).repos).toEqual([
|
|
152
|
+
{ githubId: 9, fullName: "acme/x", archived: true },
|
|
153
|
+
]);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it("resolves the installation once and caches it", async () => {
|
|
157
|
+
const { client, calls } = fakeClient();
|
|
158
|
+
const src = agentInstallationPageSource({ client, org: "acme", appId: 777 });
|
|
159
|
+
await src.fetchPage(1);
|
|
160
|
+
await src.fetchPage(2);
|
|
161
|
+
await src.resolveInstallationId();
|
|
162
|
+
expect(calls.installs).toBe(1);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it("matches by identity (client id) before app id", async () => {
|
|
166
|
+
const { client } = fakeClient({
|
|
167
|
+
installs: [
|
|
168
|
+
{ id: 900, appId: 777, clientId: "Iv1.other" },
|
|
169
|
+
{ id: 501, clientId: "Iv1.mine" },
|
|
170
|
+
],
|
|
171
|
+
});
|
|
172
|
+
const src = agentInstallationPageSource({
|
|
173
|
+
client,
|
|
174
|
+
org: "acme",
|
|
175
|
+
appId: 777,
|
|
176
|
+
identity: { clientId: "Iv1.mine" },
|
|
177
|
+
});
|
|
178
|
+
expect(await src.resolveInstallationId()).toBe(501);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it("throws AppNotInstalledError when no installation matches", async () => {
|
|
182
|
+
const { client } = fakeClient({ installs: [{ id: 1, appId: 5 }] });
|
|
183
|
+
const src = agentInstallationPageSource({ client, org: "acme", appId: 777 });
|
|
184
|
+
await expect(src.fetchPage(1)).rejects.toBeInstanceOf(AppNotInstalledError);
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
describe("crawlAgentInstallation", () => {
|
|
189
|
+
let store: MemoryStore;
|
|
190
|
+
beforeEach(() => {
|
|
191
|
+
store = new MemoryStore();
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it("crawls under appKind agent with appRef = String(appId)", async () => {
|
|
195
|
+
const { client } = fakeClient({ total: 5 });
|
|
196
|
+
const res = await crawlAgentInstallation({
|
|
197
|
+
agent: agent(),
|
|
198
|
+
org: "acme",
|
|
199
|
+
client,
|
|
200
|
+
store,
|
|
201
|
+
clock,
|
|
202
|
+
});
|
|
203
|
+
expect(res.outcome).toBe("complete");
|
|
204
|
+
expect(store.keys[0]).toEqual({
|
|
205
|
+
accountId: "acc",
|
|
206
|
+
appKind: "agent",
|
|
207
|
+
appRef: "777",
|
|
208
|
+
org: "acme",
|
|
209
|
+
});
|
|
210
|
+
expect(store.rows.size).toBe(5);
|
|
211
|
+
expect(store.snap?.completedAt).not.toBeNull();
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it("uses the deployment-wide key when the agent has no account", async () => {
|
|
215
|
+
const { client } = fakeClient();
|
|
216
|
+
await crawlAgentInstallation({
|
|
217
|
+
agent: agent({ accountId: null }),
|
|
218
|
+
org: "acme",
|
|
219
|
+
client,
|
|
220
|
+
store,
|
|
221
|
+
clock,
|
|
222
|
+
});
|
|
223
|
+
expect(store.keys[0]?.accountId).toBe(DEPLOYMENT_WIDE_ACCOUNT_KEY);
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
it("is incomplete (engine completeness check) when rows shift mid-crawl", async () => {
|
|
227
|
+
const { client } = fakeClient({ total: 5 });
|
|
228
|
+
const dup: AgentInstallationClient = {
|
|
229
|
+
listOrgInstallations: client.listOrgInstallations,
|
|
230
|
+
async listInstallationRepositoriesPage(org, id, page) {
|
|
231
|
+
const res = await client.listInstallationRepositoriesPage(org, id, page);
|
|
232
|
+
// page 2 repeats a row from page 1 and loses its own
|
|
233
|
+
return page === 2
|
|
234
|
+
? { ...res, repos: [res.repos[0], { id: 1, fullName: "acme/r1" }].filter((r): r is NonNullable<typeof r> => !!r) }
|
|
235
|
+
: res;
|
|
236
|
+
},
|
|
237
|
+
};
|
|
238
|
+
const res = await crawlAgentInstallation({
|
|
239
|
+
agent: agent(),
|
|
240
|
+
org: "acme",
|
|
241
|
+
client: dup,
|
|
242
|
+
store,
|
|
243
|
+
clock,
|
|
244
|
+
});
|
|
245
|
+
expect(res.outcome).toBe("incomplete");
|
|
246
|
+
expect(store.snap?.completedAt).toBeNull();
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
it("resumes: a second run continues from the saved page", async () => {
|
|
250
|
+
const { client, calls } = fakeClient({ total: 5 });
|
|
251
|
+
const first = await crawlAgentInstallation({
|
|
252
|
+
agent: agent(),
|
|
253
|
+
org: "acme",
|
|
254
|
+
client,
|
|
255
|
+
store,
|
|
256
|
+
clock,
|
|
257
|
+
maxPages: 1,
|
|
258
|
+
});
|
|
259
|
+
expect(first.outcome).toBe("in_progress");
|
|
260
|
+
const second = await crawlAgentInstallation({
|
|
261
|
+
agent: agent(),
|
|
262
|
+
org: "acme",
|
|
263
|
+
client,
|
|
264
|
+
store,
|
|
265
|
+
clock,
|
|
266
|
+
});
|
|
267
|
+
expect(second.outcome).toBe("complete");
|
|
268
|
+
expect(calls.pages).toEqual([1, 1, 2, 3]);
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
it("records not-installed as a completed empty snapshot, not an error", async () => {
|
|
272
|
+
store.rows.set(1, { fullName: "acme/stale", seenAt: new Date(0) });
|
|
273
|
+
const { client, calls } = fakeClient({ installs: [] });
|
|
274
|
+
const res = await crawlAgentInstallation({
|
|
275
|
+
agent: agent(),
|
|
276
|
+
org: "acme",
|
|
277
|
+
client,
|
|
278
|
+
store,
|
|
279
|
+
clock,
|
|
280
|
+
});
|
|
281
|
+
expect(res).toEqual({ outcome: "not_installed", pagesFetched: 0, swept: 1 });
|
|
282
|
+
expect(calls.pages).toEqual([]);
|
|
283
|
+
expect(store.rows.size).toBe(0);
|
|
284
|
+
expect(store.snap).toMatchObject({
|
|
285
|
+
totalCount: 0,
|
|
286
|
+
lastError: null,
|
|
287
|
+
attempts: 0,
|
|
288
|
+
nextAttemptAt: null,
|
|
289
|
+
});
|
|
290
|
+
expect(store.snap?.completedAt).toEqual(T0);
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
it("skips agents without GH_APP_ID and never touches the store or client", async () => {
|
|
294
|
+
const { client, calls } = fakeClient();
|
|
295
|
+
const res = await crawlAgentInstallation({
|
|
296
|
+
agent: agent({ appId: undefined }),
|
|
297
|
+
org: "acme",
|
|
298
|
+
client,
|
|
299
|
+
store,
|
|
300
|
+
clock,
|
|
301
|
+
});
|
|
302
|
+
expect(res).toEqual({ outcome: "skipped_no_app_id", pagesFetched: 0 });
|
|
303
|
+
expect(store.keys).toEqual([]);
|
|
304
|
+
expect(calls.installs).toBe(0);
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
it("ends the run cleanly on a rate limit", async () => {
|
|
308
|
+
const { client } = fakeClient({
|
|
309
|
+
failPage: () =>
|
|
310
|
+
new GithubEnterpriseError("rate_limit", "slow down", {
|
|
311
|
+
status: 429,
|
|
312
|
+
retryAfterSeconds: 30,
|
|
313
|
+
}),
|
|
314
|
+
});
|
|
315
|
+
const res = await crawlAgentInstallation({
|
|
316
|
+
agent: agent(),
|
|
317
|
+
org: "acme",
|
|
318
|
+
client,
|
|
319
|
+
store,
|
|
320
|
+
clock,
|
|
321
|
+
});
|
|
322
|
+
expect(res).toMatchObject({
|
|
323
|
+
outcome: "rate_limited",
|
|
324
|
+
rateLimitResetAt: new Date(T0.getTime() + 30_000),
|
|
325
|
+
});
|
|
326
|
+
expect(store.snap?.attempts).toBe(0);
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
it("records a GitHub failure as failed", async () => {
|
|
330
|
+
const { client } = fakeClient({
|
|
331
|
+
failPage: () => new GithubEnterpriseError("server", "boom"),
|
|
332
|
+
});
|
|
333
|
+
const res = await crawlAgentInstallation({
|
|
334
|
+
agent: agent(),
|
|
335
|
+
org: "acme",
|
|
336
|
+
client,
|
|
337
|
+
store,
|
|
338
|
+
clock,
|
|
339
|
+
});
|
|
340
|
+
expect(res.outcome).toBe("failed");
|
|
341
|
+
expect(store.snap?.attempts).toBe(1);
|
|
342
|
+
});
|
|
343
|
+
});
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/github-enterprise-client-page.unit.test.ts
|
|
3
|
+
* GithubEnterpriseClient.listInstallationRepositoriesPage: one page, Link-header
|
|
4
|
+
* driven hasNext, archived passthrough, validation.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { describe, expect, it } from "bun:test";
|
|
8
|
+
import { generateKeyPairSync } from "node:crypto";
|
|
9
|
+
import { GithubEnterpriseClient } from "./github-enterprise-client.ts";
|
|
10
|
+
|
|
11
|
+
const { privateKey } = generateKeyPairSync("rsa", {
|
|
12
|
+
modulusLength: 2048,
|
|
13
|
+
privateKeyEncoding: { type: "pkcs8", format: "pem" },
|
|
14
|
+
publicKeyEncoding: { type: "spki", format: "pem" },
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
function make(repoResp: () => Response) {
|
|
18
|
+
const paths: string[] = [];
|
|
19
|
+
const fetchFn = (async (input: RequestInfo | URL) => {
|
|
20
|
+
const u = new URL(input.toString());
|
|
21
|
+
paths.push(`${u.pathname}${u.search}`);
|
|
22
|
+
if (u.pathname.endsWith("/installation"))
|
|
23
|
+
return Response.json({ id: 1 });
|
|
24
|
+
if (u.pathname.endsWith("/access_tokens"))
|
|
25
|
+
return Response.json(
|
|
26
|
+
{ token: "t", expires_at: "2099-01-01T00:00:00Z" },
|
|
27
|
+
{ status: 201 },
|
|
28
|
+
);
|
|
29
|
+
return repoResp();
|
|
30
|
+
}) as typeof fetch;
|
|
31
|
+
return {
|
|
32
|
+
paths,
|
|
33
|
+
client: new GithubEnterpriseClient({
|
|
34
|
+
appId: "1",
|
|
35
|
+
enterprise: "ent",
|
|
36
|
+
privateKey,
|
|
37
|
+
apiBase: "https://ghe.test",
|
|
38
|
+
fetchFn,
|
|
39
|
+
}),
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
describe("listInstallationRepositoriesPage", () => {
|
|
44
|
+
it("fetches only the requested page and reads the Link header", async () => {
|
|
45
|
+
const { client, paths } = make(() =>
|
|
46
|
+
Response.json([{ id: 1, name: "a", full_name: "o/a", archived: true }], {
|
|
47
|
+
headers: { link: '<https://x?page=3>; rel="next"' },
|
|
48
|
+
}),
|
|
49
|
+
);
|
|
50
|
+
const res = await client.listInstallationRepositoriesPage("o", 5, 2);
|
|
51
|
+
expect(res).toEqual({
|
|
52
|
+
repos: [{ id: 1, name: "a", fullName: "o/a", archived: true }],
|
|
53
|
+
hasNext: true,
|
|
54
|
+
perPage: 100,
|
|
55
|
+
});
|
|
56
|
+
expect(paths.filter((p) => p.includes("/repositories"))).toEqual([
|
|
57
|
+
"/enterprises/ent/apps/organizations/o/installations/5/repositories?per_page=100&page=2",
|
|
58
|
+
]);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("without a Link header, hasNext is whether the page is full", async () => {
|
|
62
|
+
const full = Array.from({ length: 100 }, (_, i) => ({
|
|
63
|
+
id: i,
|
|
64
|
+
name: "n",
|
|
65
|
+
full_name: "o/n",
|
|
66
|
+
}));
|
|
67
|
+
expect(
|
|
68
|
+
(await make(() => Response.json(full)).client.listInstallationRepositoriesPage("o", 5, 1)).hasNext,
|
|
69
|
+
).toBe(true);
|
|
70
|
+
expect(
|
|
71
|
+
(await make(() => Response.json(full.slice(0, 3))).client.listInstallationRepositoriesPage("o", 5, 1)).hasNext,
|
|
72
|
+
).toBe(false);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("maps a 429 to a rate_limit error with retry-after", async () => {
|
|
76
|
+
const { client } = make(
|
|
77
|
+
() =>
|
|
78
|
+
new Response(JSON.stringify({ message: "limit" }), {
|
|
79
|
+
status: 429,
|
|
80
|
+
headers: { "retry-after": "12" },
|
|
81
|
+
}),
|
|
82
|
+
);
|
|
83
|
+
const err = await client
|
|
84
|
+
.listInstallationRepositoriesPage("o", 5, 1)
|
|
85
|
+
.catch((e) => e);
|
|
86
|
+
expect(err.kind).toBe("rate_limit");
|
|
87
|
+
expect(err.retryAfterSeconds).toBe(12);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("rejects a bad page or installation id before any request", async () => {
|
|
91
|
+
const { client, paths } = make(() => Response.json([]));
|
|
92
|
+
expect(
|
|
93
|
+
(await client.listInstallationRepositoriesPage("o", 5, 0).catch((e) => e)).kind,
|
|
94
|
+
).toBe("validation");
|
|
95
|
+
expect(
|
|
96
|
+
(await client.listInstallationRepositoriesPage("o", Number.NaN, 1).catch((e) => e)).kind,
|
|
97
|
+
).toBe("validation");
|
|
98
|
+
expect(paths).toEqual([]);
|
|
99
|
+
});
|
|
100
|
+
});
|
|
@@ -174,6 +174,14 @@ export interface AccessibleRepository {
|
|
|
174
174
|
fullName: string;
|
|
175
175
|
}
|
|
176
176
|
|
|
177
|
+
/** One page of an installation's repositories. */
|
|
178
|
+
export interface InstallationRepositoriesPage {
|
|
179
|
+
repos: (AccessibleRepository & { archived?: boolean })[];
|
|
180
|
+
hasNext: boolean;
|
|
181
|
+
/** The page size requested; every page but the last is full. */
|
|
182
|
+
perPage: number;
|
|
183
|
+
}
|
|
184
|
+
|
|
177
185
|
export interface SetRepositorySelectionInput {
|
|
178
186
|
repositorySelection: "all" | "selected";
|
|
179
187
|
/** Simple repo names (max 50); required when repositorySelection is "selected". */
|
|
@@ -365,6 +373,43 @@ export class GithubEnterpriseClient {
|
|
|
365
373
|
return rows.map(toRepository);
|
|
366
374
|
}
|
|
367
375
|
|
|
376
|
+
/**
|
|
377
|
+
* One page (1-based) of the repositories an installation can access. The
|
|
378
|
+
* endpoint returns a bare array with no total_count, so callers that need a
|
|
379
|
+
* total derive it from the page position (see GLC-1.5).
|
|
380
|
+
*/
|
|
381
|
+
async listInstallationRepositoriesPage(
|
|
382
|
+
org: string,
|
|
383
|
+
installationId: number,
|
|
384
|
+
page: number,
|
|
385
|
+
): Promise<InstallationRepositoriesPage> {
|
|
386
|
+
if (!Number.isInteger(page) || page < 1) {
|
|
387
|
+
throw new GithubEnterpriseError("validation", "page must be a positive integer");
|
|
388
|
+
}
|
|
389
|
+
const context = "GitHub installation repositories";
|
|
390
|
+
const path = this.repositoriesPath(org, installationId);
|
|
391
|
+
const { token } = await this.getInstallationToken();
|
|
392
|
+
const res = await this.request<RawRepository[]>(
|
|
393
|
+
"GET",
|
|
394
|
+
`${path}?per_page=${PER_PAGE}&page=${page}`,
|
|
395
|
+
token,
|
|
396
|
+
context,
|
|
397
|
+
);
|
|
398
|
+
if (!Array.isArray(res.data)) {
|
|
399
|
+
throw new GithubEnterpriseError("unexpected", `${context}: response is not a list`);
|
|
400
|
+
}
|
|
401
|
+
const link = res.headers.get("link");
|
|
402
|
+
const hasNext = link !== null ? /rel="next"/.test(link) : res.data.length >= PER_PAGE;
|
|
403
|
+
return {
|
|
404
|
+
repos: res.data.map((r) => ({
|
|
405
|
+
...toRepository(r),
|
|
406
|
+
...(typeof r.archived === "boolean" ? { archived: r.archived } : {}),
|
|
407
|
+
})),
|
|
408
|
+
hasNext,
|
|
409
|
+
perPage: PER_PAGE,
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
|
|
368
413
|
/** Grant an installation access to repositories, in batches of at most 50. */
|
|
369
414
|
async addRepositories(
|
|
370
415
|
org: string,
|
|
@@ -520,6 +565,7 @@ interface RawRepository {
|
|
|
520
565
|
id: number;
|
|
521
566
|
name: string;
|
|
522
567
|
full_name: string;
|
|
568
|
+
archived?: boolean;
|
|
523
569
|
}
|
|
524
570
|
|
|
525
571
|
function toRepository(r: RawRepository): AccessibleRepository {
|
|
@@ -40,12 +40,14 @@ export interface GithubInstallationWiringDeps {
|
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
/** Builds the installer client per call so key rotations are picked up. */
|
|
43
|
-
function lazyInstallerClient(
|
|
43
|
+
export function lazyInstallerClient(
|
|
44
44
|
deps: Pick<
|
|
45
45
|
GithubInstallationWiringDeps,
|
|
46
46
|
"appService" | "fetchFn" | "apiBase" | "now"
|
|
47
47
|
>,
|
|
48
|
-
): InstallationRepoClient &
|
|
48
|
+
): InstallationRepoClient &
|
|
49
|
+
InstalledOrgsClient &
|
|
50
|
+
Required<Pick<InstallationRepoClient, "listInstallationRepositoriesPage">> {
|
|
49
51
|
const client = async (
|
|
50
52
|
accountId?: string | null,
|
|
51
53
|
): Promise<GithubEnterpriseClient> => {
|
|
@@ -72,6 +74,8 @@ function lazyInstallerClient(
|
|
|
72
74
|
(await client(accountId)).listOrgInstallations(org),
|
|
73
75
|
listInstallationRepositories: async (org, id, accountId) =>
|
|
74
76
|
(await client(accountId)).listInstallationRepositories(org, id),
|
|
77
|
+
listInstallationRepositoriesPage: async (org, id, page, accountId) =>
|
|
78
|
+
(await client(accountId)).listInstallationRepositoriesPage(org, id, page),
|
|
75
79
|
addRepositories: async (org, id, names, accountId) =>
|
|
76
80
|
(await client(accountId)).addRepositories(org, id, names),
|
|
77
81
|
removeRepositories: async (org, id, names, accountId) =>
|
|
@@ -12,7 +12,11 @@ import { describe, expect, it } from "bun:test";
|
|
|
12
12
|
import { generateKeyPairSync } from "node:crypto";
|
|
13
13
|
import { readFileSync } from "node:fs";
|
|
14
14
|
import type { GithubEnterpriseErrorKind } from "./github-enterprise-client.ts";
|
|
15
|
-
import {
|
|
15
|
+
import {
|
|
16
|
+
GithubLookupClient,
|
|
17
|
+
GithubLookupRateLimitError,
|
|
18
|
+
type RepoRole,
|
|
19
|
+
} from "./github-lookup-client.ts";
|
|
16
20
|
|
|
17
21
|
interface CassetteEntry {
|
|
18
22
|
status: number;
|
|
@@ -228,6 +232,91 @@ describe("listInstallationRepositories", () => {
|
|
|
228
232
|
}
|
|
229
233
|
});
|
|
230
234
|
|
|
235
|
+
describe("listInstallationRepositoriesPage", () => {
|
|
236
|
+
const RESET = new Date("2026-11-09T12:15:00Z");
|
|
237
|
+
|
|
238
|
+
it("returns one page's rows, total_count and Link-driven hasNext", async () => {
|
|
239
|
+
const { client, calls } = makeClient({
|
|
240
|
+
[`${REPOS}?page=1`]: "repos_page_1",
|
|
241
|
+
[`${REPOS}?page=2`]: "repos_page_2",
|
|
242
|
+
});
|
|
243
|
+
const p1 = await client.listInstallationRepositoriesPage("acme-eng", 1);
|
|
244
|
+
expect(p1.repositories.map((r) => r.fullName)).toEqual([
|
|
245
|
+
"acme-eng/api",
|
|
246
|
+
"acme-eng/web",
|
|
247
|
+
]);
|
|
248
|
+
expect(p1.totalCount).toBe(3);
|
|
249
|
+
expect(p1.hasNext).toBe(true);
|
|
250
|
+
const p2 = await client.listInstallationRepositoriesPage("acme-eng", 2);
|
|
251
|
+
expect(p2.repositories.map((r) => r.id)).toEqual([3]);
|
|
252
|
+
expect(p2.hasNext).toBe(false);
|
|
253
|
+
const pages = calls.filter((c) =>
|
|
254
|
+
c.path.startsWith("/installation/repositories"),
|
|
255
|
+
);
|
|
256
|
+
expect(pages.map((c) => c.path)).toEqual([
|
|
257
|
+
"/installation/repositories?per_page=100&page=1",
|
|
258
|
+
"/installation/repositories?per_page=100&page=2",
|
|
259
|
+
]);
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
it("reports the rate-limit headers of a successful page", async () => {
|
|
263
|
+
const { client } = makeClient({
|
|
264
|
+
[REPOS]: "repos_page_1_rate_limit_exhausted",
|
|
265
|
+
});
|
|
266
|
+
const page = await client.listInstallationRepositoriesPage("acme-eng", 1);
|
|
267
|
+
expect(page.repositories).toHaveLength(2);
|
|
268
|
+
expect(page.rateLimit).toEqual({
|
|
269
|
+
remaining: 0,
|
|
270
|
+
resetAt: RESET,
|
|
271
|
+
retryAfterSeconds: null,
|
|
272
|
+
});
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
it("raises a rate-limit error carrying Retry-After as resetAt on 429", async () => {
|
|
276
|
+
const { client } = makeClient({ [REPOS]: "repos_429" });
|
|
277
|
+
const e = await errOf(
|
|
278
|
+
client.listInstallationRepositoriesPage("acme-eng", 1),
|
|
279
|
+
);
|
|
280
|
+
expect(e).toBeInstanceOf(GithubLookupRateLimitError);
|
|
281
|
+
expect(e.kind).toBe("rate_limit");
|
|
282
|
+
expect((e as GithubLookupRateLimitError).resetAt).toEqual(
|
|
283
|
+
new Date(NOW.getTime() + 30_000),
|
|
284
|
+
);
|
|
285
|
+
expectNoSecrets(e);
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
it("raises a rate-limit error carrying X-RateLimit-Reset on a primary-limit 403", async () => {
|
|
289
|
+
const { client } = makeClient({ [REPOS]: "repos_403_rate_limited" });
|
|
290
|
+
const e = await errOf(
|
|
291
|
+
client.listInstallationRepositoriesPage("acme-eng", 1),
|
|
292
|
+
);
|
|
293
|
+
expect(e).toBeInstanceOf(GithubLookupRateLimitError);
|
|
294
|
+
expect((e as GithubLookupRateLimitError).resetAt).toEqual(RESET);
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
it("raises a rate-limit error on a secondary-limit 403 with Retry-After", async () => {
|
|
298
|
+
const { client } = makeClient({
|
|
299
|
+
[REPOS]: "repos_403_secondary_retry_after",
|
|
300
|
+
});
|
|
301
|
+
const e = await errOf(
|
|
302
|
+
client.listInstallationRepositoriesPage("acme-eng", 1),
|
|
303
|
+
);
|
|
304
|
+
expect(e).toBeInstanceOf(GithubLookupRateLimitError);
|
|
305
|
+
expect((e as GithubLookupRateLimitError).resetAt).toEqual(
|
|
306
|
+
new Date(NOW.getTime() + 120_000),
|
|
307
|
+
);
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
it("keeps a plain permission 403 a permission error", async () => {
|
|
311
|
+
const { client } = makeClient({ [REPOS]: "repos_403" });
|
|
312
|
+
const e = await errOf(
|
|
313
|
+
client.listInstallationRepositoriesPage("acme-eng", 1),
|
|
314
|
+
);
|
|
315
|
+
expect(e.kind).toBe("permission");
|
|
316
|
+
expect(e).not.toBeInstanceOf(GithubLookupRateLimitError);
|
|
317
|
+
});
|
|
318
|
+
});
|
|
319
|
+
|
|
231
320
|
describe("getCollaboratorPermission", () => {
|
|
232
321
|
const roles: Array<[string, RepoRole]> = [
|
|
233
322
|
["permission_admin", "admin"],
|