@app-vitals/agent-admin 1.88.0 → 1.89.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/openapi.json +253 -0
- package/openapi.platform.json +297 -0
- package/package.json +2 -2
- package/src/admin-boot.ts +11 -0
- package/src/agents-api.smoke.test.ts +88 -0
- package/src/agents-api.ts +15 -0
- package/src/fixtures/github-enterprise-cassette.json +16 -0
- package/src/github-enterprise-api.integration.test.ts +127 -0
- package/src/github-enterprise-api.smoke.test.ts +188 -5
- package/src/github-enterprise-api.ts +212 -4
- package/src/github-enterprise-client.ts +23 -1
- package/src/github-enterprise-client.unit.test.ts +65 -0
- package/src/github-installation-wiring.ts +5 -0
- package/src/github-sync-switch.integration.test.ts +297 -0
- package/src/github-sync-switch.unit.test.ts +467 -0
- package/src/github-sync.ts +205 -24
- package/src/github-uninstall.ts +70 -0
- package/src/github-uninstall.unit.test.ts +78 -0
- package/src/installation-ports.ts +41 -0
package/src/github-sync.ts
CHANGED
|
@@ -19,9 +19,18 @@
|
|
|
19
19
|
* not-found or unverified user skips the whole call. More than
|
|
20
20
|
* MAX_UNCONFIRMED_REMOVALS removals need confirmRemovals.
|
|
21
21
|
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
22
|
+
* An org whose agent installation is on "All repositories" (read from the
|
|
23
|
+
* crawl cache; unknown is never "all") cannot take add/remove calls, so Sync
|
|
24
|
+
* switches it to "selected" first (GIS-1.2): target = installed repos minus the
|
|
25
|
+
* confirmed app removes for that org. The first MAX_SELECTION_PATCH_REPOS go in
|
|
26
|
+
* the PATCH, the rest through the batched add. The org's removes fold into the
|
|
27
|
+
* switch but still count in removedRepos. An empty target skips the org with a
|
|
28
|
+
* warning and leaves it on "all".
|
|
29
|
+
*
|
|
30
|
+
* Order: selection switches, installation adds, installation removals,
|
|
31
|
+
* agent-repo removals, one audit entry, then the agent's open work on the
|
|
32
|
+
* removed repos is blocked through the optional OpenWorkPort (a port failure is
|
|
33
|
+
* a warning).
|
|
25
34
|
*/
|
|
26
35
|
|
|
27
36
|
import type { GithubAccessPreviewFn } from "./github-access-preview.ts";
|
|
@@ -80,6 +89,22 @@ export const REVERIFY_FAILED_REASON =
|
|
|
80
89
|
export const NOT_PRESENT_REASON =
|
|
81
90
|
"not on the agent installation or in the agent's repos";
|
|
82
91
|
|
|
92
|
+
/** GitHub caps the repositories in a selection-switch PATCH at 50. */
|
|
93
|
+
export const MAX_SELECTION_PATCH_REPOS = 50;
|
|
94
|
+
|
|
95
|
+
export const EMPTY_SWITCH_TARGET_REASON =
|
|
96
|
+
"the bound user can push to none of this org's repos, so it cannot be switched to selected repositories and stays on All repositories; use Uninstall to remove the agent App from this org";
|
|
97
|
+
|
|
98
|
+
/** The installer App's permission a selection switch needs. */
|
|
99
|
+
export const SWITCH_PERMISSION_MESSAGE = (org: string): string =>
|
|
100
|
+
`GitHub refused to switch the agent App on ${org} to selected repositories (403): the installer App needs the enterprise_organization_installation_repositories permission with write access`;
|
|
101
|
+
|
|
102
|
+
/** An "all" installation Sync switches to "selected" with this repo list. */
|
|
103
|
+
export interface SelectionSwitch {
|
|
104
|
+
org: string;
|
|
105
|
+
repos: string[];
|
|
106
|
+
}
|
|
107
|
+
|
|
83
108
|
export interface SyncLists {
|
|
84
109
|
appAdds: readonly string[];
|
|
85
110
|
appRemoves: readonly string[];
|
|
@@ -93,6 +118,11 @@ export interface GithubSyncPlanInput {
|
|
|
93
118
|
agentRepos: readonly string[];
|
|
94
119
|
/** Repos the agent App's installation covers, per org login it is installed on. */
|
|
95
120
|
installationRepos: Readonly<Record<string, readonly string[]>>;
|
|
121
|
+
/**
|
|
122
|
+
* The agent installation's repository selection per org (any case); a
|
|
123
|
+
* missing or null entry is unknown and is never treated as "all".
|
|
124
|
+
*/
|
|
125
|
+
selections?: Readonly<Record<string, "all" | "selected" | null | undefined>>;
|
|
96
126
|
/**
|
|
97
127
|
* The lists the admin confirmed. When set, only these are candidates and
|
|
98
128
|
* every one left alone is reported in `notApplied`; when unset (preview),
|
|
@@ -116,6 +146,8 @@ export interface GithubSyncPlan {
|
|
|
116
146
|
agentRemoves: string[];
|
|
117
147
|
/** Every repo removed from either place, sorted. */
|
|
118
148
|
removedRepos: string[];
|
|
149
|
+
/** "All" installations to switch to "selected" first (additive). */
|
|
150
|
+
selectionSwitches?: SelectionSwitch[];
|
|
119
151
|
skippedOrgs: OrgSkip[];
|
|
120
152
|
notApplied: NotAppliedRepo[];
|
|
121
153
|
warnings: string[];
|
|
@@ -132,12 +164,43 @@ function skippedPlan(reason: string): GithubSyncPlan {
|
|
|
132
164
|
appRemoves: [],
|
|
133
165
|
agentRemoves: [],
|
|
134
166
|
removedRepos: [],
|
|
167
|
+
selectionSwitches: [],
|
|
135
168
|
skippedOrgs: [],
|
|
136
169
|
notApplied: [],
|
|
137
170
|
warnings: [],
|
|
138
171
|
};
|
|
139
172
|
}
|
|
140
173
|
|
|
174
|
+
/**
|
|
175
|
+
* Pure: the orgs on "all" and the "selected" list each switches to (installed
|
|
176
|
+
* repos minus `appRemoves`). An org with an empty target is returned in
|
|
177
|
+
* `emptyOrgs` instead. Only orgs `eligible` returns true for are considered.
|
|
178
|
+
*/
|
|
179
|
+
export function planSelectionSwitches(input: {
|
|
180
|
+
installationRepos: Readonly<Record<string, readonly string[]>>;
|
|
181
|
+
selections?: GithubSyncPlanInput["selections"];
|
|
182
|
+
appRemoves: readonly string[];
|
|
183
|
+
eligible?: (org: string) => boolean;
|
|
184
|
+
}): { switches: SelectionSwitch[]; emptyOrgs: string[] } {
|
|
185
|
+
const selections = new Map(
|
|
186
|
+
Object.entries(input.selections ?? {}).map(([o, v]) => [lower(o), v]),
|
|
187
|
+
);
|
|
188
|
+
const removed = new Set(input.appRemoves.map(lower));
|
|
189
|
+
const switches: SelectionSwitch[] = [];
|
|
190
|
+
const emptyOrgs: string[] = [];
|
|
191
|
+
for (const [org, repos] of Object.entries(input.installationRepos)) {
|
|
192
|
+
if (selections.get(lower(org)) !== "all") continue;
|
|
193
|
+
if (input.eligible && !input.eligible(org)) continue;
|
|
194
|
+
const target = sorted(repos.filter((r) => !removed.has(lower(r))));
|
|
195
|
+
if (target.length === 0) emptyOrgs.push(org);
|
|
196
|
+
else switches.push({ org, repos: target });
|
|
197
|
+
}
|
|
198
|
+
return {
|
|
199
|
+
switches: switches.sort((a, b) => a.org.localeCompare(b.org)),
|
|
200
|
+
emptyOrgs: emptyOrgs.sort(),
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
141
204
|
/** Pure: the installation adds and removes and agent-repo removes to sync. */
|
|
142
205
|
export function planGithubSync(input: GithubSyncPlanInput): GithubSyncPlan {
|
|
143
206
|
const skip = accessSkipReason(input.username, input.access);
|
|
@@ -231,9 +294,24 @@ export function planGithubSync(input: GithubSyncPlanInput): GithubSyncPlan {
|
|
|
231
294
|
}
|
|
232
295
|
return out;
|
|
233
296
|
};
|
|
234
|
-
const
|
|
297
|
+
const confirmedAppRemoves = removable(appRemoveCandidates, installed);
|
|
235
298
|
const agentRemoves = removable(agentRemoveCandidates, configured);
|
|
236
299
|
|
|
300
|
+
const { switches, emptyOrgs } = planSelectionSwitches({
|
|
301
|
+
installationRepos: input.installationRepos,
|
|
302
|
+
...(input.selections ? { selections: input.selections } : {}),
|
|
303
|
+
appRemoves: [...confirmedAppRemoves],
|
|
304
|
+
eligible: (o) => !orgSkipReason(orgResults.get(lower(o))),
|
|
305
|
+
});
|
|
306
|
+
// An empty target leaves the org on "all", where removes cannot be applied.
|
|
307
|
+
const emptySet = new Set(emptyOrgs.map(lower));
|
|
308
|
+
const appRemoves = new Set<string>();
|
|
309
|
+
for (const r of confirmedAppRemoves) {
|
|
310
|
+
if (emptySet.has(lower(repoOrg(r)))) leave(r, EMPTY_SWITCH_TARGET_REASON);
|
|
311
|
+
else appRemoves.add(r);
|
|
312
|
+
}
|
|
313
|
+
for (const o of emptyOrgs) skipReasons.set(o, EMPTY_SWITCH_TARGET_REASON);
|
|
314
|
+
|
|
237
315
|
const skippedOrgs = sorted(skipReasons.keys()).map((org) => ({
|
|
238
316
|
org,
|
|
239
317
|
reason: skipReasons.get(org) as string,
|
|
@@ -245,6 +323,7 @@ export function planGithubSync(input: GithubSyncPlanInput): GithubSyncPlan {
|
|
|
245
323
|
appRemoves: sorted(appRemoves),
|
|
246
324
|
agentRemoves: sorted(agentRemoves),
|
|
247
325
|
removedRepos: sorted(new Set([...appRemoves, ...agentRemoves])),
|
|
326
|
+
selectionSwitches: switches,
|
|
248
327
|
skippedOrgs,
|
|
249
328
|
notApplied: [...notApplied.values()].sort((a, b) =>
|
|
250
329
|
a.repo.localeCompare(b.repo),
|
|
@@ -293,7 +372,7 @@ export interface GithubSyncDeps {
|
|
|
293
372
|
) => Promise<boolean | undefined>;
|
|
294
373
|
installation: Pick<
|
|
295
374
|
InstallationReposPort,
|
|
296
|
-
"listInstalled" | "addRepos" | "removeRepos"
|
|
375
|
+
"listInstalled" | "addRepos" | "removeRepos" | "setRepositorySelection"
|
|
297
376
|
>;
|
|
298
377
|
audit: InstallationAuditPort;
|
|
299
378
|
/** Optional: with none, nothing is listed or blocked. */
|
|
@@ -340,6 +419,22 @@ export type GithubSyncApplyResult =
|
|
|
340
419
|
}
|
|
341
420
|
| { status: "failed"; plan: GithubSyncPlan; auditId: string; error: string };
|
|
342
421
|
|
|
422
|
+
/** A plan plus the installation context a re-verified plan recomputes switches from. */
|
|
423
|
+
interface PlannedSync {
|
|
424
|
+
plan: GithubSyncPlan;
|
|
425
|
+
installationRepos: Record<string, string[]>;
|
|
426
|
+
selections?: Record<string, "all" | "selected" | null>;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/** A 403 on a selection switch becomes a message naming the missing permission. */
|
|
430
|
+
function switchError(org: string, err: unknown): Error {
|
|
431
|
+
const e = err as { kind?: string; status?: number } | undefined;
|
|
432
|
+
if (e?.kind === "permission" || (e?.status === 403 && e.kind === undefined)) {
|
|
433
|
+
return new Error(SWITCH_PERMISSION_MESSAGE(org));
|
|
434
|
+
}
|
|
435
|
+
return err instanceof Error ? err : new Error(String(err));
|
|
436
|
+
}
|
|
437
|
+
|
|
343
438
|
function groupByOrg(repos: readonly string[]): Map<string, string[]> {
|
|
344
439
|
const byOrg = new Map<string, string[]>();
|
|
345
440
|
for (const r of repos) {
|
|
@@ -356,7 +451,7 @@ export class GithubSyncService {
|
|
|
356
451
|
async preview(agentId: string): Promise<GithubSyncPreviewResult> {
|
|
357
452
|
const agent = await this.deps.agents.get(agentId);
|
|
358
453
|
if (!agent) return { status: "agent_not_found" };
|
|
359
|
-
const plan = await this.planFor(agent);
|
|
454
|
+
const { plan } = await this.planFor(agent);
|
|
360
455
|
if (plan.skipped) {
|
|
361
456
|
return {
|
|
362
457
|
status: "skipped",
|
|
@@ -380,7 +475,8 @@ export class GithubSyncService {
|
|
|
380
475
|
async apply(input: GithubSyncApplyInput): Promise<GithubSyncApplyResult> {
|
|
381
476
|
const agent = await this.deps.agents.get(input.agentId);
|
|
382
477
|
if (!agent) return { status: "agent_not_found" };
|
|
383
|
-
|
|
478
|
+
const planned = await this.planFor(agent, input);
|
|
479
|
+
let plan = planned.plan;
|
|
384
480
|
if (plan.skipped) {
|
|
385
481
|
return {
|
|
386
482
|
status: "skipped",
|
|
@@ -388,8 +484,12 @@ export class GithubSyncService {
|
|
|
388
484
|
...(plan.appsState ? { appsState: plan.appsState } : {}),
|
|
389
485
|
};
|
|
390
486
|
}
|
|
391
|
-
plan = await this.reverifyRemovals(plan, agent);
|
|
392
|
-
|
|
487
|
+
plan = await this.reverifyRemovals(plan, agent, planned);
|
|
488
|
+
const switches = plan.selectionSwitches ?? [];
|
|
489
|
+
if (
|
|
490
|
+
plan.appAdds.length + plan.removedRepos.length + switches.length ===
|
|
491
|
+
0
|
|
492
|
+
) {
|
|
393
493
|
return { status: "noop", plan };
|
|
394
494
|
}
|
|
395
495
|
const removals = plan.removedRepos.length;
|
|
@@ -401,14 +501,49 @@ export class GithubSyncService {
|
|
|
401
501
|
const identity = identityOf(agent);
|
|
402
502
|
const added: string[] = [];
|
|
403
503
|
const removed: string[] = [];
|
|
504
|
+
const switchRecords: SelectionSwitch[] = [];
|
|
505
|
+
const switchedOrgs = new Set(switches.map((s) => lower(s.org)));
|
|
404
506
|
let configDone = false;
|
|
405
507
|
let error: string | undefined;
|
|
406
508
|
try {
|
|
509
|
+
for (const sw of switches) {
|
|
510
|
+
const record: SelectionSwitch = { org: sw.org, repos: [] };
|
|
511
|
+
const head = sw.repos.slice(0, MAX_SELECTION_PATCH_REPOS);
|
|
512
|
+
const rest = sw.repos.slice(MAX_SELECTION_PATCH_REPOS);
|
|
513
|
+
const setSelection = this.deps.installation.setRepositorySelection;
|
|
514
|
+
if (!setSelection) {
|
|
515
|
+
throw new Error(
|
|
516
|
+
`cannot switch ${sw.org} to selected repositories: the installation port does not support it`,
|
|
517
|
+
);
|
|
518
|
+
}
|
|
519
|
+
try {
|
|
520
|
+
await setSelection.call(
|
|
521
|
+
this.deps.installation,
|
|
522
|
+
appId,
|
|
523
|
+
sw.org,
|
|
524
|
+
head,
|
|
525
|
+
identity,
|
|
526
|
+
);
|
|
527
|
+
} catch (err) {
|
|
528
|
+
throw switchError(sw.org, err);
|
|
529
|
+
}
|
|
530
|
+
// The switch landed: the org's confirmed removes are now off the installation.
|
|
531
|
+
record.repos = head;
|
|
532
|
+
switchRecords.push(record);
|
|
533
|
+
removed.push(
|
|
534
|
+
...plan.appRemoves.filter((r) => lower(repoOrg(r)) === lower(sw.org)),
|
|
535
|
+
);
|
|
536
|
+
if (rest.length > 0) {
|
|
537
|
+
await this.deps.installation.addRepos(appId, sw.org, rest, identity);
|
|
538
|
+
record.repos = sw.repos;
|
|
539
|
+
}
|
|
540
|
+
}
|
|
407
541
|
for (const [org, repos] of groupByOrg(plan.appAdds)) {
|
|
408
542
|
await this.deps.installation.addRepos(appId, org, repos, identity);
|
|
409
543
|
added.push(...repos);
|
|
410
544
|
}
|
|
411
545
|
for (const [org, repos] of groupByOrg(plan.appRemoves)) {
|
|
546
|
+
if (switchedOrgs.has(lower(org))) continue;
|
|
412
547
|
await this.deps.installation.removeRepos(appId, org, repos, identity);
|
|
413
548
|
removed.push(...repos);
|
|
414
549
|
}
|
|
@@ -433,6 +568,9 @@ export class GithubSyncService {
|
|
|
433
568
|
installationRemoves: removed,
|
|
434
569
|
configAdds: [],
|
|
435
570
|
configRemoves: configDone ? plan.agentRemoves : [],
|
|
571
|
+
...(switchRecords.length > 0
|
|
572
|
+
? { selectionSwitches: switchRecords }
|
|
573
|
+
: {}),
|
|
436
574
|
},
|
|
437
575
|
result:
|
|
438
576
|
error === undefined
|
|
@@ -455,6 +593,7 @@ export class GithubSyncService {
|
|
|
455
593
|
private async reverifyRemovals(
|
|
456
594
|
plan: GithubSyncPlan,
|
|
457
595
|
agent: InstallationAgentRecord,
|
|
596
|
+
planned: PlannedSync,
|
|
458
597
|
): Promise<GithubSyncPlan> {
|
|
459
598
|
const verify = this.deps.verifyPush;
|
|
460
599
|
const username = agent.githubUsername?.trim();
|
|
@@ -477,10 +616,21 @@ export class GithubSyncService {
|
|
|
477
616
|
const keep = (repos: string[]) => repos.filter((r) => !dropped.has(r));
|
|
478
617
|
const appRemoves = keep(plan.appRemoves);
|
|
479
618
|
const agentRemoves = keep(plan.agentRemoves);
|
|
619
|
+
// A removal the user can still push to goes back on the switch target.
|
|
620
|
+
const switchOrgs = new Set(
|
|
621
|
+
(plan.selectionSwitches ?? []).map((s) => lower(s.org)),
|
|
622
|
+
);
|
|
623
|
+
const { switches } = planSelectionSwitches({
|
|
624
|
+
installationRepos: planned.installationRepos,
|
|
625
|
+
...(planned.selections ? { selections: planned.selections } : {}),
|
|
626
|
+
appRemoves,
|
|
627
|
+
eligible: (o) => switchOrgs.has(lower(o)),
|
|
628
|
+
});
|
|
480
629
|
return {
|
|
481
630
|
...plan,
|
|
482
631
|
appRemoves,
|
|
483
632
|
agentRemoves,
|
|
633
|
+
selectionSwitches: switches,
|
|
484
634
|
removedRepos: sorted(new Set([...appRemoves, ...agentRemoves])),
|
|
485
635
|
notApplied: [
|
|
486
636
|
...plan.notApplied,
|
|
@@ -494,7 +644,10 @@ export class GithubSyncService {
|
|
|
494
644
|
agent: InstallationAgentRecord,
|
|
495
645
|
username: string,
|
|
496
646
|
installed: { org: string; repos: string[] }[],
|
|
497
|
-
): Promise<
|
|
647
|
+
): Promise<{
|
|
648
|
+
access: UserRepoAccessResult;
|
|
649
|
+
selections: Record<string, "all" | "selected" | null>;
|
|
650
|
+
}> {
|
|
498
651
|
const cache = this.deps.cache as InstallationCachePort;
|
|
499
652
|
const accountId = agent.accountId || DEPLOYMENT_WIDE_ACCOUNT_KEY;
|
|
500
653
|
const now = (this.deps.now ?? (() => new Date()))();
|
|
@@ -582,52 +735,75 @@ export class GithubSyncService {
|
|
|
582
735
|
rows,
|
|
583
736
|
});
|
|
584
737
|
}
|
|
585
|
-
|
|
738
|
+
// Unknown (a read failure, no crawl, no recorded value) is never "all".
|
|
739
|
+
const selections: Record<string, "all" | "selected" | null> = {};
|
|
740
|
+
for (const s of agentSnaps) {
|
|
741
|
+
selections[s.org] = s.repositorySelection ?? null;
|
|
742
|
+
}
|
|
743
|
+
return {
|
|
744
|
+
access: buildCachedAccess({ username, now, maxAgeMs, orgs: inputs }),
|
|
745
|
+
selections,
|
|
746
|
+
};
|
|
586
747
|
}
|
|
587
748
|
|
|
588
749
|
private async planFor(
|
|
589
750
|
agent: InstallationAgentRecord,
|
|
590
751
|
requested?: SyncLists,
|
|
591
|
-
): Promise<
|
|
752
|
+
): Promise<PlannedSync> {
|
|
753
|
+
const none = (plan: GithubSyncPlan): PlannedSync => ({
|
|
754
|
+
plan,
|
|
755
|
+
installationRepos: {},
|
|
756
|
+
});
|
|
592
757
|
const username = agent.githubUsername?.trim();
|
|
593
758
|
if (!username) {
|
|
594
|
-
return skippedPlan(accessSkipReason(username, null) as string);
|
|
759
|
+
return none(skippedPlan(accessSkipReason(username, null) as string));
|
|
595
760
|
}
|
|
596
761
|
if (this.deps.appsState) {
|
|
597
762
|
const state = await this.deps.appsState(agent.accountId);
|
|
598
763
|
if (state !== "both") {
|
|
599
|
-
return {
|
|
764
|
+
return none({
|
|
600
765
|
...skippedPlan(GITHUB_APPS_STATE_REASONS[state]),
|
|
601
766
|
appsState: state,
|
|
602
|
-
};
|
|
767
|
+
});
|
|
603
768
|
}
|
|
604
769
|
}
|
|
605
770
|
let liveAccess: UserRepoAccessResult | null = null;
|
|
606
771
|
if (!this.deps.cache) {
|
|
607
772
|
const preview = await this.deps.access(username, agent.accountId);
|
|
608
773
|
if (preview.status === "unconfigured") {
|
|
609
|
-
return skippedPlan("GitHub Apps are not configured");
|
|
774
|
+
return none(skippedPlan("GitHub Apps are not configured"));
|
|
610
775
|
}
|
|
611
776
|
// Whole-call skips need no installation calls.
|
|
612
777
|
const skip = accessSkipReason(username, preview.access);
|
|
613
|
-
if (skip) return skippedPlan(skip);
|
|
778
|
+
if (skip) return none(skippedPlan(skip));
|
|
614
779
|
liveAccess = preview.access;
|
|
615
780
|
}
|
|
616
|
-
if (agent.appId === undefined)
|
|
781
|
+
if (agent.appId === undefined) {
|
|
782
|
+
return none(skippedPlan(MISSING_APP_ID_REASON));
|
|
783
|
+
}
|
|
617
784
|
const installed = await this.deps.installation.listInstalled(
|
|
618
785
|
agent.appId,
|
|
619
786
|
identityOf(agent),
|
|
620
787
|
);
|
|
621
|
-
if (installed.status === "none")
|
|
788
|
+
if (installed.status === "none") {
|
|
789
|
+
return none(skippedPlan(NO_INSTALLATION_REASON));
|
|
790
|
+
}
|
|
791
|
+
const cached = liveAccess
|
|
792
|
+
? undefined
|
|
793
|
+
: await this.cachedAccess(agent, username, installed.orgs);
|
|
622
794
|
const resolved =
|
|
623
|
-
liveAccess ?? (
|
|
624
|
-
|
|
795
|
+
liveAccess ?? (cached as { access: UserRepoAccessResult }).access;
|
|
796
|
+
// The live path never reads the crawl cache, so every selection is unknown.
|
|
797
|
+
const selections = cached?.selections;
|
|
798
|
+
const installationRepos = Object.fromEntries(
|
|
799
|
+
installed.orgs.map(({ org, repos }) => [org, repos]),
|
|
800
|
+
);
|
|
801
|
+
const plan = planGithubSync({
|
|
625
802
|
username,
|
|
626
803
|
access: resolved,
|
|
627
804
|
agentRepos: agent.repos,
|
|
628
|
-
installationRepos
|
|
629
|
-
|
|
630
|
-
),
|
|
805
|
+
installationRepos,
|
|
806
|
+
...(selections ? { selections } : {}),
|
|
631
807
|
...(requested
|
|
632
808
|
? {
|
|
633
809
|
requested: {
|
|
@@ -638,5 +814,10 @@ export class GithubSyncService {
|
|
|
638
814
|
}
|
|
639
815
|
: {}),
|
|
640
816
|
});
|
|
817
|
+
return {
|
|
818
|
+
plan,
|
|
819
|
+
installationRepos,
|
|
820
|
+
...(selections ? { selections } : {}),
|
|
821
|
+
};
|
|
641
822
|
}
|
|
642
823
|
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/github-uninstall.ts
|
|
3
|
+
* Pure helpers for uninstalling the agent App from an org (GIS-1.5): match the
|
|
4
|
+
* agent App's installation among an org's installations (same identity rules as
|
|
5
|
+
* the install route: client id, then slug, then app id) and plan which of the
|
|
6
|
+
* agent's repos the uninstall removes. The cleanup itself (agent repos, open
|
|
7
|
+
* work, audit) reuses Sync's removal handling from the route.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { AppInstallation } from "./github-enterprise-client.ts";
|
|
11
|
+
import { MAX_UNCONFIRMED_REMOVALS } from "./installation-ports.ts";
|
|
12
|
+
import { repoOrg } from "./repo-access-rules.ts";
|
|
13
|
+
|
|
14
|
+
/** `planHash` of the audit row an org uninstall writes. */
|
|
15
|
+
export const GITHUB_UNINSTALL_AUDIT_PLAN_HASH = "uninstall:org";
|
|
16
|
+
|
|
17
|
+
export function uninstallBlockReason(repo: string): string {
|
|
18
|
+
return `repo ${repo} was removed from the agent because the agent App was uninstalled from its org`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface AgentAppIdentity {
|
|
22
|
+
clientId?: string | null;
|
|
23
|
+
slug?: string | null;
|
|
24
|
+
appId?: number | null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** The installation of the agent's App, or undefined when it is not installed. */
|
|
28
|
+
export function findAgentInstallation(
|
|
29
|
+
installs: readonly AppInstallation[],
|
|
30
|
+
identity: AgentAppIdentity,
|
|
31
|
+
): AppInstallation | undefined {
|
|
32
|
+
const { clientId, slug, appId } = identity;
|
|
33
|
+
if (clientId) {
|
|
34
|
+
const hit = installs.find((i) => i.clientId === clientId);
|
|
35
|
+
if (hit) return hit;
|
|
36
|
+
}
|
|
37
|
+
if (slug) {
|
|
38
|
+
const hit = installs.find(
|
|
39
|
+
(i) => i.appSlug.toLowerCase() === slug.toLowerCase(),
|
|
40
|
+
);
|
|
41
|
+
if (hit) return hit;
|
|
42
|
+
}
|
|
43
|
+
if (appId) return installs.find((i) => i.appId === appId);
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface OrgUninstallPlan {
|
|
48
|
+
org: string;
|
|
49
|
+
/** The agent's repos in the org (removed from the agent), sorted. */
|
|
50
|
+
repos: string[];
|
|
51
|
+
/** True when more repos than MAX_UNCONFIRMED_REMOVALS would be removed. */
|
|
52
|
+
needsConfirm: boolean;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function planOrgUninstall(input: {
|
|
56
|
+
org: string;
|
|
57
|
+
agentRepos: readonly string[];
|
|
58
|
+
}): OrgUninstallPlan {
|
|
59
|
+
const org = input.org.toLowerCase();
|
|
60
|
+
const repos = [
|
|
61
|
+
...new Set(
|
|
62
|
+
input.agentRepos.filter((r) => repoOrg(r).toLowerCase() === org),
|
|
63
|
+
),
|
|
64
|
+
].sort();
|
|
65
|
+
return {
|
|
66
|
+
org: input.org,
|
|
67
|
+
repos,
|
|
68
|
+
needsConfirm: repos.length > MAX_UNCONFIRMED_REMOVALS,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/github-uninstall.unit.test.ts
|
|
3
|
+
* Unit tests for the org-uninstall plan and installation matching (GIS-1.5).
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { describe, expect, it } from "bun:test";
|
|
7
|
+
import {
|
|
8
|
+
findAgentInstallation,
|
|
9
|
+
GITHUB_UNINSTALL_AUDIT_PLAN_HASH,
|
|
10
|
+
planOrgUninstall,
|
|
11
|
+
uninstallBlockReason,
|
|
12
|
+
} from "./github-uninstall.ts";
|
|
13
|
+
|
|
14
|
+
const inst = (
|
|
15
|
+
id: number,
|
|
16
|
+
extra: Partial<{ appId: number; appSlug: string; clientId: string }> = {},
|
|
17
|
+
) => ({
|
|
18
|
+
id,
|
|
19
|
+
appSlug: extra.appSlug ?? "other",
|
|
20
|
+
accountLogin: "o",
|
|
21
|
+
repositorySelection: "all" as const,
|
|
22
|
+
...(extra.appId === undefined ? {} : { appId: extra.appId }),
|
|
23
|
+
...(extra.clientId === undefined ? {} : { clientId: extra.clientId }),
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe("findAgentInstallation", () => {
|
|
27
|
+
const all = [
|
|
28
|
+
inst(1),
|
|
29
|
+
inst(2, { appSlug: "My-App" }),
|
|
30
|
+
inst(3, { clientId: "Iv1.x" }),
|
|
31
|
+
inst(4, { appId: 99 }),
|
|
32
|
+
];
|
|
33
|
+
it("matches by client id, slug (case-insensitive) and app id", () => {
|
|
34
|
+
expect(findAgentInstallation(all, { clientId: "Iv1.x" })?.id).toBe(3);
|
|
35
|
+
expect(findAgentInstallation(all, { slug: "my-app" })?.id).toBe(2);
|
|
36
|
+
expect(findAgentInstallation(all, { appId: 99 })?.id).toBe(4);
|
|
37
|
+
});
|
|
38
|
+
it("prefers client id over slug", () => {
|
|
39
|
+
expect(
|
|
40
|
+
findAgentInstallation(all, { clientId: "Iv1.x", slug: "my-app" })?.id,
|
|
41
|
+
).toBe(3);
|
|
42
|
+
});
|
|
43
|
+
it("returns undefined when nothing matches or no identity is given", () => {
|
|
44
|
+
expect(findAgentInstallation(all, { slug: "nope" })).toBeUndefined();
|
|
45
|
+
expect(findAgentInstallation(all, {})).toBeUndefined();
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
describe("planOrgUninstall", () => {
|
|
50
|
+
it("selects only the agent repos in the org, case-insensitively, sorted", () => {
|
|
51
|
+
const plan = planOrgUninstall({
|
|
52
|
+
org: "Acme",
|
|
53
|
+
agentRepos: ["acme/b", "other/x", "ACME/a", "acme-two/c"],
|
|
54
|
+
});
|
|
55
|
+
expect(plan.repos).toEqual(["ACME/a", "acme/b"]);
|
|
56
|
+
});
|
|
57
|
+
it("is empty when the agent has no repos in the org", () => {
|
|
58
|
+
expect(
|
|
59
|
+
planOrgUninstall({ org: "acme", agentRepos: ["o/x"] }).repos,
|
|
60
|
+
).toEqual([]);
|
|
61
|
+
});
|
|
62
|
+
it("needs confirmation only above the removal cap", () => {
|
|
63
|
+
const mk = (n: number) => Array.from({ length: n }, (_, i) => `acme/r${i}`);
|
|
64
|
+
expect(
|
|
65
|
+
planOrgUninstall({ org: "acme", agentRepos: mk(10) }).needsConfirm,
|
|
66
|
+
).toBe(false);
|
|
67
|
+
expect(
|
|
68
|
+
planOrgUninstall({ org: "acme", agentRepos: mk(11) }).needsConfirm,
|
|
69
|
+
).toBe(true);
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
describe("labels", () => {
|
|
74
|
+
it("exposes the audit label and block reason", () => {
|
|
75
|
+
expect(GITHUB_UNINSTALL_AUDIT_PLAN_HASH).toBe("uninstall:org");
|
|
76
|
+
expect(uninstallBlockReason("acme/a")).toContain("acme/a");
|
|
77
|
+
});
|
|
78
|
+
});
|
|
@@ -83,6 +83,17 @@ export interface AppInstallationPort {
|
|
|
83
83
|
repos: readonly string[],
|
|
84
84
|
identity?: AppIdentity,
|
|
85
85
|
): Promise<void>;
|
|
86
|
+
/**
|
|
87
|
+
* Switch the installation from "all" to "selected" with exactly `repos`
|
|
88
|
+
* (owner/repo, all in `org`, at most 50). Optional so existing
|
|
89
|
+
* implementations stay valid.
|
|
90
|
+
*/
|
|
91
|
+
setRepositorySelection?(
|
|
92
|
+
appId: number,
|
|
93
|
+
org: string,
|
|
94
|
+
repos: readonly string[],
|
|
95
|
+
identity?: AppIdentity,
|
|
96
|
+
): Promise<void>;
|
|
86
97
|
}
|
|
87
98
|
|
|
88
99
|
/** Repos changed by one audited change, on the installation and in the agent's config. */
|
|
@@ -91,6 +102,8 @@ export interface InstallationAuditDiff {
|
|
|
91
102
|
installationRemoves: string[];
|
|
92
103
|
configAdds: string[];
|
|
93
104
|
configRemoves: string[];
|
|
105
|
+
/** "all" installations switched to "selected", with the repos that landed. */
|
|
106
|
+
selectionSwitches?: { org: string; repos: string[] }[];
|
|
94
107
|
}
|
|
95
108
|
|
|
96
109
|
export interface InstallationAuditEntry {
|
|
@@ -214,6 +227,13 @@ export interface InstallationRepoClient {
|
|
|
214
227
|
names: readonly string[],
|
|
215
228
|
accountId?: string | null,
|
|
216
229
|
): Promise<unknown>;
|
|
230
|
+
/** Optional: needed only to switch an "all" installation to "selected". */
|
|
231
|
+
setRepositorySelection?(
|
|
232
|
+
org: string,
|
|
233
|
+
installationId: number,
|
|
234
|
+
input: { repositorySelection: "selected"; repositories: readonly string[] },
|
|
235
|
+
accountId?: string | null,
|
|
236
|
+
): Promise<unknown>;
|
|
217
237
|
}
|
|
218
238
|
|
|
219
239
|
/** The agent App has no installation on the org (distinct from a GitHub failure). */
|
|
@@ -274,6 +294,7 @@ export function createInstallationPort(
|
|
|
274
294
|
}
|
|
275
295
|
return found.id;
|
|
276
296
|
};
|
|
297
|
+
const setSelection = client.setRepositorySelection?.bind(client);
|
|
277
298
|
return {
|
|
278
299
|
async listRepos(appId, orgs, identity) {
|
|
279
300
|
const out: Record<string, string[]> = {};
|
|
@@ -303,5 +324,25 @@ export function createInstallationPort(
|
|
|
303
324
|
identity?.accountId,
|
|
304
325
|
);
|
|
305
326
|
},
|
|
327
|
+
...(setSelection
|
|
328
|
+
? {
|
|
329
|
+
async setRepositorySelection(
|
|
330
|
+
appId: number,
|
|
331
|
+
org: string,
|
|
332
|
+
repos: readonly string[],
|
|
333
|
+
identity?: AppIdentity,
|
|
334
|
+
) {
|
|
335
|
+
await setSelection(
|
|
336
|
+
org,
|
|
337
|
+
await installationId(appId, org, identity),
|
|
338
|
+
{
|
|
339
|
+
repositorySelection: "selected",
|
|
340
|
+
repositories: repos.map(baseName),
|
|
341
|
+
},
|
|
342
|
+
identity?.accountId,
|
|
343
|
+
);
|
|
344
|
+
},
|
|
345
|
+
}
|
|
346
|
+
: {}),
|
|
306
347
|
};
|
|
307
348
|
}
|