@app-vitals/agent-admin 1.110.0 → 1.111.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 +0 -193
- package/openapi.platform.json +1 -238
- package/package.json +2 -2
- package/src/admin-boot.ts +6 -5
- package/src/admin-ui-github-identity-pages.ts +35 -139
- package/src/admin-ui-github-identity-pages.unit.test.ts +50 -173
- package/src/admin-ui-github-identity.smoke.test.ts +103 -63
- package/src/admin-ui-installation-repos-pages.ts +2 -23
- package/src/admin-ui-installation-repos-pages.unit.test.ts +41 -36
- package/src/admin-ui-installation-repos.smoke.test.ts +9 -8
- package/src/admin-ui-pages.ts +4 -5
- package/src/admin-ui.ts +51 -36
- package/src/agents-api.smoke.test.ts +26 -315
- package/src/agents-api.ts +1 -107
- package/src/github-access-preview.ts +2 -2
- package/src/github-enterprise-api.smoke.test.ts +26 -0
- package/src/github-enterprise-api.ts +18 -2
- package/src/github-enterprise-client.integration.test.ts +0 -41
- package/src/github-enterprise-client.ts +0 -25
- package/src/github-enterprise-client.unit.test.ts +0 -18
- package/src/github-installation-wiring.ts +18 -12
- package/src/github-sync-all-repos.unit.test.ts +116 -0
- package/src/github-sync-snapshot-refresh.integration.test.ts +168 -0
- package/src/github-sync-snapshot-refresh.unit.test.ts +144 -0
- package/src/github-sync.ts +103 -137
- package/src/github-sync.unit.test.ts +82 -1
- package/src/installation-ports.ts +1 -40
- package/src/reconcile-open-work.integration.test.ts +0 -1
- package/src/github-access-preview.integration.test.ts +0 -399
- package/src/github-sync-switch.integration.test.ts +0 -297
- package/src/github-sync-switch.unit.test.ts +0 -467
package/src/github-sync.ts
CHANGED
|
@@ -20,14 +20,11 @@
|
|
|
20
20
|
* MAX_UNCONFIRMED_REMOVALS removals need confirmRemovals.
|
|
21
21
|
*
|
|
22
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,
|
|
24
|
-
*
|
|
25
|
-
*
|
|
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".
|
|
23
|
+
* crawl cache; unknown is never "all") cannot take add/remove calls, and
|
|
24
|
+
* switching it in-app fails on large orgs (GitHub HTTP 400, 100 implicit
|
|
25
|
+
* removals max). Sync skips it with ALL_REPOS_INSTALLATION_REASON.
|
|
29
26
|
*
|
|
30
|
-
* Order:
|
|
27
|
+
* Order: installation adds, installation removals,
|
|
31
28
|
* agent-repo removals, one audit entry, then the agent's open work on the
|
|
32
29
|
* removed repos is blocked through the optional OpenWorkPort (a port failure is
|
|
33
30
|
* a warning).
|
|
@@ -94,21 +91,9 @@ export const NOT_PRESENT_REASON =
|
|
|
94
91
|
export const ARCHIVED_REASON =
|
|
95
92
|
"the repo is archived (read-only), so it is not added to the installation";
|
|
96
93
|
|
|
97
|
-
/**
|
|
98
|
-
export const
|
|
99
|
-
|
|
100
|
-
export const EMPTY_SWITCH_TARGET_REASON =
|
|
101
|
-
"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";
|
|
102
|
-
|
|
103
|
-
/** The installer App's permission a selection switch needs. */
|
|
104
|
-
export const SWITCH_PERMISSION_MESSAGE = (org: string): string =>
|
|
105
|
-
`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`;
|
|
106
|
-
|
|
107
|
-
/** An "all" installation Sync switches to "selected" with this repo list. */
|
|
108
|
-
export interface SelectionSwitch {
|
|
109
|
-
org: string;
|
|
110
|
-
repos: string[];
|
|
111
|
-
}
|
|
94
|
+
/** Shown for an org whose agent installation is on All repositories. */
|
|
95
|
+
export const ALL_REPOS_INSTALLATION_REASON =
|
|
96
|
+
"Sync does not work when the app has access to all repositories; uninstall the app and install again with selected repositories.";
|
|
112
97
|
|
|
113
98
|
export interface SyncLists {
|
|
114
99
|
appAdds: readonly string[];
|
|
@@ -158,8 +143,6 @@ export interface GithubSyncPlan {
|
|
|
158
143
|
agentRemoves: string[];
|
|
159
144
|
/** Every repo removed from either place, sorted. */
|
|
160
145
|
removedRepos: string[];
|
|
161
|
-
/** "All" installations to switch to "selected" first (additive). */
|
|
162
|
-
selectionSwitches?: SelectionSwitch[];
|
|
163
146
|
skippedOrgs: OrgSkip[];
|
|
164
147
|
notApplied: NotAppliedRepo[];
|
|
165
148
|
warnings: string[];
|
|
@@ -176,43 +159,12 @@ function skippedPlan(reason: string): GithubSyncPlan {
|
|
|
176
159
|
appRemoves: [],
|
|
177
160
|
agentRemoves: [],
|
|
178
161
|
removedRepos: [],
|
|
179
|
-
selectionSwitches: [],
|
|
180
162
|
skippedOrgs: [],
|
|
181
163
|
notApplied: [],
|
|
182
164
|
warnings: [],
|
|
183
165
|
};
|
|
184
166
|
}
|
|
185
167
|
|
|
186
|
-
/**
|
|
187
|
-
* Pure: the orgs on "all" and the "selected" list each switches to (installed
|
|
188
|
-
* repos minus `appRemoves`). An org with an empty target is returned in
|
|
189
|
-
* `emptyOrgs` instead. Only orgs `eligible` returns true for are considered.
|
|
190
|
-
*/
|
|
191
|
-
export function planSelectionSwitches(input: {
|
|
192
|
-
installationRepos: Readonly<Record<string, readonly string[]>>;
|
|
193
|
-
selections?: GithubSyncPlanInput["selections"];
|
|
194
|
-
appRemoves: readonly string[];
|
|
195
|
-
eligible?: (org: string) => boolean;
|
|
196
|
-
}): { switches: SelectionSwitch[]; emptyOrgs: string[] } {
|
|
197
|
-
const selections = new Map(
|
|
198
|
-
Object.entries(input.selections ?? {}).map(([o, v]) => [lower(o), v]),
|
|
199
|
-
);
|
|
200
|
-
const removed = new Set(input.appRemoves.map(lower));
|
|
201
|
-
const switches: SelectionSwitch[] = [];
|
|
202
|
-
const emptyOrgs: string[] = [];
|
|
203
|
-
for (const [org, repos] of Object.entries(input.installationRepos)) {
|
|
204
|
-
if (selections.get(lower(org)) !== "all") continue;
|
|
205
|
-
if (input.eligible && !input.eligible(org)) continue;
|
|
206
|
-
const target = sorted(repos.filter((r) => !removed.has(lower(r))));
|
|
207
|
-
if (target.length === 0) emptyOrgs.push(org);
|
|
208
|
-
else switches.push({ org, repos: target });
|
|
209
|
-
}
|
|
210
|
-
return {
|
|
211
|
-
switches: switches.sort((a, b) => a.org.localeCompare(b.org)),
|
|
212
|
-
emptyOrgs: emptyOrgs.sort(),
|
|
213
|
-
};
|
|
214
|
-
}
|
|
215
|
-
|
|
216
168
|
/** Pure: the installation adds and removes and agent-repo removes to sync. */
|
|
217
169
|
export function planGithubSync(input: GithubSyncPlanInput): GithubSyncPlan {
|
|
218
170
|
const skip = accessSkipReason(input.username, input.access);
|
|
@@ -248,6 +200,16 @@ export function planGithubSync(input: GithubSyncPlanInput): GithubSyncPlan {
|
|
|
248
200
|
: [...configured.values()];
|
|
249
201
|
|
|
250
202
|
const skipReasons = new Map<string, string>();
|
|
203
|
+
// Orgs whose installation is on "all" (unknown is never "all"): always reported.
|
|
204
|
+
const selections = new Map(
|
|
205
|
+
Object.entries(input.selections ?? {}).map(([o, v]) => [lower(o), v]),
|
|
206
|
+
);
|
|
207
|
+
const allOrgs = new Map<string, string>();
|
|
208
|
+
for (const org of Object.keys(input.installationRepos)) {
|
|
209
|
+
if (selections.get(lower(org)) !== "all") continue;
|
|
210
|
+
allOrgs.set(lower(org), org);
|
|
211
|
+
skipReasons.set(org, ALL_REPOS_INSTALLATION_REASON);
|
|
212
|
+
}
|
|
251
213
|
const skipDetails = new Map<string, OrgSkipDetail>();
|
|
252
214
|
const notApplied = new Map<string, NotAppliedRepo>();
|
|
253
215
|
const leave = (repo: string, reason: string) => {
|
|
@@ -257,6 +219,11 @@ export function planGithubSync(input: GithubSyncPlanInput): GithubSyncPlan {
|
|
|
257
219
|
// Unknown is never none: false (with the reason recorded) for a skipped org.
|
|
258
220
|
const orgUsable = (repo: string): boolean => {
|
|
259
221
|
const org = repoOrg(repo);
|
|
222
|
+
const allOrg = allOrgs.get(lower(org));
|
|
223
|
+
if (allOrg !== undefined) {
|
|
224
|
+
leave(repo, `skipped for org: ${ALL_REPOS_INSTALLATION_REASON}`);
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
260
227
|
const result = orgResults.get(lower(org));
|
|
261
228
|
const reason = orgSkipReason(result);
|
|
262
229
|
if (!reason) return true;
|
|
@@ -321,20 +288,7 @@ export function planGithubSync(input: GithubSyncPlanInput): GithubSyncPlan {
|
|
|
321
288
|
const confirmedAppRemoves = removable(appRemoveCandidates, installed);
|
|
322
289
|
const agentRemoves = removable(agentRemoveCandidates, configured);
|
|
323
290
|
|
|
324
|
-
const
|
|
325
|
-
installationRepos: input.installationRepos,
|
|
326
|
-
...(input.selections ? { selections: input.selections } : {}),
|
|
327
|
-
appRemoves: [...confirmedAppRemoves],
|
|
328
|
-
eligible: (o) => !orgSkipReason(orgResults.get(lower(o))),
|
|
329
|
-
});
|
|
330
|
-
// An empty target leaves the org on "all", where removes cannot be applied.
|
|
331
|
-
const emptySet = new Set(emptyOrgs.map(lower));
|
|
332
|
-
const appRemoves = new Set<string>();
|
|
333
|
-
for (const r of confirmedAppRemoves) {
|
|
334
|
-
if (emptySet.has(lower(repoOrg(r)))) leave(r, EMPTY_SWITCH_TARGET_REASON);
|
|
335
|
-
else appRemoves.add(r);
|
|
336
|
-
}
|
|
337
|
-
for (const o of emptyOrgs) skipReasons.set(o, EMPTY_SWITCH_TARGET_REASON);
|
|
291
|
+
const appRemoves = confirmedAppRemoves;
|
|
338
292
|
|
|
339
293
|
const skippedOrgs = sorted(skipReasons.keys()).map((org) => ({
|
|
340
294
|
org,
|
|
@@ -347,7 +301,6 @@ export function planGithubSync(input: GithubSyncPlanInput): GithubSyncPlan {
|
|
|
347
301
|
appRemoves: sorted(appRemoves),
|
|
348
302
|
agentRemoves: sorted(agentRemoves),
|
|
349
303
|
removedRepos: sorted(new Set([...appRemoves, ...agentRemoves])),
|
|
350
|
-
selectionSwitches: switches,
|
|
351
304
|
skippedOrgs,
|
|
352
305
|
notApplied: [...notApplied.values()].sort((a, b) =>
|
|
353
306
|
a.repo.localeCompare(b.repo),
|
|
@@ -403,13 +356,22 @@ export interface GithubSyncDeps {
|
|
|
403
356
|
) => Promise<boolean | undefined>;
|
|
404
357
|
installation: Pick<
|
|
405
358
|
InstallationReposPort,
|
|
406
|
-
"listInstalled" | "addRepos" | "removeRepos"
|
|
359
|
+
"listInstalled" | "addRepos" | "removeRepos"
|
|
407
360
|
>;
|
|
408
361
|
audit: InstallationAuditPort;
|
|
409
362
|
/** Optional: with none, nothing is listed or blocked. */
|
|
410
363
|
openWork?: OpenWorkPort;
|
|
411
364
|
/** Optional: which Apps are usable; without both, Sync is skipped with a specific reason. */
|
|
412
365
|
appsState?: GithubAppsStateFn;
|
|
366
|
+
/**
|
|
367
|
+
* Optional: re-crawl one org's agent-App snapshot (the cache the admin UI
|
|
368
|
+
* lists). Called after apply for each org it changed; errors are logged and
|
|
369
|
+
* never fail the apply.
|
|
370
|
+
*/
|
|
371
|
+
refreshAgentSnapshot?: (
|
|
372
|
+
agent: InstallationAgentRecord,
|
|
373
|
+
org: string,
|
|
374
|
+
) => Promise<void>;
|
|
413
375
|
}
|
|
414
376
|
|
|
415
377
|
export type GithubSyncPreviewResult =
|
|
@@ -450,22 +412,13 @@ export type GithubSyncApplyResult =
|
|
|
450
412
|
}
|
|
451
413
|
| { status: "failed"; plan: GithubSyncPlan; auditId: string; error: string };
|
|
452
414
|
|
|
453
|
-
/** A plan plus the installation context
|
|
415
|
+
/** A plan plus the installation context it was computed from. */
|
|
454
416
|
interface PlannedSync {
|
|
455
417
|
plan: GithubSyncPlan;
|
|
456
418
|
installationRepos: Record<string, string[]>;
|
|
457
419
|
selections?: Record<string, "all" | "selected" | null>;
|
|
458
420
|
}
|
|
459
421
|
|
|
460
|
-
/** A 403 on a selection switch becomes a message naming the missing permission. */
|
|
461
|
-
function switchError(org: string, err: unknown): Error {
|
|
462
|
-
const e = err as { kind?: string; status?: number } | undefined;
|
|
463
|
-
if (e?.kind === "permission" || (e?.status === 403 && e.kind === undefined)) {
|
|
464
|
-
return new Error(SWITCH_PERMISSION_MESSAGE(org));
|
|
465
|
-
}
|
|
466
|
-
return err instanceof Error ? err : new Error(String(err));
|
|
467
|
-
}
|
|
468
|
-
|
|
469
422
|
function groupByOrg(repos: readonly string[]): Map<string, string[]> {
|
|
470
423
|
const byOrg = new Map<string, string[]>();
|
|
471
424
|
for (const r of repos) {
|
|
@@ -478,6 +431,50 @@ function groupByOrg(repos: readonly string[]): Map<string, string[]> {
|
|
|
478
431
|
export class GithubSyncService {
|
|
479
432
|
constructor(private readonly deps: GithubSyncDeps) {}
|
|
480
433
|
|
|
434
|
+
/**
|
|
435
|
+
* Repo names (not owner/name) the bound user can push to, per org, for the
|
|
436
|
+
* org Install picker. Cache only: it never calls GitHub. An org with no
|
|
437
|
+
* completed cached crawl, or no ok push row, is absent (unknown is never
|
|
438
|
+
* none). Returns {} when there is no cache, lookup source or bound user.
|
|
439
|
+
*/
|
|
440
|
+
async installRepoChoices(
|
|
441
|
+
agentId: string,
|
|
442
|
+
orgs: readonly string[],
|
|
443
|
+
): Promise<Record<string, string[]>> {
|
|
444
|
+
const { cache, lookupRepos } = this.deps;
|
|
445
|
+
if (!cache || !lookupRepos) return {};
|
|
446
|
+
const agent = await this.deps.agents.get(agentId);
|
|
447
|
+
const username = agent?.githubUsername?.trim();
|
|
448
|
+
if (!agent || !username) return {};
|
|
449
|
+
const lookup = await lookupRepos(agent.accountId);
|
|
450
|
+
const out: Record<string, string[]> = {};
|
|
451
|
+
for (const org of orgs) {
|
|
452
|
+
const lk = lookup.filter(
|
|
453
|
+
(l) => l.org.toLowerCase() === org.toLowerCase() && l.completedAt,
|
|
454
|
+
);
|
|
455
|
+
const names = [
|
|
456
|
+
...new Set(
|
|
457
|
+
lk.flatMap((l) => l.repos).map((r) => r.slice(r.indexOf("/") + 1)),
|
|
458
|
+
),
|
|
459
|
+
];
|
|
460
|
+
if (names.length === 0) continue;
|
|
461
|
+
const rows = await getAccessRowsAcrossKeys(cache, {
|
|
462
|
+
accountId: agent.accountId,
|
|
463
|
+
org,
|
|
464
|
+
username,
|
|
465
|
+
repos: names,
|
|
466
|
+
});
|
|
467
|
+
const pushable = new Set(
|
|
468
|
+
rows
|
|
469
|
+
.filter((r) => r.status === "ok" && r.canPush)
|
|
470
|
+
.map((r) => r.repo.toLowerCase()),
|
|
471
|
+
);
|
|
472
|
+
const picked = names.filter((n) => pushable.has(n.toLowerCase()));
|
|
473
|
+
if (picked.length > 0) out[org] = picked.sort();
|
|
474
|
+
}
|
|
475
|
+
return out;
|
|
476
|
+
}
|
|
477
|
+
|
|
481
478
|
/** Read-only: what would change and the open work the removals would block. */
|
|
482
479
|
async preview(agentId: string): Promise<GithubSyncPreviewResult> {
|
|
483
480
|
const agent = await this.deps.agents.get(agentId);
|
|
@@ -515,12 +512,8 @@ export class GithubSyncService {
|
|
|
515
512
|
...(plan.appsState ? { appsState: plan.appsState } : {}),
|
|
516
513
|
};
|
|
517
514
|
}
|
|
518
|
-
plan = await this.reverifyRemovals(plan, agent
|
|
519
|
-
|
|
520
|
-
if (
|
|
521
|
-
plan.appAdds.length + plan.removedRepos.length + switches.length ===
|
|
522
|
-
0
|
|
523
|
-
) {
|
|
515
|
+
plan = await this.reverifyRemovals(plan, agent);
|
|
516
|
+
if (plan.appAdds.length + plan.removedRepos.length === 0) {
|
|
524
517
|
return { status: "noop", plan };
|
|
525
518
|
}
|
|
526
519
|
const removals = plan.removedRepos.length;
|
|
@@ -532,49 +525,14 @@ export class GithubSyncService {
|
|
|
532
525
|
const identity = identityOf(agent);
|
|
533
526
|
const added: string[] = [];
|
|
534
527
|
const removed: string[] = [];
|
|
535
|
-
const switchRecords: SelectionSwitch[] = [];
|
|
536
|
-
const switchedOrgs = new Set(switches.map((s) => lower(s.org)));
|
|
537
528
|
let configDone = false;
|
|
538
529
|
let error: string | undefined;
|
|
539
530
|
try {
|
|
540
|
-
for (const sw of switches) {
|
|
541
|
-
const record: SelectionSwitch = { org: sw.org, repos: [] };
|
|
542
|
-
const head = sw.repos.slice(0, MAX_SELECTION_PATCH_REPOS);
|
|
543
|
-
const rest = sw.repos.slice(MAX_SELECTION_PATCH_REPOS);
|
|
544
|
-
const setSelection = this.deps.installation.setRepositorySelection;
|
|
545
|
-
if (!setSelection) {
|
|
546
|
-
throw new Error(
|
|
547
|
-
`cannot switch ${sw.org} to selected repositories: the installation port does not support it`,
|
|
548
|
-
);
|
|
549
|
-
}
|
|
550
|
-
try {
|
|
551
|
-
await setSelection.call(
|
|
552
|
-
this.deps.installation,
|
|
553
|
-
appId,
|
|
554
|
-
sw.org,
|
|
555
|
-
head,
|
|
556
|
-
identity,
|
|
557
|
-
);
|
|
558
|
-
} catch (err) {
|
|
559
|
-
throw switchError(sw.org, err);
|
|
560
|
-
}
|
|
561
|
-
// The switch landed: the org's confirmed removes are now off the installation.
|
|
562
|
-
record.repos = head;
|
|
563
|
-
switchRecords.push(record);
|
|
564
|
-
removed.push(
|
|
565
|
-
...plan.appRemoves.filter((r) => lower(repoOrg(r)) === lower(sw.org)),
|
|
566
|
-
);
|
|
567
|
-
if (rest.length > 0) {
|
|
568
|
-
await this.deps.installation.addRepos(appId, sw.org, rest, identity);
|
|
569
|
-
record.repos = sw.repos;
|
|
570
|
-
}
|
|
571
|
-
}
|
|
572
531
|
for (const [org, repos] of groupByOrg(plan.appAdds)) {
|
|
573
532
|
await this.deps.installation.addRepos(appId, org, repos, identity);
|
|
574
533
|
added.push(...repos);
|
|
575
534
|
}
|
|
576
535
|
for (const [org, repos] of groupByOrg(plan.appRemoves)) {
|
|
577
|
-
if (switchedOrgs.has(lower(org))) continue;
|
|
578
536
|
await this.deps.installation.removeRepos(appId, org, repos, identity);
|
|
579
537
|
removed.push(...repos);
|
|
580
538
|
}
|
|
@@ -599,15 +557,15 @@ export class GithubSyncService {
|
|
|
599
557
|
installationRemoves: removed,
|
|
600
558
|
configAdds: [],
|
|
601
559
|
configRemoves: configDone ? plan.agentRemoves : [],
|
|
602
|
-
...(switchRecords.length > 0
|
|
603
|
-
? { selectionSwitches: switchRecords }
|
|
604
|
-
: {}),
|
|
605
560
|
},
|
|
606
561
|
result:
|
|
607
562
|
error === undefined
|
|
608
563
|
? { status: "applied" }
|
|
609
564
|
: { status: "failed", error },
|
|
610
565
|
});
|
|
566
|
+
// Refresh the cached snapshot for every org that changed, even after a
|
|
567
|
+
// partial failure (some changes may have landed).
|
|
568
|
+
await this.refreshChangedOrgs(agent, [...added, ...removed]);
|
|
611
569
|
if (error !== undefined) {
|
|
612
570
|
return { status: "failed", plan, auditId, error };
|
|
613
571
|
}
|
|
@@ -620,11 +578,30 @@ export class GithubSyncService {
|
|
|
620
578
|
return { status: "applied", plan, auditId, warnings };
|
|
621
579
|
}
|
|
622
580
|
|
|
581
|
+
/** Targeted per-org snapshot refresh; a failure is logged, never thrown. */
|
|
582
|
+
private async refreshChangedOrgs(
|
|
583
|
+
agent: InstallationAgentRecord,
|
|
584
|
+
repos: readonly string[],
|
|
585
|
+
): Promise<void> {
|
|
586
|
+
const refresh = this.deps.refreshAgentSnapshot;
|
|
587
|
+
if (!refresh) return;
|
|
588
|
+
const orgs = new Map<string, string>();
|
|
589
|
+
for (const r of repos) orgs.set(lower(repoOrg(r)), repoOrg(r));
|
|
590
|
+
for (const org of orgs.values()) {
|
|
591
|
+
try {
|
|
592
|
+
await refresh(agent, org);
|
|
593
|
+
} catch (err) {
|
|
594
|
+
console.error(
|
|
595
|
+
`[github-sync] snapshot refresh for ${org} failed: ${err instanceof Error ? err.message : String(err)}`,
|
|
596
|
+
);
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
|
|
623
601
|
/** Drops every removal the live permission call does not confirm (unknown is never none). */
|
|
624
602
|
private async reverifyRemovals(
|
|
625
603
|
plan: GithubSyncPlan,
|
|
626
604
|
agent: InstallationAgentRecord,
|
|
627
|
-
planned: PlannedSync,
|
|
628
605
|
): Promise<GithubSyncPlan> {
|
|
629
606
|
const verify = this.deps.verifyPush;
|
|
630
607
|
const username = agent.githubUsername?.trim();
|
|
@@ -647,21 +624,10 @@ export class GithubSyncService {
|
|
|
647
624
|
const keep = (repos: string[]) => repos.filter((r) => !dropped.has(r));
|
|
648
625
|
const appRemoves = keep(plan.appRemoves);
|
|
649
626
|
const agentRemoves = keep(plan.agentRemoves);
|
|
650
|
-
// A removal the user can still push to goes back on the switch target.
|
|
651
|
-
const switchOrgs = new Set(
|
|
652
|
-
(plan.selectionSwitches ?? []).map((s) => lower(s.org)),
|
|
653
|
-
);
|
|
654
|
-
const { switches } = planSelectionSwitches({
|
|
655
|
-
installationRepos: planned.installationRepos,
|
|
656
|
-
...(planned.selections ? { selections: planned.selections } : {}),
|
|
657
|
-
appRemoves,
|
|
658
|
-
eligible: (o) => switchOrgs.has(lower(o)),
|
|
659
|
-
});
|
|
660
627
|
return {
|
|
661
628
|
...plan,
|
|
662
629
|
appRemoves,
|
|
663
630
|
agentRemoves,
|
|
664
|
-
selectionSwitches: switches,
|
|
665
631
|
removedRepos: sorted(new Set([...appRemoves, ...agentRemoves])),
|
|
666
632
|
notApplied: [
|
|
667
633
|
...plan.notApplied,
|
|
@@ -279,7 +279,9 @@ describe("planGithubSync (archived repos)", () => {
|
|
|
279
279
|
}),
|
|
280
280
|
);
|
|
281
281
|
expect(p.skippedOrgs).toEqual([]);
|
|
282
|
-
expect(p.notApplied).toEqual([
|
|
282
|
+
expect(p.notApplied).toEqual([
|
|
283
|
+
{ repo: "other/old", reason: ARCHIVED_REASON },
|
|
284
|
+
]);
|
|
283
285
|
});
|
|
284
286
|
|
|
285
287
|
test("an installed archived repo in an unchecked org is kept, not removed", () => {
|
|
@@ -863,3 +865,82 @@ describe("skippedOrgs detail", () => {
|
|
|
863
865
|
});
|
|
864
866
|
});
|
|
865
867
|
});
|
|
868
|
+
|
|
869
|
+
describe("GithubSyncService.installRepoChoices", () => {
|
|
870
|
+
const svc = (username: string | undefined, calls: string[]) =>
|
|
871
|
+
new GithubSyncService({
|
|
872
|
+
agents: {
|
|
873
|
+
get: async () => ({
|
|
874
|
+
id: "ag",
|
|
875
|
+
githubUsername: username ?? null,
|
|
876
|
+
appId: 7,
|
|
877
|
+
repos: [],
|
|
878
|
+
}),
|
|
879
|
+
setRepos: async () => {},
|
|
880
|
+
},
|
|
881
|
+
access: async () => {
|
|
882
|
+
throw new Error("live resolver must not be used");
|
|
883
|
+
},
|
|
884
|
+
installation: {} as never,
|
|
885
|
+
audit: {} as never,
|
|
886
|
+
cache: {
|
|
887
|
+
listAgentSnapshots: async () => [],
|
|
888
|
+
getAccessRows: async (q: { repos: string[] }) => {
|
|
889
|
+
calls.push(q.repos.join(","));
|
|
890
|
+
return q.repos.map((r) => ({
|
|
891
|
+
repo: r,
|
|
892
|
+
status: "ok",
|
|
893
|
+
canPush: r !== "ro",
|
|
894
|
+
}));
|
|
895
|
+
},
|
|
896
|
+
} as never,
|
|
897
|
+
lookupRepos: async () => [
|
|
898
|
+
{
|
|
899
|
+
org: "acme",
|
|
900
|
+
completedAt: new Date(),
|
|
901
|
+
repos: ["acme/web", "acme/ro", "acme/api"],
|
|
902
|
+
},
|
|
903
|
+
{ org: "beta", completedAt: null, repos: ["beta/x"] },
|
|
904
|
+
],
|
|
905
|
+
});
|
|
906
|
+
|
|
907
|
+
it("returns sorted pushable repo names; skips incomplete orgs", async () => {
|
|
908
|
+
const calls: string[] = [];
|
|
909
|
+
expect(
|
|
910
|
+
await svc("octo", calls).installRepoChoices("ag", ["acme", "beta"]),
|
|
911
|
+
).toEqual({
|
|
912
|
+
acme: ["api", "web"],
|
|
913
|
+
});
|
|
914
|
+
});
|
|
915
|
+
|
|
916
|
+
it("returns nothing without a bound user", async () => {
|
|
917
|
+
expect(await svc(undefined, []).installRepoChoices("ag", ["acme"])).toEqual(
|
|
918
|
+
{},
|
|
919
|
+
);
|
|
920
|
+
});
|
|
921
|
+
});
|
|
922
|
+
|
|
923
|
+
describe("GithubSyncService.preview with an incomplete apps state", () => {
|
|
924
|
+
it("skips with the apps-state reason and plans nothing", async () => {
|
|
925
|
+
const svc = new GithubSyncService({
|
|
926
|
+
agents: {
|
|
927
|
+
get: async () => ({
|
|
928
|
+
id: "ag",
|
|
929
|
+
accountId: "acct",
|
|
930
|
+
githubUsername: "octo",
|
|
931
|
+
appId: 7,
|
|
932
|
+
repos: [],
|
|
933
|
+
}),
|
|
934
|
+
setRepos: async () => {},
|
|
935
|
+
},
|
|
936
|
+
access: async () => {
|
|
937
|
+
throw new Error("live resolver must not be used");
|
|
938
|
+
},
|
|
939
|
+
installation: {} as never,
|
|
940
|
+
audit: {} as never,
|
|
941
|
+
appsState: async () => "lookup_only",
|
|
942
|
+
} as never);
|
|
943
|
+
const r = await svc.preview("ag");
|
|
944
|
+
expect(r).toMatchObject({ status: "skipped", appsState: "lookup_only" });
|
|
945
|
+
});
|
|
946
|
+
});
|
|
@@ -83,17 +83,6 @@ 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>;
|
|
97
86
|
}
|
|
98
87
|
|
|
99
88
|
/** Repos changed by one audited change, on the installation and in the agent's config. */
|
|
@@ -102,7 +91,7 @@ export interface InstallationAuditDiff {
|
|
|
102
91
|
installationRemoves: string[];
|
|
103
92
|
configAdds: string[];
|
|
104
93
|
configRemoves: string[];
|
|
105
|
-
/**
|
|
94
|
+
/** Legacy: old audit rows only; Sync no longer switches installations. Keep optional so they still read. */
|
|
106
95
|
selectionSwitches?: { org: string; repos: string[] }[];
|
|
107
96
|
}
|
|
108
97
|
|
|
@@ -227,13 +216,6 @@ export interface InstallationRepoClient {
|
|
|
227
216
|
names: readonly string[],
|
|
228
217
|
accountId?: string | null,
|
|
229
218
|
): 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>;
|
|
237
219
|
}
|
|
238
220
|
|
|
239
221
|
/** The agent App has no installation on the org (distinct from a GitHub failure). */
|
|
@@ -294,7 +276,6 @@ export function createInstallationPort(
|
|
|
294
276
|
}
|
|
295
277
|
return found.id;
|
|
296
278
|
};
|
|
297
|
-
const setSelection = client.setRepositorySelection?.bind(client);
|
|
298
279
|
return {
|
|
299
280
|
async listRepos(appId, orgs, identity) {
|
|
300
281
|
const out: Record<string, string[]> = {};
|
|
@@ -324,25 +305,5 @@ export function createInstallationPort(
|
|
|
324
305
|
identity?.accountId,
|
|
325
306
|
);
|
|
326
307
|
},
|
|
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
|
-
: {}),
|
|
347
308
|
};
|
|
348
309
|
}
|
|
@@ -137,7 +137,6 @@ describeOrSkip("reconcile-open-work (integration)", () => {
|
|
|
137
137
|
calls.push("removeRepos");
|
|
138
138
|
if (removeFails) throw new Error("installation PATCH failed");
|
|
139
139
|
},
|
|
140
|
-
setRepositorySelection: async () => {},
|
|
141
140
|
},
|
|
142
141
|
audit: new PrismaInstallationAudit(prisma),
|
|
143
142
|
...(openWork ? { openWork } : {}),
|