@app-vitals/agent-admin 1.129.1 → 1.131.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-info-tip.ts +31 -0
- package/src/admin-ui-info-tip.unit.test.ts +59 -0
- package/src/admin-ui-pages.ts +20 -6
- package/src/admin-ui-pages.unit.test.ts +23 -0
- package/src/admin-ui-styles.ts +34 -0
- package/src/admin-ui-styles.unit.test.ts +16 -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
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/github-provisioning-service.integration.test.ts
|
|
3
|
+
* GithubProvisioningService against a real PostgreSQL DB with the real
|
|
4
|
+
* AgentGitHubAppService and AgentEnvService (multi-github-apps 1.5): App
|
|
5
|
+
* writes land in the table and the env mirror, reads prefer the table, and
|
|
6
|
+
* single-app behavior (overwrite, installation id kept) is unchanged.
|
|
7
|
+
*
|
|
8
|
+
* Requires DATABASE_URL_ADMIN_TEST to be set; skips otherwise.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
|
|
12
|
+
import { sign } from "hono/jwt";
|
|
13
|
+
import type { PrismaClient } from "../prisma/client/client.ts";
|
|
14
|
+
import { AgentEnvService } from "./agent-envs.ts";
|
|
15
|
+
import { AgentGitHubAppService } from "./agent-github-apps.ts";
|
|
16
|
+
import { GithubProvisioningService } from "./github-provisioning-service.ts";
|
|
17
|
+
import { createAdminPrismaClient } from "./prisma-client.ts";
|
|
18
|
+
import { makeTokenCrypto } from "./token-crypto.ts";
|
|
19
|
+
|
|
20
|
+
const TEST_DB = process.env.DATABASE_URL_ADMIN_TEST;
|
|
21
|
+
const describeOrSkip = TEST_DB ? describe : describe.skip;
|
|
22
|
+
|
|
23
|
+
const TEST_KEY =
|
|
24
|
+
"0000000000000000000000000000000000000000000000000000000000000001";
|
|
25
|
+
const SESSION_SECRET = "test-admin-session-secret-32-bytes!";
|
|
26
|
+
const PEM =
|
|
27
|
+
"-----BEGIN RSA PRIVATE KEY-----\nMIIfakeKeyProv\n-----END RSA PRIVATE KEY-----";
|
|
28
|
+
const SECRETS = new Set(["GH_APP_PRIVATE_KEY", "GH_APP_CLIENT_SECRET"]);
|
|
29
|
+
|
|
30
|
+
describeOrSkip("GithubProvisioningService (integration)", () => {
|
|
31
|
+
let prisma: PrismaClient;
|
|
32
|
+
let apps: AgentGitHubAppService;
|
|
33
|
+
let envs: AgentEnvService;
|
|
34
|
+
let service: GithubProvisioningService;
|
|
35
|
+
let agentId: string;
|
|
36
|
+
let exchange: { appId: string; slug: string };
|
|
37
|
+
|
|
38
|
+
const stateCookie = () => {
|
|
39
|
+
const now = Math.floor(Date.now() / 1000);
|
|
40
|
+
return sign(
|
|
41
|
+
{ agentId, githubOrg: "my-org", iat: now, exp: now + 300 },
|
|
42
|
+
SESSION_SECRET,
|
|
43
|
+
"HS256",
|
|
44
|
+
);
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
beforeEach(async () => {
|
|
48
|
+
prisma = createAdminPrismaClient(TEST_DB as string);
|
|
49
|
+
await prisma.agentGitHubApp.deleteMany();
|
|
50
|
+
await prisma.agentToken.deleteMany();
|
|
51
|
+
await prisma.agentCronJob.deleteMany();
|
|
52
|
+
await prisma.agentTool.deleteMany();
|
|
53
|
+
await prisma.agentEnv.deleteMany();
|
|
54
|
+
await prisma.agent.deleteMany();
|
|
55
|
+
const crypto = makeTokenCrypto(TEST_KEY);
|
|
56
|
+
apps = new AgentGitHubAppService(prisma, crypto);
|
|
57
|
+
envs = new AgentEnvService(prisma, crypto);
|
|
58
|
+
exchange = { appId: "5001", slug: "example-prov-app" };
|
|
59
|
+
service = new GithubProvisioningService({
|
|
60
|
+
githubAppClient: {
|
|
61
|
+
exchangeManifestCode: async () => ({
|
|
62
|
+
...exchange,
|
|
63
|
+
pem: PEM,
|
|
64
|
+
clientId: "Iv1.provclient",
|
|
65
|
+
clientSecret: "prov-secret",
|
|
66
|
+
}),
|
|
67
|
+
} as never,
|
|
68
|
+
agentService: { getDetail: async (id: string) => ({ id }) } as never,
|
|
69
|
+
agentEnvService: envs,
|
|
70
|
+
agentGitHubAppService: apps,
|
|
71
|
+
agentCronJobService: {
|
|
72
|
+
reconcileSystemCrons: async () => ({
|
|
73
|
+
created: 0,
|
|
74
|
+
updated: 0,
|
|
75
|
+
deleted: 0,
|
|
76
|
+
}),
|
|
77
|
+
} as never,
|
|
78
|
+
sessionSecret: SESSION_SECRET,
|
|
79
|
+
appBaseUrl: "https://example.com",
|
|
80
|
+
secretEnvVars: SECRETS,
|
|
81
|
+
});
|
|
82
|
+
agentId = (await prisma.agent.create({ data: { name: "Prov Agent" } })).id;
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
afterEach(async () => {
|
|
86
|
+
await prisma.$disconnect();
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("manifest flow then install: table row complete, env mirrored, installation id kept on a second install", async () => {
|
|
90
|
+
const connect = await service.completeConnect(
|
|
91
|
+
await stateCookie(),
|
|
92
|
+
"code",
|
|
93
|
+
agentId,
|
|
94
|
+
);
|
|
95
|
+
expect(connect.outcome).toBe("success");
|
|
96
|
+
|
|
97
|
+
let row = await apps.getPrimary(agentId);
|
|
98
|
+
expect(row).toMatchObject({
|
|
99
|
+
appId: "5001",
|
|
100
|
+
slug: "example-prov-app",
|
|
101
|
+
clientId: "Iv1.provclient",
|
|
102
|
+
installationId: null,
|
|
103
|
+
hasPrivateKey: true,
|
|
104
|
+
hasClientSecret: true,
|
|
105
|
+
});
|
|
106
|
+
const creds = await apps.listCredentials(agentId);
|
|
107
|
+
expect(creds[0]?.privateKey).toBe(PEM);
|
|
108
|
+
|
|
109
|
+
const installed = await service.completeInstalled(
|
|
110
|
+
await stateCookie(),
|
|
111
|
+
"90001",
|
|
112
|
+
agentId,
|
|
113
|
+
);
|
|
114
|
+
expect(installed).toMatchObject({
|
|
115
|
+
outcome: "success",
|
|
116
|
+
alreadyStored: false,
|
|
117
|
+
});
|
|
118
|
+
row = await apps.getPrimary(agentId);
|
|
119
|
+
expect(row?.installationId).toBe("90001");
|
|
120
|
+
|
|
121
|
+
// Additional-org install: no cookie, stored id wins, nothing rewritten.
|
|
122
|
+
const again = await service.completeInstalled(undefined, "90002", agentId);
|
|
123
|
+
expect(again).toMatchObject({ outcome: "success", alreadyStored: true });
|
|
124
|
+
expect((await apps.getPrimary(agentId))?.installationId).toBe("90001");
|
|
125
|
+
|
|
126
|
+
// Env mirror (runtime still reads env until 1.3/1.11).
|
|
127
|
+
const env = (await envs.getByAgentId(agentId))?.env ?? {};
|
|
128
|
+
expect(env).toMatchObject({
|
|
129
|
+
GH_APP_ID: "5001",
|
|
130
|
+
GH_APP_SLUG: "example-prov-app",
|
|
131
|
+
GH_APP_CLIENT_ID: "Iv1.provclient",
|
|
132
|
+
GH_APP_INSTALLATION_ID: "90001",
|
|
133
|
+
});
|
|
134
|
+
expect(await prisma.agentGitHubApp.count({ where: { agentId } })).toBe(1);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it("manual paste writes table and env; re-pasting a new app replaces the single app", async () => {
|
|
138
|
+
const first = await service.startAppManualConnect(agentId, {
|
|
139
|
+
ghAppId: "6001",
|
|
140
|
+
ghAppInstallationId: "70001",
|
|
141
|
+
ghAppPrivateKey: PEM,
|
|
142
|
+
});
|
|
143
|
+
expect(first.ok).toBe(true);
|
|
144
|
+
expect(await apps.getPrimary(agentId)).toMatchObject({
|
|
145
|
+
appId: "6001",
|
|
146
|
+
installationId: "70001",
|
|
147
|
+
slug: null,
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
await service.startAppManualConnect(agentId, {
|
|
151
|
+
ghAppId: "6002",
|
|
152
|
+
ghAppInstallationId: "70002",
|
|
153
|
+
ghAppPrivateKey: PEM,
|
|
154
|
+
});
|
|
155
|
+
const rows = await apps.list(agentId);
|
|
156
|
+
expect(rows).toHaveLength(1);
|
|
157
|
+
expect(rows[0]).toMatchObject({ appId: "6002", installationId: "70002" });
|
|
158
|
+
expect((await envs.getByAgentId(agentId))?.env.GH_APP_ID).toBe("6002");
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it("getAddOrgUrl reads the slug from the table, and falls back to env for an unbackfilled agent", async () => {
|
|
162
|
+
await apps.create(agentId, {
|
|
163
|
+
appId: "6003",
|
|
164
|
+
slug: "table-slug",
|
|
165
|
+
privateKey: PEM,
|
|
166
|
+
});
|
|
167
|
+
expect(await service.getAddOrgUrl(agentId)).toEqual({
|
|
168
|
+
ok: true,
|
|
169
|
+
url: "https://github.com/apps/table-slug/installations/new",
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
const other = (await prisma.agent.create({ data: { name: "Env Only" } }))
|
|
173
|
+
.id;
|
|
174
|
+
await envs.patch(other, { GH_APP_ID: "1", GH_APP_SLUG: "env-slug" });
|
|
175
|
+
expect(await service.getAddOrgUrl(other)).toEqual({
|
|
176
|
+
ok: true,
|
|
177
|
+
url: "https://github.com/apps/env-slug/installations/new",
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it("env-only agent: a stored env installation id is honoured (no table row to read)", async () => {
|
|
182
|
+
await envs.patch(agentId, { GH_APP_ID: "1", GH_APP_INSTALLATION_ID: "55" });
|
|
183
|
+
const result = await service.completeInstalled(undefined, "99", agentId);
|
|
184
|
+
expect(result).toMatchObject({ outcome: "success", alreadyStored: true });
|
|
185
|
+
expect((await envs.getByAgentId(agentId))?.env.GH_APP_INSTALLATION_ID).toBe(
|
|
186
|
+
"55",
|
|
187
|
+
);
|
|
188
|
+
});
|
|
189
|
+
});
|
|
@@ -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
|
+
});
|