@app-vitals/agent-admin 1.129.0 → 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 +103 -0
- package/src/agents-api.ts +76 -1
- package/src/github-agent-installation-crawler.integration.test.ts +182 -11
- package/src/github-agent-installation-crawler.ts +41 -1
- package/src/github-agent-installation-crawler.unit.test.ts +155 -0
- package/src/github-enterprise-api.ts +14 -1
- package/src/github-installation-wiring.ts +7 -5
- package/src/github-installation.fixtures.ts +2 -0
- 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-repo-refresh-discovery.ts +41 -31
- package/src/github-repo-refresh-discovery.unit.test.ts +86 -0
- 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
- package/src/installation-ports.integration.test.ts +79 -3
- package/src/installation-ports.ts +87 -12
- package/src/installation-ports.unit.test.ts +23 -0
- package/src/installation-repos-service.ts +40 -5
- package/src/installation-repos-service.unit.test.ts +100 -0
|
@@ -4,7 +4,9 @@
|
|
|
4
4
|
*
|
|
5
5
|
* A PageSource over the installer client's single-page installation listing,
|
|
6
6
|
* fed through the same resumable engine (crawlRepos) and completeness check as
|
|
7
|
-
* the lookup crawl, under appKind "agent" and appRef = the
|
|
7
|
+
* the lookup crawl, under appKind "agent" and appRef = the App's id. An agent
|
|
8
|
+
* with several Apps is crawled once per App (crawlAgentInstallations), each
|
|
9
|
+
* into its own snapshot with its own repository selection.
|
|
8
10
|
* Scheduling lives in github-repo-refresher.ts (GLC-1.4).
|
|
9
11
|
*/
|
|
10
12
|
|
|
@@ -20,6 +22,7 @@ import {
|
|
|
20
22
|
import {
|
|
21
23
|
type AppIdentity,
|
|
22
24
|
AppNotInstalledError,
|
|
25
|
+
agentApps,
|
|
23
26
|
type InstallationAgentRecord,
|
|
24
27
|
type InstallationRepoClient,
|
|
25
28
|
matchInstallation,
|
|
@@ -268,3 +271,40 @@ export async function crawlAgentInstallation(
|
|
|
268
271
|
};
|
|
269
272
|
}
|
|
270
273
|
}
|
|
274
|
+
|
|
275
|
+
export interface CrawlAgentInstallationsOptions
|
|
276
|
+
extends Omit<CrawlAgentInstallationOptions, "agent"> {
|
|
277
|
+
agent: Pick<
|
|
278
|
+
InstallationAgentRecord,
|
|
279
|
+
"accountId" | "appId" | "clientId" | "slug" | "apps"
|
|
280
|
+
>;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** One App's crawl result from crawlAgentInstallations. */
|
|
284
|
+
export interface AgentAppCrawlResult {
|
|
285
|
+
appId: number;
|
|
286
|
+
result: AgentInstallationCrawlResult;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Crawl every App of the agent (agentApps, priority order) on one org, one
|
|
291
|
+
* crawlAgentInstallation per App, so each App's installation and repository
|
|
292
|
+
* selection land in its own snapshot (appRef = that App's id). An agent with
|
|
293
|
+
* no App crawls nothing.
|
|
294
|
+
*/
|
|
295
|
+
export async function crawlAgentInstallations(
|
|
296
|
+
opts: CrawlAgentInstallationsOptions,
|
|
297
|
+
): Promise<AgentAppCrawlResult[]> {
|
|
298
|
+
const { agent, ...rest } = opts;
|
|
299
|
+
const out: AgentAppCrawlResult[] = [];
|
|
300
|
+
for (const app of agentApps(agent)) {
|
|
301
|
+
out.push({
|
|
302
|
+
appId: app.appId,
|
|
303
|
+
result: await crawlAgentInstallation({
|
|
304
|
+
...rest,
|
|
305
|
+
agent: { accountId: agent.accountId, ...app },
|
|
306
|
+
}),
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
return out;
|
|
310
|
+
}
|
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
type AgentInstallationClient,
|
|
11
11
|
agentInstallationPageSource,
|
|
12
12
|
crawlAgentInstallation,
|
|
13
|
+
crawlAgentInstallations,
|
|
13
14
|
DEPLOYMENT_WIDE_ACCOUNT_KEY,
|
|
14
15
|
getAccessRowsAcrossKeys,
|
|
15
16
|
lookupAccountKeys,
|
|
@@ -542,3 +543,157 @@ describe("getAccessRowsAcrossKeys", () => {
|
|
|
542
543
|
expect(store.queried).toEqual([DEPLOYMENT_WIDE_ACCOUNT_KEY]);
|
|
543
544
|
});
|
|
544
545
|
});
|
|
546
|
+
|
|
547
|
+
/** A CrawlStore keyed by SnapshotKey, so several apps' snapshots coexist. */
|
|
548
|
+
class KeyedStore implements CrawlStore {
|
|
549
|
+
snaps = new Map<string, CrawlSnapshot>();
|
|
550
|
+
rows = new Map<string, Map<number, Date>>();
|
|
551
|
+
private k = (key: SnapshotKey) =>
|
|
552
|
+
`${key.accountId}|${key.appKind}|${key.appRef}|${key.org}`;
|
|
553
|
+
async getSnapshot(key: SnapshotKey) {
|
|
554
|
+
const s = this.snaps.get(this.k(key));
|
|
555
|
+
return s ? { ...s } : null;
|
|
556
|
+
}
|
|
557
|
+
async upsertSnapshot(key: SnapshotKey, progress: SnapshotProgress = {}) {
|
|
558
|
+
const id = this.k(key);
|
|
559
|
+
const next = {
|
|
560
|
+
...(this.snaps.get(id) ?? {
|
|
561
|
+
id,
|
|
562
|
+
crawlPage: 0,
|
|
563
|
+
crawlStartedAt: null,
|
|
564
|
+
completedAt: null,
|
|
565
|
+
totalCount: null,
|
|
566
|
+
lastError: null,
|
|
567
|
+
attempts: 0,
|
|
568
|
+
nextAttemptAt: null,
|
|
569
|
+
rateLimitResetAt: null,
|
|
570
|
+
lastTickAt: null,
|
|
571
|
+
}),
|
|
572
|
+
} as Record<string, unknown>;
|
|
573
|
+
for (const [f, v] of Object.entries(progress)) {
|
|
574
|
+
if (v !== undefined) next[f] = v;
|
|
575
|
+
}
|
|
576
|
+
this.snaps.set(id, next as unknown as CrawlSnapshot);
|
|
577
|
+
return { ...(next as unknown as CrawlSnapshot) };
|
|
578
|
+
}
|
|
579
|
+
async upsertRepos(id: string, repos: CachedRepoInput[], seenAt: Date) {
|
|
580
|
+
const m = this.rows.get(id) ?? new Map<number, Date>();
|
|
581
|
+
for (const r of repos) m.set(Number(r.githubId), seenAt);
|
|
582
|
+
this.rows.set(id, m);
|
|
583
|
+
return repos.length;
|
|
584
|
+
}
|
|
585
|
+
async countReposSeenSince(id: string, since: Date) {
|
|
586
|
+
return [...(this.rows.get(id)?.values() ?? [])].filter((d) => d >= since)
|
|
587
|
+
.length;
|
|
588
|
+
}
|
|
589
|
+
async sweepGeneration() {
|
|
590
|
+
return 0;
|
|
591
|
+
}
|
|
592
|
+
selection(id: string) {
|
|
593
|
+
return (this.snaps.get(id) as unknown as Record<string, unknown>)
|
|
594
|
+
?.repositorySelection;
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
describe("crawlAgentInstallations (one crawl per app)", () => {
|
|
599
|
+
/** Two apps installed on the org with different selections; app 3 is not. */
|
|
600
|
+
function multiClient() {
|
|
601
|
+
const pagesFor: number[] = [];
|
|
602
|
+
const client: AgentInstallationClient = {
|
|
603
|
+
async listOrgInstallations() {
|
|
604
|
+
return [
|
|
605
|
+
{ id: 501, appId: 777, repositorySelection: "selected" as const },
|
|
606
|
+
{
|
|
607
|
+
id: 601,
|
|
608
|
+
appSlug: "second-app",
|
|
609
|
+
repositorySelection: "all" as const,
|
|
610
|
+
},
|
|
611
|
+
];
|
|
612
|
+
},
|
|
613
|
+
async listInstallationRepositoriesPage(_org, installationId) {
|
|
614
|
+
pagesFor.push(installationId);
|
|
615
|
+
const base = installationId * 10;
|
|
616
|
+
return {
|
|
617
|
+
repos: [1, 2].map((i) => ({
|
|
618
|
+
id: base + i,
|
|
619
|
+
fullName: `acme/r${base + i}`,
|
|
620
|
+
})),
|
|
621
|
+
hasNext: false,
|
|
622
|
+
perPage: 100,
|
|
623
|
+
};
|
|
624
|
+
},
|
|
625
|
+
};
|
|
626
|
+
return { client, pagesFor };
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
it("crawls every app into its own snapshot and records each app's selection", async () => {
|
|
630
|
+
const store = new KeyedStore();
|
|
631
|
+
const { client, pagesFor } = multiClient();
|
|
632
|
+
const results = await crawlAgentInstallations({
|
|
633
|
+
agent: {
|
|
634
|
+
accountId: "acc",
|
|
635
|
+
appId: 777,
|
|
636
|
+
apps: [
|
|
637
|
+
{ appId: 777 },
|
|
638
|
+
{ appId: 888, slug: "second-app" },
|
|
639
|
+
{ appId: 999, slug: "missing-app" },
|
|
640
|
+
],
|
|
641
|
+
},
|
|
642
|
+
org: "acme",
|
|
643
|
+
client,
|
|
644
|
+
store,
|
|
645
|
+
clock,
|
|
646
|
+
});
|
|
647
|
+
expect(results.map((r) => [r.appId, r.result.outcome])).toEqual([
|
|
648
|
+
[777, "complete"],
|
|
649
|
+
[888, "complete"],
|
|
650
|
+
[999, "not_installed"],
|
|
651
|
+
]);
|
|
652
|
+
expect(pagesFor).toEqual([501, 601]);
|
|
653
|
+
expect([...store.snaps.keys()]).toEqual([
|
|
654
|
+
"acc|agent|777|acme",
|
|
655
|
+
"acc|agent|888|acme",
|
|
656
|
+
"acc|agent|999|acme",
|
|
657
|
+
]);
|
|
658
|
+
expect(store.selection("acc|agent|777|acme")).toBe("selected");
|
|
659
|
+
expect(store.selection("acc|agent|888|acme")).toBe("all");
|
|
660
|
+
expect(store.selection("acc|agent|999|acme")).toBeNull();
|
|
661
|
+
expect(store.rows.get("acc|agent|888|acme")?.size).toBe(2);
|
|
662
|
+
});
|
|
663
|
+
|
|
664
|
+
it("a single-app agent crawls exactly as crawlAgentInstallation does", async () => {
|
|
665
|
+
const viaMany = new KeyedStore();
|
|
666
|
+
const viaOne = new KeyedStore();
|
|
667
|
+
const single = { accountId: null, appId: 777 };
|
|
668
|
+
const many = await crawlAgentInstallations({
|
|
669
|
+
agent: single,
|
|
670
|
+
org: "acme",
|
|
671
|
+
client: multiClient().client,
|
|
672
|
+
store: viaMany,
|
|
673
|
+
clock,
|
|
674
|
+
});
|
|
675
|
+
const one = await crawlAgentInstallation({
|
|
676
|
+
agent: single,
|
|
677
|
+
org: "acme",
|
|
678
|
+
client: multiClient().client,
|
|
679
|
+
store: viaOne,
|
|
680
|
+
clock,
|
|
681
|
+
});
|
|
682
|
+
expect(many).toEqual([{ appId: 777, result: one }]);
|
|
683
|
+
expect([...viaMany.snaps.entries()]).toEqual([...viaOne.snaps.entries()]);
|
|
684
|
+
});
|
|
685
|
+
|
|
686
|
+
it("does nothing for an agent with no app", async () => {
|
|
687
|
+
const store = new KeyedStore();
|
|
688
|
+
expect(
|
|
689
|
+
await crawlAgentInstallations({
|
|
690
|
+
agent: { accountId: "acc" },
|
|
691
|
+
org: "acme",
|
|
692
|
+
client: multiClient().client,
|
|
693
|
+
store,
|
|
694
|
+
clock,
|
|
695
|
+
}),
|
|
696
|
+
).toEqual([]);
|
|
697
|
+
expect(store.snaps.size).toBe(0);
|
|
698
|
+
});
|
|
699
|
+
});
|
|
@@ -20,6 +20,10 @@ import { createPrivateKey } from "node:crypto";
|
|
|
20
20
|
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
|
|
21
21
|
import { HTTPException } from "hono/http-exception";
|
|
22
22
|
import type { AgentEnvService } from "./agent-envs.ts";
|
|
23
|
+
import {
|
|
24
|
+
type AgentGitHubAppService,
|
|
25
|
+
overlayGitHubAppEnv,
|
|
26
|
+
} from "./agent-github-apps.ts";
|
|
23
27
|
import type { AgentTokenService } from "./agent-tokens.ts";
|
|
24
28
|
import type { AgentService } from "./agents.ts";
|
|
25
29
|
import {
|
|
@@ -87,6 +91,12 @@ export interface GithubEnterpriseApiDeps {
|
|
|
87
91
|
>;
|
|
88
92
|
agentService: Pick<AgentService, "getSummary">;
|
|
89
93
|
agentEnvService: Pick<AgentEnvService, "getByAgentId">;
|
|
94
|
+
/**
|
|
95
|
+
* multi-github-apps 1.5: the agent's App is read from the AgentGitHubApp
|
|
96
|
+
* table (primary app), falling back to its GH_APP_* env when the agent has no
|
|
97
|
+
* row. Optional so existing wiring stays valid.
|
|
98
|
+
*/
|
|
99
|
+
agentGitHubAppService?: Pick<AgentGitHubAppService, "getPrimary">;
|
|
90
100
|
sessionSecret: string;
|
|
91
101
|
adminApiKeys?: Map<string, AdminApiKey>;
|
|
92
102
|
agentTokenService: Pick<AgentTokenService, "validate">;
|
|
@@ -776,7 +786,10 @@ export function createGithubEnterpriseApp(
|
|
|
776
786
|
if (!(await deps.agentService.getSummary(agentId))) {
|
|
777
787
|
throw new NotFoundError(`agent ${agentId} not found`);
|
|
778
788
|
}
|
|
779
|
-
const
|
|
789
|
+
const primary = await deps.agentGitHubAppService?.getPrimary(agentId);
|
|
790
|
+
const env = primary
|
|
791
|
+
? overlayGitHubAppEnv({}, primary)
|
|
792
|
+
: (await deps.agentEnvService.getByAgentId(agentId))?.env;
|
|
780
793
|
const storedClientId = env?.GH_APP_CLIENT_ID?.trim();
|
|
781
794
|
const clientId =
|
|
782
795
|
storedClientId && CLIENT_ID_RE.test(storedClientId)
|
|
@@ -3,8 +3,9 @@
|
|
|
3
3
|
* Composition of the installation repos list and GitHub Sync services for
|
|
4
4
|
* admin boot, from the stored installer App and an optional host-supplied
|
|
5
5
|
* OpenWorkPort, plus the GitHub cache refresher's target discovery over the
|
|
6
|
-
* stored Apps (GLC-1.4). The agent
|
|
7
|
-
* GH_APP_ID; without an open-work port nothing is
|
|
6
|
+
* stored Apps (GLC-1.4). The agent Apps come from each agent's AgentGitHubApp
|
|
7
|
+
* rows (else its stored GH_APP_ID); without an open-work port nothing is
|
|
8
|
+
* listed or blocked.
|
|
8
9
|
*/
|
|
9
10
|
|
|
10
11
|
import type { PrismaClient } from "../prisma/client/client.ts";
|
|
@@ -12,7 +13,7 @@ import type { AgentEnvService } from "./agent-envs.ts";
|
|
|
12
13
|
import type { Clock } from "./clock.ts";
|
|
13
14
|
import type { GithubAccessPreviewFn } from "./github-access-preview.ts";
|
|
14
15
|
import {
|
|
15
|
-
|
|
16
|
+
crawlAgentInstallations,
|
|
16
17
|
DEPLOYMENT_WIDE_ACCOUNT_KEY,
|
|
17
18
|
lookupKeysForApp,
|
|
18
19
|
} from "./github-agent-installation-crawler.ts";
|
|
@@ -256,9 +257,10 @@ export function createGithubSync(
|
|
|
256
257
|
return undefined;
|
|
257
258
|
}
|
|
258
259
|
},
|
|
259
|
-
// Re-crawl just the changed org so the UI list is
|
|
260
|
+
// Re-crawl just the changed org (every agent App) so the UI list is
|
|
261
|
+
// current after apply.
|
|
260
262
|
refreshAgentSnapshot: async (agent, org) => {
|
|
261
|
-
await
|
|
263
|
+
await crawlAgentInstallations({
|
|
262
264
|
agent,
|
|
263
265
|
org,
|
|
264
266
|
client,
|
|
@@ -91,6 +91,8 @@ export function fakePrisma(repos: string[], cache: FakeCache = {}) {
|
|
|
91
91
|
state.repos = args.data.repos;
|
|
92
92
|
},
|
|
93
93
|
},
|
|
94
|
+
// No AgentGitHubApp rows: the agent's App comes from its stored GH_APP_ID.
|
|
95
|
+
agentGitHubApp: { findMany: async () => [] },
|
|
94
96
|
gitHubRepoSnapshot: {
|
|
95
97
|
findMany: async (args: unknown) => {
|
|
96
98
|
state.snapshotQueries.push(args);
|
|
@@ -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,
|