@app-vitals/agent-admin 1.35.1 → 1.36.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.platform.json +55 -295
- package/package.json +2 -2
- package/src/admin-boot.ts +3 -3
- package/src/admin-extension.ts +1 -1
- package/src/admin-ui-github-identity.smoke.test.ts +24 -2
- package/src/admin-ui-installation-repos-pages.ts +198 -231
- package/src/admin-ui-installation-repos-pages.unit.test.ts +199 -0
- package/src/admin-ui-installation-repos.smoke.test.ts +166 -172
- package/src/admin-ui-pages.ts +2 -2
- package/src/admin-ui.ts +103 -89
- package/src/agents-api.smoke.test.ts +193 -290
- package/src/agents-api.ts +83 -210
- package/src/github-installation-wiring.ts +10 -12
- package/src/github-installation-wiring.unit.test.ts +13 -9
- package/src/github-installation.fixtures.ts +1 -1
- package/src/github-sync.ts +407 -0
- package/src/{lost-access-removal.unit.test.ts → github-sync.unit.test.ts} +214 -109
- package/src/installation-ports.ts +1 -1
- package/src/installation-repos-service.ts +3 -98
- package/src/installation-repos-service.unit.test.ts +5 -90
- package/src/reconcile-open-work.ts +1 -1
- package/src/repo-access-rules.ts +1 -1
- package/src/lost-access-removal.ts +0 -333
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* admin/src/installation-repos-service.ts
|
|
3
|
-
*
|
|
4
|
-
* (IRM-1.2)
|
|
5
|
-
* installer App through the installation port
|
|
6
|
-
* add/remove to the existing GitHubReconcileAudit table (no schema change):
|
|
7
|
-
* `planHash` is "direct:add" / "direct:remove" and the changed repos land in
|
|
8
|
-
* the diff's installationAdds / installationRemoves.
|
|
3
|
+
* Read-only listing of the repos on an agent's own GitHub App installation
|
|
4
|
+
* (IRM-1.2), with the bound user's push hint. Changes go through the Sync flow
|
|
5
|
+
* (github-sync.ts, IRM-3.1). Uses the installer App through the installation port.
|
|
9
6
|
*/
|
|
10
7
|
|
|
11
8
|
import type { GithubAccessPreviewFn } from "./github-access-preview.ts";
|
|
@@ -13,10 +10,8 @@ import {
|
|
|
13
10
|
AppNotInstalledError,
|
|
14
11
|
MISSING_APP_ID_REASON,
|
|
15
12
|
type InstallationAgentsPort,
|
|
16
|
-
type InstallationAuditPort,
|
|
17
13
|
type AppInstallationPort,
|
|
18
14
|
} from "./installation-ports.ts";
|
|
19
|
-
import { repoOrg } from "./repo-access-rules.ts";
|
|
20
15
|
import type {
|
|
21
16
|
OrgAccessResult,
|
|
22
17
|
UserRepoAccessResult,
|
|
@@ -43,18 +38,10 @@ export type InstallationListResult =
|
|
|
43
38
|
| { status: "no_app_id"; reason: string }
|
|
44
39
|
| { status: "ok"; appId: number; orgs: InstallationOrgView[] };
|
|
45
40
|
|
|
46
|
-
export type InstallationChangeResult =
|
|
47
|
-
| { status: "agent_not_found" }
|
|
48
|
-
| { status: "no_app_id"; reason: string }
|
|
49
|
-
| { status: "not_installed"; org: string; reason: string }
|
|
50
|
-
| { status: "applied"; auditId: string; repos: string[] }
|
|
51
|
-
| { status: "failed"; auditId: string; error: string };
|
|
52
|
-
|
|
53
41
|
export interface InstallationReposDeps {
|
|
54
42
|
agents: Pick<InstallationAgentsPort, "get">;
|
|
55
43
|
access: GithubAccessPreviewFn;
|
|
56
44
|
installation: InstallationReposPort;
|
|
57
|
-
audit: InstallationAuditPort;
|
|
58
45
|
}
|
|
59
46
|
|
|
60
47
|
const orgKey = (org: string): string => org.toLowerCase();
|
|
@@ -92,15 +79,6 @@ export function userPushHints(
|
|
|
92
79
|
});
|
|
93
80
|
}
|
|
94
81
|
|
|
95
|
-
function groupByOrg(repos: readonly string[]): Map<string, string[]> {
|
|
96
|
-
const byOrg = new Map<string, string[]>();
|
|
97
|
-
for (const r of repos) {
|
|
98
|
-
const org = repoOrg(r);
|
|
99
|
-
byOrg.set(org, [...(byOrg.get(org) ?? []), r]);
|
|
100
|
-
}
|
|
101
|
-
return byOrg;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
82
|
export class InstallationReposService {
|
|
105
83
|
constructor(private readonly deps: InstallationReposDeps) {}
|
|
106
84
|
|
|
@@ -130,79 +108,6 @@ export class InstallationReposService {
|
|
|
130
108
|
})),
|
|
131
109
|
};
|
|
132
110
|
}
|
|
133
|
-
|
|
134
|
-
add(
|
|
135
|
-
agentId: string,
|
|
136
|
-
caller: string,
|
|
137
|
-
repos: readonly string[],
|
|
138
|
-
): Promise<InstallationChangeResult> {
|
|
139
|
-
return this.change("add", agentId, caller, repos);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
remove(
|
|
143
|
-
agentId: string,
|
|
144
|
-
caller: string,
|
|
145
|
-
repos: readonly string[],
|
|
146
|
-
): Promise<InstallationChangeResult> {
|
|
147
|
-
return this.change("remove", agentId, caller, repos);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
private async change(
|
|
151
|
-
kind: "add" | "remove",
|
|
152
|
-
agentId: string,
|
|
153
|
-
caller: string,
|
|
154
|
-
input: readonly string[],
|
|
155
|
-
): Promise<InstallationChangeResult> {
|
|
156
|
-
const agent = await this.deps.agents.get(agentId);
|
|
157
|
-
if (!agent) return { status: "agent_not_found" };
|
|
158
|
-
if (agent.appId === undefined) {
|
|
159
|
-
return { status: "no_app_id", reason: MISSING_APP_ID_REASON };
|
|
160
|
-
}
|
|
161
|
-
const appId = agent.appId;
|
|
162
|
-
const repos = [...new Set(input)];
|
|
163
|
-
const byOrg = groupByOrg(repos);
|
|
164
|
-
const { installation } = this.deps;
|
|
165
|
-
// Nothing is written (or audited) when an org has no installation.
|
|
166
|
-
try {
|
|
167
|
-
await installation.listRepos(appId, [...byOrg.keys()]);
|
|
168
|
-
} catch (err) {
|
|
169
|
-
if (err instanceof AppNotInstalledError) {
|
|
170
|
-
return { status: "not_installed", org: err.org, reason: err.message };
|
|
171
|
-
}
|
|
172
|
-
throw err;
|
|
173
|
-
}
|
|
174
|
-
const changed: string[] = [];
|
|
175
|
-
let error: string | undefined;
|
|
176
|
-
try {
|
|
177
|
-
for (const [org, orgRepos] of byOrg) {
|
|
178
|
-
if (kind === "add") await installation.addRepos(appId, org, orgRepos);
|
|
179
|
-
else await installation.removeRepos(appId, org, orgRepos);
|
|
180
|
-
changed.push(...orgRepos);
|
|
181
|
-
}
|
|
182
|
-
} catch (err) {
|
|
183
|
-
error = err instanceof Error ? err.message : String(err);
|
|
184
|
-
}
|
|
185
|
-
// Record what actually changed; on failure, only the repos that landed.
|
|
186
|
-
const done = error === undefined ? repos : changed;
|
|
187
|
-
const auditId = await this.deps.audit.write({
|
|
188
|
-
agentId,
|
|
189
|
-
caller,
|
|
190
|
-
planHash: `direct:${kind}`,
|
|
191
|
-
diff: {
|
|
192
|
-
installationAdds: kind === "add" ? done : [],
|
|
193
|
-
installationRemoves: kind === "remove" ? done : [],
|
|
194
|
-
configAdds: [],
|
|
195
|
-
configRemoves: [],
|
|
196
|
-
},
|
|
197
|
-
result:
|
|
198
|
-
error === undefined
|
|
199
|
-
? { status: "applied" }
|
|
200
|
-
: { status: "failed", error },
|
|
201
|
-
});
|
|
202
|
-
return error === undefined
|
|
203
|
-
? { status: "applied", auditId, repos }
|
|
204
|
-
: { status: "failed", auditId, error };
|
|
205
|
-
}
|
|
206
111
|
}
|
|
207
112
|
|
|
208
113
|
/** The slice of the installer client the installation-repos port needs. */
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* admin/src/installation-repos-service.unit.test.ts
|
|
3
|
-
* IRM-1.2: the user-can-push hint logic and the list
|
|
3
|
+
* IRM-1.2: the user-can-push hint logic and the list service, with
|
|
4
4
|
* in-memory ports.
|
|
5
5
|
*/
|
|
6
6
|
|
|
@@ -10,10 +10,6 @@ import {
|
|
|
10
10
|
type InstallationReposPort,
|
|
11
11
|
userPushHints,
|
|
12
12
|
} from "./installation-repos-service.ts";
|
|
13
|
-
import {
|
|
14
|
-
AppNotInstalledError,
|
|
15
|
-
type InstallationAuditEntry,
|
|
16
|
-
} from "./installation-ports.ts";
|
|
17
13
|
import type {
|
|
18
14
|
OrgAccessResult,
|
|
19
15
|
UserRepoAccessResult,
|
|
@@ -111,29 +107,15 @@ function build(
|
|
|
111
107
|
| { status: "unconfigured" }
|
|
112
108
|
| { status: "ok"; access: UserRepoAccessResult }
|
|
113
109
|
>;
|
|
114
|
-
failAdd?: boolean;
|
|
115
|
-
notInstalled?: string;
|
|
116
110
|
} = {},
|
|
117
111
|
) {
|
|
118
|
-
const audits: InstallationAuditEntry[] = [];
|
|
119
|
-
const calls: string[] = [];
|
|
120
112
|
const installation: InstallationReposPort = {
|
|
121
113
|
listInstalled: async () => [
|
|
122
114
|
{ org: "acme", repos: ["acme/push", "acme/unlisted"] },
|
|
123
115
|
],
|
|
124
|
-
listRepos: async (
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
}
|
|
128
|
-
return {};
|
|
129
|
-
},
|
|
130
|
-
addRepos: async (_a, org, repos) => {
|
|
131
|
-
if (opts.failAdd && org === "beta") throw new Error("boom");
|
|
132
|
-
calls.push(`add ${org} ${repos.join(",")}`);
|
|
133
|
-
},
|
|
134
|
-
removeRepos: async (_a, org, repos) => {
|
|
135
|
-
calls.push(`remove ${org} ${repos.join(",")}`);
|
|
136
|
-
},
|
|
116
|
+
listRepos: async () => ({}),
|
|
117
|
+
addRepos: async () => {},
|
|
118
|
+
removeRepos: async () => {},
|
|
137
119
|
};
|
|
138
120
|
const agent =
|
|
139
121
|
opts.agent === undefined
|
|
@@ -145,14 +127,8 @@ function build(
|
|
|
145
127
|
opts.access ??
|
|
146
128
|
(async () => ({ status: "ok", access: accessOf([orgResult()]) })),
|
|
147
129
|
installation,
|
|
148
|
-
audit: {
|
|
149
|
-
write: async (e) => {
|
|
150
|
-
audits.push(e);
|
|
151
|
-
return `audit-${audits.length}`;
|
|
152
|
-
},
|
|
153
|
-
},
|
|
154
130
|
});
|
|
155
|
-
return { svc
|
|
131
|
+
return { svc };
|
|
156
132
|
}
|
|
157
133
|
|
|
158
134
|
describe("InstallationReposService.list", () => {
|
|
@@ -207,64 +183,3 @@ describe("InstallationReposService.list", () => {
|
|
|
207
183
|
expect(r.status).toBe("no_app_id");
|
|
208
184
|
});
|
|
209
185
|
});
|
|
210
|
-
|
|
211
|
-
describe("InstallationReposService.add/remove", () => {
|
|
212
|
-
it("adds per org, dedupes, and writes one audit entry with the caller", async () => {
|
|
213
|
-
const { svc, audits, calls } = build();
|
|
214
|
-
const r = await svc.add("a1", "root", ["acme/a", "acme/a", "beta/b"]);
|
|
215
|
-
expect(r).toEqual({
|
|
216
|
-
status: "applied",
|
|
217
|
-
auditId: "audit-1",
|
|
218
|
-
repos: ["acme/a", "beta/b"],
|
|
219
|
-
});
|
|
220
|
-
expect(calls).toEqual(["add acme acme/a", "add beta beta/b"]);
|
|
221
|
-
expect(audits).toHaveLength(1);
|
|
222
|
-
expect(audits[0]).toMatchObject({
|
|
223
|
-
agentId: "a1",
|
|
224
|
-
caller: "root",
|
|
225
|
-
planHash: "direct:add",
|
|
226
|
-
diff: {
|
|
227
|
-
installationAdds: ["acme/a", "beta/b"],
|
|
228
|
-
installationRemoves: [],
|
|
229
|
-
configAdds: [],
|
|
230
|
-
configRemoves: [],
|
|
231
|
-
},
|
|
232
|
-
result: { status: "applied" },
|
|
233
|
-
});
|
|
234
|
-
});
|
|
235
|
-
|
|
236
|
-
it("remove records installationRemoves", async () => {
|
|
237
|
-
const { svc, audits } = build();
|
|
238
|
-
await svc.remove("a1", "root", ["acme/a"]);
|
|
239
|
-
expect(audits[0]?.diff.installationRemoves).toEqual(["acme/a"]);
|
|
240
|
-
expect(audits[0]?.planHash).toBe("direct:remove");
|
|
241
|
-
});
|
|
242
|
-
|
|
243
|
-
it("not installed: distinct status, nothing changed or audited", async () => {
|
|
244
|
-
const { svc, audits, calls } = build({ notInstalled: "beta" });
|
|
245
|
-
const r = await svc.add("a1", "root", ["acme/a", "beta/b"]);
|
|
246
|
-
expect(r).toMatchObject({ status: "not_installed", org: "beta" });
|
|
247
|
-
expect(calls).toEqual([]);
|
|
248
|
-
expect(audits).toEqual([]);
|
|
249
|
-
});
|
|
250
|
-
|
|
251
|
-
it("a mid-way GitHub failure audits a failed entry with only the repos that landed", async () => {
|
|
252
|
-
const { svc, audits } = build({ failAdd: true });
|
|
253
|
-
const r = await svc.add("a1", "root", ["acme/a", "beta/b"]);
|
|
254
|
-
expect(r).toEqual({ status: "failed", auditId: "audit-1", error: "boom" });
|
|
255
|
-
expect(audits[0]?.diff.installationAdds).toEqual(["acme/a"]);
|
|
256
|
-
expect(audits[0]?.result).toEqual({ status: "failed", error: "boom" });
|
|
257
|
-
});
|
|
258
|
-
|
|
259
|
-
it("unknown agent and no GH_APP_ID write nothing", async () => {
|
|
260
|
-
const a = build({ agent: null });
|
|
261
|
-
expect((await a.svc.add("x", "root", ["acme/a"])).status).toBe(
|
|
262
|
-
"agent_not_found",
|
|
263
|
-
);
|
|
264
|
-
const b = build({ agent: { githubUsername: null } });
|
|
265
|
-
expect((await b.svc.remove("a1", "root", ["acme/a"])).status).toBe(
|
|
266
|
-
"no_app_id",
|
|
267
|
-
);
|
|
268
|
-
expect(a.audits.concat(b.audits)).toEqual([]);
|
|
269
|
-
});
|
|
270
|
-
});
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* a consumer that does (shipwright) may supply this port so open work can be
|
|
5
5
|
* listed and blocked when a repo leaves an agent's repos. Consumed by
|
|
6
6
|
* repo-removal-open-work.ts (repo removed from the agent) and
|
|
7
|
-
*
|
|
7
|
+
* github-sync.ts (bound GitHub user lost push access).
|
|
8
8
|
* Additive and optional: with no port, nothing is listed or blocked.
|
|
9
9
|
*/
|
|
10
10
|
|
package/src/repo-access-rules.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* admin/src/repo-access-rules.ts
|
|
3
3
|
* Pure rules for deciding what a bound GitHub user's access result can safely
|
|
4
|
-
* justify (reused by the
|
|
4
|
+
* justify (reused by the GitHub Sync, IRM-3.1). No I/O.
|
|
5
5
|
*
|
|
6
6
|
* - Unknown is never none: an org whose resolver result is missing, partial,
|
|
7
7
|
* failed, or whose lookup installation is selected-only is skipped (with a
|
|
@@ -1,333 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* admin/src/lost-access-removal.ts
|
|
3
|
-
* Lost-access removal (IRM-1.3): removes, from both the agent App's
|
|
4
|
-
* installation and the agent's own repos, every repo the agent's bound GitHub
|
|
5
|
-
* user can no longer push to. It never adds repos.
|
|
6
|
-
*
|
|
7
|
-
* The admin reviews a read-only preview, then sends the exact repos they
|
|
8
|
-
* confirmed; the server re-checks each against the user's current access and
|
|
9
|
-
* removes only those still without it (no plan hash). The safety rules are the
|
|
10
|
-
* shared access rules (accessSkipReason / orgSkipReason): unknown is never
|
|
11
|
-
* none, so orgs with a missing, partial or failed listing, or a selected-only
|
|
12
|
-
* lookup install, are skipped and reported; an unbound, not-found or
|
|
13
|
-
* unverified user skips the whole call. More than MAX_UNCONFIRMED_REMOVALS
|
|
14
|
-
* removals need confirmRemovals.
|
|
15
|
-
*
|
|
16
|
-
* Order: installation removals, then the agent's repos, then one audit entry,
|
|
17
|
-
* then the agent's open work on the removed repos is blocked through the
|
|
18
|
-
* optional OpenWorkPort (a port failure is a warning, never a failed call).
|
|
19
|
-
*/
|
|
20
|
-
|
|
21
|
-
import type { GithubAccessPreviewFn } from "./github-access-preview.ts";
|
|
22
|
-
import type { InstallationReposPort } from "./installation-repos-service.ts";
|
|
23
|
-
import {
|
|
24
|
-
MAX_UNCONFIRMED_REMOVALS,
|
|
25
|
-
MISSING_APP_ID_REASON,
|
|
26
|
-
type InstallationAgentRecord,
|
|
27
|
-
type InstallationAgentsPort,
|
|
28
|
-
type InstallationAuditPort,
|
|
29
|
-
} from "./installation-ports.ts";
|
|
30
|
-
import type { OpenWorkItem, OpenWorkPort } from "./reconcile-open-work.ts";
|
|
31
|
-
import {
|
|
32
|
-
accessSkipReason,
|
|
33
|
-
type OrgSkip,
|
|
34
|
-
orgSkipReason,
|
|
35
|
-
repoOrg,
|
|
36
|
-
} from "./repo-access-rules.ts";
|
|
37
|
-
import {
|
|
38
|
-
blockRepoRemovalWork,
|
|
39
|
-
previewRepoRemovalWork,
|
|
40
|
-
repoLostAccessBlockReason,
|
|
41
|
-
} from "./repo-removal-open-work.ts";
|
|
42
|
-
import type { UserRepoAccessResult } from "./user-repo-access-resolver.ts";
|
|
43
|
-
|
|
44
|
-
/** `planHash` of the audit row a lost-access removal writes. */
|
|
45
|
-
export const LOST_ACCESS_AUDIT_PLAN_HASH = "lost-access:remove";
|
|
46
|
-
|
|
47
|
-
export const STILL_PUSHABLE_REASON =
|
|
48
|
-
"the bound user can still push to this repo";
|
|
49
|
-
export const NOT_PRESENT_REASON =
|
|
50
|
-
"not on the agent installation or in the agent's repos";
|
|
51
|
-
|
|
52
|
-
export interface LostAccessPlanInput {
|
|
53
|
-
username: string | null | undefined;
|
|
54
|
-
/** Null when the resolver was not run. */
|
|
55
|
-
access: UserRepoAccessResult | null;
|
|
56
|
-
agentRepos: readonly string[];
|
|
57
|
-
/** Repos the agent App's installation covers, per org login. */
|
|
58
|
-
installationRepos: Readonly<Record<string, readonly string[]>>;
|
|
59
|
-
/**
|
|
60
|
-
* The repos the admin confirmed. When set, only these are candidates and
|
|
61
|
-
* every one left alone is reported in `notRemoved`; when unset (preview),
|
|
62
|
-
* every installation and agent repo is a candidate.
|
|
63
|
-
*/
|
|
64
|
-
requested?: readonly string[];
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export interface NotRemovedRepo {
|
|
68
|
-
repo: string;
|
|
69
|
-
reason: string;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export interface LostAccessPlan {
|
|
73
|
-
skipped: boolean;
|
|
74
|
-
skipReason?: string;
|
|
75
|
-
/** Every repo removed from either place, sorted. */
|
|
76
|
-
repos: string[];
|
|
77
|
-
installationRemoves: string[];
|
|
78
|
-
configRemoves: string[];
|
|
79
|
-
skippedOrgs: OrgSkip[];
|
|
80
|
-
notRemoved: NotRemovedRepo[];
|
|
81
|
-
warnings: string[];
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const lower = (s: string): string => s.toLowerCase();
|
|
85
|
-
const sorted = (xs: Iterable<string>): string[] => [...xs].sort();
|
|
86
|
-
|
|
87
|
-
function skippedPlan(reason: string): LostAccessPlan {
|
|
88
|
-
return {
|
|
89
|
-
skipped: true,
|
|
90
|
-
skipReason: reason,
|
|
91
|
-
repos: [],
|
|
92
|
-
installationRemoves: [],
|
|
93
|
-
configRemoves: [],
|
|
94
|
-
skippedOrgs: [],
|
|
95
|
-
notRemoved: [],
|
|
96
|
-
warnings: [],
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/** Pure: the repos to remove because the bound user lost push access. */
|
|
101
|
-
export function planLostAccessRemoval(
|
|
102
|
-
input: LostAccessPlanInput,
|
|
103
|
-
): LostAccessPlan {
|
|
104
|
-
const skip = accessSkipReason(input.username, input.access);
|
|
105
|
-
if (skip) return skippedPlan(skip);
|
|
106
|
-
const access = input.access as UserRepoAccessResult;
|
|
107
|
-
|
|
108
|
-
const orgResults = new Map(access.orgs.map((o) => [lower(o.org), o]));
|
|
109
|
-
const pushable = new Set<string>();
|
|
110
|
-
for (const o of access.orgs) {
|
|
111
|
-
for (const r of o.repos) if (r.canPush) pushable.add(lower(r.fullName));
|
|
112
|
-
}
|
|
113
|
-
// Keyed by lower-case name; values keep the stored spelling.
|
|
114
|
-
const installed = new Map<string, string>();
|
|
115
|
-
for (const repos of Object.values(input.installationRepos)) {
|
|
116
|
-
for (const r of repos) installed.set(lower(r), r);
|
|
117
|
-
}
|
|
118
|
-
const configured = new Map(input.agentRepos.map((r) => [lower(r), r]));
|
|
119
|
-
|
|
120
|
-
const candidates = input.requested
|
|
121
|
-
? sorted(new Set(input.requested))
|
|
122
|
-
: sorted(new Set([...configured.values(), ...installed.values()]));
|
|
123
|
-
|
|
124
|
-
const skipReasons = new Map<string, string>();
|
|
125
|
-
const notRemoved: NotRemovedRepo[] = [];
|
|
126
|
-
const leave = (repo: string, reason: string) => {
|
|
127
|
-
if (input.requested) notRemoved.push({ repo, reason });
|
|
128
|
-
};
|
|
129
|
-
const installationRemoves = new Set<string>();
|
|
130
|
-
const configRemoves = new Set<string>();
|
|
131
|
-
for (const repo of candidates) {
|
|
132
|
-
const org = repoOrg(repo);
|
|
133
|
-
const orgSkip = orgSkipReason(orgResults.get(lower(org)));
|
|
134
|
-
if (orgSkip) {
|
|
135
|
-
// Unknown is never none.
|
|
136
|
-
skipReasons.set(org, orgSkip);
|
|
137
|
-
leave(repo, `removals skipped for org: ${orgSkip}`);
|
|
138
|
-
continue;
|
|
139
|
-
}
|
|
140
|
-
if (pushable.has(lower(repo))) {
|
|
141
|
-
leave(repo, STILL_PUSHABLE_REASON);
|
|
142
|
-
continue;
|
|
143
|
-
}
|
|
144
|
-
const inInstallation = installed.get(lower(repo));
|
|
145
|
-
const inConfig = configured.get(lower(repo));
|
|
146
|
-
if (!inInstallation && !inConfig) {
|
|
147
|
-
leave(repo, NOT_PRESENT_REASON);
|
|
148
|
-
continue;
|
|
149
|
-
}
|
|
150
|
-
if (inInstallation) installationRemoves.add(inInstallation);
|
|
151
|
-
if (inConfig) configRemoves.add(inConfig);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
const skippedOrgs = sorted(skipReasons.keys()).map((org) => ({
|
|
155
|
-
org,
|
|
156
|
-
reason: skipReasons.get(org) as string,
|
|
157
|
-
}));
|
|
158
|
-
return {
|
|
159
|
-
skipped: false,
|
|
160
|
-
repos: sorted(new Set([...installationRemoves, ...configRemoves])),
|
|
161
|
-
installationRemoves: sorted(installationRemoves),
|
|
162
|
-
configRemoves: sorted(configRemoves),
|
|
163
|
-
skippedOrgs,
|
|
164
|
-
notRemoved,
|
|
165
|
-
warnings: skippedOrgs.map(
|
|
166
|
-
(s) => `${s.org}: removals skipped (${s.reason})`,
|
|
167
|
-
),
|
|
168
|
-
};
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
export interface LostAccessRemovalDeps {
|
|
172
|
-
agents: InstallationAgentsPort;
|
|
173
|
-
access: GithubAccessPreviewFn;
|
|
174
|
-
installation: Pick<InstallationReposPort, "listInstalled" | "removeRepos">;
|
|
175
|
-
audit: InstallationAuditPort;
|
|
176
|
-
/** Optional: with none, nothing is listed or blocked. */
|
|
177
|
-
openWork?: OpenWorkPort;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
export type LostAccessPreviewResult =
|
|
181
|
-
| { status: "agent_not_found" }
|
|
182
|
-
| { status: "skipped"; reason: string }
|
|
183
|
-
| {
|
|
184
|
-
status: "previewed";
|
|
185
|
-
plan: LostAccessPlan;
|
|
186
|
-
openWork: OpenWorkItem[];
|
|
187
|
-
warnings: string[];
|
|
188
|
-
};
|
|
189
|
-
|
|
190
|
-
export interface LostAccessRemoveInput {
|
|
191
|
-
agentId: string;
|
|
192
|
-
/** Who asked (audit trail). */
|
|
193
|
-
caller: string;
|
|
194
|
-
/** The repos the admin confirmed from the preview. */
|
|
195
|
-
repos: readonly string[];
|
|
196
|
-
confirmRemovals?: boolean;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
export type LostAccessRemoveResult =
|
|
200
|
-
| { status: "agent_not_found" }
|
|
201
|
-
| { status: "skipped"; reason: string }
|
|
202
|
-
| { status: "noop"; plan: LostAccessPlan }
|
|
203
|
-
| { status: "removal_cap"; plan: LostAccessPlan; removals: number }
|
|
204
|
-
| {
|
|
205
|
-
status: "applied";
|
|
206
|
-
plan: LostAccessPlan;
|
|
207
|
-
auditId: string;
|
|
208
|
-
warnings: string[];
|
|
209
|
-
}
|
|
210
|
-
| { status: "failed"; plan: LostAccessPlan; auditId: string; error: string };
|
|
211
|
-
|
|
212
|
-
function groupByOrg(repos: readonly string[]): Map<string, string[]> {
|
|
213
|
-
const byOrg = new Map<string, string[]>();
|
|
214
|
-
for (const r of repos) {
|
|
215
|
-
const org = repoOrg(r);
|
|
216
|
-
byOrg.set(org, [...(byOrg.get(org) ?? []), r]);
|
|
217
|
-
}
|
|
218
|
-
return byOrg;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
export class LostAccessRemovalService {
|
|
222
|
-
constructor(private readonly deps: LostAccessRemovalDeps) {}
|
|
223
|
-
|
|
224
|
-
/** Read-only: what would be removed and the open work it would block. */
|
|
225
|
-
async preview(agentId: string): Promise<LostAccessPreviewResult> {
|
|
226
|
-
const agent = await this.deps.agents.get(agentId);
|
|
227
|
-
if (!agent) return { status: "agent_not_found" };
|
|
228
|
-
const plan = await this.planFor(agent);
|
|
229
|
-
if (plan.skipped) {
|
|
230
|
-
return { status: "skipped", reason: plan.skipReason ?? "skipped" };
|
|
231
|
-
}
|
|
232
|
-
const work = await previewRepoRemovalWork(
|
|
233
|
-
this.deps.openWork,
|
|
234
|
-
agentId,
|
|
235
|
-
plan.repos,
|
|
236
|
-
);
|
|
237
|
-
return {
|
|
238
|
-
status: "previewed",
|
|
239
|
-
plan,
|
|
240
|
-
openWork: work.openWork,
|
|
241
|
-
warnings: [...plan.warnings, ...work.warnings],
|
|
242
|
-
};
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
async remove(input: LostAccessRemoveInput): Promise<LostAccessRemoveResult> {
|
|
246
|
-
const agent = await this.deps.agents.get(input.agentId);
|
|
247
|
-
if (!agent) return { status: "agent_not_found" };
|
|
248
|
-
const plan = await this.planFor(agent, input.repos);
|
|
249
|
-
if (plan.skipped) {
|
|
250
|
-
return { status: "skipped", reason: plan.skipReason ?? "skipped" };
|
|
251
|
-
}
|
|
252
|
-
if (plan.repos.length === 0) return { status: "noop", plan };
|
|
253
|
-
const removals = plan.repos.length;
|
|
254
|
-
if (removals > MAX_UNCONFIRMED_REMOVALS && !input.confirmRemovals) {
|
|
255
|
-
return { status: "removal_cap", plan, removals };
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
const appId = agent.appId as number;
|
|
259
|
-
const removedFromInstallation: string[] = [];
|
|
260
|
-
let configDone = false;
|
|
261
|
-
let error: string | undefined;
|
|
262
|
-
try {
|
|
263
|
-
for (const [org, repos] of groupByOrg(plan.installationRemoves)) {
|
|
264
|
-
await this.deps.installation.removeRepos(appId, org, repos);
|
|
265
|
-
removedFromInstallation.push(...repos);
|
|
266
|
-
}
|
|
267
|
-
if (plan.configRemoves.length > 0) {
|
|
268
|
-
const drop = new Set(plan.configRemoves);
|
|
269
|
-
await this.deps.agents.setRepos(
|
|
270
|
-
agent.id,
|
|
271
|
-
agent.repos.filter((r) => !drop.has(r)),
|
|
272
|
-
);
|
|
273
|
-
}
|
|
274
|
-
configDone = true;
|
|
275
|
-
} catch (err) {
|
|
276
|
-
error = err instanceof Error ? err.message : String(err);
|
|
277
|
-
}
|
|
278
|
-
// Record what actually changed; on failure, only what landed.
|
|
279
|
-
const auditId = await this.deps.audit.write({
|
|
280
|
-
agentId: input.agentId,
|
|
281
|
-
caller: input.caller,
|
|
282
|
-
planHash: LOST_ACCESS_AUDIT_PLAN_HASH,
|
|
283
|
-
diff: {
|
|
284
|
-
installationAdds: [],
|
|
285
|
-
installationRemoves: removedFromInstallation,
|
|
286
|
-
configAdds: [],
|
|
287
|
-
configRemoves: configDone ? plan.configRemoves : [],
|
|
288
|
-
},
|
|
289
|
-
result:
|
|
290
|
-
error === undefined
|
|
291
|
-
? { status: "applied" }
|
|
292
|
-
: { status: "failed", error },
|
|
293
|
-
});
|
|
294
|
-
if (error !== undefined) {
|
|
295
|
-
return { status: "failed", plan, auditId, error };
|
|
296
|
-
}
|
|
297
|
-
const warnings = await blockRepoRemovalWork(
|
|
298
|
-
this.deps.openWork,
|
|
299
|
-
input.agentId,
|
|
300
|
-
plan.repos,
|
|
301
|
-
repoLostAccessBlockReason,
|
|
302
|
-
);
|
|
303
|
-
return { status: "applied", plan, auditId, warnings };
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
private async planFor(
|
|
307
|
-
agent: InstallationAgentRecord,
|
|
308
|
-
requested?: readonly string[],
|
|
309
|
-
): Promise<LostAccessPlan> {
|
|
310
|
-
const username = agent.githubUsername?.trim();
|
|
311
|
-
if (!username) {
|
|
312
|
-
return skippedPlan(accessSkipReason(username, null) as string);
|
|
313
|
-
}
|
|
314
|
-
const preview = await this.deps.access(username);
|
|
315
|
-
if (preview.status === "unconfigured") {
|
|
316
|
-
return skippedPlan("GitHub Apps are not configured");
|
|
317
|
-
}
|
|
318
|
-
// Whole-call skips need no installation calls.
|
|
319
|
-
const skip = accessSkipReason(username, preview.access);
|
|
320
|
-
if (skip) return skippedPlan(skip);
|
|
321
|
-
if (agent.appId === undefined) return skippedPlan(MISSING_APP_ID_REASON);
|
|
322
|
-
const installed = await this.deps.installation.listInstalled(agent.appId);
|
|
323
|
-
return planLostAccessRemoval({
|
|
324
|
-
username,
|
|
325
|
-
access: preview.access,
|
|
326
|
-
agentRepos: agent.repos,
|
|
327
|
-
installationRepos: Object.fromEntries(
|
|
328
|
-
installed.map(({ org, repos }) => [org, repos]),
|
|
329
|
-
),
|
|
330
|
-
...(requested ? { requested } : {}),
|
|
331
|
-
});
|
|
332
|
-
}
|
|
333
|
-
}
|