@app-vitals/agent-admin 1.35.1 → 1.36.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 +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/fixtures/enterprise-org-installations.json +26 -0
- package/src/github-enterprise-api.ts +1 -3
- package/src/github-enterprise-client.ts +9 -4
- 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 +414 -0
- package/src/{lost-access-removal.unit.test.ts → github-sync.unit.test.ts} +214 -109
- package/src/installation-ports-fixture.integration.test.ts +78 -0
- package/src/installation-ports.ts +84 -13
- package/src/installation-ports.unit.test.ts +66 -14
- package/src/installation-repos-service.ts +25 -102
- 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,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* admin/src/installation-ports.ts
|
|
3
3
|
* Ports and Prisma/GitHub-backed adapters shared by the installation repos
|
|
4
|
-
* service (IRM-1.2) and
|
|
4
|
+
* service (IRM-1.2) and GitHub Sync (IRM-3.1): the agent record
|
|
5
5
|
* with its own GH_APP_ID, installation list/add/remove scoped to that App, and
|
|
6
6
|
* the audit table (GitHubReconcileAudit) every change is recorded in.
|
|
7
7
|
*/
|
|
@@ -15,6 +15,17 @@ function parseAppId(raw: string | undefined): number | undefined {
|
|
|
15
15
|
return Number.isInteger(n) && n > 0 ? n : undefined;
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
+
/** GitHub App slug shape (also validates a stored GH_APP_SLUG). */
|
|
19
|
+
export const SLUG_RE = /^[A-Za-z0-9](?:[A-Za-z0-9-]{0,38})$/;
|
|
20
|
+
/** GitHub App client ids look like Iv1.<hex> / Iv23li<alnum>; no path or query chars. */
|
|
21
|
+
export const CLIENT_ID_RE = /^[A-Za-z0-9._-]{1,64}$/;
|
|
22
|
+
|
|
23
|
+
/** A stored env value that passes `re` (trimmed), else undefined. */
|
|
24
|
+
function validEnv(raw: string | undefined, re: RegExp): string | undefined {
|
|
25
|
+
const v = raw?.trim();
|
|
26
|
+
return v && re.test(v) ? v : undefined;
|
|
27
|
+
}
|
|
28
|
+
|
|
18
29
|
/** More removals than this need confirmRemovals. */
|
|
19
30
|
export const MAX_UNCONFIRMED_REMOVALS = 10;
|
|
20
31
|
|
|
@@ -24,6 +35,19 @@ export interface InstallationAgentRecord {
|
|
|
24
35
|
githubUsername: string | null;
|
|
25
36
|
/** The agent's own GitHub App id (its stored GH_APP_ID); undefined when unset or not numeric. */
|
|
26
37
|
appId?: number;
|
|
38
|
+
/** The agent App's client id (stored GH_APP_CLIENT_ID); undefined when unset or malformed. */
|
|
39
|
+
clientId?: string;
|
|
40
|
+
/** The agent App's slug (stored GH_APP_SLUG); undefined when unset or malformed. */
|
|
41
|
+
slug?: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* How an installation is matched to the agent's App when the enterprise API
|
|
46
|
+
* omits `app_id`: by client id, then slug (case-insensitive), then appId.
|
|
47
|
+
*/
|
|
48
|
+
export interface AppIdentity {
|
|
49
|
+
clientId?: string;
|
|
50
|
+
slug?: string;
|
|
27
51
|
}
|
|
28
52
|
|
|
29
53
|
export interface InstallationAgentsPort {
|
|
@@ -40,13 +64,20 @@ export interface AppInstallationPort {
|
|
|
40
64
|
listRepos(
|
|
41
65
|
appId: number,
|
|
42
66
|
orgs: readonly string[],
|
|
67
|
+
identity?: AppIdentity,
|
|
43
68
|
): Promise<Record<string, string[]>>;
|
|
44
69
|
/** Grant the installation access to repos (owner/repo, all in `org`). */
|
|
45
|
-
addRepos(
|
|
70
|
+
addRepos(
|
|
71
|
+
appId: number,
|
|
72
|
+
org: string,
|
|
73
|
+
repos: readonly string[],
|
|
74
|
+
identity?: AppIdentity,
|
|
75
|
+
): Promise<void>;
|
|
46
76
|
removeRepos(
|
|
47
77
|
appId: number,
|
|
48
78
|
org: string,
|
|
49
79
|
repos: readonly string[],
|
|
80
|
+
identity?: AppIdentity,
|
|
50
81
|
): Promise<void>;
|
|
51
82
|
}
|
|
52
83
|
|
|
@@ -111,9 +142,17 @@ export class PrismaInstallationAgents implements InstallationAgentsPort {
|
|
|
111
142
|
select: { id: true, repos: true, githubUsername: true },
|
|
112
143
|
});
|
|
113
144
|
if (!agent) return null;
|
|
114
|
-
const
|
|
145
|
+
const env = (await this.env.getConfigBundle(agentId))?.env;
|
|
146
|
+
const raw = env?.GH_APP_ID;
|
|
115
147
|
const appId = parseAppId(raw);
|
|
116
|
-
|
|
148
|
+
const clientId = validEnv(env?.GH_APP_CLIENT_ID, CLIENT_ID_RE);
|
|
149
|
+
const slug = validEnv(env?.GH_APP_SLUG, SLUG_RE);
|
|
150
|
+
return {
|
|
151
|
+
...agent,
|
|
152
|
+
...(appId === undefined ? {} : { appId }),
|
|
153
|
+
...(clientId === undefined ? {} : { clientId }),
|
|
154
|
+
...(slug === undefined ? {} : { slug }),
|
|
155
|
+
};
|
|
117
156
|
}
|
|
118
157
|
|
|
119
158
|
async setRepos(agentId: string, repos: string[]): Promise<void> {
|
|
@@ -126,7 +165,15 @@ export class PrismaInstallationAgents implements InstallationAgentsPort {
|
|
|
126
165
|
|
|
127
166
|
/** The slice of GithubEnterpriseClient the installation port needs. */
|
|
128
167
|
export interface InstallationRepoClient {
|
|
129
|
-
listOrgInstallations(org: string): Promise<
|
|
168
|
+
listOrgInstallations(org: string): Promise<
|
|
169
|
+
{
|
|
170
|
+
id: number;
|
|
171
|
+
/** Absent on the enterprise-organization-installation schema. */
|
|
172
|
+
appId?: number;
|
|
173
|
+
appSlug?: string;
|
|
174
|
+
clientId?: string;
|
|
175
|
+
}[]
|
|
176
|
+
>;
|
|
130
177
|
listInstallationRepositories(
|
|
131
178
|
org: string,
|
|
132
179
|
installationId: number,
|
|
@@ -156,6 +203,27 @@ export class AppNotInstalledError extends Error {
|
|
|
156
203
|
}
|
|
157
204
|
}
|
|
158
205
|
|
|
206
|
+
/**
|
|
207
|
+
* Pick the agent App's installation: client id, then slug (case-insensitive),
|
|
208
|
+
* then appId. Each key only counts when both sides carry it.
|
|
209
|
+
*/
|
|
210
|
+
export function matchInstallation<
|
|
211
|
+
T extends { appId?: number; appSlug?: string; clientId?: string },
|
|
212
|
+
>(
|
|
213
|
+
installs: readonly T[],
|
|
214
|
+
appId: number,
|
|
215
|
+
identity?: AppIdentity,
|
|
216
|
+
): T | undefined {
|
|
217
|
+
const { clientId, slug } = identity ?? {};
|
|
218
|
+
return (
|
|
219
|
+
(clientId ? installs.find((i) => i.clientId === clientId) : undefined) ??
|
|
220
|
+
(slug
|
|
221
|
+
? installs.find((i) => i.appSlug?.toLowerCase() === slug.toLowerCase())
|
|
222
|
+
: undefined) ??
|
|
223
|
+
installs.find((i) => i.appId !== undefined && i.appId === appId)
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
|
|
159
227
|
const baseName = (fullName: string): string => fullName.split("/")[1] ?? "";
|
|
160
228
|
|
|
161
229
|
/**
|
|
@@ -168,9 +236,12 @@ export function createInstallationPort(
|
|
|
168
236
|
const installationId = async (
|
|
169
237
|
appId: number,
|
|
170
238
|
org: string,
|
|
239
|
+
identity?: AppIdentity,
|
|
171
240
|
): Promise<number> => {
|
|
172
|
-
const found = (
|
|
173
|
-
(
|
|
241
|
+
const found = matchInstallation(
|
|
242
|
+
await client.listOrgInstallations(org),
|
|
243
|
+
appId,
|
|
244
|
+
identity,
|
|
174
245
|
);
|
|
175
246
|
if (!found) {
|
|
176
247
|
throw new AppNotInstalledError(appId, org);
|
|
@@ -178,28 +249,28 @@ export function createInstallationPort(
|
|
|
178
249
|
return found.id;
|
|
179
250
|
};
|
|
180
251
|
return {
|
|
181
|
-
async listRepos(appId, orgs) {
|
|
252
|
+
async listRepos(appId, orgs, identity) {
|
|
182
253
|
const out: Record<string, string[]> = {};
|
|
183
254
|
for (const org of orgs) {
|
|
184
255
|
const repos = await client.listInstallationRepositories(
|
|
185
256
|
org,
|
|
186
|
-
await installationId(appId, org),
|
|
257
|
+
await installationId(appId, org, identity),
|
|
187
258
|
);
|
|
188
259
|
out[org] = repos.map((r) => r.fullName);
|
|
189
260
|
}
|
|
190
261
|
return out;
|
|
191
262
|
},
|
|
192
|
-
async addRepos(appId, org, repos) {
|
|
263
|
+
async addRepos(appId, org, repos, identity) {
|
|
193
264
|
await client.addRepositories(
|
|
194
265
|
org,
|
|
195
|
-
await installationId(appId, org),
|
|
266
|
+
await installationId(appId, org, identity),
|
|
196
267
|
repos.map(baseName),
|
|
197
268
|
);
|
|
198
269
|
},
|
|
199
|
-
async removeRepos(appId, org, repos) {
|
|
270
|
+
async removeRepos(appId, org, repos, identity) {
|
|
200
271
|
await client.removeRepositories(
|
|
201
272
|
org,
|
|
202
|
-
await installationId(appId, org),
|
|
273
|
+
await installationId(appId, org, identity),
|
|
203
274
|
repos.map(baseName),
|
|
204
275
|
);
|
|
205
276
|
},
|
|
@@ -4,18 +4,67 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import { describe, expect, it } from "bun:test";
|
|
7
|
+
import enterpriseOrgInstallations from "./fixtures/enterprise-org-installations.json";
|
|
7
8
|
import {
|
|
8
9
|
AppNotInstalledError,
|
|
9
10
|
createInstallationPort,
|
|
11
|
+
matchInstallation,
|
|
10
12
|
} from "./installation-ports.ts";
|
|
11
13
|
|
|
14
|
+
describe("enterprise-org-installations fixture", () => {
|
|
15
|
+
it("has no app_id on any row (GitHub's schema omits it)", () => {
|
|
16
|
+
expect(enterpriseOrgInstallations.length).toBeGreaterThan(0);
|
|
17
|
+
for (const row of enterpriseOrgInstallations) {
|
|
18
|
+
expect("app_id" in row).toBe(false);
|
|
19
|
+
expect(typeof row.app_slug).toBe("string");
|
|
20
|
+
expect(typeof row.client_id).toBe("string");
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
describe("matchInstallation", () => {
|
|
26
|
+
const installs = [
|
|
27
|
+
{ id: 1, appId: 10, appSlug: "slug-one", clientId: "Iv1.aaa" },
|
|
28
|
+
{ id: 2, appId: 20, appSlug: "Slug-Two", clientId: "Iv1.bbb" },
|
|
29
|
+
{ id: 3, appSlug: "slug-three" },
|
|
30
|
+
];
|
|
31
|
+
|
|
32
|
+
it("prefers client id over slug over appId", () => {
|
|
33
|
+
expect(
|
|
34
|
+
matchInstallation(installs, 10, { clientId: "Iv1.bbb", slug: "slug-one" })
|
|
35
|
+
?.id,
|
|
36
|
+
).toBe(2);
|
|
37
|
+
expect(matchInstallation(installs, 10, { slug: "slug-three" })?.id).toBe(3);
|
|
38
|
+
expect(matchInstallation(installs, 20)?.id).toBe(2);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("falls through to the next key when one does not match", () => {
|
|
42
|
+
expect(
|
|
43
|
+
matchInstallation(installs, 10, { clientId: "Iv1.zzz", slug: "SLUG-TWO" })
|
|
44
|
+
?.id,
|
|
45
|
+
).toBe(2);
|
|
46
|
+
expect(
|
|
47
|
+
matchInstallation(installs, 20, { clientId: "Iv1.zzz", slug: "nope" })
|
|
48
|
+
?.id,
|
|
49
|
+
).toBe(2);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("matches the slug case-insensitively", () => {
|
|
53
|
+
expect(matchInstallation(installs, 99, { slug: "SLUG-ONE" })?.id).toBe(1);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("never matches an installation without appId on appId alone", () => {
|
|
57
|
+
expect(matchInstallation([{}], 0)).toBeUndefined();
|
|
58
|
+
expect(matchInstallation(installs, 99, {})).toBeUndefined();
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
|
|
12
62
|
describe("createInstallationPort", () => {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const port = createInstallationPort({
|
|
63
|
+
const fake = (seen: string[]) =>
|
|
64
|
+
createInstallationPort({
|
|
16
65
|
listOrgInstallations: async () => [
|
|
17
|
-
{ id: 1,
|
|
18
|
-
{ id: 2,
|
|
66
|
+
{ id: 1, appSlug: "app-one" },
|
|
67
|
+
{ id: 2, appSlug: "app-two" },
|
|
19
68
|
],
|
|
20
69
|
listInstallationRepositories: async (_o, id) => {
|
|
21
70
|
seen.push(`list:${id}`);
|
|
@@ -28,19 +77,22 @@ describe("createInstallationPort", () => {
|
|
|
28
77
|
seen.push(`rm:${id}:${names.join(",")}`);
|
|
29
78
|
},
|
|
30
79
|
});
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
80
|
+
|
|
81
|
+
it("scopes to the identified App's installation per call and sends simple names", async () => {
|
|
82
|
+
const seen: string[] = [];
|
|
83
|
+
const port = fake(seen);
|
|
84
|
+
expect(await port.listRepos(0, ["acme"], { slug: "app-two" })).toEqual({
|
|
85
|
+
acme: ["acme/a"],
|
|
86
|
+
});
|
|
87
|
+
await port.addRepos(0, "acme", ["acme/b"], { slug: "APP-TWO" });
|
|
88
|
+
await port.removeRepos(0, "acme", ["acme/c", "acme/d"], {
|
|
89
|
+
slug: "app-one",
|
|
90
|
+
});
|
|
34
91
|
expect(seen).toEqual(["list:2", "add:2:b", "rm:1:c,d"]);
|
|
35
92
|
});
|
|
36
93
|
|
|
37
94
|
it("names the org when the App is not installed there", async () => {
|
|
38
|
-
const port =
|
|
39
|
-
listOrgInstallations: async () => [{ id: 1, appId: 10 }],
|
|
40
|
-
listInstallationRepositories: async () => [],
|
|
41
|
-
addRepositories: async () => {},
|
|
42
|
-
removeRepositories: async () => {},
|
|
43
|
-
});
|
|
95
|
+
const port = fake([]);
|
|
44
96
|
await expect(port.listRepos(99, ["acme"])).rejects.toThrow(
|
|
45
97
|
/99.*not installed on org acme/,
|
|
46
98
|
);
|
|
@@ -1,22 +1,18 @@
|
|
|
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";
|
|
12
9
|
import {
|
|
13
10
|
AppNotInstalledError,
|
|
14
11
|
MISSING_APP_ID_REASON,
|
|
12
|
+
type AppIdentity,
|
|
15
13
|
type InstallationAgentsPort,
|
|
16
|
-
type InstallationAuditPort,
|
|
17
14
|
type AppInstallationPort,
|
|
18
15
|
} from "./installation-ports.ts";
|
|
19
|
-
import { repoOrg } from "./repo-access-rules.ts";
|
|
20
16
|
import type {
|
|
21
17
|
OrgAccessResult,
|
|
22
18
|
UserRepoAccessResult,
|
|
@@ -24,7 +20,10 @@ import type {
|
|
|
24
20
|
|
|
25
21
|
/** Installation port plus discovery of the orgs the agent App is installed on. */
|
|
26
22
|
export interface InstallationReposPort extends AppInstallationPort {
|
|
27
|
-
listInstalled(
|
|
23
|
+
listInstalled(
|
|
24
|
+
appId: number,
|
|
25
|
+
identity?: AppIdentity,
|
|
26
|
+
): Promise<{ org: string; repos: string[] }[]>;
|
|
28
27
|
}
|
|
29
28
|
|
|
30
29
|
export interface InstallationRepoView {
|
|
@@ -43,18 +42,21 @@ export type InstallationListResult =
|
|
|
43
42
|
| { status: "no_app_id"; reason: string }
|
|
44
43
|
| { status: "ok"; appId: number; orgs: InstallationOrgView[] };
|
|
45
44
|
|
|
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
45
|
export interface InstallationReposDeps {
|
|
54
46
|
agents: Pick<InstallationAgentsPort, "get">;
|
|
55
47
|
access: GithubAccessPreviewFn;
|
|
56
48
|
installation: InstallationReposPort;
|
|
57
|
-
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** The agent's App identity keys (client id, slug), when stored. */
|
|
52
|
+
export function identityOf(agent: {
|
|
53
|
+
clientId?: string;
|
|
54
|
+
slug?: string;
|
|
55
|
+
}): AppIdentity {
|
|
56
|
+
return {
|
|
57
|
+
...(agent.clientId ? { clientId: agent.clientId } : {}),
|
|
58
|
+
...(agent.slug ? { slug: agent.slug } : {}),
|
|
59
|
+
};
|
|
58
60
|
}
|
|
59
61
|
|
|
60
62
|
const orgKey = (org: string): string => org.toLowerCase();
|
|
@@ -92,15 +94,6 @@ export function userPushHints(
|
|
|
92
94
|
});
|
|
93
95
|
}
|
|
94
96
|
|
|
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
97
|
export class InstallationReposService {
|
|
105
98
|
constructor(private readonly deps: InstallationReposDeps) {}
|
|
106
99
|
|
|
@@ -110,7 +103,10 @@ export class InstallationReposService {
|
|
|
110
103
|
if (agent.appId === undefined) {
|
|
111
104
|
return { status: "no_app_id", reason: MISSING_APP_ID_REASON };
|
|
112
105
|
}
|
|
113
|
-
const installed = await this.deps.installation.listInstalled(
|
|
106
|
+
const installed = await this.deps.installation.listInstalled(
|
|
107
|
+
agent.appId,
|
|
108
|
+
identityOf(agent),
|
|
109
|
+
);
|
|
114
110
|
const username = agent.githubUsername?.trim();
|
|
115
111
|
let access: UserRepoAccessResult | undefined;
|
|
116
112
|
if (username) {
|
|
@@ -130,79 +126,6 @@ export class InstallationReposService {
|
|
|
130
126
|
})),
|
|
131
127
|
};
|
|
132
128
|
}
|
|
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
129
|
}
|
|
207
130
|
|
|
208
131
|
/** The slice of the installer client the installation-repos port needs. */
|
|
@@ -220,11 +143,11 @@ export function createInstallationReposPort(
|
|
|
220
143
|
): InstallationReposPort {
|
|
221
144
|
return {
|
|
222
145
|
...base,
|
|
223
|
-
async listInstalled(appId) {
|
|
146
|
+
async listInstalled(appId, identity) {
|
|
224
147
|
const out: { org: string; repos: string[] }[] = [];
|
|
225
148
|
for (const { login } of await client.listInstallableOrganizations()) {
|
|
226
149
|
try {
|
|
227
|
-
const repos = await base.listRepos(appId, [login]);
|
|
150
|
+
const repos = await base.listRepos(appId, [login], identity);
|
|
228
151
|
out.push({ org: login, repos: repos[login] ?? [] });
|
|
229
152
|
} catch (err) {
|
|
230
153
|
if (err instanceof AppNotInstalledError) continue;
|
|
@@ -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
|