@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,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
|
+
});
|
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
|
}
|