@app-vitals/agent-admin 1.35.0 → 1.35.1
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/openapi.platform.json +2 -392
- package/package.json +2 -2
- package/src/admin-boot.ts +1 -12
- package/src/admin-extension.ts +3 -3
- package/src/admin-ui-github-identity.smoke.test.ts +0 -114
- package/src/admin-ui-installation-repos-pages.ts +2 -2
- package/src/admin-ui-installation-repos.smoke.test.ts +0 -1
- package/src/admin-ui.ts +0 -82
- package/src/agents-api.smoke.test.ts +2 -352
- package/src/agents-api.ts +5 -269
- package/src/github-enterprise-isolation.unit.test.ts +4 -4
- package/src/{github-reconcile-wiring.ts → github-installation-wiring.ts} +18 -38
- package/src/github-installation-wiring.unit.test.ts +111 -0
- package/src/github-installation.fixtures.ts +76 -0
- package/src/installation-ports.integration.test.ts +89 -0
- package/src/installation-ports.ts +207 -0
- package/src/installation-ports.unit.test.ts +51 -0
- package/src/installation-repos-service.ts +12 -12
- package/src/installation-repos-service.unit.test.ts +3 -3
- package/src/lost-access-removal.ts +9 -9
- package/src/lost-access-removal.unit.test.ts +7 -7
- package/src/reconcile-open-work.ts +5 -7
- package/src/repo-access-rules.ts +58 -0
- package/src/repo-access-rules.unit.test.ts +98 -0
- package/src/admin-ui-github-reconcile-pages.ts +0 -208
- package/src/admin-ui-github-reconcile-pages.unit.test.ts +0 -226
- package/src/github-reconcile-routes.integration.test.ts +0 -206
- package/src/github-reconcile-wiring.unit.test.ts +0 -170
- package/src/reconcile-apply-service.integration.test.ts +0 -271
- package/src/reconcile-apply-service.ts +0 -439
- package/src/reconcile-apply-service.unit.test.ts +0 -279
- package/src/reconcile-planner.ts +0 -264
- package/src/reconcile-planner.unit.test.ts +0 -274
|
@@ -1,439 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* admin/src/reconcile-apply-service.ts
|
|
3
|
-
* ReconcileApplyService (GHE-3.3): the applying half of the GitHub repo
|
|
4
|
-
* reconcile. Re-plans server-side with planReconcile and applies only when the
|
|
5
|
-
* fresh plan still matches the hash the admin reviewed.
|
|
6
|
-
*
|
|
7
|
-
* Order of checks: skipped plan (nothing changes, no audit) -> empty plan
|
|
8
|
-
* (idempotent no-op) -> stale hash -> removal cap -> apply. Apply runs
|
|
9
|
-
* installation changes first (adds, then removes), then the agent config, then
|
|
10
|
-
* writes the audit entry. Open work on removed repos is blocked last, through
|
|
11
|
-
* the optional OpenWorkPort; a port failure is a warning, never a failed apply.
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
import type { PrismaClient } from "../prisma/client/client.ts";
|
|
15
|
-
import type { AgentEnvService } from "./agent-envs.ts";
|
|
16
|
-
import type { GithubAccessPreviewFn } from "./github-access-preview.ts";
|
|
17
|
-
import {
|
|
18
|
-
type OpenWorkItem,
|
|
19
|
-
type OpenWorkPort,
|
|
20
|
-
removedRepoBlockReason,
|
|
21
|
-
} from "./reconcile-open-work.ts";
|
|
22
|
-
import {
|
|
23
|
-
planReconcile,
|
|
24
|
-
type ReconcileDiff,
|
|
25
|
-
type ReconcilePlan,
|
|
26
|
-
repoOrg,
|
|
27
|
-
} from "./reconcile-planner.ts";
|
|
28
|
-
|
|
29
|
-
/** Parses a stored GH_APP_ID; unset or non-numeric -> undefined. */
|
|
30
|
-
function parseAppId(raw: string | undefined): number | undefined {
|
|
31
|
-
const n = Number(raw?.trim());
|
|
32
|
-
return Number.isInteger(n) && n > 0 ? n : undefined;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/** More removals than this need confirmRemovals. */
|
|
36
|
-
export const MAX_UNCONFIRMED_REMOVALS = 10;
|
|
37
|
-
|
|
38
|
-
export interface ReconcileAgentRecord {
|
|
39
|
-
id: string;
|
|
40
|
-
repos: string[];
|
|
41
|
-
githubUsername: string | null;
|
|
42
|
-
/** The agent's own GitHub App id (its stored GH_APP_ID); undefined when unset or not numeric. */
|
|
43
|
-
appId?: number;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export interface ReconcileAgentsPort {
|
|
47
|
-
get(agentId: string): Promise<ReconcileAgentRecord | null>;
|
|
48
|
-
setRepos(agentId: string, repos: string[]): Promise<void>;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Repo access on an agent App's installations, per org login. Each agent has
|
|
53
|
-
* its own App, so every call names the App (`appId`) whose installation is used.
|
|
54
|
-
*/
|
|
55
|
-
export interface ReconcileInstallationPort {
|
|
56
|
-
/** Repos the agent App covers in each org; only the named orgs are needed. */
|
|
57
|
-
listRepos(
|
|
58
|
-
appId: number,
|
|
59
|
-
orgs: readonly string[],
|
|
60
|
-
): Promise<Record<string, string[]>>;
|
|
61
|
-
/** Grant the installation access to repos (owner/repo, all in `org`). */
|
|
62
|
-
addRepos(appId: number, org: string, repos: readonly string[]): Promise<void>;
|
|
63
|
-
removeRepos(
|
|
64
|
-
appId: number,
|
|
65
|
-
org: string,
|
|
66
|
-
repos: readonly string[],
|
|
67
|
-
): Promise<void>;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export interface ReconcileAuditEntry {
|
|
71
|
-
agentId: string;
|
|
72
|
-
caller: string;
|
|
73
|
-
planHash: string;
|
|
74
|
-
diff: ReconcileDiff;
|
|
75
|
-
result: ReconcileAuditResult;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export interface ReconcileAuditResult {
|
|
79
|
-
status: "applied" | "failed";
|
|
80
|
-
error?: string;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export interface ReconcileAuditPort {
|
|
84
|
-
write(entry: ReconcileAuditEntry): Promise<string>;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
export interface ReconcileApplyDeps {
|
|
88
|
-
agents: ReconcileAgentsPort;
|
|
89
|
-
/** Resolves the bound user's access; "unconfigured" skips the plan. */
|
|
90
|
-
access: GithubAccessPreviewFn;
|
|
91
|
-
installation: ReconcileInstallationPort;
|
|
92
|
-
audit: ReconcileAuditPort;
|
|
93
|
-
/** Optional: with none, nothing is listed or blocked. */
|
|
94
|
-
openWork?: OpenWorkPort;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
export interface ReconcileApplyInput {
|
|
98
|
-
agentId: string;
|
|
99
|
-
/** Who asked for the apply (audit trail). */
|
|
100
|
-
caller: string;
|
|
101
|
-
/** Hash of the plan the admin reviewed. */
|
|
102
|
-
planHash: string;
|
|
103
|
-
namedAdds?: readonly string[];
|
|
104
|
-
confirmRemovals?: boolean;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
export type ReconcileApplyResult =
|
|
108
|
-
| { status: "agent_not_found" }
|
|
109
|
-
| { status: "skipped"; plan: ReconcilePlan }
|
|
110
|
-
| { status: "noop"; plan: ReconcilePlan }
|
|
111
|
-
| { status: "stale_plan"; plan: ReconcilePlan }
|
|
112
|
-
| { status: "removal_cap"; plan: ReconcilePlan; removals: number }
|
|
113
|
-
| {
|
|
114
|
-
status: "applied";
|
|
115
|
-
plan: ReconcilePlan;
|
|
116
|
-
auditId: string;
|
|
117
|
-
warnings: string[];
|
|
118
|
-
}
|
|
119
|
-
| {
|
|
120
|
-
status: "failed";
|
|
121
|
-
plan: ReconcilePlan;
|
|
122
|
-
auditId: string;
|
|
123
|
-
error: string;
|
|
124
|
-
};
|
|
125
|
-
|
|
126
|
-
/** Skip reason when the agent has no usable GH_APP_ID in its stored env. */
|
|
127
|
-
export const MISSING_APP_ID_REASON =
|
|
128
|
-
"agent has no GH_APP_ID in its stored env (its own GitHub App is not set up)";
|
|
129
|
-
|
|
130
|
-
const orgsOf = (repos: readonly string[]): string[] =>
|
|
131
|
-
[...new Set(repos.map(repoOrg))].sort();
|
|
132
|
-
|
|
133
|
-
function groupByOrg(repos: readonly string[]): Map<string, string[]> {
|
|
134
|
-
const byOrg = new Map<string, string[]>();
|
|
135
|
-
for (const r of repos) {
|
|
136
|
-
const org = repoOrg(r);
|
|
137
|
-
byOrg.set(org, [...(byOrg.get(org) ?? []), r]);
|
|
138
|
-
}
|
|
139
|
-
return byOrg;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
export class ReconcileApplyService {
|
|
143
|
-
constructor(private readonly deps: ReconcileApplyDeps) {}
|
|
144
|
-
|
|
145
|
-
/** Plan only: no writes. Throws when the agent does not exist. */
|
|
146
|
-
async plan(
|
|
147
|
-
agentId: string,
|
|
148
|
-
namedAdds?: readonly string[],
|
|
149
|
-
): Promise<ReconcilePlan> {
|
|
150
|
-
const agent = await this.deps.agents.get(agentId);
|
|
151
|
-
if (!agent) throw new Error(`agent ${agentId} not found`);
|
|
152
|
-
return this.planFor(agent, namedAdds);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Plan plus the open work the removals would affect (GHE-3.4). Read-only.
|
|
157
|
-
* Without a port the list is empty; a port failure adds a plan warning
|
|
158
|
-
* rather than failing the preview.
|
|
159
|
-
*/
|
|
160
|
-
async planWithOpenWork(
|
|
161
|
-
agentId: string,
|
|
162
|
-
namedAdds?: readonly string[],
|
|
163
|
-
): Promise<{ plan: ReconcilePlan; openWork: OpenWorkItem[] }> {
|
|
164
|
-
const plan = await this.plan(agentId, namedAdds);
|
|
165
|
-
const port = this.deps.openWork;
|
|
166
|
-
const removed = plan.diff.configRemoves;
|
|
167
|
-
if (!port || plan.skipped || removed.length === 0) {
|
|
168
|
-
return { plan, openWork: [] };
|
|
169
|
-
}
|
|
170
|
-
try {
|
|
171
|
-
return { plan, openWork: await port.listOpenWork(agentId, removed) };
|
|
172
|
-
} catch (err) {
|
|
173
|
-
const msg = err instanceof Error ? err.message : String(err);
|
|
174
|
-
return {
|
|
175
|
-
plan: {
|
|
176
|
-
...plan,
|
|
177
|
-
warnings: [...plan.warnings, `open work could not be listed: ${msg}`],
|
|
178
|
-
},
|
|
179
|
-
openWork: [],
|
|
180
|
-
};
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
private async planFor(
|
|
185
|
-
agent: ReconcileAgentRecord,
|
|
186
|
-
namedAdds?: readonly string[],
|
|
187
|
-
): Promise<ReconcilePlan> {
|
|
188
|
-
const username = agent.githubUsername?.trim();
|
|
189
|
-
const preview = username ? await this.deps.access(username) : null;
|
|
190
|
-
// Unbound user or unconfigured Apps: a skipped plan, no installation calls.
|
|
191
|
-
if (!preview || preview.status === "unconfigured") {
|
|
192
|
-
const plan = planReconcile({
|
|
193
|
-
username,
|
|
194
|
-
access: null,
|
|
195
|
-
agentRepos: agent.repos,
|
|
196
|
-
installationRepos: {},
|
|
197
|
-
});
|
|
198
|
-
return preview
|
|
199
|
-
? { ...plan, skipReason: "GitHub Apps are not configured" }
|
|
200
|
-
: plan;
|
|
201
|
-
}
|
|
202
|
-
if (agent.appId === undefined) {
|
|
203
|
-
const plan = planReconcile({
|
|
204
|
-
username,
|
|
205
|
-
access: null,
|
|
206
|
-
agentRepos: agent.repos,
|
|
207
|
-
installationRepos: {},
|
|
208
|
-
});
|
|
209
|
-
return { ...plan, skipReason: MISSING_APP_ID_REASON };
|
|
210
|
-
}
|
|
211
|
-
const installationRepos = await this.deps.installation.listRepos(
|
|
212
|
-
agent.appId,
|
|
213
|
-
orgsOf([...agent.repos, ...(namedAdds ?? [])]),
|
|
214
|
-
);
|
|
215
|
-
return planReconcile({
|
|
216
|
-
username,
|
|
217
|
-
access: preview.access,
|
|
218
|
-
agentRepos: agent.repos,
|
|
219
|
-
installationRepos,
|
|
220
|
-
namedAdds,
|
|
221
|
-
});
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
async apply(input: ReconcileApplyInput): Promise<ReconcileApplyResult> {
|
|
225
|
-
const agent = await this.deps.agents.get(input.agentId);
|
|
226
|
-
if (!agent) return { status: "agent_not_found" };
|
|
227
|
-
|
|
228
|
-
const plan = await this.planFor(agent, input.namedAdds);
|
|
229
|
-
if (plan.skipped) return { status: "skipped", plan };
|
|
230
|
-
if (plan.empty) return { status: "noop", plan };
|
|
231
|
-
if (plan.planHash !== input.planHash) return { status: "stale_plan", plan };
|
|
232
|
-
|
|
233
|
-
const removals = plan.diff.configRemoves.length;
|
|
234
|
-
if (removals > MAX_UNCONFIRMED_REMOVALS && !input.confirmRemovals) {
|
|
235
|
-
return { status: "removal_cap", plan, removals };
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
const entry = {
|
|
239
|
-
agentId: input.agentId,
|
|
240
|
-
caller: input.caller,
|
|
241
|
-
planHash: plan.planHash,
|
|
242
|
-
diff: plan.diff,
|
|
243
|
-
};
|
|
244
|
-
try {
|
|
245
|
-
await this.applyInstallation(agent, plan.diff);
|
|
246
|
-
await this.applyConfig(agent, plan.diff);
|
|
247
|
-
} catch (err) {
|
|
248
|
-
const error = err instanceof Error ? err.message : String(err);
|
|
249
|
-
const auditId = await this.deps.audit.write({
|
|
250
|
-
...entry,
|
|
251
|
-
result: { status: "failed", error },
|
|
252
|
-
});
|
|
253
|
-
return { status: "failed", plan, auditId, error };
|
|
254
|
-
}
|
|
255
|
-
const auditId = await this.deps.audit.write({
|
|
256
|
-
...entry,
|
|
257
|
-
result: { status: "applied" },
|
|
258
|
-
});
|
|
259
|
-
const warnings = await this.blockRemoved(
|
|
260
|
-
input.agentId,
|
|
261
|
-
plan.diff.configRemoves,
|
|
262
|
-
);
|
|
263
|
-
return { status: "applied", plan, auditId, warnings };
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
private async applyInstallation(
|
|
267
|
-
agent: ReconcileAgentRecord,
|
|
268
|
-
diff: ReconcileDiff,
|
|
269
|
-
): Promise<void> {
|
|
270
|
-
const { installation } = this.deps;
|
|
271
|
-
const hasChanges =
|
|
272
|
-
diff.installationAdds.length + diff.installationRemoves.length > 0;
|
|
273
|
-
if (agent.appId === undefined) {
|
|
274
|
-
if (hasChanges) throw new Error(MISSING_APP_ID_REASON);
|
|
275
|
-
return;
|
|
276
|
-
}
|
|
277
|
-
for (const [org, repos] of groupByOrg(diff.installationAdds)) {
|
|
278
|
-
await installation.addRepos(agent.appId, org, repos);
|
|
279
|
-
}
|
|
280
|
-
for (const [org, repos] of groupByOrg(diff.installationRemoves)) {
|
|
281
|
-
await installation.removeRepos(agent.appId, org, repos);
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
private async applyConfig(
|
|
286
|
-
agent: ReconcileAgentRecord,
|
|
287
|
-
diff: ReconcileDiff,
|
|
288
|
-
): Promise<void> {
|
|
289
|
-
const removed = new Set(diff.configRemoves);
|
|
290
|
-
const next = agent.repos.filter((r) => !removed.has(r));
|
|
291
|
-
for (const r of diff.configAdds) if (!next.includes(r)) next.push(r);
|
|
292
|
-
await this.deps.agents.setRepos(agent.id, next);
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
private async blockRemoved(
|
|
296
|
-
agentId: string,
|
|
297
|
-
removed: readonly string[],
|
|
298
|
-
): Promise<string[]> {
|
|
299
|
-
const port = this.deps.openWork;
|
|
300
|
-
if (!port || removed.length === 0) return [];
|
|
301
|
-
const warnings: string[] = [];
|
|
302
|
-
for (const repo of removed) {
|
|
303
|
-
try {
|
|
304
|
-
await port.listOpenWork(agentId, [repo]);
|
|
305
|
-
await port.blockOpenWork(agentId, [repo], removedRepoBlockReason(repo));
|
|
306
|
-
} catch (err) {
|
|
307
|
-
const msg = err instanceof Error ? err.message : String(err);
|
|
308
|
-
warnings.push(`open work for ${repo} was not blocked: ${msg}`);
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
return warnings;
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
/** Prisma-backed audit store (GitHubReconcileAudit). */
|
|
316
|
-
export class PrismaReconcileAudit implements ReconcileAuditPort {
|
|
317
|
-
constructor(private readonly prisma: PrismaClient) {}
|
|
318
|
-
|
|
319
|
-
async write(entry: ReconcileAuditEntry): Promise<string> {
|
|
320
|
-
const row = await this.prisma.gitHubReconcileAudit.create({
|
|
321
|
-
data: {
|
|
322
|
-
agentId: entry.agentId,
|
|
323
|
-
caller: entry.caller,
|
|
324
|
-
planHash: entry.planHash,
|
|
325
|
-
diff: entry.diff as unknown as object,
|
|
326
|
-
result: entry.result as unknown as object,
|
|
327
|
-
},
|
|
328
|
-
});
|
|
329
|
-
return row.id;
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
/** Prisma-backed agent reads/writes for the reconcile. */
|
|
334
|
-
export class PrismaReconcileAgents implements ReconcileAgentsPort {
|
|
335
|
-
constructor(
|
|
336
|
-
private readonly prisma: PrismaClient,
|
|
337
|
-
private readonly env: Pick<AgentEnvService, "getConfigBundle">,
|
|
338
|
-
) {}
|
|
339
|
-
|
|
340
|
-
async get(agentId: string): Promise<ReconcileAgentRecord | null> {
|
|
341
|
-
const agent = await this.prisma.agent.findUnique({
|
|
342
|
-
where: { id: agentId },
|
|
343
|
-
select: { id: true, repos: true, githubUsername: true },
|
|
344
|
-
});
|
|
345
|
-
if (!agent) return null;
|
|
346
|
-
const raw = (await this.env.getConfigBundle(agentId))?.env.GH_APP_ID;
|
|
347
|
-
const appId = parseAppId(raw);
|
|
348
|
-
return appId === undefined ? agent : { ...agent, appId };
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
async setRepos(agentId: string, repos: string[]): Promise<void> {
|
|
352
|
-
await this.prisma.agent.update({
|
|
353
|
-
where: { id: agentId },
|
|
354
|
-
data: { repos },
|
|
355
|
-
});
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
/** The slice of GithubEnterpriseClient the installation port needs. */
|
|
360
|
-
export interface InstallationRepoClient {
|
|
361
|
-
listOrgInstallations(org: string): Promise<{ id: number; appId: number }[]>;
|
|
362
|
-
listInstallationRepositories(
|
|
363
|
-
org: string,
|
|
364
|
-
installationId: number,
|
|
365
|
-
): Promise<{ fullName: string }[]>;
|
|
366
|
-
addRepositories(
|
|
367
|
-
org: string,
|
|
368
|
-
installationId: number,
|
|
369
|
-
names: readonly string[],
|
|
370
|
-
): Promise<unknown>;
|
|
371
|
-
removeRepositories(
|
|
372
|
-
org: string,
|
|
373
|
-
installationId: number,
|
|
374
|
-
names: readonly string[],
|
|
375
|
-
): Promise<unknown>;
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
/** The agent App has no installation on the org (distinct from a GitHub failure). */
|
|
379
|
-
export class AppNotInstalledError extends Error {
|
|
380
|
-
constructor(
|
|
381
|
-
readonly appId: number,
|
|
382
|
-
readonly org: string,
|
|
383
|
-
) {
|
|
384
|
-
super(
|
|
385
|
-
`agent App ${appId} (the agent's GH_APP_ID) is not installed on org ${org}`,
|
|
386
|
-
);
|
|
387
|
-
this.name = "AppNotInstalledError";
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
const baseName = (fullName: string): string => fullName.split("/")[1] ?? "";
|
|
392
|
-
|
|
393
|
-
/**
|
|
394
|
-
* ReconcileInstallationPort over GithubEnterpriseClient (GHE-3.1). Each call
|
|
395
|
-
* is scoped to the named agent App's installation on the org.
|
|
396
|
-
*/
|
|
397
|
-
export function createInstallationPort(
|
|
398
|
-
client: InstallationRepoClient,
|
|
399
|
-
): ReconcileInstallationPort {
|
|
400
|
-
const installationId = async (
|
|
401
|
-
appId: number,
|
|
402
|
-
org: string,
|
|
403
|
-
): Promise<number> => {
|
|
404
|
-
const found = (await client.listOrgInstallations(org)).find(
|
|
405
|
-
(i) => i.appId === appId,
|
|
406
|
-
);
|
|
407
|
-
if (!found) {
|
|
408
|
-
throw new AppNotInstalledError(appId, org);
|
|
409
|
-
}
|
|
410
|
-
return found.id;
|
|
411
|
-
};
|
|
412
|
-
return {
|
|
413
|
-
async listRepos(appId, orgs) {
|
|
414
|
-
const out: Record<string, string[]> = {};
|
|
415
|
-
for (const org of orgs) {
|
|
416
|
-
const repos = await client.listInstallationRepositories(
|
|
417
|
-
org,
|
|
418
|
-
await installationId(appId, org),
|
|
419
|
-
);
|
|
420
|
-
out[org] = repos.map((r) => r.fullName);
|
|
421
|
-
}
|
|
422
|
-
return out;
|
|
423
|
-
},
|
|
424
|
-
async addRepos(appId, org, repos) {
|
|
425
|
-
await client.addRepositories(
|
|
426
|
-
org,
|
|
427
|
-
await installationId(appId, org),
|
|
428
|
-
repos.map(baseName),
|
|
429
|
-
);
|
|
430
|
-
},
|
|
431
|
-
async removeRepos(appId, org, repos) {
|
|
432
|
-
await client.removeRepositories(
|
|
433
|
-
org,
|
|
434
|
-
await installationId(appId, org),
|
|
435
|
-
repos.map(baseName),
|
|
436
|
-
);
|
|
437
|
-
},
|
|
438
|
-
};
|
|
439
|
-
}
|
|
@@ -1,279 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* admin/src/reconcile-apply-service.unit.test.ts
|
|
3
|
-
* Unit tests for the open-work port call and the apply guards (fakes only).
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { describe, expect, it } from "bun:test";
|
|
7
|
-
import type { GithubAccessPreview } from "./github-access-preview.ts";
|
|
8
|
-
import {
|
|
9
|
-
createInstallationPort,
|
|
10
|
-
type ReconcileApplyDeps,
|
|
11
|
-
ReconcileApplyService,
|
|
12
|
-
} from "./reconcile-apply-service.ts";
|
|
13
|
-
import type { OpenWorkPort } from "./reconcile-open-work.ts";
|
|
14
|
-
import type { UserRepoAccessResult } from "./user-repo-access-resolver.ts";
|
|
15
|
-
|
|
16
|
-
function access(pushable: string[]): UserRepoAccessResult {
|
|
17
|
-
return {
|
|
18
|
-
username: "dev",
|
|
19
|
-
outcome: "ok",
|
|
20
|
-
userVerified: true,
|
|
21
|
-
complete: true,
|
|
22
|
-
unresolved: 0,
|
|
23
|
-
checkedAt: "2026-11-11T00:00:00.000Z",
|
|
24
|
-
orgs: [
|
|
25
|
-
{
|
|
26
|
-
org: "acme",
|
|
27
|
-
status: "ok",
|
|
28
|
-
unresolved: 0,
|
|
29
|
-
repos: pushable.map((fullName) => ({
|
|
30
|
-
fullName,
|
|
31
|
-
level: "write" as const,
|
|
32
|
-
canPush: true,
|
|
33
|
-
})),
|
|
34
|
-
},
|
|
35
|
-
],
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function build(opts: {
|
|
40
|
-
appId?: number | null;
|
|
41
|
-
repos: string[];
|
|
42
|
-
pushable: string[];
|
|
43
|
-
installed?: string[];
|
|
44
|
-
openWork?: OpenWorkPort;
|
|
45
|
-
preview?: GithubAccessPreview;
|
|
46
|
-
}) {
|
|
47
|
-
const state = { repos: [...opts.repos] };
|
|
48
|
-
const calls: string[] = [];
|
|
49
|
-
const audits: unknown[] = [];
|
|
50
|
-
const deps: ReconcileApplyDeps = {
|
|
51
|
-
agents: {
|
|
52
|
-
get: async () => ({
|
|
53
|
-
id: "a1",
|
|
54
|
-
repos: state.repos,
|
|
55
|
-
githubUsername: "dev",
|
|
56
|
-
...(opts.appId === null ? {} : { appId: opts.appId ?? 42 }),
|
|
57
|
-
}),
|
|
58
|
-
setRepos: async (_id, repos) => {
|
|
59
|
-
calls.push("config");
|
|
60
|
-
state.repos = repos;
|
|
61
|
-
},
|
|
62
|
-
},
|
|
63
|
-
access: async () =>
|
|
64
|
-
opts.preview ?? { status: "ok", access: access(opts.pushable) },
|
|
65
|
-
installation: {
|
|
66
|
-
listRepos: async (_app, _orgs) => ({
|
|
67
|
-
acme: opts.installed ?? opts.repos,
|
|
68
|
-
}),
|
|
69
|
-
addRepos: async (_app) => {
|
|
70
|
-
calls.push("inst-add");
|
|
71
|
-
},
|
|
72
|
-
removeRepos: async () => {
|
|
73
|
-
calls.push("inst-remove");
|
|
74
|
-
},
|
|
75
|
-
},
|
|
76
|
-
audit: {
|
|
77
|
-
write: async (e) => {
|
|
78
|
-
audits.push(e);
|
|
79
|
-
return "audit-1";
|
|
80
|
-
},
|
|
81
|
-
},
|
|
82
|
-
openWork: opts.openWork,
|
|
83
|
-
};
|
|
84
|
-
return { svc: new ReconcileApplyService(deps), state, calls, audits };
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
describe("ReconcileApplyService (unit)", () => {
|
|
88
|
-
it("calls blockOpenWork with the reconcile reason for each removed repo", async () => {
|
|
89
|
-
const blocked: { repos: readonly string[]; reason: string }[] = [];
|
|
90
|
-
const openWork: OpenWorkPort = {
|
|
91
|
-
listOpenWork: async (_a, repos) =>
|
|
92
|
-
repos.map((repo) => ({ id: `t-${repo}`, repo })),
|
|
93
|
-
blockOpenWork: async (_a, repos, reason) => {
|
|
94
|
-
blocked.push({ repos, reason });
|
|
95
|
-
},
|
|
96
|
-
};
|
|
97
|
-
const { svc } = build({
|
|
98
|
-
repos: ["acme/a", "acme/b"],
|
|
99
|
-
pushable: ["acme/a"],
|
|
100
|
-
openWork,
|
|
101
|
-
});
|
|
102
|
-
const plan = await svc.plan("a1");
|
|
103
|
-
const res = await svc.apply({
|
|
104
|
-
agentId: "a1",
|
|
105
|
-
caller: "dan@x.io",
|
|
106
|
-
planHash: plan.planHash,
|
|
107
|
-
});
|
|
108
|
-
expect(res.status).toBe("applied");
|
|
109
|
-
expect(blocked).toEqual([
|
|
110
|
-
{
|
|
111
|
-
repos: ["acme/b"],
|
|
112
|
-
reason: "repo removed from agent by GitHub reconcile (acme/b)",
|
|
113
|
-
},
|
|
114
|
-
]);
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
it("succeeds with no port and does not block", async () => {
|
|
118
|
-
const { svc } = build({
|
|
119
|
-
repos: ["acme/a", "acme/b"],
|
|
120
|
-
pushable: ["acme/a"],
|
|
121
|
-
});
|
|
122
|
-
const plan = await svc.plan("a1");
|
|
123
|
-
const res = await svc.apply({
|
|
124
|
-
agentId: "a1",
|
|
125
|
-
caller: "c",
|
|
126
|
-
planHash: plan.planHash,
|
|
127
|
-
});
|
|
128
|
-
expect(res.status).toBe("applied");
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
it("a port failure does not fail an applied reconcile", async () => {
|
|
132
|
-
const openWork: OpenWorkPort = {
|
|
133
|
-
listOpenWork: async () => [],
|
|
134
|
-
blockOpenWork: async () => {
|
|
135
|
-
throw new Error("store down");
|
|
136
|
-
},
|
|
137
|
-
};
|
|
138
|
-
const { svc } = build({
|
|
139
|
-
repos: ["acme/a", "acme/b"],
|
|
140
|
-
pushable: ["acme/a"],
|
|
141
|
-
openWork,
|
|
142
|
-
});
|
|
143
|
-
const plan = await svc.plan("a1");
|
|
144
|
-
const res = await svc.apply({
|
|
145
|
-
agentId: "a1",
|
|
146
|
-
caller: "c",
|
|
147
|
-
planHash: plan.planHash,
|
|
148
|
-
});
|
|
149
|
-
expect(res.status).toBe("applied");
|
|
150
|
-
expect(res.status === "applied" && res.warnings.join()).toContain(
|
|
151
|
-
"store down",
|
|
152
|
-
);
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
it("agent without GH_APP_ID is skipped with a reason naming GH_APP_ID", async () => {
|
|
156
|
-
const { svc, calls } = build({
|
|
157
|
-
appId: null,
|
|
158
|
-
repos: ["acme/a"],
|
|
159
|
-
pushable: ["acme/a"],
|
|
160
|
-
});
|
|
161
|
-
const plan = await svc.plan("a1");
|
|
162
|
-
expect(plan.skipped).toBe(true);
|
|
163
|
-
expect(plan.skipReason).toContain("GH_APP_ID");
|
|
164
|
-
expect(plan.skipReason).not.toContain("not configured");
|
|
165
|
-
const res = await svc.apply({ agentId: "a1", caller: "c", planHash: "x" });
|
|
166
|
-
expect(res.status).toBe("skipped");
|
|
167
|
-
expect(calls).toEqual([]);
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
it("passes the agent's own App id to the installation port", async () => {
|
|
171
|
-
const seen: number[] = [];
|
|
172
|
-
const { svc } = build({
|
|
173
|
-
appId: 77,
|
|
174
|
-
repos: ["acme/a"],
|
|
175
|
-
pushable: ["acme/a"],
|
|
176
|
-
});
|
|
177
|
-
// @ts-expect-error reach into deps to spy
|
|
178
|
-
const inst = svc.deps.installation;
|
|
179
|
-
const orig = inst.listRepos;
|
|
180
|
-
inst.listRepos = async (app: number, orgs: readonly string[]) => {
|
|
181
|
-
seen.push(app);
|
|
182
|
-
return orig(app, orgs);
|
|
183
|
-
};
|
|
184
|
-
await svc.plan("a1");
|
|
185
|
-
expect(seen).toEqual([77]);
|
|
186
|
-
});
|
|
187
|
-
|
|
188
|
-
it("skipped plan (unconfigured) changes nothing and writes no audit", async () => {
|
|
189
|
-
const { svc, calls, audits } = build({
|
|
190
|
-
repos: ["acme/a"],
|
|
191
|
-
pushable: [],
|
|
192
|
-
preview: { status: "unconfigured" },
|
|
193
|
-
});
|
|
194
|
-
const res = await svc.apply({ agentId: "a1", caller: "c", planHash: "x" });
|
|
195
|
-
expect(res.status).toBe("skipped");
|
|
196
|
-
expect(calls).toEqual([]);
|
|
197
|
-
expect(audits).toEqual([]);
|
|
198
|
-
});
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
describe("createInstallationPort (unit)", () => {
|
|
202
|
-
it("scopes to the given App's installation per call and sends simple names", async () => {
|
|
203
|
-
const seen: string[] = [];
|
|
204
|
-
const port = createInstallationPort({
|
|
205
|
-
listOrgInstallations: async () => [
|
|
206
|
-
{ id: 1, appId: 10 },
|
|
207
|
-
{ id: 2, appId: 20 },
|
|
208
|
-
],
|
|
209
|
-
listInstallationRepositories: async (_o, id) => {
|
|
210
|
-
seen.push(`list:${id}`);
|
|
211
|
-
return [{ fullName: "acme/a" }];
|
|
212
|
-
},
|
|
213
|
-
addRepositories: async (_o, id, names) => {
|
|
214
|
-
seen.push(`add:${id}:${names.join(",")}`);
|
|
215
|
-
},
|
|
216
|
-
removeRepositories: async (_o, id, names) => {
|
|
217
|
-
seen.push(`rm:${id}:${names.join(",")}`);
|
|
218
|
-
},
|
|
219
|
-
});
|
|
220
|
-
expect(await port.listRepos(20, ["acme"])).toEqual({ acme: ["acme/a"] });
|
|
221
|
-
await port.addRepos(20, "acme", ["acme/b"]);
|
|
222
|
-
await port.removeRepos(10, "acme", ["acme/c", "acme/d"]);
|
|
223
|
-
expect(seen).toEqual(["list:2", "add:2:b", "rm:1:c,d"]);
|
|
224
|
-
});
|
|
225
|
-
|
|
226
|
-
it("names the org when the App is not installed there", async () => {
|
|
227
|
-
const port = createInstallationPort({
|
|
228
|
-
listOrgInstallations: async () => [{ id: 1, appId: 10 }],
|
|
229
|
-
listInstallationRepositories: async () => [],
|
|
230
|
-
addRepositories: async () => {},
|
|
231
|
-
removeRepositories: async () => {},
|
|
232
|
-
});
|
|
233
|
-
await expect(port.listRepos(99, ["acme"])).rejects.toThrow(
|
|
234
|
-
/99.*not installed on org acme/,
|
|
235
|
-
);
|
|
236
|
-
});
|
|
237
|
-
});
|
|
238
|
-
|
|
239
|
-
describe("ReconcileApplyService.planWithOpenWork (unit)", () => {
|
|
240
|
-
const removing = { repos: ["acme/a", "acme/b"], pushable: ["acme/a"] };
|
|
241
|
-
|
|
242
|
-
it("lists open work on the repos the plan removes", async () => {
|
|
243
|
-
const seen: (readonly string[])[] = [];
|
|
244
|
-
const { svc } = build({
|
|
245
|
-
...removing,
|
|
246
|
-
openWork: {
|
|
247
|
-
listOpenWork: async (_a, repos) => {
|
|
248
|
-
seen.push(repos);
|
|
249
|
-
return [{ id: "t1", repo: "acme/b", title: "x" }];
|
|
250
|
-
},
|
|
251
|
-
blockOpenWork: async () => {},
|
|
252
|
-
},
|
|
253
|
-
});
|
|
254
|
-
const res = await svc.planWithOpenWork("a1");
|
|
255
|
-
expect(seen).toEqual([["acme/b"]]);
|
|
256
|
-
expect(res.openWork).toEqual([{ id: "t1", repo: "acme/b", title: "x" }]);
|
|
257
|
-
expect(res.plan.diff.configRemoves).toEqual(["acme/b"]);
|
|
258
|
-
});
|
|
259
|
-
|
|
260
|
-
it("returns an empty list with no port", async () => {
|
|
261
|
-
const { svc } = build(removing);
|
|
262
|
-
expect((await svc.planWithOpenWork("a1")).openWork).toEqual([]);
|
|
263
|
-
});
|
|
264
|
-
|
|
265
|
-
it("a port failure becomes a warning, not an error", async () => {
|
|
266
|
-
const { svc } = build({
|
|
267
|
-
...removing,
|
|
268
|
-
openWork: {
|
|
269
|
-
listOpenWork: async () => {
|
|
270
|
-
throw new Error("store down");
|
|
271
|
-
},
|
|
272
|
-
blockOpenWork: async () => {},
|
|
273
|
-
},
|
|
274
|
-
});
|
|
275
|
-
const res = await svc.planWithOpenWork("a1");
|
|
276
|
-
expect(res.openWork).toEqual([]);
|
|
277
|
-
expect(res.plan.warnings.join(" ")).toContain("store down");
|
|
278
|
-
});
|
|
279
|
-
});
|