@app-vitals/agent-admin 1.36.0 → 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/package.json +2 -2
- 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-sync.ts +11 -4
- package/src/installation-ports-fixture.integration.test.ts +78 -0
- package/src/installation-ports.ts +83 -12
- package/src/installation-ports.unit.test.ts +66 -14
- package/src/installation-repos-service.ts +22 -4
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@app-vitals/agent-admin",
|
|
3
|
-
"version": "1.36.
|
|
3
|
+
"version": "1.36.1",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/app-vitals/agent.git",
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
44
|
"@anthropic-ai/sdk": "0.135.0",
|
|
45
|
-
"@app-vitals/agent-lib": "1.36.
|
|
45
|
+
"@app-vitals/agent-lib": "1.36.1",
|
|
46
46
|
"@hono/zod-openapi": "^1.0.0",
|
|
47
47
|
"@prisma/adapter-pg": "^7.10.0",
|
|
48
48
|
"@prisma/client": "^7.10.0",
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"id": 1000001,
|
|
4
|
+
"app_slug": "example-agent-app",
|
|
5
|
+
"client_id": "Iv1.0000000000000000",
|
|
6
|
+
"account": { "login": "example-org" },
|
|
7
|
+
"repository_selection": "selected",
|
|
8
|
+
"repositories_url": "https://api.github.com/enterprises/example-ent/apps/organizations/example-org/installations/1000001/repositories",
|
|
9
|
+
"permissions": { "contents": "write", "metadata": "read" },
|
|
10
|
+
"events": ["push"],
|
|
11
|
+
"created_at": "2026-01-01T00:00:00Z",
|
|
12
|
+
"updated_at": "2026-01-02T00:00:00Z"
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
"id": 1000002,
|
|
16
|
+
"app_slug": "example-other-app",
|
|
17
|
+
"client_id": "Iv1.1111111111111111",
|
|
18
|
+
"account": { "login": "example-org" },
|
|
19
|
+
"repository_selection": "all",
|
|
20
|
+
"repositories_url": "https://api.github.com/enterprises/example-ent/apps/organizations/example-org/installations/1000002/repositories",
|
|
21
|
+
"permissions": { "metadata": "read" },
|
|
22
|
+
"events": [],
|
|
23
|
+
"created_at": "2026-01-01T00:00:00Z",
|
|
24
|
+
"updated_at": "2026-01-02T00:00:00Z"
|
|
25
|
+
}
|
|
26
|
+
]
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
* Auth: admin key or admin session (every other caller class gets 403).
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
+
import { CLIENT_ID_RE, SLUG_RE } from "./installation-ports.ts";
|
|
19
20
|
import { createPrivateKey } from "node:crypto";
|
|
20
21
|
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
|
|
21
22
|
import { HTTPException } from "hono/http-exception";
|
|
@@ -84,9 +85,6 @@ const REQUIRED_PERMISSION: Record<
|
|
|
84
85
|
lookup: { name: "metadata", levels: ["read", "write"] },
|
|
85
86
|
};
|
|
86
87
|
|
|
87
|
-
const SLUG_RE = /^[A-Za-z0-9](?:[A-Za-z0-9-]{0,38})$/;
|
|
88
|
-
/** GitHub App client ids look like Iv1.<hex> / Iv23li<alnum>; no path or query chars. */
|
|
89
|
-
const CLIENT_ID_RE = /^[A-Za-z0-9._-]{1,64}$/;
|
|
90
88
|
const ORG_RE = /^[A-Za-z0-9](?:[A-Za-z0-9-]{0,38})$/;
|
|
91
89
|
|
|
92
90
|
// ─── Schemas ──────────────────────────────────────────────────────────────────
|
|
@@ -142,7 +142,12 @@ export interface InstallableOrganization {
|
|
|
142
142
|
|
|
143
143
|
export interface AppInstallation {
|
|
144
144
|
id: number;
|
|
145
|
-
|
|
145
|
+
/**
|
|
146
|
+
* GitHub's enterprise-organization-installation schema omits app_id, so this
|
|
147
|
+
* is undefined for installations listed through the enterprise API; match by
|
|
148
|
+
* clientId or appSlug instead.
|
|
149
|
+
*/
|
|
150
|
+
appId?: number;
|
|
146
151
|
appSlug: string;
|
|
147
152
|
clientId?: string;
|
|
148
153
|
accountLogin: string;
|
|
@@ -504,7 +509,7 @@ export class GithubEnterpriseClient {
|
|
|
504
509
|
|
|
505
510
|
interface RawInstallation {
|
|
506
511
|
id: number;
|
|
507
|
-
app_id
|
|
512
|
+
app_id?: number;
|
|
508
513
|
app_slug: string;
|
|
509
514
|
client_id?: string;
|
|
510
515
|
account?: { login?: string } | null;
|
|
@@ -521,10 +526,10 @@ function toRepository(r: RawRepository): AccessibleRepository {
|
|
|
521
526
|
return { id: r.id, name: r.name, fullName: r.full_name };
|
|
522
527
|
}
|
|
523
528
|
|
|
524
|
-
function toInstallation(r: RawInstallation): AppInstallation {
|
|
529
|
+
export function toInstallation(r: RawInstallation): AppInstallation {
|
|
525
530
|
return {
|
|
526
531
|
id: r.id,
|
|
527
|
-
appId: r.app_id,
|
|
532
|
+
...(r.app_id === undefined ? {} : { appId: r.app_id }),
|
|
528
533
|
appSlug: r.app_slug,
|
|
529
534
|
...(r.client_id ? { clientId: r.client_id } : {}),
|
|
530
535
|
accountLogin: r.account?.login ?? "",
|
package/src/github-sync.ts
CHANGED
|
@@ -25,7 +25,10 @@
|
|
|
25
25
|
*/
|
|
26
26
|
|
|
27
27
|
import type { GithubAccessPreviewFn } from "./github-access-preview.ts";
|
|
28
|
-
import
|
|
28
|
+
import {
|
|
29
|
+
identityOf,
|
|
30
|
+
type InstallationReposPort,
|
|
31
|
+
} from "./installation-repos-service.ts";
|
|
29
32
|
import {
|
|
30
33
|
MAX_UNCONFIRMED_REMOVALS,
|
|
31
34
|
MISSING_APP_ID_REASON,
|
|
@@ -317,17 +320,18 @@ export class GithubSyncService {
|
|
|
317
320
|
}
|
|
318
321
|
|
|
319
322
|
const appId = agent.appId as number;
|
|
323
|
+
const identity = identityOf(agent);
|
|
320
324
|
const added: string[] = [];
|
|
321
325
|
const removed: string[] = [];
|
|
322
326
|
let configDone = false;
|
|
323
327
|
let error: string | undefined;
|
|
324
328
|
try {
|
|
325
329
|
for (const [org, repos] of groupByOrg(plan.appAdds)) {
|
|
326
|
-
await this.deps.installation.addRepos(appId, org, repos);
|
|
330
|
+
await this.deps.installation.addRepos(appId, org, repos, identity);
|
|
327
331
|
added.push(...repos);
|
|
328
332
|
}
|
|
329
333
|
for (const [org, repos] of groupByOrg(plan.appRemoves)) {
|
|
330
|
-
await this.deps.installation.removeRepos(appId, org, repos);
|
|
334
|
+
await this.deps.installation.removeRepos(appId, org, repos, identity);
|
|
331
335
|
removed.push(...repos);
|
|
332
336
|
}
|
|
333
337
|
if (plan.agentRemoves.length > 0) {
|
|
@@ -385,7 +389,10 @@ export class GithubSyncService {
|
|
|
385
389
|
const skip = accessSkipReason(username, preview.access);
|
|
386
390
|
if (skip) return skippedPlan(skip);
|
|
387
391
|
if (agent.appId === undefined) return skippedPlan(MISSING_APP_ID_REASON);
|
|
388
|
-
const installed = await this.deps.installation.listInstalled(
|
|
392
|
+
const installed = await this.deps.installation.listInstalled(
|
|
393
|
+
agent.appId,
|
|
394
|
+
identityOf(agent),
|
|
395
|
+
);
|
|
389
396
|
return planGithubSync({
|
|
390
397
|
username,
|
|
391
398
|
access: preview.access,
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/installation-ports-fixture.integration.test.ts
|
|
3
|
+
* Installation ports driven by the synthetic enterprise-organization-installation
|
|
4
|
+
* fixture (no app_id), mapped through the real client mapper. No network, no DB.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { describe, expect, it } from "bun:test";
|
|
8
|
+
import { toInstallation } from "./github-enterprise-client.ts";
|
|
9
|
+
import enterpriseOrgInstallations from "./fixtures/enterprise-org-installations.json";
|
|
10
|
+
import { createInstallationReposPort } from "./installation-repos-service.ts";
|
|
11
|
+
import {
|
|
12
|
+
AppNotInstalledError,
|
|
13
|
+
createInstallationPort,
|
|
14
|
+
} from "./installation-ports.ts";
|
|
15
|
+
|
|
16
|
+
const ORG = "example-org";
|
|
17
|
+
const CLIENT_ID = "Iv1.0000000000000000";
|
|
18
|
+
const SLUG = "example-agent-app";
|
|
19
|
+
|
|
20
|
+
function harness() {
|
|
21
|
+
const calls: string[] = [];
|
|
22
|
+
const port = createInstallationPort({
|
|
23
|
+
listOrgInstallations: async () =>
|
|
24
|
+
enterpriseOrgInstallations.map((r) =>
|
|
25
|
+
toInstallation(r as Parameters<typeof toInstallation>[0]),
|
|
26
|
+
),
|
|
27
|
+
listInstallationRepositories: async (_o, id) => {
|
|
28
|
+
calls.push(`list:${id}`);
|
|
29
|
+
return [{ fullName: `${ORG}/repo-a` }];
|
|
30
|
+
},
|
|
31
|
+
addRepositories: async (_o, id, names) => {
|
|
32
|
+
calls.push(`add:${id}:${names.join(",")}`);
|
|
33
|
+
},
|
|
34
|
+
removeRepositories: async (_o, id, names) => {
|
|
35
|
+
calls.push(`rm:${id}:${names.join(",")}`);
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
const reposPort = createInstallationReposPort(port, {
|
|
39
|
+
listInstallableOrganizations: async () => [{ login: ORG }],
|
|
40
|
+
});
|
|
41
|
+
return { calls, port, reposPort };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
describe("installation ports with the no-app_id fixture", () => {
|
|
45
|
+
for (const [label, identity] of [
|
|
46
|
+
["client id", { clientId: CLIENT_ID }],
|
|
47
|
+
["slug", { slug: SLUG.toUpperCase() }],
|
|
48
|
+
] as const) {
|
|
49
|
+
it(`finds the installation by ${label}`, async () => {
|
|
50
|
+
const { calls, port, reposPort } = harness();
|
|
51
|
+
expect(await port.listRepos(7, [ORG], identity)).toEqual({
|
|
52
|
+
[ORG]: [`${ORG}/repo-a`],
|
|
53
|
+
});
|
|
54
|
+
await port.addRepos(7, ORG, [`${ORG}/repo-b`], identity);
|
|
55
|
+
await port.removeRepos(7, ORG, [`${ORG}/repo-c`], identity);
|
|
56
|
+
expect(await reposPort.listInstalled(7, identity)).toEqual([
|
|
57
|
+
{ org: ORG, repos: [`${ORG}/repo-a`] },
|
|
58
|
+
]);
|
|
59
|
+
expect(calls).toEqual([
|
|
60
|
+
"list:1000001",
|
|
61
|
+
"add:1000001:repo-b",
|
|
62
|
+
"rm:1000001:repo-c",
|
|
63
|
+
"list:1000001",
|
|
64
|
+
]);
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
it("lists no orgs and throws for an agent with no identifier", async () => {
|
|
69
|
+
const { port, reposPort } = harness();
|
|
70
|
+
await expect(port.listRepos(7, [ORG])).rejects.toBeInstanceOf(
|
|
71
|
+
AppNotInstalledError,
|
|
72
|
+
);
|
|
73
|
+
await expect(
|
|
74
|
+
port.addRepos(7, ORG, [`${ORG}/x`], {}),
|
|
75
|
+
).rejects.toBeInstanceOf(AppNotInstalledError);
|
|
76
|
+
expect(await reposPort.listInstalled(7)).toEqual([]);
|
|
77
|
+
});
|
|
78
|
+
});
|
|
@@ -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
|
);
|
|
@@ -9,6 +9,7 @@ import type { GithubAccessPreviewFn } from "./github-access-preview.ts";
|
|
|
9
9
|
import {
|
|
10
10
|
AppNotInstalledError,
|
|
11
11
|
MISSING_APP_ID_REASON,
|
|
12
|
+
type AppIdentity,
|
|
12
13
|
type InstallationAgentsPort,
|
|
13
14
|
type AppInstallationPort,
|
|
14
15
|
} from "./installation-ports.ts";
|
|
@@ -19,7 +20,10 @@ import type {
|
|
|
19
20
|
|
|
20
21
|
/** Installation port plus discovery of the orgs the agent App is installed on. */
|
|
21
22
|
export interface InstallationReposPort extends AppInstallationPort {
|
|
22
|
-
listInstalled(
|
|
23
|
+
listInstalled(
|
|
24
|
+
appId: number,
|
|
25
|
+
identity?: AppIdentity,
|
|
26
|
+
): Promise<{ org: string; repos: string[] }[]>;
|
|
23
27
|
}
|
|
24
28
|
|
|
25
29
|
export interface InstallationRepoView {
|
|
@@ -44,6 +48,17 @@ export interface InstallationReposDeps {
|
|
|
44
48
|
installation: InstallationReposPort;
|
|
45
49
|
}
|
|
46
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
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
47
62
|
const orgKey = (org: string): string => org.toLowerCase();
|
|
48
63
|
|
|
49
64
|
/**
|
|
@@ -88,7 +103,10 @@ export class InstallationReposService {
|
|
|
88
103
|
if (agent.appId === undefined) {
|
|
89
104
|
return { status: "no_app_id", reason: MISSING_APP_ID_REASON };
|
|
90
105
|
}
|
|
91
|
-
const installed = await this.deps.installation.listInstalled(
|
|
106
|
+
const installed = await this.deps.installation.listInstalled(
|
|
107
|
+
agent.appId,
|
|
108
|
+
identityOf(agent),
|
|
109
|
+
);
|
|
92
110
|
const username = agent.githubUsername?.trim();
|
|
93
111
|
let access: UserRepoAccessResult | undefined;
|
|
94
112
|
if (username) {
|
|
@@ -125,11 +143,11 @@ export function createInstallationReposPort(
|
|
|
125
143
|
): InstallationReposPort {
|
|
126
144
|
return {
|
|
127
145
|
...base,
|
|
128
|
-
async listInstalled(appId) {
|
|
146
|
+
async listInstalled(appId, identity) {
|
|
129
147
|
const out: { org: string; repos: string[] }[] = [];
|
|
130
148
|
for (const { login } of await client.listInstallableOrganizations()) {
|
|
131
149
|
try {
|
|
132
|
-
const repos = await base.listRepos(appId, [login]);
|
|
150
|
+
const repos = await base.listRepos(appId, [login], identity);
|
|
133
151
|
out.push({ org: login, repos: repos[login] ?? [] });
|
|
134
152
|
} catch (err) {
|
|
135
153
|
if (err instanceof AppNotInstalledError) continue;
|