@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
package/src/github-sync.ts
CHANGED
|
@@ -24,6 +24,11 @@
|
|
|
24
24
|
* switching it in-app fails on large orgs (GitHub HTTP 400, 100 implicit
|
|
25
25
|
* removals max). Sync skips it with ALL_REPOS_INSTALLATION_REASON.
|
|
26
26
|
*
|
|
27
|
+
* An agent can have several Apps. Each org is routed to the App already
|
|
28
|
+
* installed on it (the lowest-priority one when more than one is); adds and
|
|
29
|
+
* removals on that org go through that App's installation. An org with no
|
|
30
|
+
* installation of any of the agent's Apps is reported, never auto-routed.
|
|
31
|
+
*
|
|
27
32
|
* Order: installation adds, installation removals,
|
|
28
33
|
* agent-repo removals, one audit entry, then the agent's open work on the
|
|
29
34
|
* removed repos is blocked through the optional OpenWorkPort (a port failure is
|
|
@@ -46,6 +51,9 @@ import {
|
|
|
46
51
|
type CachedOrgInput,
|
|
47
52
|
} from "./github-sync-cached-access.ts";
|
|
48
53
|
import {
|
|
54
|
+
type AgentAppRef,
|
|
55
|
+
type AppIdentity,
|
|
56
|
+
agentApps,
|
|
49
57
|
type InstallationAgentRecord,
|
|
50
58
|
type InstallationAgentsPort,
|
|
51
59
|
type InstallationAuditPort,
|
|
@@ -416,6 +424,8 @@ export type GithubSyncApplyResult =
|
|
|
416
424
|
interface PlannedSync {
|
|
417
425
|
plan: GithubSyncPlan;
|
|
418
426
|
installationRepos: Record<string, string[]>;
|
|
427
|
+
/** The App routed to each installed org (key: lower-case org login). */
|
|
428
|
+
orgApps: Map<string, AgentAppRef>;
|
|
419
429
|
selections?: Record<string, "all" | "selected" | null>;
|
|
420
430
|
}
|
|
421
431
|
|
|
@@ -521,18 +531,26 @@ export class GithubSyncService {
|
|
|
521
531
|
return { status: "removal_cap", plan, removals };
|
|
522
532
|
}
|
|
523
533
|
|
|
524
|
-
const
|
|
525
|
-
|
|
534
|
+
const route = (org: string): { appId: number; identity: AppIdentity } => {
|
|
535
|
+
const app = planned.orgApps.get(lower(org));
|
|
536
|
+
if (!app) throw new Error(`no agent App is installed on org ${org}`);
|
|
537
|
+
return {
|
|
538
|
+
appId: app.appId,
|
|
539
|
+
identity: identityOf({ ...app, accountId: agent.accountId }),
|
|
540
|
+
};
|
|
541
|
+
};
|
|
526
542
|
const added: string[] = [];
|
|
527
543
|
const removed: string[] = [];
|
|
528
544
|
let configDone = false;
|
|
529
545
|
let error: string | undefined;
|
|
530
546
|
try {
|
|
531
547
|
for (const [org, repos] of groupByOrg(plan.appAdds)) {
|
|
548
|
+
const { appId, identity } = route(org);
|
|
532
549
|
await this.deps.installation.addRepos(appId, org, repos, identity);
|
|
533
550
|
added.push(...repos);
|
|
534
551
|
}
|
|
535
552
|
for (const [org, repos] of groupByOrg(plan.appRemoves)) {
|
|
553
|
+
const { appId, identity } = route(org);
|
|
536
554
|
await this.deps.installation.removeRepos(appId, org, repos, identity);
|
|
537
555
|
removed.push(...repos);
|
|
538
556
|
}
|
|
@@ -647,6 +665,7 @@ export class GithubSyncService {
|
|
|
647
665
|
agent: InstallationAgentRecord,
|
|
648
666
|
username: string,
|
|
649
667
|
installed: { org: string; repos: string[] }[],
|
|
668
|
+
orgApps: ReadonlyMap<string, AgentAppRef>,
|
|
650
669
|
): Promise<{
|
|
651
670
|
access: UserRepoAccessResult;
|
|
652
671
|
archivedRepos: string[];
|
|
@@ -656,16 +675,24 @@ export class GithubSyncService {
|
|
|
656
675
|
const accountId = agent.accountId || DEPLOYMENT_WIDE_ACCOUNT_KEY;
|
|
657
676
|
const now = (this.deps.now ?? (() => new Date()))();
|
|
658
677
|
const maxAgeMs = this.deps.maxAgeMs ?? DEFAULT_SYNC_MAX_AGE_MS;
|
|
659
|
-
|
|
678
|
+
// Each org's snapshot is the one crawled for the App routed to it.
|
|
679
|
+
const agentSnaps: Awaited<
|
|
660
680
|
ReturnType<InstallationCachePort["listAgentSnapshots"]>
|
|
661
681
|
> = [];
|
|
662
682
|
let lookup: CachedLookupOrg[] = [];
|
|
663
683
|
let readFailed = false;
|
|
664
684
|
try {
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
685
|
+
for (const app of agentApps(agent)) {
|
|
686
|
+
const snaps = await cache.listAgentSnapshots(
|
|
687
|
+
accountId,
|
|
688
|
+
String(app.appId),
|
|
689
|
+
);
|
|
690
|
+
agentSnaps.push(
|
|
691
|
+
...snaps.filter(
|
|
692
|
+
(s) => orgApps.get(s.org.toLowerCase())?.appId === app.appId,
|
|
693
|
+
),
|
|
694
|
+
);
|
|
695
|
+
}
|
|
669
696
|
if (this.deps.lookupRepos)
|
|
670
697
|
lookup = await this.deps.lookupRepos(agent.accountId);
|
|
671
698
|
} catch {
|
|
@@ -768,6 +795,7 @@ export class GithubSyncService {
|
|
|
768
795
|
const none = (plan: GithubSyncPlan): PlannedSync => ({
|
|
769
796
|
plan,
|
|
770
797
|
installationRepos: {},
|
|
798
|
+
orgApps: new Map(),
|
|
771
799
|
});
|
|
772
800
|
const username = agent.githubUsername?.trim();
|
|
773
801
|
if (!username) {
|
|
@@ -793,21 +821,37 @@ export class GithubSyncService {
|
|
|
793
821
|
if (skip) return none(skippedPlan(skip));
|
|
794
822
|
liveAccess = preview.access;
|
|
795
823
|
}
|
|
796
|
-
|
|
824
|
+
const apps = agentApps(agent);
|
|
825
|
+
if (apps.length === 0) {
|
|
797
826
|
return none(skippedPlan(MISSING_APP_ID_REASON));
|
|
798
827
|
}
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
828
|
+
// Route each org to the first (lowest-priority) App installed on it.
|
|
829
|
+
const orgApps = new Map<string, AgentAppRef>();
|
|
830
|
+
const installedOrgs = new Map<string, { org: string; repos: string[] }>();
|
|
831
|
+
let anyFound = false;
|
|
832
|
+
for (const app of apps) {
|
|
833
|
+
const found = await this.deps.installation.listInstalled(
|
|
834
|
+
app.appId,
|
|
835
|
+
identityOf({ ...app, accountId: agent.accountId }),
|
|
836
|
+
);
|
|
837
|
+
if (found.status === "none") continue;
|
|
838
|
+
anyFound = true;
|
|
839
|
+
for (const o of found.orgs) {
|
|
840
|
+
const k = lower(o.org);
|
|
841
|
+
if (orgApps.has(k)) continue;
|
|
842
|
+
orgApps.set(k, app);
|
|
843
|
+
installedOrgs.set(k, { org: o.org, repos: o.repos });
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
if (!anyFound) {
|
|
804
847
|
return none(skippedPlan(NO_INSTALLATION_REASON));
|
|
805
848
|
}
|
|
849
|
+
const installed = { orgs: [...installedOrgs.values()] };
|
|
806
850
|
// The live resolver reports no archived flag, so only the cached path
|
|
807
851
|
// leaves archived repos alone.
|
|
808
852
|
const cached = liveAccess
|
|
809
853
|
? undefined
|
|
810
|
-
: await this.cachedAccess(agent, username, installed.orgs);
|
|
854
|
+
: await this.cachedAccess(agent, username, installed.orgs, orgApps);
|
|
811
855
|
// The live path never reads the crawl cache, so every selection is unknown.
|
|
812
856
|
const selections = cached?.selections;
|
|
813
857
|
const installationRepos = Object.fromEntries(
|
|
@@ -835,6 +879,7 @@ export class GithubSyncService {
|
|
|
835
879
|
return {
|
|
836
880
|
plan,
|
|
837
881
|
installationRepos,
|
|
882
|
+
orgApps,
|
|
838
883
|
...(selections ? { selections } : {}),
|
|
839
884
|
};
|
|
840
885
|
}
|
|
@@ -36,6 +36,7 @@ describeOrSkip("installation ports (integration)", () => {
|
|
|
36
36
|
await prisma.agentTool.deleteMany();
|
|
37
37
|
await prisma.agentEnv.deleteMany();
|
|
38
38
|
await prisma.agentMember.deleteMany();
|
|
39
|
+
await prisma.agentGitHubApp.deleteMany();
|
|
39
40
|
await prisma.agent.deleteMany();
|
|
40
41
|
});
|
|
41
42
|
|
|
@@ -43,24 +44,99 @@ describeOrSkip("installation ports (integration)", () => {
|
|
|
43
44
|
await prisma.$disconnect();
|
|
44
45
|
});
|
|
45
46
|
|
|
46
|
-
|
|
47
|
+
const addApp = (
|
|
48
|
+
agentId: string,
|
|
49
|
+
appId: string,
|
|
50
|
+
over: {
|
|
51
|
+
slug?: string | null;
|
|
52
|
+
clientId?: string | null;
|
|
53
|
+
priority?: number;
|
|
54
|
+
createdAt?: Date;
|
|
55
|
+
} = {},
|
|
56
|
+
) =>
|
|
57
|
+
prisma.agentGitHubApp.create({
|
|
58
|
+
data: {
|
|
59
|
+
agentId,
|
|
60
|
+
appId,
|
|
61
|
+
privateKeyEncrypted: "ciphertext",
|
|
62
|
+
slug: over.slug ?? null,
|
|
63
|
+
clientId: over.clientId ?? null,
|
|
64
|
+
priority: over.priority ?? 0,
|
|
65
|
+
...(over.createdAt ? { createdAt: over.createdAt } : {}),
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("falls back to the stored GH_APP_ID as a single app and writes repos", async () => {
|
|
47
70
|
const a = await prisma.agent.create({
|
|
48
71
|
data: { name: "A", repos: ["acme/a"], githubUsername: "dev" },
|
|
49
72
|
});
|
|
50
73
|
const agents = new PrismaInstallationAgents(prisma, env());
|
|
51
|
-
|
|
52
|
-
|
|
74
|
+
const none = await agents.get(a.id);
|
|
75
|
+
expect(none?.appId).toBeUndefined();
|
|
76
|
+
expect(none?.apps).toBeUndefined();
|
|
77
|
+
await env().patch(a.id, { GH_APP_ID: "42", GH_APP_SLUG: "env-app" });
|
|
53
78
|
expect(await agents.get(a.id)).toMatchObject({
|
|
54
79
|
id: a.id,
|
|
55
80
|
repos: ["acme/a"],
|
|
56
81
|
githubUsername: "dev",
|
|
57
82
|
appId: 42,
|
|
83
|
+
slug: "env-app",
|
|
84
|
+
apps: [{ appId: 42, slug: "env-app" }],
|
|
58
85
|
});
|
|
59
86
|
await agents.setRepos(a.id, ["acme/b"]);
|
|
60
87
|
expect((await agents.get(a.id))?.repos).toEqual(["acme/b"]);
|
|
61
88
|
expect(await agents.get("missing")).toBeNull();
|
|
62
89
|
});
|
|
63
90
|
|
|
91
|
+
it("resolves every app from the agent's GitHub apps table in priority order", async () => {
|
|
92
|
+
const a = await prisma.agent.create({ data: { name: "A", repos: [] } });
|
|
93
|
+
// The table is authoritative once it has rows: the env app is ignored.
|
|
94
|
+
await env().patch(a.id, { GH_APP_ID: "999" });
|
|
95
|
+
await addApp(a.id, "300", { priority: 2, slug: "third-app" });
|
|
96
|
+
await addApp(a.id, "100", {
|
|
97
|
+
priority: 0,
|
|
98
|
+
clientId: "Iv1.first",
|
|
99
|
+
slug: "first-app",
|
|
100
|
+
});
|
|
101
|
+
await addApp(a.id, "200", { priority: 1, clientId: "bad client/id" });
|
|
102
|
+
const rec = await new PrismaInstallationAgents(prisma, env()).get(a.id);
|
|
103
|
+
expect(rec?.apps).toEqual([
|
|
104
|
+
{ appId: 100, clientId: "Iv1.first", slug: "first-app" },
|
|
105
|
+
// A malformed client id is dropped, like a malformed env value.
|
|
106
|
+
{ appId: 200 },
|
|
107
|
+
{ appId: 300, slug: "third-app" },
|
|
108
|
+
]);
|
|
109
|
+
// The primary (highest-priority) app keeps the single-app fields.
|
|
110
|
+
expect(rec).toMatchObject({
|
|
111
|
+
appId: 100,
|
|
112
|
+
clientId: "Iv1.first",
|
|
113
|
+
slug: "first-app",
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("an agent with one table app resolves exactly as its GH_APP_ID did", async () => {
|
|
118
|
+
const viaEnv = await prisma.agent.create({
|
|
119
|
+
data: { name: "E", repos: [] },
|
|
120
|
+
});
|
|
121
|
+
await env().patch(viaEnv.id, {
|
|
122
|
+
GH_APP_ID: "42",
|
|
123
|
+
GH_APP_CLIENT_ID: "Iv1.one",
|
|
124
|
+
GH_APP_SLUG: "one-app",
|
|
125
|
+
});
|
|
126
|
+
const viaTable = await prisma.agent.create({
|
|
127
|
+
data: { name: "T", repos: [] },
|
|
128
|
+
});
|
|
129
|
+
await addApp(viaTable.id, "42", { clientId: "Iv1.one", slug: "one-app" });
|
|
130
|
+
const agents = new PrismaInstallationAgents(prisma, env());
|
|
131
|
+
const withoutId = async (id: string) => {
|
|
132
|
+
const rec = await agents.get(id);
|
|
133
|
+
return { ...rec, id: "" };
|
|
134
|
+
};
|
|
135
|
+
const fromEnv = await withoutId(viaEnv.id);
|
|
136
|
+
const fromTable = await withoutId(viaTable.id);
|
|
137
|
+
expect(fromTable).toEqual(fromEnv);
|
|
138
|
+
});
|
|
139
|
+
|
|
64
140
|
it("writes one audit row per entry and returns its id", async () => {
|
|
65
141
|
const a = await prisma.agent.create({ data: { name: "A", repos: [] } });
|
|
66
142
|
const audit = new PrismaInstallationAudit(prisma);
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* admin/src/installation-ports.ts
|
|
3
3
|
* Ports and Prisma/GitHub-backed adapters shared by the installation repos
|
|
4
|
-
* service (IRM-1.2) and GitHub Sync (IRM-3.1): the agent record
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* service (IRM-1.2) and GitHub Sync (IRM-3.1): the agent record with all of
|
|
5
|
+
* its own GitHub Apps (the AgentGitHubApp table, else its legacy GH_APP_ID),
|
|
6
|
+
* installation list/add/remove scoped to one named App, and the audit table
|
|
7
|
+
* (GitHubReconcileAudit) every change is recorded in.
|
|
7
8
|
*/
|
|
8
9
|
|
|
9
10
|
import type { PrismaClient } from "../prisma/client/client.ts";
|
|
10
11
|
import type { AgentEnvService } from "./agent-envs.ts";
|
|
12
|
+
import { sortAgentGitHubApps } from "./agent-github-apps.ts";
|
|
11
13
|
|
|
12
14
|
/** Parses a stored GH_APP_ID; unset or non-numeric -> undefined. */
|
|
13
15
|
function parseAppId(raw: string | undefined): number | undefined {
|
|
@@ -29,6 +31,15 @@ function validEnv(raw: string | undefined, re: RegExp): string | undefined {
|
|
|
29
31
|
/** More removals than this need confirmRemovals. */
|
|
30
32
|
export const MAX_UNCONFIRMED_REMOVALS = 10;
|
|
31
33
|
|
|
34
|
+
/** One of an agent's own GitHub Apps, as the installation code matches it. */
|
|
35
|
+
export interface AgentAppRef {
|
|
36
|
+
appId: number;
|
|
37
|
+
/** Omitted when unset or malformed. */
|
|
38
|
+
clientId?: string;
|
|
39
|
+
/** Omitted when unset or malformed. */
|
|
40
|
+
slug?: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
32
43
|
export interface InstallationAgentRecord {
|
|
33
44
|
id: string;
|
|
34
45
|
/** The agent's account; selects that account's GitHub App pair (null/absent: deployment-wide). */
|
|
@@ -41,6 +52,31 @@ export interface InstallationAgentRecord {
|
|
|
41
52
|
clientId?: string;
|
|
42
53
|
/** The agent App's slug (stored GH_APP_SLUG); undefined when unset or malformed. */
|
|
43
54
|
slug?: string;
|
|
55
|
+
/**
|
|
56
|
+
* Every App of the agent in priority order (lowest priority first). The
|
|
57
|
+
* first is the primary App that appId/clientId/slug describe. Absent when
|
|
58
|
+
* the agent has no App; read through agentApps().
|
|
59
|
+
*/
|
|
60
|
+
apps?: AgentAppRef[];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* The agent's Apps in priority order: `apps` when set, else the single primary
|
|
65
|
+
* App (appId/clientId/slug), else none. Records built without `apps` keep
|
|
66
|
+
* their single-App behaviour.
|
|
67
|
+
*/
|
|
68
|
+
export function agentApps(
|
|
69
|
+
agent: Pick<InstallationAgentRecord, "apps" | "appId" | "clientId" | "slug">,
|
|
70
|
+
): AgentAppRef[] {
|
|
71
|
+
if (agent.apps && agent.apps.length > 0) return agent.apps;
|
|
72
|
+
if (agent.appId === undefined) return [];
|
|
73
|
+
return [
|
|
74
|
+
{
|
|
75
|
+
appId: agent.appId,
|
|
76
|
+
...(agent.clientId === undefined ? {} : { clientId: agent.clientId }),
|
|
77
|
+
...(agent.slug === undefined ? {} : { slug: agent.slug }),
|
|
78
|
+
},
|
|
79
|
+
];
|
|
44
80
|
}
|
|
45
81
|
|
|
46
82
|
/**
|
|
@@ -135,7 +171,28 @@ export class PrismaInstallationAudit implements InstallationAuditPort {
|
|
|
135
171
|
}
|
|
136
172
|
}
|
|
137
173
|
|
|
138
|
-
/**
|
|
174
|
+
/** An AgentAppRef from stored values; null when the app id is unusable. */
|
|
175
|
+
function appRef(
|
|
176
|
+
rawAppId: string | undefined,
|
|
177
|
+
rawClientId: string | null | undefined,
|
|
178
|
+
rawSlug: string | null | undefined,
|
|
179
|
+
): AgentAppRef | null {
|
|
180
|
+
const appId = parseAppId(rawAppId);
|
|
181
|
+
if (appId === undefined) return null;
|
|
182
|
+
const clientId = validEnv(rawClientId ?? undefined, CLIENT_ID_RE);
|
|
183
|
+
const slug = validEnv(rawSlug ?? undefined, SLUG_RE);
|
|
184
|
+
return {
|
|
185
|
+
appId,
|
|
186
|
+
...(clientId === undefined ? {} : { clientId }),
|
|
187
|
+
...(slug === undefined ? {} : { slug }),
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Prisma-backed agent reads/writes. The agent's Apps come from the
|
|
193
|
+
* AgentGitHubApp table, which is authoritative once it has rows; an agent
|
|
194
|
+
* without rows falls back to its legacy GH_APP_* env as one App.
|
|
195
|
+
*/
|
|
139
196
|
export class PrismaInstallationAgents implements InstallationAgentsPort {
|
|
140
197
|
constructor(
|
|
141
198
|
private readonly prisma: PrismaClient,
|
|
@@ -148,19 +205,37 @@ export class PrismaInstallationAgents implements InstallationAgentsPort {
|
|
|
148
205
|
select: { id: true, repos: true, githubUsername: true, accountId: true },
|
|
149
206
|
});
|
|
150
207
|
if (!agent) return null;
|
|
151
|
-
const
|
|
152
|
-
const
|
|
153
|
-
const appId = parseAppId(raw);
|
|
154
|
-
const clientId = validEnv(env?.GH_APP_CLIENT_ID, CLIENT_ID_RE);
|
|
155
|
-
const slug = validEnv(env?.GH_APP_SLUG, SLUG_RE);
|
|
208
|
+
const apps = await this.resolveApps(agentId);
|
|
209
|
+
const primary = apps[0];
|
|
156
210
|
return {
|
|
157
211
|
...agent,
|
|
158
|
-
...(
|
|
159
|
-
...(
|
|
160
|
-
...(slug === undefined ? {} : { slug }),
|
|
212
|
+
...(primary ?? {}),
|
|
213
|
+
...(apps.length > 0 ? { apps } : {}),
|
|
161
214
|
};
|
|
162
215
|
}
|
|
163
216
|
|
|
217
|
+
private async resolveApps(agentId: string): Promise<AgentAppRef[]> {
|
|
218
|
+
const rows = await this.prisma.agentGitHubApp.findMany({
|
|
219
|
+
where: { agentId },
|
|
220
|
+
select: {
|
|
221
|
+
id: true,
|
|
222
|
+
appId: true,
|
|
223
|
+
slug: true,
|
|
224
|
+
clientId: true,
|
|
225
|
+
priority: true,
|
|
226
|
+
createdAt: true,
|
|
227
|
+
},
|
|
228
|
+
});
|
|
229
|
+
if (rows.length > 0) {
|
|
230
|
+
return sortAgentGitHubApps(rows)
|
|
231
|
+
.map((r) => appRef(r.appId, r.clientId, r.slug))
|
|
232
|
+
.filter((a): a is AgentAppRef => a !== null);
|
|
233
|
+
}
|
|
234
|
+
const env = (await this.env.getConfigBundle(agentId))?.env;
|
|
235
|
+
const app = appRef(env?.GH_APP_ID, env?.GH_APP_CLIENT_ID, env?.GH_APP_SLUG);
|
|
236
|
+
return app ? [app] : [];
|
|
237
|
+
}
|
|
238
|
+
|
|
164
239
|
async setRepos(agentId: string, repos: string[]): Promise<void> {
|
|
165
240
|
await this.prisma.agent.update({
|
|
166
241
|
where: { id: agentId },
|
|
@@ -7,6 +7,7 @@ import { describe, expect, it } from "bun:test";
|
|
|
7
7
|
import enterpriseOrgInstallations from "./fixtures/enterprise-org-installations.json";
|
|
8
8
|
import {
|
|
9
9
|
AppNotInstalledError,
|
|
10
|
+
agentApps,
|
|
10
11
|
createInstallationPort,
|
|
11
12
|
matchInstallation,
|
|
12
13
|
} from "./installation-ports.ts";
|
|
@@ -101,3 +102,25 @@ describe("createInstallationPort", () => {
|
|
|
101
102
|
);
|
|
102
103
|
});
|
|
103
104
|
});
|
|
105
|
+
|
|
106
|
+
describe("agentApps", () => {
|
|
107
|
+
it("returns every resolved app in priority order", () => {
|
|
108
|
+
const apps = [
|
|
109
|
+
{ appId: 1, slug: "first-app" },
|
|
110
|
+
{ appId: 2, clientId: "Iv1.second" },
|
|
111
|
+
];
|
|
112
|
+
expect(agentApps({ appId: 1, slug: "first-app", apps })).toEqual(apps);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("falls back to the single primary app when apps is absent or empty", () => {
|
|
116
|
+
expect(
|
|
117
|
+
agentApps({ appId: 42, clientId: "Iv1.aaa", slug: "app-one" }),
|
|
118
|
+
).toEqual([{ appId: 42, clientId: "Iv1.aaa", slug: "app-one" }]);
|
|
119
|
+
expect(agentApps({ appId: 42, apps: [] })).toEqual([{ appId: 42 }]);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("is empty for an agent with no app", () => {
|
|
123
|
+
expect(agentApps({})).toEqual([]);
|
|
124
|
+
expect(agentApps({ apps: [] })).toEqual([]);
|
|
125
|
+
});
|
|
126
|
+
});
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* admin/src/installation-repos-service.ts
|
|
3
|
-
* Read-only listing of the repos on an agent's own GitHub App
|
|
4
|
-
* (IRM-1.2), with the bound user's push hint.
|
|
3
|
+
* Read-only listing of the repos on an agent's own GitHub App installations
|
|
4
|
+
* (IRM-1.2), with the bound user's push hint. An agent with several Apps
|
|
5
|
+
* lists each org once, from the highest-priority App installed there. list() reads only the repo/access
|
|
5
6
|
* cache (GLC-2.2): no GitHub call, sync detail alongside. Changes go through
|
|
6
7
|
* the Sync flow (github-sync.ts, IRM-3.1), which still uses the installer App
|
|
7
8
|
* through the installation port.
|
|
@@ -21,6 +22,7 @@ import {
|
|
|
21
22
|
type AppIdentity,
|
|
22
23
|
type AppInstallationPort,
|
|
23
24
|
AppNotInstalledError,
|
|
25
|
+
agentApps,
|
|
24
26
|
type InstallationAgentsPort,
|
|
25
27
|
MISSING_APP_ID_REASON,
|
|
26
28
|
} from "./installation-ports.ts";
|
|
@@ -245,6 +247,33 @@ function withAccessDetail(
|
|
|
245
247
|
});
|
|
246
248
|
}
|
|
247
249
|
|
|
250
|
+
/** True when the snapshot shows the App installed on its org. */
|
|
251
|
+
const installedOn = (s: InstallationSnapshotRow): boolean =>
|
|
252
|
+
(s.repositorySelection ?? null) !== null || s.repos.length > 0;
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Merges each App's cached snapshots (lists in App priority order) into one
|
|
256
|
+
* snapshot per org: the highest-priority App installed on the org (a known
|
|
257
|
+
* repository selection or any cached repo), else the first App's snapshot. An
|
|
258
|
+
* App not installed on an org still records an empty snapshot for it, which
|
|
259
|
+
* must not hide a lower-priority App's installation. Orgs keep first-seen
|
|
260
|
+
* order; a single list is returned as is.
|
|
261
|
+
*/
|
|
262
|
+
export function mergeAppSnapshots(
|
|
263
|
+
perApp: readonly (readonly InstallationSnapshotRow[])[],
|
|
264
|
+
): InstallationSnapshotRow[] {
|
|
265
|
+
if (perApp.length === 1) return perApp[0] as InstallationSnapshotRow[];
|
|
266
|
+
const byOrg = new Map<string, InstallationSnapshotRow>();
|
|
267
|
+
for (const list of perApp) {
|
|
268
|
+
for (const s of list) {
|
|
269
|
+
const k = orgKey(s.org);
|
|
270
|
+
const held = byOrg.get(k);
|
|
271
|
+
if (!held || (!installedOn(held) && installedOn(s))) byOrg.set(k, s);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
return [...byOrg.values()];
|
|
275
|
+
}
|
|
276
|
+
|
|
248
277
|
const iso = (d: Date | null): string | undefined =>
|
|
249
278
|
d ? d.toISOString() : undefined;
|
|
250
279
|
|
|
@@ -308,9 +337,15 @@ export class InstallationReposService {
|
|
|
308
337
|
}
|
|
309
338
|
const cache = this.deps.cache;
|
|
310
339
|
const accountId = agent.accountId || DEPLOYMENT_WIDE_ACCOUNT_KEY;
|
|
311
|
-
const
|
|
312
|
-
|
|
313
|
-
|
|
340
|
+
const perApp: InstallationSnapshotRow[][] = [];
|
|
341
|
+
if (cache) {
|
|
342
|
+
for (const app of agentApps(agent)) {
|
|
343
|
+
perApp.push(
|
|
344
|
+
await cache.listAgentSnapshots(accountId, String(app.appId)),
|
|
345
|
+
);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
const snapshots = mergeAppSnapshots(perApp);
|
|
314
349
|
const state = this.deps.appsState
|
|
315
350
|
? await this.deps.appsState(agent.accountId)
|
|
316
351
|
: "both";
|
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
type InstallationCachePort,
|
|
11
11
|
InstallationReposService,
|
|
12
12
|
type InstallationSnapshotRow,
|
|
13
|
+
mergeAppSnapshots,
|
|
13
14
|
userPushHints,
|
|
14
15
|
} from "./installation-repos-service.ts";
|
|
15
16
|
import type {
|
|
@@ -337,3 +338,102 @@ describe("InstallationReposService.list debug detail", () => {
|
|
|
337
338
|
expect(r2.attempts).toBeUndefined();
|
|
338
339
|
});
|
|
339
340
|
});
|
|
341
|
+
|
|
342
|
+
describe("mergeAppSnapshots", () => {
|
|
343
|
+
const s = (
|
|
344
|
+
org: string,
|
|
345
|
+
over: Partial<InstallationSnapshotRow> = {},
|
|
346
|
+
): InstallationSnapshotRow => ({
|
|
347
|
+
org,
|
|
348
|
+
completedAt: T0,
|
|
349
|
+
totalCount: 0,
|
|
350
|
+
lastError: null,
|
|
351
|
+
rateLimitResetAt: null,
|
|
352
|
+
repositorySelection: null,
|
|
353
|
+
repos: [],
|
|
354
|
+
...over,
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
it("returns a single app's snapshots unchanged", () => {
|
|
358
|
+
const one = [s("acme"), s("beta", { repositorySelection: "all" })];
|
|
359
|
+
expect(mergeAppSnapshots([one])).toBe(one);
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
it("keeps one snapshot per org: the highest-priority app installed there", () => {
|
|
363
|
+
const first = [
|
|
364
|
+
s("acme", { repositorySelection: "selected" }),
|
|
365
|
+
// Not installed here (empty, no selection): the second app wins beta.
|
|
366
|
+
s("beta"),
|
|
367
|
+
];
|
|
368
|
+
const second = [
|
|
369
|
+
s("acme", {
|
|
370
|
+
repositorySelection: "all",
|
|
371
|
+
repos: [{ fullName: "acme/x" }],
|
|
372
|
+
}),
|
|
373
|
+
s("Beta", {
|
|
374
|
+
repositorySelection: "all",
|
|
375
|
+
repos: [{ fullName: "Beta/y" }],
|
|
376
|
+
}),
|
|
377
|
+
s("gamma"),
|
|
378
|
+
];
|
|
379
|
+
const merged = mergeAppSnapshots([first, second]);
|
|
380
|
+
expect(merged.map((m) => [m.org, m.repositorySelection])).toEqual([
|
|
381
|
+
["acme", "selected"],
|
|
382
|
+
["Beta", "all"],
|
|
383
|
+
["gamma", null],
|
|
384
|
+
]);
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
it("an org no app is installed on keeps the first app's snapshot", () => {
|
|
388
|
+
const a = s("acme", { lastError: "first" });
|
|
389
|
+
const b = s("acme", { lastError: "second" });
|
|
390
|
+
expect(mergeAppSnapshots([[a], [b]])).toEqual([a]);
|
|
391
|
+
});
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
describe("InstallationReposService.list with several apps", () => {
|
|
395
|
+
it("reads every app's snapshots and merges them per org", async () => {
|
|
396
|
+
const calls: string[] = [];
|
|
397
|
+
const byApp: Record<string, InstallationSnapshotRow[]> = {
|
|
398
|
+
"1": [snap({ org: "acme", repositorySelection: "selected" })],
|
|
399
|
+
"2": [
|
|
400
|
+
snap({
|
|
401
|
+
org: "beta",
|
|
402
|
+
repositorySelection: "all",
|
|
403
|
+
repos: [{ fullName: "beta/z" }],
|
|
404
|
+
totalCount: 1,
|
|
405
|
+
}),
|
|
406
|
+
],
|
|
407
|
+
};
|
|
408
|
+
const svc = new InstallationReposService({
|
|
409
|
+
agents: {
|
|
410
|
+
get: async () => ({
|
|
411
|
+
id: "a1",
|
|
412
|
+
repos: [],
|
|
413
|
+
githubUsername: null,
|
|
414
|
+
accountId: "acct-1",
|
|
415
|
+
appId: 1,
|
|
416
|
+
apps: [{ appId: 1 }, { appId: 2, slug: "second-app" }],
|
|
417
|
+
}),
|
|
418
|
+
},
|
|
419
|
+
access: async () => ({ status: "unconfigured" }),
|
|
420
|
+
installation: {} as never,
|
|
421
|
+
cache: {
|
|
422
|
+
listAgentSnapshots: async (a, r) => {
|
|
423
|
+
calls.push(`${a}:${r}`);
|
|
424
|
+
return byApp[r] ?? [];
|
|
425
|
+
},
|
|
426
|
+
getAccessRows: async () => [],
|
|
427
|
+
},
|
|
428
|
+
});
|
|
429
|
+
const r = await svc.list("a1");
|
|
430
|
+
expect(calls).toEqual(["acct-1:1", "acct-1:2"]);
|
|
431
|
+
if (r.status !== "ok") throw new Error(r.status);
|
|
432
|
+
expect(r.appId).toBe(1);
|
|
433
|
+
expect(r.orgs.map((o) => [o.org, o.repos.length])).toEqual([
|
|
434
|
+
["acme", 2],
|
|
435
|
+
["beta", 1],
|
|
436
|
+
]);
|
|
437
|
+
expect(r).toMatchObject({ reposSeen: 3, totalCount: 3, complete: true });
|
|
438
|
+
});
|
|
439
|
+
});
|