@app-vitals/agent-admin 1.20.1 → 1.22.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 +15 -1
- package/package.json +2 -2
- package/src/admin-ui-github-enterprise-pages.ts +27 -5
- package/src/admin-ui-github-enterprise-pages.unit.test.ts +44 -0
- package/src/admin-ui-github-enterprise.smoke.test.ts +21 -0
- package/src/admin-ui-github-enterprise.ts +12 -2
- package/src/fixtures/github-enterprise-cassette.json +37 -0
- package/src/github-enterprise-api.integration.test.ts +39 -0
- package/src/github-enterprise-api.smoke.test.ts +179 -0
- package/src/github-enterprise-api.ts +110 -47
- package/src/github-enterprise-client.integration.test.ts +14 -0
- package/src/github-enterprise-client.ts +22 -7
- package/src/github-enterprise-warnings.ts +60 -0
- package/src/github-enterprise-warnings.unit.test.ts +58 -0
package/openapi.platform.json
CHANGED
|
@@ -3649,6 +3649,13 @@
|
|
|
3649
3649
|
"null"
|
|
3650
3650
|
]
|
|
3651
3651
|
},
|
|
3652
|
+
"warnings": {
|
|
3653
|
+
"type": "array",
|
|
3654
|
+
"items": {
|
|
3655
|
+
"type": "string"
|
|
3656
|
+
},
|
|
3657
|
+
"description": "Advisory, non-failing notes (e.g. missing optional permissions). Never change `ok`."
|
|
3658
|
+
},
|
|
3652
3659
|
"app": {
|
|
3653
3660
|
"$ref": "#/components/schemas/GithubEnterpriseApp"
|
|
3654
3661
|
}
|
|
@@ -3675,6 +3682,13 @@
|
|
|
3675
3682
|
"null"
|
|
3676
3683
|
]
|
|
3677
3684
|
},
|
|
3685
|
+
"agentAppReason": {
|
|
3686
|
+
"type": [
|
|
3687
|
+
"string",
|
|
3688
|
+
"null"
|
|
3689
|
+
],
|
|
3690
|
+
"description": "Why the agent's App cannot be installed through the enterprise API (private App with no stored GH_APP_CLIENT_ID, or no App at all). Null when it can; present only with agentId."
|
|
3691
|
+
},
|
|
3678
3692
|
"orgs": {
|
|
3679
3693
|
"type": "array",
|
|
3680
3694
|
"items": {
|
|
@@ -7293,7 +7307,7 @@
|
|
|
7293
7307
|
"github-enterprise"
|
|
7294
7308
|
],
|
|
7295
7309
|
"summary": "Install an agent's App on an organization",
|
|
7296
|
-
"description": "Admin-only.
|
|
7310
|
+
"description": "Admin-only. Uses the agent's stored GH_APP_CLIENT_ID when set; otherwise resolves the stored GH_APP_SLUG and looks the App up through the public GET /apps/{slug} to get its client id. Installs it on the org with the installer App. 422 for an agent with neither a stored client id nor a slug, and for a private App (not visible to the public lookup) with no stored client id; 409 when the installer App is not configured.",
|
|
7297
7311
|
"parameters": [
|
|
7298
7312
|
{
|
|
7299
7313
|
"schema": {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@app-vitals/agent-admin",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.22.0",
|
|
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.
|
|
45
|
+
"@app-vitals/agent-lib": "1.22.0",
|
|
46
46
|
"@hono/zod-openapi": "^1.0.0",
|
|
47
47
|
"@prisma/adapter-pg": "^7.10.0",
|
|
48
48
|
"@prisma/client": "^7.10.0",
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
* never renders a key (the status API only reports `keySet`).
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
+
import { GHE_WARNINGS, type GheWarningCode } from "./github-enterprise-warnings.ts";
|
|
10
11
|
import { renderAdminPage } from "./admin-ui-layout.ts";
|
|
11
12
|
import { escapeHtml, renderAdminToolbar } from "./admin-ui-styles.ts";
|
|
12
13
|
|
|
@@ -42,6 +43,8 @@ export interface GheOrg {
|
|
|
42
43
|
export interface GheOrgs {
|
|
43
44
|
configured: boolean;
|
|
44
45
|
agentAppSlug?: string | null;
|
|
46
|
+
/** Why the agent's App cannot be installed (e.g. private, no client id). */
|
|
47
|
+
agentAppReason?: string | null;
|
|
45
48
|
orgs: GheOrg[];
|
|
46
49
|
}
|
|
47
50
|
|
|
@@ -56,6 +59,8 @@ export interface GithubEnterpriseSettingsPageOpts {
|
|
|
56
59
|
agentId?: string;
|
|
57
60
|
error?: string;
|
|
58
61
|
notice?: string;
|
|
62
|
+
/** Warning codes from the last validate (carried via ?warn=); unknown codes are ignored. */
|
|
63
|
+
warnings?: string[];
|
|
59
64
|
}
|
|
60
65
|
|
|
61
66
|
const BASE = ADMIN_GITHUB_ENTERPRISE_PATH;
|
|
@@ -105,12 +110,26 @@ function postButton(
|
|
|
105
110
|
return `<form method="POST" action="${action}" style="display:inline;margin:0"><button type="submit" class="btn ${cls}" style="font-size:12px;padding:4px 10px">${label}</button></form>`;
|
|
106
111
|
}
|
|
107
112
|
|
|
108
|
-
function
|
|
113
|
+
function warningsBlock(role: "installer" | "lookup", codes: string[] | undefined): string {
|
|
114
|
+
return (codes ?? [])
|
|
115
|
+
.filter((c): c is GheWarningCode => c in GHE_WARNINGS && c.startsWith(`${role}_`))
|
|
116
|
+
.map(
|
|
117
|
+
(c) =>
|
|
118
|
+
`<div class="help-text gh-ent-warning" style="font-size:12px;margin-top:4px">Warning: ${escapeHtml(GHE_WARNINGS[c])}</div>`,
|
|
119
|
+
)
|
|
120
|
+
.join("");
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function appCard(
|
|
124
|
+
role: "installer" | "lookup",
|
|
125
|
+
app: GheApp | null,
|
|
126
|
+
warnings?: string[],
|
|
127
|
+
): string {
|
|
109
128
|
const copy = ROLE_COPY[role];
|
|
110
129
|
const action = `${BASE}/apps/${role}`;
|
|
111
130
|
const summary = app
|
|
112
131
|
? `<p style="margin:8px 0">App ID <strong>${escapeHtml(app.appId)}</strong> · slug <strong>${escapeHtml(app.slug)}</strong>${app.enterpriseSlug ? ` · enterprise <strong>${escapeHtml(app.enterpriseSlug)}</strong>` : ""}</p>
|
|
113
|
-
${statusLine(app)}
|
|
132
|
+
${statusLine(app)}${warningsBlock(role, warnings)}
|
|
114
133
|
<div style="display:flex;gap:8px;margin:12px 0">${postButton(`${action}/validate`, "Validate")}${postButton(`${action}/delete`, "Delete", "btn-danger")}</div>`
|
|
115
134
|
: `<p class="help-text" style="font-size:12px;margin:8px 0">Not configured.</p>`;
|
|
116
135
|
const enterprise =
|
|
@@ -177,7 +196,10 @@ function orgsCard(opts: GithubEnterpriseSettingsPageOpts): string {
|
|
|
177
196
|
</tr>`;
|
|
178
197
|
})
|
|
179
198
|
.join("\n");
|
|
180
|
-
|
|
199
|
+
const reason = opts.orgs?.agentAppReason
|
|
200
|
+
? `<div class="alert alert-error">${escapeHtml(opts.orgs.agentAppReason)}</div>`
|
|
201
|
+
: "";
|
|
202
|
+
body = `${reason}<table class="data-table">
|
|
181
203
|
<thead><tr><th>Organization</th><th>Lookup app</th><th>Agent app</th><th></th></tr></thead>
|
|
182
204
|
<tbody>${rows}</tbody>
|
|
183
205
|
</table>`;
|
|
@@ -211,8 +233,8 @@ export function renderGithubEnterpriseSettingsPage(
|
|
|
211
233
|
<div class="vos-page">
|
|
212
234
|
<div class="page-header"><h1 class="page-title">GitHub Enterprise apps</h1></div>
|
|
213
235
|
${error}${notice}${emptyState}
|
|
214
|
-
${appCard("installer", status.installer)}
|
|
215
|
-
${appCard("lookup", status.lookup)}
|
|
236
|
+
${appCard("installer", status.installer, opts.warnings)}
|
|
237
|
+
${appCard("lookup", status.lookup, opts.warnings)}
|
|
216
238
|
${orgsCard(opts)}
|
|
217
239
|
</div>`,
|
|
218
240
|
});
|
|
@@ -102,6 +102,27 @@ describe("renderGithubEnterpriseSettingsPage", () => {
|
|
|
102
102
|
expect(html).not.toContain("<b>x</b>");
|
|
103
103
|
});
|
|
104
104
|
|
|
105
|
+
test("warnings render on the matching card only, as non-error notes", () => {
|
|
106
|
+
const html = renderGithubEnterpriseSettingsPage({
|
|
107
|
+
...base,
|
|
108
|
+
status: { configured: true, installer: app(), lookup: app({ role: "lookup", enterpriseSlug: null }) },
|
|
109
|
+
warnings: ["installer_repositories_write"],
|
|
110
|
+
});
|
|
111
|
+
expect(html).toContain("enterprise_organization_installation_repositories");
|
|
112
|
+
expect(html.match(/gh-ent-warning/g)?.length).toBe(1);
|
|
113
|
+
expect(html).not.toContain("Organization Members");
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
test("unknown warning codes are ignored", () => {
|
|
117
|
+
const html = renderGithubEnterpriseSettingsPage({
|
|
118
|
+
...base,
|
|
119
|
+
status: { configured: true, installer: app(), lookup: null },
|
|
120
|
+
warnings: ["<script>alert(1)</script>"],
|
|
121
|
+
});
|
|
122
|
+
expect(html).not.toContain("gh-ent-warning");
|
|
123
|
+
expect(html).not.toContain("<script>alert");
|
|
124
|
+
});
|
|
125
|
+
|
|
105
126
|
test("key not set shows 'Key not set'", () => {
|
|
106
127
|
const html = renderGithubEnterpriseSettingsPage({
|
|
107
128
|
...base,
|
|
@@ -159,6 +180,29 @@ describe("renderGithubEnterpriseSettingsPage", () => {
|
|
|
159
180
|
expect(html).toContain("Installed");
|
|
160
181
|
});
|
|
161
182
|
|
|
183
|
+
test("check agent shows the private-App reason, escaped", () => {
|
|
184
|
+
const html = renderGithubEnterpriseSettingsPage({
|
|
185
|
+
...base,
|
|
186
|
+
agentId: "agent-1",
|
|
187
|
+
status: { configured: true, installer: app(), lookup: null },
|
|
188
|
+
orgs: {
|
|
189
|
+
configured: true,
|
|
190
|
+
agentAppSlug: "agent-app",
|
|
191
|
+
agentAppReason: "the agent's App 'agent-app' is private <b>",
|
|
192
|
+
orgs: [
|
|
193
|
+
{
|
|
194
|
+
id: 1,
|
|
195
|
+
login: "org-a",
|
|
196
|
+
lookupAppInstalled: null,
|
|
197
|
+
agentAppInstalled: false,
|
|
198
|
+
},
|
|
199
|
+
],
|
|
200
|
+
},
|
|
201
|
+
});
|
|
202
|
+
expect(html).toContain("is private <b>");
|
|
203
|
+
expect(html).not.toContain("private <b>");
|
|
204
|
+
});
|
|
205
|
+
|
|
162
206
|
test("org list failure and top-level error/notice are escaped messages", () => {
|
|
163
207
|
const html = renderGithubEnterpriseSettingsPage({
|
|
164
208
|
...base,
|
|
@@ -97,6 +97,7 @@ function setup(opts: { isAdmin?: boolean; fetchStatus?: number } = {}) {
|
|
|
97
97
|
expires_at: "2099-01-01T00:00:00Z",
|
|
98
98
|
permissions: { enterprise_organization_installations: "write" },
|
|
99
99
|
});
|
|
100
|
+
if (url.endsWith("/installation")) return json({ id: 1 });
|
|
100
101
|
return json({});
|
|
101
102
|
}) as unknown as typeof fetch;
|
|
102
103
|
const api = createGithubEnterpriseApp({
|
|
@@ -219,6 +220,26 @@ describe("form posts", () => {
|
|
|
219
220
|
expect(html).not.toContain("Not validated yet");
|
|
220
221
|
});
|
|
221
222
|
|
|
223
|
+
it("shows non-failing permission warnings on the card after validate", async () => {
|
|
224
|
+
const { app } = setup();
|
|
225
|
+
await app.request(
|
|
226
|
+
`${BASE}/apps/installer`,
|
|
227
|
+
form({
|
|
228
|
+
appId: "42",
|
|
229
|
+
slug: "acme-inst",
|
|
230
|
+
enterpriseSlug: "acme",
|
|
231
|
+
privateKey: PEM,
|
|
232
|
+
}),
|
|
233
|
+
);
|
|
234
|
+
const res = await app.request(`${BASE}/apps/installer/validate`, form({}));
|
|
235
|
+
expect(res.status).toBe(302);
|
|
236
|
+
const loc = res.headers.get("location") as string;
|
|
237
|
+
expect(loc).toContain("warn=installer_repositories_write");
|
|
238
|
+
const html = await (await app.request(loc)).text();
|
|
239
|
+
expect(html).toContain("enterprise_organization_installation_repositories");
|
|
240
|
+
expect(html).toContain("gh-ent-warning");
|
|
241
|
+
});
|
|
242
|
+
|
|
222
243
|
it("validate on an unconfigured role is a readable 404 message", async () => {
|
|
223
244
|
const { app } = setup();
|
|
224
245
|
const res = await app.request(`${BASE}/apps/lookup/validate`, form({}));
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
import type { Context, Hono, MiddlewareHandler } from "hono";
|
|
11
|
+
import { warningCodeFor } from "./github-enterprise-warnings.ts";
|
|
11
12
|
import type { AdminUIEnv } from "./admin-ui.ts";
|
|
12
13
|
import {
|
|
13
14
|
ADMIN_GITHUB_ENTERPRISE_PATH as BASE,
|
|
@@ -127,6 +128,7 @@ export function registerAdminGithubEnterpriseRoutes(
|
|
|
127
128
|
agentId,
|
|
128
129
|
error: flash.error,
|
|
129
130
|
notice,
|
|
131
|
+
warnings: c.req.query("warn")?.split(",").filter(Boolean),
|
|
130
132
|
}),
|
|
131
133
|
{ status: flash.status ?? 200 },
|
|
132
134
|
);
|
|
@@ -173,12 +175,20 @@ export function registerAdminGithubEnterpriseRoutes(
|
|
|
173
175
|
async (c) => {
|
|
174
176
|
const role = c.req.param("role");
|
|
175
177
|
if (!isRole(role)) return new Response("Not Found", { status: 404 });
|
|
176
|
-
const r = await call(
|
|
178
|
+
const r = await call<{ warnings?: string[] }>(
|
|
177
179
|
c,
|
|
178
180
|
"POST",
|
|
179
181
|
`/github-enterprise/apps/${role}/validate`,
|
|
180
182
|
);
|
|
181
|
-
|
|
183
|
+
if (!r.ok) return fail(c, r);
|
|
184
|
+
// Warnings ride the redirect as stable codes, never free text.
|
|
185
|
+
const codes = (r.data?.warnings ?? [])
|
|
186
|
+
.map(warningCodeFor)
|
|
187
|
+
.filter((x): x is NonNullable<typeof x> => x !== undefined);
|
|
188
|
+
return c.redirect(
|
|
189
|
+
`${BASE}?notice=validated${codes.length ? `&warn=${codes.join(",")}` : ""}`,
|
|
190
|
+
302,
|
|
191
|
+
);
|
|
182
192
|
},
|
|
183
193
|
);
|
|
184
194
|
|
|
@@ -671,5 +671,42 @@
|
|
|
671
671
|
},
|
|
672
672
|
"repository_selection": "all"
|
|
673
673
|
}
|
|
674
|
+
},
|
|
675
|
+
"access_token_all_permissions": {
|
|
676
|
+
"status": 201,
|
|
677
|
+
"body": {
|
|
678
|
+
"token": "ghs_cassetteInstallationToken0000000000",
|
|
679
|
+
"expires_at": "2026-11-09T13:00:00Z",
|
|
680
|
+
"permissions": {
|
|
681
|
+
"enterprise_organization_installations": "write",
|
|
682
|
+
"enterprise_organization_installation_repositories": "write",
|
|
683
|
+
"metadata": "read"
|
|
684
|
+
},
|
|
685
|
+
"repository_selection": "all"
|
|
686
|
+
}
|
|
687
|
+
},
|
|
688
|
+
"access_token_repositories_read_only": {
|
|
689
|
+
"status": 201,
|
|
690
|
+
"body": {
|
|
691
|
+
"token": "ghs_cassetteInstallationToken0000000000",
|
|
692
|
+
"expires_at": "2026-11-09T13:00:00Z",
|
|
693
|
+
"permissions": {
|
|
694
|
+
"enterprise_organization_installations": "write",
|
|
695
|
+
"enterprise_organization_installation_repositories": "read",
|
|
696
|
+
"metadata": "read"
|
|
697
|
+
},
|
|
698
|
+
"repository_selection": "all"
|
|
699
|
+
}
|
|
700
|
+
},
|
|
701
|
+
"app_get_all_permissions": {
|
|
702
|
+
"status": 200,
|
|
703
|
+
"body": {
|
|
704
|
+
"id": 555,
|
|
705
|
+
"slug": "example-lookup-app",
|
|
706
|
+
"permissions": {
|
|
707
|
+
"metadata": "read",
|
|
708
|
+
"members": "read"
|
|
709
|
+
}
|
|
710
|
+
}
|
|
674
711
|
}
|
|
675
712
|
}
|
|
@@ -207,6 +207,45 @@ describeOrSkip("github-enterprise API (integration)", () => {
|
|
|
207
207
|
expect(await res.json()).toMatchObject({ created: true, org: "org-1" });
|
|
208
208
|
});
|
|
209
209
|
|
|
210
|
+
it("install prefers the stored GH_APP_CLIENT_ID and skips the public lookup", async () => {
|
|
211
|
+
const agent = await prisma.agent.create({ data: { name: "Ghe Agent" } });
|
|
212
|
+
await new AgentEnvService(prisma, identityCrypto).upsert(agent.id, {
|
|
213
|
+
GH_APP_SLUG: "example-agent-app",
|
|
214
|
+
GH_APP_CLIENT_ID: "Iv1.targetapp0001",
|
|
215
|
+
});
|
|
216
|
+
// No "GET /apps/..." route: a public lookup would throw.
|
|
217
|
+
const app = build({
|
|
218
|
+
"POST /enterprises/acme/apps/organizations/org-1/installations":
|
|
219
|
+
"install_created",
|
|
220
|
+
});
|
|
221
|
+
await put(app);
|
|
222
|
+
const res = await app.request("/github-enterprise/orgs/org-1/install", {
|
|
223
|
+
method: "POST",
|
|
224
|
+
headers: { ...ADMIN, "Content-Type": "application/json" },
|
|
225
|
+
body: JSON.stringify({ agentId: agent.id }),
|
|
226
|
+
});
|
|
227
|
+
expect(res.status).toBe(200);
|
|
228
|
+
expect(await res.json()).toMatchObject({ created: true, org: "org-1" });
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
it("install refuses a private App when no client id is stored", async () => {
|
|
232
|
+
const agent = await prisma.agent.create({ data: { name: "Ghe Agent" } });
|
|
233
|
+
await new AgentEnvService(prisma, identityCrypto).upsert(agent.id, {
|
|
234
|
+
GH_APP_SLUG: "example-agent-app",
|
|
235
|
+
});
|
|
236
|
+
const app = build({
|
|
237
|
+
"GET /apps/example-agent-app": "public_app_private_404",
|
|
238
|
+
});
|
|
239
|
+
await put(app);
|
|
240
|
+
const res = await app.request("/github-enterprise/orgs/org-1/install", {
|
|
241
|
+
method: "POST",
|
|
242
|
+
headers: { ...ADMIN, "Content-Type": "application/json" },
|
|
243
|
+
body: JSON.stringify({ agentId: agent.id }),
|
|
244
|
+
});
|
|
245
|
+
expect(res.status).toBe(422);
|
|
246
|
+
expect(((await res.json()) as { error: string }).error).toMatch(/private/i);
|
|
247
|
+
});
|
|
248
|
+
|
|
210
249
|
it("isolation: no agent-facing path returns the stored key", async () => {
|
|
211
250
|
const agent = await prisma.agent.create({ data: { name: "Iso Agent" } });
|
|
212
251
|
const envService = new AgentEnvService(prisma, identityCrypto);
|
|
@@ -381,6 +381,110 @@ describe("POST validate", () => {
|
|
|
381
381
|
);
|
|
382
382
|
});
|
|
383
383
|
|
|
384
|
+
it("installer: a 404 on the installation lookup names the enterprise and the likely causes", async () => {
|
|
385
|
+
const h = build({ routes: { [ENT]: "enterprise_installation_404" } });
|
|
386
|
+
await seed(h);
|
|
387
|
+
const res = await h.app.request(
|
|
388
|
+
"/github-enterprise/apps/installer/validate",
|
|
389
|
+
{ method: "POST", headers: ADMIN },
|
|
390
|
+
);
|
|
391
|
+
const body = (await res.json()) as { ok: boolean; status: string; error: string };
|
|
392
|
+
expect(body).toMatchObject({ ok: false, status: "error" });
|
|
393
|
+
expect(body.error).toContain("not installed on enterprise acme");
|
|
394
|
+
expect(body.error).toContain("slug");
|
|
395
|
+
expect(h.appService.rows.get("installer")?.lastValidationError).toBe(body.error);
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
it("installer: a 404 on the token mint keeps the generic reason", async () => {
|
|
399
|
+
const h = build({ routes: { [MINT]: "access_token_404" } });
|
|
400
|
+
await seed(h);
|
|
401
|
+
const res = await h.app.request(
|
|
402
|
+
"/github-enterprise/apps/installer/validate",
|
|
403
|
+
{ method: "POST", headers: ADMIN },
|
|
404
|
+
);
|
|
405
|
+
const body = (await res.json()) as { error: string };
|
|
406
|
+
expect(body.error).toBe(
|
|
407
|
+
"GitHub could not find the enterprise, organization or App for this request.",
|
|
408
|
+
);
|
|
409
|
+
});
|
|
410
|
+
|
|
411
|
+
it("installer: warns (still valid) when the repositories permission is missing", async () => {
|
|
412
|
+
const h = build();
|
|
413
|
+
await seed(h);
|
|
414
|
+
const res = await h.app.request(
|
|
415
|
+
"/github-enterprise/apps/installer/validate",
|
|
416
|
+
{ method: "POST", headers: ADMIN },
|
|
417
|
+
);
|
|
418
|
+
const body = (await res.json()) as { ok: boolean; status: string; warnings: string[] };
|
|
419
|
+
expect(body).toMatchObject({ ok: true, status: "valid" });
|
|
420
|
+
expect(body.warnings).toHaveLength(1);
|
|
421
|
+
expect(body.warnings[0]).toContain("enterprise_organization_installation_repositories");
|
|
422
|
+
});
|
|
423
|
+
|
|
424
|
+
it("installer: read-only repositories permission also warns; write does not", async () => {
|
|
425
|
+
const ro = build({ routes: { [MINT]: "access_token_repositories_read_only" } });
|
|
426
|
+
await seed(ro);
|
|
427
|
+
const r1 = await ro.app.request("/github-enterprise/apps/installer/validate", {
|
|
428
|
+
method: "POST",
|
|
429
|
+
headers: ADMIN,
|
|
430
|
+
});
|
|
431
|
+
expect(((await r1.json()) as { warnings: string[] }).warnings).toHaveLength(1);
|
|
432
|
+
|
|
433
|
+
const full = build({ routes: { [MINT]: "access_token_all_permissions" } });
|
|
434
|
+
await seed(full);
|
|
435
|
+
const r2 = await full.app.request("/github-enterprise/apps/installer/validate", {
|
|
436
|
+
method: "POST",
|
|
437
|
+
headers: ADMIN,
|
|
438
|
+
});
|
|
439
|
+
expect(await r2.json()).toMatchObject({ ok: true, status: "valid", warnings: [] });
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
it("lookup: warns (still valid) without Organization Members read; none with it", async () => {
|
|
443
|
+
const seedLookup = (h: ReturnType<typeof build>) =>
|
|
444
|
+
h.appService.save({
|
|
445
|
+
role: "lookup",
|
|
446
|
+
appId: "555",
|
|
447
|
+
slug: "example-lookup-app",
|
|
448
|
+
privateKey: PEM,
|
|
449
|
+
});
|
|
450
|
+
const h = build({ routes: { "GET /app": "app_get_success" } });
|
|
451
|
+
await seedLookup(h);
|
|
452
|
+
const res = await h.app.request("/github-enterprise/apps/lookup/validate", {
|
|
453
|
+
method: "POST",
|
|
454
|
+
headers: ADMIN,
|
|
455
|
+
});
|
|
456
|
+
const body = (await res.json()) as { ok: boolean; warnings: string[] };
|
|
457
|
+
expect(body.ok).toBe(true);
|
|
458
|
+
expect(body.warnings).toHaveLength(1);
|
|
459
|
+
expect(body.warnings[0]).toContain("Members");
|
|
460
|
+
|
|
461
|
+
const h2 = build({ routes: { "GET /app": "app_get_all_permissions" } });
|
|
462
|
+
await seedLookup(h2);
|
|
463
|
+
const res2 = await h2.app.request("/github-enterprise/apps/lookup/validate", {
|
|
464
|
+
method: "POST",
|
|
465
|
+
headers: ADMIN,
|
|
466
|
+
});
|
|
467
|
+
expect(await res2.json()).toMatchObject({ ok: true, status: "valid", warnings: [] });
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
it("warnings never flip validity: a missing required permission stays invalid, an error has none", async () => {
|
|
471
|
+
const h = build({ routes: { [MINT]: "access_token_no_installer_permission" } });
|
|
472
|
+
await seed(h);
|
|
473
|
+
const res = await h.app.request("/github-enterprise/apps/installer/validate", {
|
|
474
|
+
method: "POST",
|
|
475
|
+
headers: ADMIN,
|
|
476
|
+
});
|
|
477
|
+
expect(await res.json()).toMatchObject({ ok: false, status: "missing_permission" });
|
|
478
|
+
|
|
479
|
+
const e = build({ routes: { [ENT]: "enterprise_installation_403" } });
|
|
480
|
+
await seed(e);
|
|
481
|
+
const r = await e.app.request("/github-enterprise/apps/installer/validate", {
|
|
482
|
+
method: "POST",
|
|
483
|
+
headers: ADMIN,
|
|
484
|
+
});
|
|
485
|
+
expect(await r.json()).toMatchObject({ ok: false, status: "error", warnings: [] });
|
|
486
|
+
});
|
|
487
|
+
|
|
384
488
|
it("lookup: checks metadata read via GET /app", async () => {
|
|
385
489
|
const h = build({ routes: { "GET /app": "app_get_success" } });
|
|
386
490
|
await h.appService.save({
|
|
@@ -452,6 +556,53 @@ describe("GET orgs", () => {
|
|
|
452
556
|
});
|
|
453
557
|
});
|
|
454
558
|
|
|
559
|
+
it("check agent reports the private-App reason when no client id is stored", async () => {
|
|
560
|
+
const h = build({
|
|
561
|
+
routes: {
|
|
562
|
+
[ORGS]: "installable_orgs_success",
|
|
563
|
+
[INSTS]: "org_installations_success",
|
|
564
|
+
"GET /enterprises/acme/apps/organizations/org-2/installations":
|
|
565
|
+
"installable_orgs_empty",
|
|
566
|
+
"GET /apps/example-agent-app": "public_app_private_404",
|
|
567
|
+
},
|
|
568
|
+
});
|
|
569
|
+
await seed(h);
|
|
570
|
+
const res = await h.app.request("/github-enterprise/orgs?agentId=agent-1", {
|
|
571
|
+
headers: ADMIN,
|
|
572
|
+
});
|
|
573
|
+
expect(res.status).toBe(200);
|
|
574
|
+
const body = (await res.json()) as { agentAppReason: string | null };
|
|
575
|
+
expect(body.agentAppReason).toMatch(/private/i);
|
|
576
|
+
});
|
|
577
|
+
|
|
578
|
+
it("check agent has no reason when a client id is stored (no public lookup)", async () => {
|
|
579
|
+
const h = build({
|
|
580
|
+
agents: {
|
|
581
|
+
"agent-1": {
|
|
582
|
+
GH_APP_SLUG: "example-agent-app",
|
|
583
|
+
GH_APP_CLIENT_ID: "Iv1.targetapp0001",
|
|
584
|
+
},
|
|
585
|
+
},
|
|
586
|
+
routes: {
|
|
587
|
+
[ORGS]: "installable_orgs_success",
|
|
588
|
+
[INSTS]: "org_installations_success",
|
|
589
|
+
"GET /enterprises/acme/apps/organizations/org-2/installations":
|
|
590
|
+
"installable_orgs_empty",
|
|
591
|
+
},
|
|
592
|
+
});
|
|
593
|
+
await seed(h);
|
|
594
|
+
const res = await h.app.request("/github-enterprise/orgs?agentId=agent-1", {
|
|
595
|
+
headers: ADMIN,
|
|
596
|
+
});
|
|
597
|
+
expect(res.status).toBe(200);
|
|
598
|
+
expect(
|
|
599
|
+
((await res.json()) as { agentAppReason: string | null }).agentAppReason,
|
|
600
|
+
).toBeNull();
|
|
601
|
+
expect(h.calls.some((k) => k.includes("/apps/example-agent-app"))).toBe(
|
|
602
|
+
false,
|
|
603
|
+
);
|
|
604
|
+
});
|
|
605
|
+
|
|
455
606
|
it("without agentId or lookup app the flags are null", async () => {
|
|
456
607
|
const h = build({
|
|
457
608
|
routes: {
|
|
@@ -519,6 +670,34 @@ describe("POST install", () => {
|
|
|
519
670
|
expect(h.calls).not.toContain(INSTALL);
|
|
520
671
|
});
|
|
521
672
|
|
|
673
|
+
it("uses the agent's stored GH_APP_CLIENT_ID and skips the public lookup", async () => {
|
|
674
|
+
const h = build({
|
|
675
|
+
agents: {
|
|
676
|
+
"agent-1": {
|
|
677
|
+
GH_APP_SLUG: "example-agent-app",
|
|
678
|
+
GH_APP_CLIENT_ID: "Iv1.targetapp0001",
|
|
679
|
+
},
|
|
680
|
+
},
|
|
681
|
+
routes: { [INSTALL]: "install_created" },
|
|
682
|
+
});
|
|
683
|
+
await seed(h);
|
|
684
|
+
const res = await post(h, { agentId: "agent-1" });
|
|
685
|
+
expect(res.status).toBe(200);
|
|
686
|
+
expect(h.calls).toContain(INSTALL);
|
|
687
|
+
expect(h.calls.some((k) => k.includes("/apps/example-agent-app"))).toBe(
|
|
688
|
+
false,
|
|
689
|
+
);
|
|
690
|
+
});
|
|
691
|
+
|
|
692
|
+
it("installs a slug-less agent that has a stored client id", async () => {
|
|
693
|
+
const h = build({
|
|
694
|
+
agents: { "agent-1": { GH_APP_CLIENT_ID: "Iv1.targetapp0001" } },
|
|
695
|
+
routes: { [INSTALL]: "install_created" },
|
|
696
|
+
});
|
|
697
|
+
await seed(h);
|
|
698
|
+
expect((await post(h, { agentId: "agent-1" })).status).toBe(200);
|
|
699
|
+
});
|
|
700
|
+
|
|
522
701
|
it("refuses an App with no client id", async () => {
|
|
523
702
|
const h = build({
|
|
524
703
|
routes: { "GET /apps/example-agent-app": "public_app_no_client_id" },
|
|
@@ -51,6 +51,7 @@ import {
|
|
|
51
51
|
import { GithubLookupClient } from "./github-lookup-client.ts";
|
|
52
52
|
import type { GithubAccessPreviewFn } from "./github-access-preview.ts";
|
|
53
53
|
import { ErrorSchema } from "./openapi-schemas.ts";
|
|
54
|
+
import { resolveValidation } from "./github-enterprise-warnings.ts";
|
|
54
55
|
import { UserRepoAccessResolver } from "./user-repo-access-resolver.ts";
|
|
55
56
|
|
|
56
57
|
// ─── Deps ─────────────────────────────────────────────────────────────────────
|
|
@@ -84,6 +85,8 @@ const REQUIRED_PERMISSION: Record<
|
|
|
84
85
|
};
|
|
85
86
|
|
|
86
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}$/;
|
|
87
90
|
const ORG_RE = /^[A-Za-z0-9](?:[A-Za-z0-9-]{0,38})$/;
|
|
88
91
|
|
|
89
92
|
// ─── Schemas ──────────────────────────────────────────────────────────────────
|
|
@@ -162,6 +165,10 @@ const ValidateResultSchema = z
|
|
|
162
165
|
requiredPermission: z.string(),
|
|
163
166
|
grantedPermissions: z.record(z.string(), z.string()),
|
|
164
167
|
error: z.string().nullable(),
|
|
168
|
+
warnings: z.array(z.string()).optional().openapi({
|
|
169
|
+
description:
|
|
170
|
+
"Advisory, non-failing notes (e.g. missing optional permissions). Never change `ok`.",
|
|
171
|
+
}),
|
|
165
172
|
app: AppMetadataSchema.nullable(),
|
|
166
173
|
})
|
|
167
174
|
.openapi("GithubEnterpriseValidateResult");
|
|
@@ -178,6 +185,14 @@ const OrgsResultSchema = z
|
|
|
178
185
|
.object({
|
|
179
186
|
configured: z.boolean(),
|
|
180
187
|
agentAppSlug: z.string().nullable().optional(),
|
|
188
|
+
agentAppReason: z
|
|
189
|
+
.string()
|
|
190
|
+
.nullable()
|
|
191
|
+
.optional()
|
|
192
|
+
.openapi({
|
|
193
|
+
description:
|
|
194
|
+
"Why the agent's App cannot be installed through the enterprise API (private App with no stored GH_APP_CLIENT_ID, or no App at all). Null when it can; present only with agentId.",
|
|
195
|
+
}),
|
|
181
196
|
orgs: z.array(
|
|
182
197
|
z.object({
|
|
183
198
|
id: z.number(),
|
|
@@ -339,7 +354,7 @@ const installRoute = createRoute({
|
|
|
339
354
|
tags,
|
|
340
355
|
summary: "Install an agent's App on an organization",
|
|
341
356
|
description:
|
|
342
|
-
"Admin-only.
|
|
357
|
+
"Admin-only. Uses the agent's stored GH_APP_CLIENT_ID when set; otherwise resolves the stored GH_APP_SLUG and looks the App up through the public GET /apps/{slug} to get its client id. Installs it on the org with the installer App. 422 for an agent with neither a stored client id nor a slug, and for a private App (not visible to the public lookup) with no stored client id; 409 when the installer App is not configured.",
|
|
343
358
|
request: {
|
|
344
359
|
params: OrgParamSchema,
|
|
345
360
|
body: { content: { "application/json": { schema: InstallBodySchema } } },
|
|
@@ -567,21 +582,54 @@ export function createGithubEnterpriseApp(
|
|
|
567
582
|
});
|
|
568
583
|
};
|
|
569
584
|
|
|
570
|
-
/**
|
|
571
|
-
|
|
585
|
+
/**
|
|
586
|
+
* Agent's stored App slug (null when absent/invalid) and stored client id
|
|
587
|
+
* (null when absent/malformed). 404 when the agent is unknown.
|
|
588
|
+
*/
|
|
589
|
+
const agentApp = async (
|
|
572
590
|
agentId: string,
|
|
573
|
-
): Promise<{
|
|
591
|
+
): Promise<{
|
|
592
|
+
slug: string | null;
|
|
593
|
+
invalid: boolean;
|
|
594
|
+
clientId: string | null;
|
|
595
|
+
}> => {
|
|
574
596
|
if (!(await deps.agentService.getSummary(agentId))) {
|
|
575
597
|
throw new NotFoundError(`agent ${agentId} not found`);
|
|
576
598
|
}
|
|
577
|
-
const
|
|
578
|
-
|
|
579
|
-
|
|
599
|
+
const env = (await deps.agentEnvService.getByAgentId(agentId))?.env;
|
|
600
|
+
const storedClientId = env?.GH_APP_CLIENT_ID?.trim();
|
|
601
|
+
const clientId =
|
|
602
|
+
storedClientId && CLIENT_ID_RE.test(storedClientId)
|
|
603
|
+
? storedClientId
|
|
604
|
+
: null;
|
|
605
|
+
const slug = env?.GH_APP_SLUG;
|
|
606
|
+
if (!slug) return { slug: null, invalid: false, clientId };
|
|
580
607
|
return SLUG_RE.test(slug)
|
|
581
|
-
? { slug, invalid: false }
|
|
582
|
-
: { slug: null, invalid: true };
|
|
608
|
+
? { slug, invalid: false, clientId }
|
|
609
|
+
: { slug: null, invalid: true, clientId };
|
|
583
610
|
};
|
|
584
611
|
|
|
612
|
+
/** Public-lookup client id; reason is set when the App is private (404). */
|
|
613
|
+
const publicClientId = async (
|
|
614
|
+
slug: string,
|
|
615
|
+
): Promise<{ clientId: string | null; privateApp: boolean }> => {
|
|
616
|
+
try {
|
|
617
|
+
const app = await fetchPublicApp(slug, {
|
|
618
|
+
apiBase: deps.apiBase,
|
|
619
|
+
fetchFn: deps.fetchFn,
|
|
620
|
+
});
|
|
621
|
+
return { clientId: app.clientId, privateApp: false };
|
|
622
|
+
} catch (err) {
|
|
623
|
+
if (err instanceof GithubEnterpriseError && err.kind === "not_found") {
|
|
624
|
+
return { clientId: null, privateApp: true };
|
|
625
|
+
}
|
|
626
|
+
throw err;
|
|
627
|
+
}
|
|
628
|
+
};
|
|
629
|
+
|
|
630
|
+
const privateAppMessage = (slug: string) =>
|
|
631
|
+
`the agent's App '${slug}' is private (not visible to the public lookup) and cannot be installed through the enterprise API; store its GH_APP_CLIENT_ID on the agent`;
|
|
632
|
+
|
|
585
633
|
app.openapi(statusRoute, async (c) => {
|
|
586
634
|
requireAdmin(c);
|
|
587
635
|
const rows = await appService.list();
|
|
@@ -644,6 +692,7 @@ export function createGithubEnterpriseApp(
|
|
|
644
692
|
let granted: Record<string, string> = {};
|
|
645
693
|
let status: "valid" | "missing_permission" | "error";
|
|
646
694
|
let error: string | null = null;
|
|
695
|
+
let warnings: string[] = [];
|
|
647
696
|
try {
|
|
648
697
|
if (role === "installer") {
|
|
649
698
|
if (!meta.enterpriseSlug) {
|
|
@@ -671,15 +720,19 @@ export function createGithubEnterpriseApp(
|
|
|
671
720
|
})
|
|
672
721
|
).permissions;
|
|
673
722
|
}
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
error = `the App does not hold ${required.name} (${required.levels.join(" or ")})`;
|
|
679
|
-
}
|
|
723
|
+
const resolved = resolveValidation(role, required, granted);
|
|
724
|
+
status = resolved.status;
|
|
725
|
+
error = resolved.error;
|
|
726
|
+
warnings = resolved.warnings;
|
|
680
727
|
} catch (err) {
|
|
681
728
|
status = "error";
|
|
682
|
-
if (
|
|
729
|
+
if (
|
|
730
|
+
err instanceof GithubEnterpriseError &&
|
|
731
|
+
err.kind === "not_found" &&
|
|
732
|
+
err.step === "installation_lookup"
|
|
733
|
+
) {
|
|
734
|
+
error = `the App is not installed on enterprise ${meta.enterpriseSlug}, or the enterprise slug is wrong`;
|
|
735
|
+
} else if (err instanceof GithubEnterpriseError) {
|
|
683
736
|
error = mapEnterpriseFailure(err.kind).message;
|
|
684
737
|
} else if (err instanceof ApiError) {
|
|
685
738
|
error = err.message;
|
|
@@ -699,6 +752,7 @@ export function createGithubEnterpriseApp(
|
|
|
699
752
|
requiredPermission: required.name,
|
|
700
753
|
grantedPermissions: granted,
|
|
701
754
|
error,
|
|
755
|
+
warnings,
|
|
702
756
|
app: updated ? serializeApp(updated) : null,
|
|
703
757
|
},
|
|
704
758
|
200,
|
|
@@ -712,7 +766,25 @@ export function createGithubEnterpriseApp(
|
|
|
712
766
|
if (!installer) return c.json({ configured: false, orgs: [] }, 200);
|
|
713
767
|
|
|
714
768
|
let agentAppSlug: string | null = null;
|
|
715
|
-
|
|
769
|
+
let agentAppReason: string | null = null;
|
|
770
|
+
if (agentId) {
|
|
771
|
+
const agent = await agentApp(agentId);
|
|
772
|
+
agentAppSlug = agent.slug;
|
|
773
|
+
if (!agent.clientId) {
|
|
774
|
+
if (!agent.slug) {
|
|
775
|
+
agentAppReason =
|
|
776
|
+
"the agent has no GH_APP_SLUG or GH_APP_CLIENT_ID; it has no GitHub App to install";
|
|
777
|
+
} else {
|
|
778
|
+
try {
|
|
779
|
+
if ((await publicClientId(agent.slug)).privateApp) {
|
|
780
|
+
agentAppReason = privateAppMessage(agent.slug);
|
|
781
|
+
}
|
|
782
|
+
} catch {
|
|
783
|
+
// The reason is advisory; a lookup failure must not fail the listing.
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
}
|
|
716
788
|
const lookup = await appService.get("lookup");
|
|
717
789
|
|
|
718
790
|
const client = await clientFor(installer);
|
|
@@ -739,7 +811,7 @@ export function createGithubEnterpriseApp(
|
|
|
739
811
|
return c.json(
|
|
740
812
|
{
|
|
741
813
|
configured: true,
|
|
742
|
-
...(agentId ? { agentAppSlug } : {}),
|
|
814
|
+
...(agentId ? { agentAppSlug, agentAppReason } : {}),
|
|
743
815
|
orgs: result,
|
|
744
816
|
},
|
|
745
817
|
200,
|
|
@@ -755,38 +827,29 @@ export function createGithubEnterpriseApp(
|
|
|
755
827
|
if (!installer) {
|
|
756
828
|
throw new ConflictError("the installer App is not configured");
|
|
757
829
|
}
|
|
758
|
-
const
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
);
|
|
763
|
-
}
|
|
764
|
-
if (!slug) {
|
|
765
|
-
throw new UnprocessableEntityError(
|
|
766
|
-
"the agent has no GH_APP_SLUG; it has no GitHub App to install",
|
|
767
|
-
);
|
|
768
|
-
}
|
|
769
|
-
|
|
770
|
-
let clientId: string | null;
|
|
771
|
-
try {
|
|
772
|
-
clientId = (
|
|
773
|
-
await fetchPublicApp(slug, {
|
|
774
|
-
apiBase: deps.apiBase,
|
|
775
|
-
fetchFn: deps.fetchFn,
|
|
776
|
-
})
|
|
777
|
-
).clientId;
|
|
778
|
-
} catch (err) {
|
|
779
|
-
if (err instanceof GithubEnterpriseError && err.kind === "not_found") {
|
|
830
|
+
const agent = await agentApp(body.agentId);
|
|
831
|
+
let clientId = agent.clientId;
|
|
832
|
+
if (!clientId) {
|
|
833
|
+
if (agent.invalid) {
|
|
780
834
|
throw new UnprocessableEntityError(
|
|
781
|
-
|
|
835
|
+
"the agent's stored GH_APP_SLUG is not a valid GitHub App slug",
|
|
836
|
+
);
|
|
837
|
+
}
|
|
838
|
+
if (!agent.slug) {
|
|
839
|
+
throw new UnprocessableEntityError(
|
|
840
|
+
"the agent has no GH_APP_SLUG or GH_APP_CLIENT_ID; it has no GitHub App to install",
|
|
841
|
+
);
|
|
842
|
+
}
|
|
843
|
+
const lookedUp = await publicClientId(agent.slug);
|
|
844
|
+
if (lookedUp.privateApp) {
|
|
845
|
+
throw new UnprocessableEntityError(privateAppMessage(agent.slug));
|
|
846
|
+
}
|
|
847
|
+
clientId = lookedUp.clientId;
|
|
848
|
+
if (!clientId) {
|
|
849
|
+
throw new UnprocessableEntityError(
|
|
850
|
+
`the agent's App '${agent.slug}' did not expose a client id and cannot be installed`,
|
|
782
851
|
);
|
|
783
852
|
}
|
|
784
|
-
throw err;
|
|
785
|
-
}
|
|
786
|
-
if (!clientId) {
|
|
787
|
-
throw new UnprocessableEntityError(
|
|
788
|
-
`the agent's App '${slug}' did not expose a client id and cannot be installed`,
|
|
789
|
-
);
|
|
790
853
|
}
|
|
791
854
|
|
|
792
855
|
const client = await clientFor(installer);
|
|
@@ -117,6 +117,20 @@ function expectNoSecrets(e: Error) {
|
|
|
117
117
|
}
|
|
118
118
|
|
|
119
119
|
describe("getInstallationToken", () => {
|
|
120
|
+
it("tags a 404 on the enterprise installation lookup with its step (GHE-1.10)", async () => {
|
|
121
|
+
const { client } = makeClient({ [ENT]: "enterprise_installation_404" });
|
|
122
|
+
const e = await errOf(client.getInstallationToken());
|
|
123
|
+
expect(e.kind).toBe("not_found");
|
|
124
|
+
expect((e as { step?: string }).step).toBe("installation_lookup");
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("does not tag a 404 on the access-token mint as an installation lookup", async () => {
|
|
128
|
+
const { client } = makeClient({ [MINT]: "access_token_404" });
|
|
129
|
+
const e = await errOf(client.getInstallationToken());
|
|
130
|
+
expect(e.kind).toBe("not_found");
|
|
131
|
+
expect((e as { step?: string }).step).toBeUndefined();
|
|
132
|
+
});
|
|
133
|
+
|
|
120
134
|
it("looks up the enterprise installation with a JWT then mints a token", async () => {
|
|
121
135
|
const { client, calls } = makeClient({});
|
|
122
136
|
const t = await client.getInstallationToken();
|
|
@@ -69,17 +69,20 @@ export class GithubEnterpriseError extends Error {
|
|
|
69
69
|
readonly kind: GithubEnterpriseErrorKind;
|
|
70
70
|
readonly status?: number;
|
|
71
71
|
readonly retryAfterSeconds?: number;
|
|
72
|
+
/** Set when the failure came from the enterprise installation lookup step. */
|
|
73
|
+
readonly step?: "installation_lookup";
|
|
72
74
|
|
|
73
75
|
constructor(
|
|
74
76
|
kind: GithubEnterpriseErrorKind,
|
|
75
77
|
message: string,
|
|
76
|
-
opts?: { status?: number; retryAfterSeconds?: number },
|
|
78
|
+
opts?: { status?: number; retryAfterSeconds?: number; step?: "installation_lookup" },
|
|
77
79
|
) {
|
|
78
80
|
super(message);
|
|
79
81
|
this.name = "GithubEnterpriseError";
|
|
80
82
|
this.kind = kind;
|
|
81
83
|
this.status = opts?.status;
|
|
82
84
|
this.retryAfterSeconds = opts?.retryAfterSeconds;
|
|
85
|
+
this.step = opts?.step;
|
|
83
86
|
}
|
|
84
87
|
}
|
|
85
88
|
|
|
@@ -210,12 +213,24 @@ export class GithubEnterpriseClient {
|
|
|
210
213
|
now: this.now(),
|
|
211
214
|
});
|
|
212
215
|
const ent = encodeURIComponent(this.enterprise);
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
216
|
+
let inst: { data: { id?: number } };
|
|
217
|
+
try {
|
|
218
|
+
inst = await this.request<{ id?: number }>(
|
|
219
|
+
"GET",
|
|
220
|
+
`/enterprises/${ent}/installation`,
|
|
221
|
+
jwt,
|
|
222
|
+
"GitHub enterprise installation lookup",
|
|
223
|
+
);
|
|
224
|
+
} catch (err) {
|
|
225
|
+
if (err instanceof GithubEnterpriseError && err.kind === "not_found") {
|
|
226
|
+
throw new GithubEnterpriseError("not_found", err.message, {
|
|
227
|
+
status: err.status,
|
|
228
|
+
retryAfterSeconds: err.retryAfterSeconds,
|
|
229
|
+
step: "installation_lookup",
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
throw err;
|
|
233
|
+
}
|
|
219
234
|
if (typeof inst.data?.id !== "number") {
|
|
220
235
|
throw new GithubEnterpriseError(
|
|
221
236
|
"unexpected",
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/github-enterprise-warnings.ts
|
|
3
|
+
* GHE-1.10: pure validation logic for the enterprise GitHub Apps. Optional
|
|
4
|
+
* permission warnings are advisory: they are returned next to the result and
|
|
5
|
+
* never influence `status`, so a deployment that was valid stays valid.
|
|
6
|
+
* No key or token ever flows through here (only permission names/levels).
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export type GheRole = "installer" | "lookup";
|
|
10
|
+
export type GheValidationStatus = "valid" | "missing_permission" | "error";
|
|
11
|
+
|
|
12
|
+
/** Warning code -> message. Codes are stable; the UI carries them in a redirect. */
|
|
13
|
+
export const GHE_WARNINGS = {
|
|
14
|
+
installer_repositories_write:
|
|
15
|
+
"The installer App lacks enterprise_organization_installation_repositories (write). It is not needed yet but will be required for repository management.",
|
|
16
|
+
lookup_members_read:
|
|
17
|
+
"The lookup App lacks the Organization Members (read) permission. Validation still passes; grant it to let the lookup see organization membership.",
|
|
18
|
+
} as const;
|
|
19
|
+
|
|
20
|
+
export type GheWarningCode = keyof typeof GHE_WARNINGS;
|
|
21
|
+
|
|
22
|
+
export function warningCodeFor(message: string): GheWarningCode | undefined {
|
|
23
|
+
return (Object.keys(GHE_WARNINGS) as GheWarningCode[]).find(
|
|
24
|
+
(k) => GHE_WARNINGS[k] === message,
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function permissionWarnings(
|
|
29
|
+
role: GheRole,
|
|
30
|
+
granted: Record<string, string>,
|
|
31
|
+
): string[] {
|
|
32
|
+
if (role === "installer") {
|
|
33
|
+
return granted.enterprise_organization_installation_repositories === "write"
|
|
34
|
+
? []
|
|
35
|
+
: [GHE_WARNINGS.installer_repositories_write];
|
|
36
|
+
}
|
|
37
|
+
return ["read", "write"].includes(granted.members ?? "")
|
|
38
|
+
? []
|
|
39
|
+
: [GHE_WARNINGS.lookup_members_read];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Status comes from the required permission only; warnings ride alongside. */
|
|
43
|
+
export function resolveValidation(
|
|
44
|
+
role: GheRole,
|
|
45
|
+
required: { name: string; levels: readonly string[] },
|
|
46
|
+
granted: Record<string, string>,
|
|
47
|
+
): {
|
|
48
|
+
status: "valid" | "missing_permission";
|
|
49
|
+
error: string | null;
|
|
50
|
+
warnings: string[];
|
|
51
|
+
} {
|
|
52
|
+
const ok = required.levels.includes(granted[required.name] ?? "");
|
|
53
|
+
return {
|
|
54
|
+
status: ok ? "valid" : "missing_permission",
|
|
55
|
+
error: ok
|
|
56
|
+
? null
|
|
57
|
+
: `the App does not hold ${required.name} (${required.levels.join(" or ")})`,
|
|
58
|
+
warnings: permissionWarnings(role, granted),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unit tests for GHE-1.10 permission warnings: advisory only, never validity.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { describe, expect, it } from "bun:test";
|
|
6
|
+
import {
|
|
7
|
+
GHE_WARNINGS,
|
|
8
|
+
permissionWarnings,
|
|
9
|
+
resolveValidation,
|
|
10
|
+
} from "./github-enterprise-warnings.ts";
|
|
11
|
+
|
|
12
|
+
describe("permissionWarnings", () => {
|
|
13
|
+
it("installer: warns unless the repositories permission is write", () => {
|
|
14
|
+
expect(permissionWarnings("installer", {})).toEqual([GHE_WARNINGS.installer_repositories_write]);
|
|
15
|
+
expect(
|
|
16
|
+
permissionWarnings("installer", { enterprise_organization_installation_repositories: "read" }),
|
|
17
|
+
).toHaveLength(1);
|
|
18
|
+
expect(
|
|
19
|
+
permissionWarnings("installer", { enterprise_organization_installation_repositories: "write" }),
|
|
20
|
+
).toEqual([]);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("lookup: warns unless members is read or write", () => {
|
|
24
|
+
expect(permissionWarnings("lookup", { metadata: "read" })).toEqual([GHE_WARNINGS.lookup_members_read]);
|
|
25
|
+
expect(permissionWarnings("lookup", { members: "read" })).toEqual([]);
|
|
26
|
+
expect(permissionWarnings("lookup", { members: "write" })).toEqual([]);
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
describe("resolveValidation", () => {
|
|
31
|
+
const installer = { name: "enterprise_organization_installations", levels: ["write"] };
|
|
32
|
+
|
|
33
|
+
it("a warning never turns a valid result invalid", () => {
|
|
34
|
+
const r = resolveValidation("installer", installer, {
|
|
35
|
+
enterprise_organization_installations: "write",
|
|
36
|
+
});
|
|
37
|
+
expect(r.status).toBe("valid");
|
|
38
|
+
expect(r.warnings.length).toBeGreaterThan(0);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("a missing required permission stays missing_permission with warnings alongside", () => {
|
|
42
|
+
const r = resolveValidation("installer", installer, {});
|
|
43
|
+
expect(r.status).toBe("missing_permission");
|
|
44
|
+
expect(r.error).toContain("enterprise_organization_installations");
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("validity is identical with and without the optional permissions", () => {
|
|
48
|
+
const without = resolveValidation("installer", installer, {
|
|
49
|
+
enterprise_organization_installations: "write",
|
|
50
|
+
});
|
|
51
|
+
const withAll = resolveValidation("installer", installer, {
|
|
52
|
+
enterprise_organization_installations: "write",
|
|
53
|
+
enterprise_organization_installation_repositories: "write",
|
|
54
|
+
});
|
|
55
|
+
expect(without.status).toBe(withAll.status);
|
|
56
|
+
expect(withAll.warnings).toEqual([]);
|
|
57
|
+
});
|
|
58
|
+
});
|