@app-vitals/agent-admin 1.129.1 → 1.130.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/admin-ui-pages.ts +20 -6
- package/src/admin-ui-pages.unit.test.ts +23 -0
- package/src/admin-ui.ts +36 -2
- package/src/agent-github-apps-api.smoke.test.ts +155 -0
- package/src/agent-github-apps.integration.test.ts +168 -0
- package/src/agent-github-apps.ts +157 -0
- package/src/agent-github-apps.unit.test.ts +34 -0
- package/src/agents-api.smoke.test.ts +101 -0
- package/src/agents-api.ts +76 -1
- package/src/github-enterprise-api.ts +14 -1
- package/src/github-provisioning-service.integration.test.ts +189 -0
- package/src/github-provisioning-service.ts +66 -29
- package/src/github-provisioning-service.unit.test.ts +114 -6
- package/src/github-sync-multi-app.integration.test.ts +122 -0
- package/src/github-sync-multi-app.unit.test.ts +156 -0
- package/src/github-sync.ts +59 -14
|
@@ -26,6 +26,10 @@ import { sign, verify } from "hono/jwt";
|
|
|
26
26
|
import type { AdminUIGithubAppClient } from "./admin-ui.ts";
|
|
27
27
|
import type { AgentCronJobService } from "./agent-cron-jobs.ts";
|
|
28
28
|
import type { AgentEnvService } from "./agent-envs.ts";
|
|
29
|
+
import type {
|
|
30
|
+
AgentGitHubAppService,
|
|
31
|
+
LegacyGitHubAppEnvPatch,
|
|
32
|
+
} from "./agent-github-apps.ts";
|
|
29
33
|
import type { AgentService } from "./agents.ts";
|
|
30
34
|
import { buildAgentAppManifest } from "./github-app-provisioning-client.ts";
|
|
31
35
|
|
|
@@ -128,6 +132,16 @@ interface GithubProvisioningServiceDeps {
|
|
|
128
132
|
githubAppClient: AdminUIGithubAppClient;
|
|
129
133
|
agentService: Pick<AgentService, "getDetail">;
|
|
130
134
|
agentEnvService: Pick<AgentEnvService, "patch" | "getByAgentId">;
|
|
135
|
+
/**
|
|
136
|
+
* The AgentGitHubApp table (multi-github-apps 1.5). App credentials are
|
|
137
|
+
* written here as well as mirrored to env (until 1.11), and the stored
|
|
138
|
+
* installation id / slug are read from it, falling back to env for an agent
|
|
139
|
+
* the backfill has not reached. Optional so existing wiring stays valid.
|
|
140
|
+
*/
|
|
141
|
+
agentGitHubAppService?: Pick<
|
|
142
|
+
AgentGitHubAppService,
|
|
143
|
+
"getPrimary" | "applyLegacyEnvPatch"
|
|
144
|
+
>;
|
|
131
145
|
agentCronJobService: Pick<AgentCronJobService, "reconcileSystemCrons">;
|
|
132
146
|
sessionSecret: string;
|
|
133
147
|
appBaseUrl: string;
|
|
@@ -205,6 +219,39 @@ export class GithubProvisioningService {
|
|
|
205
219
|
return { ok: true, agentId };
|
|
206
220
|
}
|
|
207
221
|
|
|
222
|
+
// ── App table access ────────────────────────────────────────────────────
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Writes App credentials to the table (when wired) and mirrors them to env.
|
|
226
|
+
* The table write comes first so a failure leaves env untouched; env stays
|
|
227
|
+
* the runtime's source until 1.3/1.11.
|
|
228
|
+
*/
|
|
229
|
+
private async storeApp(
|
|
230
|
+
agentId: string,
|
|
231
|
+
patch: LegacyGitHubAppEnvPatch,
|
|
232
|
+
): Promise<void> {
|
|
233
|
+
await this.deps.agentGitHubAppService?.applyLegacyEnvPatch(agentId, patch);
|
|
234
|
+
await this.deps.agentEnvService.patch(
|
|
235
|
+
agentId,
|
|
236
|
+
patch,
|
|
237
|
+
this.deps.secretEnvVars,
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** The stored App slug and installation id: table primary, else env. */
|
|
242
|
+
private async readStoredApp(
|
|
243
|
+
agentId: string,
|
|
244
|
+
): Promise<{ slug: string | undefined; installationId: string | undefined }> {
|
|
245
|
+
const primary = await this.deps.agentGitHubAppService?.getPrimary(agentId);
|
|
246
|
+
const env = (await this.deps.agentEnvService.getByAgentId(agentId))?.env;
|
|
247
|
+
// Table first; env fills any field the table lacks (e.g. an installation id
|
|
248
|
+
// the backfill dropped as malformed) until the env path is removed.
|
|
249
|
+
return {
|
|
250
|
+
slug: primary?.slug ?? env?.GH_APP_SLUG,
|
|
251
|
+
installationId: primary?.installationId ?? env?.GH_APP_INSTALLATION_ID,
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
|
|
208
255
|
// ── App manual-paste mode ───────────────────────────────────────────────
|
|
209
256
|
|
|
210
257
|
/**
|
|
@@ -247,15 +294,11 @@ export class GithubProvisioningService {
|
|
|
247
294
|
return { ok: false, agentId, error: "Agent not found." };
|
|
248
295
|
}
|
|
249
296
|
|
|
250
|
-
await this.
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
GH_APP_PRIVATE_KEY: fields.ghAppPrivateKey,
|
|
256
|
-
},
|
|
257
|
-
this.deps.secretEnvVars,
|
|
258
|
-
);
|
|
297
|
+
await this.storeApp(agentId, {
|
|
298
|
+
GH_APP_ID: fields.ghAppId,
|
|
299
|
+
GH_APP_INSTALLATION_ID: fields.ghAppInstallationId,
|
|
300
|
+
GH_APP_PRIVATE_KEY: fields.ghAppPrivateKey,
|
|
301
|
+
});
|
|
259
302
|
|
|
260
303
|
return { ok: true, agentId };
|
|
261
304
|
}
|
|
@@ -435,17 +478,13 @@ export class GithubProvisioningService {
|
|
|
435
478
|
};
|
|
436
479
|
}
|
|
437
480
|
|
|
438
|
-
await this.
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
GH_APP_SLUG: exchangeResult.slug,
|
|
446
|
-
},
|
|
447
|
-
this.deps.secretEnvVars,
|
|
448
|
-
);
|
|
481
|
+
await this.storeApp(state.agentId, {
|
|
482
|
+
GH_APP_ID: exchangeResult.appId,
|
|
483
|
+
GH_APP_PRIVATE_KEY: exchangeResult.pem,
|
|
484
|
+
GH_APP_CLIENT_ID: exchangeResult.clientId,
|
|
485
|
+
GH_APP_CLIENT_SECRET: exchangeResult.clientSecret,
|
|
486
|
+
GH_APP_SLUG: exchangeResult.slug,
|
|
487
|
+
});
|
|
449
488
|
|
|
450
489
|
return {
|
|
451
490
|
outcome: "success",
|
|
@@ -473,9 +512,8 @@ export class GithubProvisioningService {
|
|
|
473
512
|
// session: validate the ID and acknowledge without consulting the cookie
|
|
474
513
|
// or writing anything. First installs still require the state cookie.
|
|
475
514
|
if (expectedAgentId !== undefined) {
|
|
476
|
-
const existing = (
|
|
477
|
-
|
|
478
|
-
)?.env.GH_APP_INSTALLATION_ID;
|
|
515
|
+
const existing = (await this.readStoredApp(expectedAgentId))
|
|
516
|
+
.installationId;
|
|
479
517
|
if (existing) {
|
|
480
518
|
if (!isNumericId(installationId)) {
|
|
481
519
|
return {
|
|
@@ -516,11 +554,11 @@ export class GithubProvisioningService {
|
|
|
516
554
|
// A stored installation ID is left alone: additional-org installs of a
|
|
517
555
|
// public App also land here, and overwriting would repoint the agent at
|
|
518
556
|
// the newly added org. Only a first install stores its ID.
|
|
519
|
-
const
|
|
520
|
-
|
|
521
|
-
|
|
557
|
+
const alreadyStored = Boolean(
|
|
558
|
+
(await this.readStoredApp(state.agentId)).installationId,
|
|
559
|
+
);
|
|
522
560
|
if (!alreadyStored) {
|
|
523
|
-
await this.
|
|
561
|
+
await this.storeApp(state.agentId, {
|
|
524
562
|
GH_APP_INSTALLATION_ID: installationId,
|
|
525
563
|
});
|
|
526
564
|
}
|
|
@@ -549,8 +587,7 @@ export class GithubProvisioningService {
|
|
|
549
587
|
async getAddOrgUrl(
|
|
550
588
|
agentId: string,
|
|
551
589
|
): Promise<{ ok: true; url: string } | { ok: false; error: string }> {
|
|
552
|
-
const slug =
|
|
553
|
-
.GH_APP_SLUG;
|
|
590
|
+
const { slug } = await this.readStoredApp(agentId);
|
|
554
591
|
if (!slug) {
|
|
555
592
|
return {
|
|
556
593
|
ok: false,
|
|
@@ -12,23 +12,56 @@ import { GithubProvisioningService } from "./github-provisioning-service.ts";
|
|
|
12
12
|
const SESSION_SECRET = "test-admin-session-secret-32-bytes!";
|
|
13
13
|
const AGENT_ID = "agent-test-123";
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
/**
|
|
16
|
+
* Table-based fixture (multi-github-apps 1.5): the stored installation id
|
|
17
|
+
* lives on the agent's primary AgentGitHubApp row, and App writes go to the
|
|
18
|
+
* table as well as the env mirror. `envOnly` keeps one env-fallback fixture
|
|
19
|
+
* for an agent the backfill has not reached.
|
|
20
|
+
*/
|
|
21
|
+
function makeService(
|
|
22
|
+
storedInstallationId?: string,
|
|
23
|
+
opts: { envOnly?: boolean; slug?: string } = {},
|
|
24
|
+
) {
|
|
16
25
|
const patchCalls: Array<Record<string, string>> = [];
|
|
26
|
+
const tableCalls: Array<Record<string, string>> = [];
|
|
17
27
|
const reconcileCalls: string[] = [];
|
|
28
|
+
const primary =
|
|
29
|
+
storedInstallationId || opts.slug
|
|
30
|
+
? {
|
|
31
|
+
id: "row-1",
|
|
32
|
+
agentId: AGENT_ID,
|
|
33
|
+
appId: "42",
|
|
34
|
+
slug: opts.slug ?? null,
|
|
35
|
+
clientId: null,
|
|
36
|
+
installationId: storedInstallationId ?? null,
|
|
37
|
+
priority: 0,
|
|
38
|
+
}
|
|
39
|
+
: null;
|
|
18
40
|
const service = new GithubProvisioningService({
|
|
19
41
|
githubAppClient: {} as never,
|
|
20
42
|
agentService: {} as never,
|
|
21
43
|
agentEnvService: {
|
|
22
44
|
getByAgentId: async () => ({
|
|
23
|
-
env:
|
|
24
|
-
|
|
25
|
-
|
|
45
|
+
env:
|
|
46
|
+
opts.envOnly && storedInstallationId
|
|
47
|
+
? { GH_APP_INSTALLATION_ID: storedInstallationId }
|
|
48
|
+
: {},
|
|
26
49
|
secretKeys: [],
|
|
27
50
|
}),
|
|
28
51
|
patch: async (_id: string, env: Record<string, string>) => {
|
|
29
52
|
patchCalls.push(env);
|
|
30
53
|
},
|
|
31
54
|
} as never,
|
|
55
|
+
agentGitHubAppService: {
|
|
56
|
+
getPrimary: async () => (opts.envOnly ? null : primary),
|
|
57
|
+
applyLegacyEnvPatch: async (
|
|
58
|
+
_id: string,
|
|
59
|
+
patch: Record<string, string>,
|
|
60
|
+
) => {
|
|
61
|
+
tableCalls.push(patch);
|
|
62
|
+
return null;
|
|
63
|
+
},
|
|
64
|
+
} as never,
|
|
32
65
|
agentCronJobService: {
|
|
33
66
|
reconcileSystemCrons: async (id: string) => {
|
|
34
67
|
reconcileCalls.push(id);
|
|
@@ -39,7 +72,7 @@ function makeService(storedInstallationId?: string) {
|
|
|
39
72
|
appBaseUrl: "https://example.com",
|
|
40
73
|
secretEnvVars: new Set(),
|
|
41
74
|
});
|
|
42
|
-
return { service, patchCalls, reconcileCalls };
|
|
75
|
+
return { service, patchCalls, tableCalls, reconcileCalls };
|
|
43
76
|
}
|
|
44
77
|
|
|
45
78
|
async function stateCookie(agentId = AGENT_ID): Promise<string> {
|
|
@@ -96,7 +129,7 @@ describe("GithubProvisioningService.completeInstalled", () => {
|
|
|
96
129
|
});
|
|
97
130
|
|
|
98
131
|
it("no stored ID + valid cookie → stores the installation ID and reconciles crons", async () => {
|
|
99
|
-
const { service, patchCalls, reconcileCalls } = makeService();
|
|
132
|
+
const { service, patchCalls, tableCalls, reconcileCalls } = makeService();
|
|
100
133
|
const result = await service.completeInstalled(
|
|
101
134
|
await stateCookie(),
|
|
102
135
|
"778899",
|
|
@@ -108,9 +141,28 @@ describe("GithubProvisioningService.completeInstalled", () => {
|
|
|
108
141
|
alreadyStored: false,
|
|
109
142
|
});
|
|
110
143
|
expect(patchCalls).toEqual([{ GH_APP_INSTALLATION_ID: "778899" }]);
|
|
144
|
+
expect(tableCalls).toEqual([{ GH_APP_INSTALLATION_ID: "778899" }]);
|
|
111
145
|
expect(reconcileCalls).toEqual([AGENT_ID]);
|
|
112
146
|
});
|
|
113
147
|
|
|
148
|
+
it("env-only agent (no table row): the stored env installation ID is still honoured", async () => {
|
|
149
|
+
const { service, patchCalls, tableCalls } = makeService("111", {
|
|
150
|
+
envOnly: true,
|
|
151
|
+
});
|
|
152
|
+
const result = await service.completeInstalled(
|
|
153
|
+
undefined,
|
|
154
|
+
"778899",
|
|
155
|
+
AGENT_ID,
|
|
156
|
+
);
|
|
157
|
+
expect(result).toEqual({
|
|
158
|
+
outcome: "success",
|
|
159
|
+
agentId: AGENT_ID,
|
|
160
|
+
alreadyStored: true,
|
|
161
|
+
});
|
|
162
|
+
expect(patchCalls).toHaveLength(0);
|
|
163
|
+
expect(tableCalls).toHaveLength(0);
|
|
164
|
+
});
|
|
165
|
+
|
|
114
166
|
it("no stored ID + cookie for a different agent → agent_mismatch", async () => {
|
|
115
167
|
const { service } = makeService();
|
|
116
168
|
const result = await service.completeInstalled(
|
|
@@ -124,6 +176,7 @@ describe("GithubProvisioningService.completeInstalled", () => {
|
|
|
124
176
|
|
|
125
177
|
describe("GithubProvisioningService.completeConnect (SWI-16.1)", () => {
|
|
126
178
|
it("success result exposes githubOrg from the state cookie", async () => {
|
|
179
|
+
const tableWrites: unknown[] = [];
|
|
127
180
|
const service = new GithubProvisioningService({
|
|
128
181
|
githubAppClient: {
|
|
129
182
|
exchangeManifestCode: async () => ({
|
|
@@ -136,6 +189,13 @@ describe("GithubProvisioningService.completeConnect (SWI-16.1)", () => {
|
|
|
136
189
|
} as never,
|
|
137
190
|
agentService: {} as never,
|
|
138
191
|
agentEnvService: { patch: async () => {} } as never,
|
|
192
|
+
agentGitHubAppService: {
|
|
193
|
+
getPrimary: async () => null,
|
|
194
|
+
applyLegacyEnvPatch: async (_id: string, patch: unknown) => {
|
|
195
|
+
tableWrites.push(patch);
|
|
196
|
+
return null;
|
|
197
|
+
},
|
|
198
|
+
} as never,
|
|
139
199
|
agentCronJobService: {} as never,
|
|
140
200
|
sessionSecret: SESSION_SECRET,
|
|
141
201
|
appBaseUrl: "https://example.com",
|
|
@@ -152,5 +212,53 @@ describe("GithubProvisioningService.completeConnect (SWI-16.1)", () => {
|
|
|
152
212
|
installUrl: "https://github.com/apps/my-app/installations/new",
|
|
153
213
|
githubOrg: "my-org",
|
|
154
214
|
});
|
|
215
|
+
expect(tableWrites).toEqual([
|
|
216
|
+
{
|
|
217
|
+
GH_APP_ID: "1",
|
|
218
|
+
GH_APP_PRIVATE_KEY: "pem",
|
|
219
|
+
GH_APP_CLIENT_ID: "cid",
|
|
220
|
+
GH_APP_CLIENT_SECRET: "secret",
|
|
221
|
+
GH_APP_SLUG: "my-app",
|
|
222
|
+
},
|
|
223
|
+
]);
|
|
224
|
+
});
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
describe("GithubProvisioningService App writes (multi-github-apps 1.5)", () => {
|
|
228
|
+
it("manual paste writes the table and mirrors env with the same fields", async () => {
|
|
229
|
+
const { service, patchCalls, tableCalls } = makeService();
|
|
230
|
+
const svc = service as unknown as {
|
|
231
|
+
deps: { agentService: { getDetail: () => Promise<unknown> } };
|
|
232
|
+
};
|
|
233
|
+
svc.deps.agentService.getDetail = async () => ({ id: AGENT_ID });
|
|
234
|
+
const pem =
|
|
235
|
+
"-----BEGIN RSA PRIVATE KEY-----\nabc\n-----END RSA PRIVATE KEY-----";
|
|
236
|
+
const result = await service.startAppManualConnect(AGENT_ID, {
|
|
237
|
+
ghAppId: "42",
|
|
238
|
+
ghAppInstallationId: "7",
|
|
239
|
+
ghAppPrivateKey: pem,
|
|
240
|
+
});
|
|
241
|
+
expect(result.ok).toBe(true);
|
|
242
|
+
const expected = {
|
|
243
|
+
GH_APP_ID: "42",
|
|
244
|
+
GH_APP_INSTALLATION_ID: "7",
|
|
245
|
+
GH_APP_PRIVATE_KEY: pem,
|
|
246
|
+
};
|
|
247
|
+
expect(tableCalls).toEqual([expected]);
|
|
248
|
+
expect(patchCalls).toEqual([expected]);
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
it("getAddOrgUrl reads the slug from the table primary", async () => {
|
|
252
|
+
const { service } = makeService(undefined, { slug: "my-app" });
|
|
253
|
+
expect(await service.getAddOrgUrl(AGENT_ID)).toEqual({
|
|
254
|
+
ok: true,
|
|
255
|
+
url: "https://github.com/apps/my-app/installations/new",
|
|
256
|
+
});
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it("getAddOrgUrl without a slug reports the missing slug", async () => {
|
|
260
|
+
const { service } = makeService();
|
|
261
|
+
const result = await service.getAddOrgUrl(AGENT_ID);
|
|
262
|
+
expect(result.ok).toBe(false);
|
|
155
263
|
});
|
|
156
264
|
});
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { describe, expect, it } from "bun:test";
|
|
2
|
+
import { GithubSyncService } from "./github-sync.ts";
|
|
3
|
+
import type { InstallationAgentRecord } from "./installation-ports.ts";
|
|
4
|
+
import type { InstallationCachePort } from "./installation-repos-service.ts";
|
|
5
|
+
|
|
6
|
+
// Sync over the cached path with two Apps: each org's snapshot comes from the
|
|
7
|
+
// App routed to it, and apply goes through that App's installation.
|
|
8
|
+
|
|
9
|
+
const NOW = new Date("2026-12-01T00:00:00.000Z");
|
|
10
|
+
const ORGS: Record<
|
|
11
|
+
string,
|
|
12
|
+
{ repos: string[]; pushable: string[]; app: number }
|
|
13
|
+
> = {
|
|
14
|
+
alpha: { repos: ["a", "b"], pushable: ["a"], app: 1 },
|
|
15
|
+
beta: { repos: ["x", "y"], pushable: ["x"], app: 2 },
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const snapshot = (org: string, selection: "all" | "selected") => ({
|
|
19
|
+
org,
|
|
20
|
+
completedAt: NOW,
|
|
21
|
+
totalCount: ORGS[org]?.repos.length ?? 0,
|
|
22
|
+
lastError: null,
|
|
23
|
+
rateLimitResetAt: null,
|
|
24
|
+
repositorySelection: selection,
|
|
25
|
+
repos: (ORGS[org]?.repos ?? []).map((r) => ({ fullName: `${org}/${r}` })),
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
function setup(selections: Record<number, "all" | "selected"> = {}) {
|
|
29
|
+
const calls: unknown[][] = [];
|
|
30
|
+
const cache: InstallationCachePort = {
|
|
31
|
+
// Every App records a snapshot for every org it was crawled on; only the
|
|
32
|
+
// installed one carries repos.
|
|
33
|
+
listAgentSnapshots: async (_acct, appRef) =>
|
|
34
|
+
Object.entries(ORGS).map(([org, o]) =>
|
|
35
|
+
o.app === Number(appRef)
|
|
36
|
+
? snapshot(org, selections[o.app] ?? "selected")
|
|
37
|
+
: {
|
|
38
|
+
...snapshot(org, "selected"),
|
|
39
|
+
repositorySelection: null,
|
|
40
|
+
repos: [],
|
|
41
|
+
totalCount: 0,
|
|
42
|
+
},
|
|
43
|
+
),
|
|
44
|
+
getAccessRows: async ({ org, repos }) =>
|
|
45
|
+
repos.map((repo) => ({
|
|
46
|
+
repo,
|
|
47
|
+
status: "ok",
|
|
48
|
+
canPush: ORGS[org]?.pushable.includes(repo) ?? false,
|
|
49
|
+
checkedAt: NOW,
|
|
50
|
+
})),
|
|
51
|
+
};
|
|
52
|
+
const agent: InstallationAgentRecord = {
|
|
53
|
+
id: "ag",
|
|
54
|
+
githubUsername: "octo",
|
|
55
|
+
appId: 1,
|
|
56
|
+
apps: [{ appId: 1 }, { appId: 2 }],
|
|
57
|
+
repos: [],
|
|
58
|
+
};
|
|
59
|
+
const svc = new GithubSyncService({
|
|
60
|
+
agents: { get: async () => agent, setRepos: async () => {} },
|
|
61
|
+
access: async () => ({ status: "unconfigured" }),
|
|
62
|
+
cache,
|
|
63
|
+
lookupRepos: async () =>
|
|
64
|
+
Object.entries(ORGS).map(([org, o]) => ({
|
|
65
|
+
org,
|
|
66
|
+
completedAt: NOW,
|
|
67
|
+
repos: o.repos.map((r) => `${org}/${r}`).concat(`${org}/new`),
|
|
68
|
+
})),
|
|
69
|
+
now: () => NOW,
|
|
70
|
+
installation: {
|
|
71
|
+
listInstalled: async (appId) => {
|
|
72
|
+
const orgs = Object.entries(ORGS)
|
|
73
|
+
.filter(([, o]) => o.app === appId)
|
|
74
|
+
.map(([org, o]) => ({
|
|
75
|
+
org,
|
|
76
|
+
repos: o.repos.map((r) => `${org}/${r}`),
|
|
77
|
+
}));
|
|
78
|
+
return orgs.length ? { status: "found", orgs } : { status: "none" };
|
|
79
|
+
},
|
|
80
|
+
addRepos: async (appId, org, repos) => {
|
|
81
|
+
calls.push(["add", appId, org, [...repos]]);
|
|
82
|
+
},
|
|
83
|
+
removeRepos: async (appId, org, repos) => {
|
|
84
|
+
calls.push(["remove", appId, org, [...repos]]);
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
audit: { write: async () => "audit-1" },
|
|
88
|
+
});
|
|
89
|
+
return { svc, calls };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
describe("sync over the cache with two Apps", () => {
|
|
93
|
+
it("plans removals per org from each org's own App snapshot", async () => {
|
|
94
|
+
const r = await setup().svc.preview("ag");
|
|
95
|
+
if (r.status !== "previewed") throw new Error(r.status);
|
|
96
|
+
expect(r.plan.appRemoves).toEqual(["alpha/b", "beta/y"]);
|
|
97
|
+
expect(r.plan.skippedOrgs).toEqual([]);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("applies each org's removals through its own App", async () => {
|
|
101
|
+
const { svc, calls } = setup();
|
|
102
|
+
const r = await svc.apply({
|
|
103
|
+
agentId: "ag",
|
|
104
|
+
caller: "root",
|
|
105
|
+
appAdds: [],
|
|
106
|
+
appRemoves: ["alpha/b", "beta/y"],
|
|
107
|
+
agentRemoves: [],
|
|
108
|
+
});
|
|
109
|
+
expect(r.status).toBe("applied");
|
|
110
|
+
expect(calls).toEqual([
|
|
111
|
+
["remove", 1, "alpha", ["alpha/b"]],
|
|
112
|
+
["remove", 2, "beta", ["beta/y"]],
|
|
113
|
+
]);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("skips only the org whose App is on All repositories", async () => {
|
|
117
|
+
const r = await setup({ 2: "all" }).svc.preview("ag");
|
|
118
|
+
if (r.status !== "previewed") throw new Error(r.status);
|
|
119
|
+
expect(r.plan.appRemoves).toEqual(["alpha/b"]);
|
|
120
|
+
expect(r.plan.skippedOrgs.map((s) => s.org)).toEqual(["beta"]);
|
|
121
|
+
});
|
|
122
|
+
});
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { describe, expect, it } from "bun:test";
|
|
2
|
+
import { GithubSyncService } from "./github-sync.ts";
|
|
3
|
+
import type { InstallationAgentRecord } from "./installation-ports.ts";
|
|
4
|
+
import type { UserRepoAccessResult } from "./user-repo-access-resolver.ts";
|
|
5
|
+
|
|
6
|
+
const orgAccess = (org: string, repos: Record<string, boolean>) => ({
|
|
7
|
+
org,
|
|
8
|
+
status: "ok" as const,
|
|
9
|
+
unresolved: 0,
|
|
10
|
+
repos: Object.entries(repos).map(([n, canPush]) => ({
|
|
11
|
+
fullName: `${org}/${n}`,
|
|
12
|
+
level: canPush ? ("write" as const) : ("read" as const),
|
|
13
|
+
canPush,
|
|
14
|
+
})),
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
const ACCESS: UserRepoAccessResult = {
|
|
18
|
+
username: "octo",
|
|
19
|
+
outcome: "ok",
|
|
20
|
+
userVerified: true,
|
|
21
|
+
orgs: [
|
|
22
|
+
orgAccess("alpha", { a: true, b: false, c: true }),
|
|
23
|
+
orgAccess("beta", { x: true, y: false, z: true }),
|
|
24
|
+
orgAccess("gamma", { q: true }),
|
|
25
|
+
],
|
|
26
|
+
unresolved: 0,
|
|
27
|
+
complete: true,
|
|
28
|
+
checkedAt: "2026-11-11T00:00:00.000Z",
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
type Install = Record<number, { org: string; repos: string[] }[]>;
|
|
32
|
+
|
|
33
|
+
function harness(installs: Install, apps = [{ appId: 1 }, { appId: 2 }]) {
|
|
34
|
+
const calls: unknown[][] = [];
|
|
35
|
+
const agent: InstallationAgentRecord = {
|
|
36
|
+
id: "ag",
|
|
37
|
+
githubUsername: "octo",
|
|
38
|
+
appId: 1,
|
|
39
|
+
apps,
|
|
40
|
+
repos: [],
|
|
41
|
+
};
|
|
42
|
+
const svc = new GithubSyncService({
|
|
43
|
+
agents: { get: async () => agent, setRepos: async () => {} },
|
|
44
|
+
access: async () => ({ status: "ok", access: ACCESS }),
|
|
45
|
+
installation: {
|
|
46
|
+
listInstalled: async (appId) => {
|
|
47
|
+
const orgs = installs[appId];
|
|
48
|
+
return orgs ? { status: "found", orgs } : { status: "none" };
|
|
49
|
+
},
|
|
50
|
+
addRepos: async (appId, org, repos) => {
|
|
51
|
+
calls.push(["add", appId, org, [...repos]]);
|
|
52
|
+
},
|
|
53
|
+
removeRepos: async (appId, org, repos) => {
|
|
54
|
+
calls.push(["remove", appId, org, [...repos]]);
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
audit: { write: async () => "audit-1" },
|
|
58
|
+
});
|
|
59
|
+
return { svc, calls };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const lists = (over: object) => ({
|
|
63
|
+
agentId: "ag",
|
|
64
|
+
caller: "root",
|
|
65
|
+
appAdds: [],
|
|
66
|
+
appRemoves: [],
|
|
67
|
+
agentRemoves: [],
|
|
68
|
+
...over,
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
const MIXED: Install = {
|
|
72
|
+
1: [{ org: "alpha", repos: ["alpha/a", "alpha/b"] }],
|
|
73
|
+
2: [{ org: "beta", repos: ["beta/x", "beta/y"] }],
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
describe("GithubSyncService with several Apps", () => {
|
|
77
|
+
it("previews adds and removes for orgs on different Apps", async () => {
|
|
78
|
+
const r = await harness(MIXED).svc.preview("ag");
|
|
79
|
+
if (r.status !== "previewed") throw new Error(r.status);
|
|
80
|
+
expect(r.plan.appAdds).toEqual(["alpha/c", "beta/z"]);
|
|
81
|
+
expect(r.plan.appRemoves).toEqual(["alpha/b", "beta/y"]);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("routes each org's changes through the App installed on it", async () => {
|
|
85
|
+
const { svc, calls } = harness(MIXED);
|
|
86
|
+
const r = await svc.apply(
|
|
87
|
+
lists({
|
|
88
|
+
appAdds: ["alpha/c", "beta/z"],
|
|
89
|
+
appRemoves: ["alpha/b", "beta/y"],
|
|
90
|
+
}),
|
|
91
|
+
);
|
|
92
|
+
expect(r.status).toBe("applied");
|
|
93
|
+
expect(calls).toEqual([
|
|
94
|
+
["add", 1, "alpha", ["alpha/c"]],
|
|
95
|
+
["add", 2, "beta", ["beta/z"]],
|
|
96
|
+
["remove", 1, "alpha", ["alpha/b"]],
|
|
97
|
+
["remove", 2, "beta", ["beta/y"]],
|
|
98
|
+
]);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("uses the lowest-priority App when two are installed on one org", async () => {
|
|
102
|
+
const { svc, calls } = harness({
|
|
103
|
+
1: [{ org: "alpha", repos: ["alpha/a"] }],
|
|
104
|
+
2: [{ org: "alpha", repos: ["alpha/a", "alpha/b"] }],
|
|
105
|
+
});
|
|
106
|
+
await svc.apply(lists({ appAdds: ["alpha/c"] }));
|
|
107
|
+
expect(calls).toEqual([["add", 1, "alpha", ["alpha/c"]]]);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("reports an org with no installation instead of routing it", async () => {
|
|
111
|
+
const { svc, calls } = harness(MIXED);
|
|
112
|
+
const r = await svc.apply(lists({ appAdds: ["gamma/q"] }));
|
|
113
|
+
expect(r.status).toBe("noop");
|
|
114
|
+
if (r.status !== "noop") return;
|
|
115
|
+
expect(r.plan.notApplied).toEqual([
|
|
116
|
+
expect.objectContaining({ repo: "gamma/q" }),
|
|
117
|
+
]);
|
|
118
|
+
expect(calls).toEqual([]);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("still caps unconfirmed removals across Apps", async () => {
|
|
122
|
+
const many = (org: string) =>
|
|
123
|
+
Array.from({ length: 6 }, (_, i) => `${org}/r${i}`);
|
|
124
|
+
const access: UserRepoAccessResult = {
|
|
125
|
+
...ACCESS,
|
|
126
|
+
orgs: [
|
|
127
|
+
orgAccess(
|
|
128
|
+
"alpha",
|
|
129
|
+
Object.fromEntries(many("alpha").map((r) => [r.slice(6), false])),
|
|
130
|
+
),
|
|
131
|
+
orgAccess(
|
|
132
|
+
"beta",
|
|
133
|
+
Object.fromEntries(many("beta").map((r) => [r.slice(5), false])),
|
|
134
|
+
),
|
|
135
|
+
],
|
|
136
|
+
};
|
|
137
|
+
const { svc } = harness({
|
|
138
|
+
1: [{ org: "alpha", repos: many("alpha") }],
|
|
139
|
+
2: [{ org: "beta", repos: many("beta") }],
|
|
140
|
+
});
|
|
141
|
+
// Reuse the harness service with a different access answer.
|
|
142
|
+
const capped = new GithubSyncService({
|
|
143
|
+
...(
|
|
144
|
+
svc as unknown as {
|
|
145
|
+
deps: ConstructorParameters<typeof GithubSyncService>[0];
|
|
146
|
+
}
|
|
147
|
+
).deps,
|
|
148
|
+
access: async () => ({ status: "ok", access }),
|
|
149
|
+
});
|
|
150
|
+
const input = lists({ appRemoves: [...many("alpha"), ...many("beta")] });
|
|
151
|
+
expect((await capped.apply(input)).status).toBe("removal_cap");
|
|
152
|
+
expect(
|
|
153
|
+
(await capped.apply({ ...input, confirmRemovals: true })).status,
|
|
154
|
+
).toBe("applied");
|
|
155
|
+
});
|
|
156
|
+
});
|