editlet 0.0.0-stage → 0.1.0-alpha.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/LICENSE +21 -0
- package/README.md +113 -2
- package/dist/admin/config.d.ts +22 -0
- package/dist/admin/config.js +75 -0
- package/dist/admin/dist/assets/RichEditor-BlaCRrEX.css +1 -0
- package/dist/admin/dist/assets/RichEditor-IZKdt6MK.js +42 -0
- package/dist/admin/dist/assets/blank-line-CbGzlqBI.js +1 -0
- package/dist/admin/dist/assets/channel-CljeMIwA.js +1 -0
- package/dist/admin/dist/assets/db-CCMyPgQa.js +1 -0
- package/dist/admin/dist/assets/index-CWoJh4H8.js +257 -0
- package/dist/admin/dist/assets/index-DkRMnD_M.css +1 -0
- package/dist/admin/dist/assets/mdast-util-from-markdown-C79q1_d8.js +3 -0
- package/dist/admin/dist/assets/mdast-util-gfm-cM-_xFJE.js +19 -0
- package/dist/admin/dist/assets/micromark-extension-gfm-Wz6-oszS.js +1 -0
- package/dist/admin/dist/assets/micromark-util-classify-character-BgXaGAja.js +1 -0
- package/dist/admin/dist/assets/micromark-util-html-tag-name-B61T18yC.js +1 -0
- package/dist/admin/dist/assets/rolldown-runtime-XKyaHtl9.js +1 -0
- package/dist/admin/dist/index.html +16 -0
- package/dist/admin/dist/preview.js +1 -0
- package/dist/admin/dist/window.js +1 -0
- package/dist/astro/content.d.ts +21 -0
- package/dist/astro/content.js +235 -0
- package/dist/astro/devServer.js +89 -0
- package/dist/astro/index.d.ts +15 -0
- package/dist/astro/index.js +98 -0
- package/dist/astro/nodeBridge.js +36 -0
- package/dist/astro/shared.js +10 -0
- package/dist/astro/stub/index.html +71 -0
- package/dist/auth/client.d.ts +24 -0
- package/dist/auth/client.js +120 -0
- package/dist/auth/handler.d.ts +16 -0
- package/dist/auth/handler.js +281 -0
- package/dist/auth/protocol.d.ts +25 -0
- package/dist/auth/protocol.js +3 -0
- package/dist/core/assets.d.ts +73 -0
- package/dist/core/assets.js +405 -0
- package/dist/core/devApi.d.ts +28 -0
- package/dist/core/devApi.js +3 -0
- package/dist/core/entryFile.d.ts +13 -0
- package/dist/core/entryFile.js +81 -0
- package/dist/core/fields.js +147 -0
- package/dist/core/generate.d.ts +3 -0
- package/dist/core/generate.js +70 -0
- package/dist/core/gitStyle.d.ts +92 -0
- package/dist/core/gitStyle.js +206 -0
- package/dist/core/index.d.ts +16 -0
- package/dist/core/index.js +16 -0
- package/dist/core/issues.d.ts +10 -0
- package/dist/core/issues.js +10 -0
- package/dist/core/paths.d.ts +31 -0
- package/dist/core/paths.js +114 -0
- package/dist/core/project.js +56 -0
- package/dist/core/references.d.ts +68 -0
- package/dist/core/references.js +103 -0
- package/dist/core/rename.d.ts +60 -0
- package/dist/core/rename.js +133 -0
- package/dist/core/schema.d.ts +117 -0
- package/dist/core/schema.js +299 -0
- package/dist/core/sha.d.ts +2 -0
- package/dist/core/sha.js +13 -0
- package/dist/core/source.d.ts +48 -0
- package/dist/core/source.js +57 -0
- package/dist/core/validate.d.ts +22 -0
- package/dist/core/validate.js +148 -0
- package/dist/core/walk.d.ts +23 -0
- package/dist/core/walk.js +94 -0
- package/dist/core/writes.d.ts +37 -0
- package/dist/core/writes.js +166 -0
- package/dist/github/base64.js +16 -0
- package/dist/github/cache.d.ts +34 -0
- package/dist/github/cache.js +126 -0
- package/dist/github/client.d.ts +38 -0
- package/dist/github/client.js +228 -0
- package/dist/github/codeowners.d.ts +23 -0
- package/dist/github/codeowners.js +96 -0
- package/dist/github/commitReader.d.ts +25 -0
- package/dist/github/commitReader.js +38 -0
- package/dist/github/deploy.d.ts +15 -0
- package/dist/github/deploy.js +33 -0
- package/dist/github/index.d.ts +9 -0
- package/dist/github/index.js +8 -0
- package/dist/github/repo.d.ts +95 -0
- package/dist/github/repo.js +375 -0
- package/dist/github/source.d.ts +115 -0
- package/dist/github/source.js +558 -0
- package/dist/loader/index.d.ts +27 -0
- package/dist/loader/index.js +151 -0
- package/dist/loader/protocol.d.ts +95 -0
- package/dist/loader/protocol.js +120 -0
- package/dist/loader/window/geometry.js +9 -0
- package/dist/next/EditletLoader.d.ts +5 -0
- package/dist/next/EditletLoader.js +33 -0
- package/dist/next/assetUrl.d.ts +9 -0
- package/dist/next/assetUrl.js +13 -0
- package/dist/next/buildGuard.js +26 -0
- package/dist/next/config.d.ts +18 -0
- package/dist/next/config.js +255 -0
- package/dist/next/devServer.js +70 -0
- package/dist/next/editletField.d.ts +7 -0
- package/dist/next/editletField.js +7 -0
- package/dist/next/index.d.ts +8 -0
- package/dist/next/index.js +6 -0
- package/dist/next/preview.d.ts +18 -0
- package/dist/next/preview.js +100 -0
- package/dist/next/previewCookie.d.ts +5 -0
- package/dist/next/previewCookie.js +72 -0
- package/dist/next/previewEnv.js +25 -0
- package/dist/next/previewRef.d.ts +8 -0
- package/dist/next/previewRef.js +17 -0
- package/dist/next/publicDir.js +65 -0
- package/dist/next/reader.d.ts +49 -0
- package/dist/next/reader.js +161 -0
- package/dist/next/stub/index.html +56 -0
- package/dist/node/devHandler.d.ts +6 -0
- package/dist/node/devHandler.js +430 -0
- package/dist/node/fs.js +37 -0
- package/dist/node/index.d.ts +3 -0
- package/dist/node/index.js +3 -0
- package/dist/node/project.d.ts +12 -0
- package/dist/node/project.js +43 -0
- package/package.json +187 -4
- package/skills/README.md +84 -0
- package/skills/editlet/SKILL.md +119 -0
- package/skills/editlet/references/content-format.md +191 -0
- package/skills/editlet/references/preview.md +75 -0
- package/skills/editlet/references/publishing.md +137 -0
- package/skills/editlet/references/schema-changes.md +127 -0
- package/skills/editlet/scripts/guard.mjs +1061 -0
- package/skills/editlet/scripts/link.mjs +193 -0
- package/skills/editlet/scripts/refs.mjs +1480 -0
- package/skills/editlet/scripts/schema.mjs +1340 -0
- package/skills/editlet/scripts/style.mjs +657 -0
- package/skills/editlet/scripts/types.mjs +525 -0
- package/skills/editlet/scripts/validate.mjs +724 -0
- package/skills/editlet-migrate/SKILL.md +92 -0
- package/skills/editlet-migrate/references/mapping.md +73 -0
- package/skills/editlet-migrate/references/sources.md +70 -0
- package/skills/editlet-setup/SKILL.md +108 -0
- package/skills/editlet-setup/references/astro.md +83 -0
- package/skills/editlet-setup/references/doctor.md +89 -0
- package/skills/editlet-setup/references/next.md +140 -0
- package/skills/editlet-setup/references/preview.md +54 -0
- package/skills/editlet-setup/references/production.md +153 -0
- package/skills/editlet-setup/references/roles.md +238 -0
- package/skills/editlet-setup/references/skills.md +99 -0
- package/skills/editlet-setup/scripts/doctor.mjs +1378 -0
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { AuthError } from "../auth/client.js";
|
|
2
|
+
// github/client.ts
|
|
3
|
+
var GitHubError = class extends Error {
|
|
4
|
+
code;
|
|
5
|
+
status;
|
|
6
|
+
retryAt;
|
|
7
|
+
graphqlErrors;
|
|
8
|
+
constructor(code, message, extra) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = "GitHubError";
|
|
11
|
+
this.code = code;
|
|
12
|
+
this.status = extra?.status;
|
|
13
|
+
this.retryAt = extra?.retryAt;
|
|
14
|
+
this.graphqlErrors = extra?.graphqlErrors;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
/** Encodes each segment of a ref/branch name, keeping `/`. */
|
|
18
|
+
function encodeRef(name) {
|
|
19
|
+
return name.split("/").map(encodeURIComponent).join("/");
|
|
20
|
+
}
|
|
21
|
+
const API = "https://api.github.com";
|
|
22
|
+
const API_VERSION = "2022-11-28";
|
|
23
|
+
const MAX_ATTEMPTS = 3;
|
|
24
|
+
const MAX_WAIT_MS = 6e4;
|
|
25
|
+
const READ_CONCURRENCY = 6;
|
|
26
|
+
const WRITE_WINDOWS = [[6e4, 80], [36e5, 500]];
|
|
27
|
+
var Semaphore = class {
|
|
28
|
+
limit;
|
|
29
|
+
active = 0;
|
|
30
|
+
waiters = [];
|
|
31
|
+
constructor(limit) {
|
|
32
|
+
this.limit = limit;
|
|
33
|
+
}
|
|
34
|
+
async acquire() {
|
|
35
|
+
if (this.active < this.limit) {
|
|
36
|
+
this.active++;
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
await new Promise((resolve) => this.waiters.push(resolve));
|
|
40
|
+
}
|
|
41
|
+
release() {
|
|
42
|
+
const next = this.waiters.shift();
|
|
43
|
+
if (next) next();
|
|
44
|
+
else this.active--;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
function parseJson(body) {
|
|
48
|
+
if (body.length === 0) return void 0;
|
|
49
|
+
try {
|
|
50
|
+
return JSON.parse(new TextDecoder().decode(body));
|
|
51
|
+
} catch {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function messageOf(parsed, status) {
|
|
56
|
+
if (parsed && typeof parsed === "object" && typeof parsed.message === "string") {
|
|
57
|
+
const details = errorsOf(parsed).map((e) => typeof e === "string" ? e : e?.message).filter((m) => typeof m === "string" && m !== "");
|
|
58
|
+
const message = parsed.message;
|
|
59
|
+
return details.length > 0 ? `${message}: ${details.join("; ")}` : message;
|
|
60
|
+
}
|
|
61
|
+
return `GitHub request failed with status ${status}`;
|
|
62
|
+
}
|
|
63
|
+
function errorsOf(parsed) {
|
|
64
|
+
const errs = parsed?.errors;
|
|
65
|
+
return Array.isArray(errs) ? errs : [];
|
|
66
|
+
}
|
|
67
|
+
function mapError(res, parsed) {
|
|
68
|
+
const message = messageOf(parsed, res.status);
|
|
69
|
+
if (res.status === 401) return new AuthError("unauthorized", message);
|
|
70
|
+
if (res.status === 403) return new AuthError("forbidden", message);
|
|
71
|
+
if (res.status === 404) return new GitHubError("not_found", message, { status: 404 });
|
|
72
|
+
return new GitHubError("http_error", message, { status: res.status });
|
|
73
|
+
}
|
|
74
|
+
function createGitHubClient(options) {
|
|
75
|
+
const doFetch = options.fetch ?? ((...args) => globalThis.fetch(...args));
|
|
76
|
+
const sleep = options.sleep ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
|
|
77
|
+
const now = options.now ?? Date.now;
|
|
78
|
+
const readSlots = new Semaphore(READ_CONCURRENCY);
|
|
79
|
+
const writeSlots = new Semaphore(1);
|
|
80
|
+
const writeStarts = [];
|
|
81
|
+
async function waitForWriteSlot() {
|
|
82
|
+
const t = now();
|
|
83
|
+
let wait = 0;
|
|
84
|
+
for (const [windowMs, max] of WRITE_WINDOWS) {
|
|
85
|
+
const recent = writeStarts.filter((s) => t - s < windowMs);
|
|
86
|
+
if (recent.length >= max) wait = Math.max(wait, recent[recent.length - max] + windowMs - t);
|
|
87
|
+
}
|
|
88
|
+
if (wait > 0) await sleep(wait);
|
|
89
|
+
const start = now();
|
|
90
|
+
writeStarts.push(start);
|
|
91
|
+
const cutoff = start - WRITE_WINDOWS[1][0];
|
|
92
|
+
while (writeStarts.length > 0 && writeStarts[0] < cutoff) writeStarts.shift();
|
|
93
|
+
}
|
|
94
|
+
async function withGate(isWrite, fn) {
|
|
95
|
+
if (!isWrite) {
|
|
96
|
+
await readSlots.acquire();
|
|
97
|
+
try {
|
|
98
|
+
return await fn();
|
|
99
|
+
} finally {
|
|
100
|
+
readSlots.release();
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
await writeSlots.acquire();
|
|
104
|
+
try {
|
|
105
|
+
await waitForWriteSlot();
|
|
106
|
+
return await fn();
|
|
107
|
+
} finally {
|
|
108
|
+
writeSlots.release();
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
async function send(url, init) {
|
|
112
|
+
const token = typeof options.token === "function" ? await options.token() : options.token;
|
|
113
|
+
const headers = {
|
|
114
|
+
...init.headers,
|
|
115
|
+
Authorization: `Bearer ${token}`
|
|
116
|
+
};
|
|
117
|
+
try {
|
|
118
|
+
const res = await doFetch(url, {
|
|
119
|
+
...init,
|
|
120
|
+
headers
|
|
121
|
+
});
|
|
122
|
+
const body = new Uint8Array(await res.arrayBuffer());
|
|
123
|
+
return {
|
|
124
|
+
status: res.status,
|
|
125
|
+
headers: res.headers,
|
|
126
|
+
body
|
|
127
|
+
};
|
|
128
|
+
} catch (e) {
|
|
129
|
+
throw new GitHubError("network_error", e instanceof Error ? e.message : "Network request failed");
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/** Wait in ms when the response is a rate limit, else null. */
|
|
133
|
+
function rateLimitWait(res, parsed) {
|
|
134
|
+
if (res.status !== 403 && res.status !== 429) return null;
|
|
135
|
+
const retryAfter = res.headers.get("retry-after");
|
|
136
|
+
const message = parsed && typeof parsed === "object" ? parsed.message : void 0;
|
|
137
|
+
if (!(res.headers.get("x-ratelimit-remaining") === "0" || retryAfter !== null || typeof message === "string" && /rate limit/i.test(message))) return null;
|
|
138
|
+
return waitMs(res.headers);
|
|
139
|
+
}
|
|
140
|
+
function waitMs(headers) {
|
|
141
|
+
const retryAfter = headers.get("retry-after");
|
|
142
|
+
if (retryAfter !== null && retryAfter !== "" && Number.isFinite(Number(retryAfter))) return Number(retryAfter) * 1e3;
|
|
143
|
+
const reset = headers.get("x-ratelimit-reset");
|
|
144
|
+
if (reset !== null && reset !== "" && Number.isFinite(Number(reset))) return Math.max(0, Number(reset) * 1e3 - now());
|
|
145
|
+
return MAX_WAIT_MS;
|
|
146
|
+
}
|
|
147
|
+
/** Sends with rate-limit retry; resolves with a 2xx response, or throws. Gate is held by the caller. */
|
|
148
|
+
async function execute(url, init, graphqlRetryable) {
|
|
149
|
+
for (let attempt = 1;; attempt++) {
|
|
150
|
+
const res = await send(url, init);
|
|
151
|
+
const parsed = res.status === 204 ? void 0 : parseJson(res.body);
|
|
152
|
+
let wait = null;
|
|
153
|
+
if (res.status >= 200 && res.status < 300) {
|
|
154
|
+
wait = graphqlRetryable ? graphqlRetryable(parsed) : null;
|
|
155
|
+
if (wait === null) return {
|
|
156
|
+
res,
|
|
157
|
+
parsed
|
|
158
|
+
};
|
|
159
|
+
} else {
|
|
160
|
+
wait = rateLimitWait(res, parsed);
|
|
161
|
+
if (wait === null) throw mapError(res, parsed);
|
|
162
|
+
}
|
|
163
|
+
if (wait > MAX_WAIT_MS || attempt >= MAX_ATTEMPTS) throw new GitHubError("rate_limited", "GitHub rate limit exceeded", {
|
|
164
|
+
status: res.status,
|
|
165
|
+
retryAt: now() + wait
|
|
166
|
+
});
|
|
167
|
+
await sleep(wait);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
async function rest(method, path, init = {}) {
|
|
171
|
+
const verb = method.toUpperCase();
|
|
172
|
+
let url = `${API}${path}`;
|
|
173
|
+
if (init.query) {
|
|
174
|
+
const params = new URLSearchParams();
|
|
175
|
+
for (const [k, v] of Object.entries(init.query)) if (v !== void 0) params.set(k, String(v));
|
|
176
|
+
const qs = params.toString();
|
|
177
|
+
if (qs) url += `${url.includes("?") ? "&" : "?"}${qs}`;
|
|
178
|
+
}
|
|
179
|
+
const headers = {
|
|
180
|
+
Accept: init.accept ?? "application/vnd.github+json",
|
|
181
|
+
"X-GitHub-Api-Version": API_VERSION
|
|
182
|
+
};
|
|
183
|
+
const req = {
|
|
184
|
+
method: verb,
|
|
185
|
+
headers
|
|
186
|
+
};
|
|
187
|
+
if (init.body !== void 0) {
|
|
188
|
+
headers["Content-Type"] = "application/json";
|
|
189
|
+
req.body = JSON.stringify(init.body);
|
|
190
|
+
}
|
|
191
|
+
if (init.fresh) req.cache = "no-cache";
|
|
192
|
+
const { res, parsed } = await withGate(verb !== "GET" && verb !== "HEAD", () => execute(url, req));
|
|
193
|
+
if (res.status === 204) return void 0;
|
|
194
|
+
if (init.raw) return res.body;
|
|
195
|
+
return parsed;
|
|
196
|
+
}
|
|
197
|
+
async function graphql(query, variables) {
|
|
198
|
+
const req = {
|
|
199
|
+
method: "POST",
|
|
200
|
+
headers: {
|
|
201
|
+
Accept: "application/vnd.github+json",
|
|
202
|
+
"X-GitHub-Api-Version": API_VERSION,
|
|
203
|
+
"Content-Type": "application/json"
|
|
204
|
+
},
|
|
205
|
+
body: JSON.stringify({
|
|
206
|
+
query,
|
|
207
|
+
variables
|
|
208
|
+
})
|
|
209
|
+
};
|
|
210
|
+
const isMutation = /^\s*mutation/.test(query);
|
|
211
|
+
const retryable = (parsed) => {
|
|
212
|
+
const errs = errorsOf(parsed);
|
|
213
|
+
if (errs.length > 0 && errs.every((e) => e.type === "RATE_LIMITED")) return MAX_WAIT_MS;
|
|
214
|
+
return null;
|
|
215
|
+
};
|
|
216
|
+
const { parsed } = await withGate(isMutation, () => execute(`${API}/graphql`, req, retryable));
|
|
217
|
+
const errs = errorsOf(parsed);
|
|
218
|
+
if (errs.find((e) => e.type === "FORBIDDEN")) throw new AuthError("forbidden", errs.map((e) => e.message).join("; "));
|
|
219
|
+
if (errs.length > 0) throw new GitHubError("graphql_error", errs.map((e) => e.message).join("; "), { graphqlErrors: errs });
|
|
220
|
+
return parsed.data;
|
|
221
|
+
}
|
|
222
|
+
return {
|
|
223
|
+
rest,
|
|
224
|
+
graphql,
|
|
225
|
+
sleep
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
export { GitHubError, createGitHubClient, encodeRef };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { GitHubClient } from "./client.js";
|
|
2
|
+
/** One valid `CODEOWNERS` line: its pattern and owners (`@login`, `@org/team` or an email; may be empty). */
|
|
3
|
+
export interface CodeownersRule {
|
|
4
|
+
pattern: string;
|
|
5
|
+
owners: string[];
|
|
6
|
+
/** 1-based line number in the file. */
|
|
7
|
+
line: number;
|
|
8
|
+
regex: RegExp;
|
|
9
|
+
}
|
|
10
|
+
/** The valid rules of a `CODEOWNERS` file, in file order. Comment, blank and unsupported lines are skipped, as GitHub does. */
|
|
11
|
+
export declare function parseCodeowners(text: string): CodeownersRule[];
|
|
12
|
+
/** Owners of `path` by the last matching rule; `[]` when that rule names none, `null` when no rule matches. */
|
|
13
|
+
export declare function ownersOf(rules: CodeownersRule[], path: string): string[] | null;
|
|
14
|
+
/** The owners of several paths, deduplicated, in first-seen order. */
|
|
15
|
+
export declare function ownersOfAll(rules: CodeownersRule[], paths: string[]): string[];
|
|
16
|
+
/**
|
|
17
|
+
* The `CODEOWNERS` file GitHub uses on `ref` (`.github/`, then the root, then `docs/`), or `null` when there is none.
|
|
18
|
+
* One `GET contents` per location tried; needs Contents read.
|
|
19
|
+
*/
|
|
20
|
+
export declare function readCodeowners(client: GitHubClient, owner: string, repo: string, ref: string): Promise<{
|
|
21
|
+
path: string;
|
|
22
|
+
text: string;
|
|
23
|
+
} | null>;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { GitHubError } from "./client.js";
|
|
2
|
+
// github/codeowners.ts
|
|
3
|
+
/** Where GitHub looks, first found wins. */
|
|
4
|
+
const CODEOWNERS_PATHS = [
|
|
5
|
+
".github/CODEOWNERS",
|
|
6
|
+
"CODEOWNERS",
|
|
7
|
+
"docs/CODEOWNERS"
|
|
8
|
+
];
|
|
9
|
+
const escape = (c) => c.replace(/[.+^${}()|\\]/g, "\\$&");
|
|
10
|
+
/**
|
|
11
|
+
* A `.gitignore`-style pattern as GitHub reads `CODEOWNERS`: `!`, `[ ]` and `\` escapes are not supported (`null`).
|
|
12
|
+
* A pattern without an inner `/` matches at any depth; a leading `/` or an inner one anchors it at the root; a trailing
|
|
13
|
+
* `/` matches everything inside the directory; a last segment without wildcards also matches a directory and its
|
|
14
|
+
* contents (`docs/*` matches `docs/a.md`, not `docs/a/b.md`).
|
|
15
|
+
*/
|
|
16
|
+
function compilePattern(pattern) {
|
|
17
|
+
if (pattern.startsWith("!") || /[[\]\\]/.test(pattern)) return null;
|
|
18
|
+
let p = pattern;
|
|
19
|
+
const dirOnly = p.endsWith("/");
|
|
20
|
+
if (dirOnly) p = p.slice(0, -1);
|
|
21
|
+
const anchored = p.includes("/");
|
|
22
|
+
if (p.startsWith("/")) p = p.slice(1);
|
|
23
|
+
if (p === "") return null;
|
|
24
|
+
let re = "";
|
|
25
|
+
for (let i = 0; i < p.length;) if (p.startsWith("**/", i)) {
|
|
26
|
+
re += "(?:.*/)?";
|
|
27
|
+
i += 3;
|
|
28
|
+
} else if (p.startsWith("/**", i) && i + 3 === p.length) {
|
|
29
|
+
re += "(?:/.*)?";
|
|
30
|
+
i += 3;
|
|
31
|
+
} else if (p.startsWith("**", i)) {
|
|
32
|
+
re += ".*";
|
|
33
|
+
i += 2;
|
|
34
|
+
} else if (p[i] === "*") {
|
|
35
|
+
re += "[^/]*";
|
|
36
|
+
i += 1;
|
|
37
|
+
} else if (p[i] === "?") {
|
|
38
|
+
re += "[^/]";
|
|
39
|
+
i += 1;
|
|
40
|
+
} else {
|
|
41
|
+
re += escape(p[i]);
|
|
42
|
+
i += 1;
|
|
43
|
+
}
|
|
44
|
+
const literalLast = !/[*?]/.test(p.split("/").pop());
|
|
45
|
+
return new RegExp(`^${anchored ? "" : "(?:.*/)?"}${re}${dirOnly ? "/.*" : literalLast ? "(?:/.*)?" : ""}$`);
|
|
46
|
+
}
|
|
47
|
+
/** The valid rules of a `CODEOWNERS` file, in file order. Comment, blank and unsupported lines are skipped, as GitHub does. */
|
|
48
|
+
function parseCodeowners(text) {
|
|
49
|
+
const rules = [];
|
|
50
|
+
text.split(/\r?\n/).forEach((raw, i) => {
|
|
51
|
+
const [pattern, ...rest] = raw.trim().split(/\s+/);
|
|
52
|
+
if (!pattern || pattern.startsWith("#")) return;
|
|
53
|
+
const regex = compilePattern(pattern);
|
|
54
|
+
if (!regex) return;
|
|
55
|
+
const comment = rest.findIndex((t) => t.startsWith("#"));
|
|
56
|
+
const owners = (comment < 0 ? rest : rest.slice(0, comment)).filter((t) => t.includes("@"));
|
|
57
|
+
rules.push({
|
|
58
|
+
pattern,
|
|
59
|
+
owners,
|
|
60
|
+
line: i + 1,
|
|
61
|
+
regex
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
return rules;
|
|
65
|
+
}
|
|
66
|
+
/** Owners of `path` by the last matching rule; `[]` when that rule names none, `null` when no rule matches. */
|
|
67
|
+
function ownersOf(rules, path) {
|
|
68
|
+
for (let i = rules.length - 1; i >= 0; i--) if (rules[i].regex.test(path)) return rules[i].owners;
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
/** The owners of several paths, deduplicated, in first-seen order. */
|
|
72
|
+
function ownersOfAll(rules, paths) {
|
|
73
|
+
return [...new Set(paths.flatMap((p) => ownersOf(rules, p) ?? []))];
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* The `CODEOWNERS` file GitHub uses on `ref` (`.github/`, then the root, then `docs/`), or `null` when there is none.
|
|
77
|
+
* One `GET contents` per location tried; needs Contents read.
|
|
78
|
+
*/
|
|
79
|
+
async function readCodeowners(client, owner, repo, ref) {
|
|
80
|
+
for (const path of CODEOWNERS_PATHS) try {
|
|
81
|
+
const body = await client.rest("GET", `/repos/${owner}/${repo}/contents/${path}`, {
|
|
82
|
+
query: { ref },
|
|
83
|
+
raw: true,
|
|
84
|
+
accept: "application/vnd.github.raw+json"
|
|
85
|
+
});
|
|
86
|
+
return {
|
|
87
|
+
path,
|
|
88
|
+
text: new TextDecoder().decode(body)
|
|
89
|
+
};
|
|
90
|
+
} catch (error) {
|
|
91
|
+
if (error instanceof GitHubError && error.code === "not_found") continue;
|
|
92
|
+
throw error;
|
|
93
|
+
}
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
export { CODEOWNERS_PATHS, compilePattern, ownersOf, ownersOfAll, parseCodeowners, readCodeowners };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { GitHubCache } from "./cache.js";
|
|
2
|
+
import { GitHubClient, GitHubClientOptions } from "./client.js";
|
|
3
|
+
import { Schema } from "../core/schema.js";
|
|
4
|
+
export interface ReadCommitOptions {
|
|
5
|
+
owner: string;
|
|
6
|
+
repo: string;
|
|
7
|
+
/** Full commit sha. */
|
|
8
|
+
sha: string;
|
|
9
|
+
/** Exactly one of `token` or `client`. */
|
|
10
|
+
token?: GitHubClientOptions['token'];
|
|
11
|
+
client?: GitHubClient;
|
|
12
|
+
/** Default `cms/schema.json`. */
|
|
13
|
+
schemaPath?: string;
|
|
14
|
+
/** Default: one module-level memory cache shared by every call (blobs are keyed by sha, so sharing is safe). */
|
|
15
|
+
cache?: GitHubCache;
|
|
16
|
+
}
|
|
17
|
+
export interface CommitFiles {
|
|
18
|
+
schema: Schema;
|
|
19
|
+
/** The schema file plus every non-hidden content file except images. */
|
|
20
|
+
files: Map<string, Uint8Array>;
|
|
21
|
+
/** Every path in the commit tree, images and hidden files included (for existence checks). */
|
|
22
|
+
paths: Set<string>;
|
|
23
|
+
}
|
|
24
|
+
/** Reads the schema and content files of one commit. Server-side: never touches IndexedDB. */
|
|
25
|
+
export declare function readCommit(options: ReadCommitOptions): Promise<CommitFiles>;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { createMemoryCache } from "./cache.js";
|
|
2
|
+
import { createGitHubClient } from "./client.js";
|
|
3
|
+
import { loadSnapshotAt, readBlobs } from "./source.js";
|
|
4
|
+
import { EditletError } from "../core/issues.js";
|
|
5
|
+
import { isHiddenContentPath, isImagePath } from "../core/paths.js";
|
|
6
|
+
// github/commitReader.ts
|
|
7
|
+
const sharedCache = createMemoryCache();
|
|
8
|
+
/** Reads the schema and content files of one commit. Server-side: never touches IndexedDB. */
|
|
9
|
+
async function readCommit(options) {
|
|
10
|
+
if (options.token === void 0 === (options.client === void 0)) throw new TypeError("readCommit: pass exactly one of `token` or `client`");
|
|
11
|
+
const ctx = {
|
|
12
|
+
client: options.client ?? createGitHubClient({ token: options.token }),
|
|
13
|
+
cache: options.cache ?? sharedCache,
|
|
14
|
+
owner: options.owner,
|
|
15
|
+
repo: options.repo,
|
|
16
|
+
schemaPath: options.schemaPath ?? "cms/schema.json"
|
|
17
|
+
};
|
|
18
|
+
const snapshot = await loadSnapshotAt(ctx, options.sha);
|
|
19
|
+
const { schema } = snapshot;
|
|
20
|
+
if (!schema) {
|
|
21
|
+
const message = snapshot.schemaSha === null ? `Schema not found: ${ctx.schemaPath}` : "Invalid schema";
|
|
22
|
+
throw new EditletError([{
|
|
23
|
+
file: ctx.schemaPath,
|
|
24
|
+
path: "",
|
|
25
|
+
message
|
|
26
|
+
}]);
|
|
27
|
+
}
|
|
28
|
+
const wanted = [...snapshot.files].filter(([path]) => path === ctx.schemaPath || !isHiddenContentPath(schema, path) && !isImagePath(path));
|
|
29
|
+
const blobs = await readBlobs(ctx, [...new Set(wanted.map(([, sha]) => sha))]);
|
|
30
|
+
const files = /* @__PURE__ */ new Map();
|
|
31
|
+
for (const [path, sha] of wanted) files.set(path, blobs.get(sha));
|
|
32
|
+
return {
|
|
33
|
+
schema,
|
|
34
|
+
files,
|
|
35
|
+
paths: new Set(snapshot.files.keys())
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
export { readCommit };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface DeployHookResult {
|
|
2
|
+
/** `null` = the response could not be read (CORS or network). The request may still have been delivered. */
|
|
3
|
+
ok: boolean | null;
|
|
4
|
+
status: number | null;
|
|
5
|
+
}
|
|
6
|
+
/** Throws `TypeError` for an unparsable or non-https deploy hook URL. */
|
|
7
|
+
export declare function assertDeployHookUrl(url: string): void;
|
|
8
|
+
/**
|
|
9
|
+
* Calls a deploy hook (Vercel, Netlify, Cloudflare Pages). HTTPS only; a bare `POST` with no headers and no body, so
|
|
10
|
+
* the browser treats it as a simple CORS request. Deliberately does not use the GitHub client: the token must never
|
|
11
|
+
* reach the hook host.
|
|
12
|
+
*/
|
|
13
|
+
export declare function triggerDeployHook(url: string, options?: {
|
|
14
|
+
fetch?: typeof fetch;
|
|
15
|
+
}): Promise<DeployHookResult>;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// github/deploy.ts
|
|
2
|
+
/** Throws `TypeError` for an unparsable or non-https deploy hook URL. */
|
|
3
|
+
function assertDeployHookUrl(url) {
|
|
4
|
+
let protocol;
|
|
5
|
+
try {
|
|
6
|
+
protocol = new URL(url).protocol;
|
|
7
|
+
} catch {
|
|
8
|
+
throw new TypeError("Deploy hook URL must be https");
|
|
9
|
+
}
|
|
10
|
+
if (protocol !== "https:") throw new TypeError("Deploy hook URL must be https");
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Calls a deploy hook (Vercel, Netlify, Cloudflare Pages). HTTPS only; a bare `POST` with no headers and no body, so
|
|
14
|
+
* the browser treats it as a simple CORS request. Deliberately does not use the GitHub client: the token must never
|
|
15
|
+
* reach the hook host.
|
|
16
|
+
*/
|
|
17
|
+
async function triggerDeployHook(url, options = {}) {
|
|
18
|
+
assertDeployHookUrl(url);
|
|
19
|
+
const doFetch = options.fetch ?? globalThis.fetch;
|
|
20
|
+
try {
|
|
21
|
+
const res = await doFetch(url, { method: "POST" });
|
|
22
|
+
return {
|
|
23
|
+
ok: res.ok,
|
|
24
|
+
status: res.status
|
|
25
|
+
};
|
|
26
|
+
} catch {
|
|
27
|
+
return {
|
|
28
|
+
ok: null,
|
|
29
|
+
status: null
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
export { assertDeployHookUrl, triggerDeployHook };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { GitHubClient, GitHubClientOptions, GitHubError, GitHubErrorCode, RestInit, createGitHubClient, encodeRef } from "./client.js";
|
|
2
|
+
import { GitHubCache, PendingChange, PendingQueue, createMemoryCache, openGitHubCache } from "./cache.js";
|
|
3
|
+
import { GitHubFileChange, GitHubSource, GitHubSourceOptions, createGitHubSource } from "./source.js";
|
|
4
|
+
import { CommitFiles, ReadCommitOptions, readCommit } from "./commitReader.js";
|
|
5
|
+
import { BranchDiff, BranchDiffFile, BranchDiffOtherFile, CmsBranch, DeploymentInfo, OpenedPullRequest, PublishOptions, PublishResult, PullRequestInfo, PullRequestReview } from "./repo.js";
|
|
6
|
+
import { isEditletBranchName } from "../core/gitStyle.js";
|
|
7
|
+
import { CodeownersRule, ownersOf, ownersOfAll, parseCodeowners, readCodeowners } from "./codeowners.js";
|
|
8
|
+
import { DeployHookResult, assertDeployHookUrl, triggerDeployHook } from "./deploy.js";
|
|
9
|
+
export { type BranchDiff, type BranchDiffFile, type BranchDiffOtherFile, type CmsBranch, type CodeownersRule, type CommitFiles, type DeployHookResult, type DeploymentInfo, type GitHubCache, type GitHubClient, type GitHubClientOptions, GitHubError, type GitHubErrorCode, type GitHubFileChange, type GitHubSource, type GitHubSourceOptions, type OpenedPullRequest, type PendingChange, type PendingQueue, type PublishOptions, type PublishResult, type PullRequestInfo, type PullRequestReview, type ReadCommitOptions, type RestInit, assertDeployHookUrl, createGitHubClient, createGitHubSource, createMemoryCache, encodeRef, isEditletBranchName, openGitHubCache, ownersOf, ownersOfAll, parseCodeowners, readCodeowners, readCommit, triggerDeployHook };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { GitHubError, createGitHubClient, encodeRef } from "./client.js";
|
|
2
|
+
import { createMemoryCache, openGitHubCache } from "./cache.js";
|
|
3
|
+
import { createGitHubSource } from "./source.js";
|
|
4
|
+
import { readCommit } from "./commitReader.js";
|
|
5
|
+
import { isEditletBranchName } from "../core/gitStyle.js";
|
|
6
|
+
import { ownersOf, ownersOfAll, parseCodeowners, readCodeowners } from "./codeowners.js";
|
|
7
|
+
import { assertDeployHookUrl, triggerDeployHook } from "./deploy.js";
|
|
8
|
+
export { GitHubError, assertDeployHookUrl, createGitHubClient, createGitHubSource, createMemoryCache, encodeRef, isEditletBranchName, openGitHubCache, ownersOf, ownersOfAll, parseCodeowners, readCodeowners, readCommit, triggerDeployHook };
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { DeployHookResult } from "./deploy.js";
|
|
2
|
+
export interface PullRequestInfo {
|
|
3
|
+
number: number;
|
|
4
|
+
url: string;
|
|
5
|
+
title: string;
|
|
6
|
+
isDraft: boolean;
|
|
7
|
+
headOid: string;
|
|
8
|
+
}
|
|
9
|
+
/** A pull request that `openPullRequest` returns: `labelError` when it was opened but the labels were not added. */
|
|
10
|
+
export type OpenedPullRequest = PullRequestInfo & {
|
|
11
|
+
labelError?: string;
|
|
12
|
+
};
|
|
13
|
+
/** Review state of an open pull request, as GitHub reports it. */
|
|
14
|
+
export interface PullRequestReview {
|
|
15
|
+
/** `null` when the base branch does not require reviews. */
|
|
16
|
+
decision: 'APPROVED' | 'CHANGES_REQUESTED' | 'REVIEW_REQUIRED' | null;
|
|
17
|
+
/** Requested reviewers: `@login` or `@org/team`. */
|
|
18
|
+
requested: string[];
|
|
19
|
+
/** Logins whose latest review approves. */
|
|
20
|
+
approvedBy: string[];
|
|
21
|
+
/** Logins whose latest review asks for changes. */
|
|
22
|
+
changesRequestedBy: string[];
|
|
23
|
+
}
|
|
24
|
+
export interface CmsBranch {
|
|
25
|
+
/** Full name, `<prefix><x>`. */
|
|
26
|
+
name: string;
|
|
27
|
+
headOid: string;
|
|
28
|
+
committedAt: string | null;
|
|
29
|
+
/** The open pull request into the base branch, if any. */
|
|
30
|
+
pullRequest: PullRequestInfo | null;
|
|
31
|
+
}
|
|
32
|
+
export interface BranchDiffFile {
|
|
33
|
+
path: string;
|
|
34
|
+
status: 'added' | 'modified' | 'removed';
|
|
35
|
+
/** Blob sha on the branch (`null` = removed). */
|
|
36
|
+
sha: string | null;
|
|
37
|
+
/** Blob sha at the merge base (`null` = added). */
|
|
38
|
+
baseSha: string | null;
|
|
39
|
+
}
|
|
40
|
+
export interface BranchDiffOtherFile {
|
|
41
|
+
path: string;
|
|
42
|
+
status: 'added' | 'modified' | 'removed' | 'renamed';
|
|
43
|
+
}
|
|
44
|
+
export interface BranchDiff {
|
|
45
|
+
status: 'ahead' | 'behind' | 'diverged' | 'identical';
|
|
46
|
+
aheadBy: number;
|
|
47
|
+
behindBy: number;
|
|
48
|
+
mergeBaseOid: string;
|
|
49
|
+
headOid: string;
|
|
50
|
+
/** Changed CMS files (schema + non-hidden `contentDir` files), from the snapshots at the merge base and the head. */
|
|
51
|
+
files: BranchDiffFile[];
|
|
52
|
+
/**
|
|
53
|
+
* Other changed paths that a merge would also bring in (outside the schema and `contentDir`, or hidden under it),
|
|
54
|
+
* from GitHub's compare. Shown, never blocking.
|
|
55
|
+
*/
|
|
56
|
+
otherFiles: BranchDiffOtherFile[];
|
|
57
|
+
/** `false` when GitHub's compare listed 300 or more files: `otherFiles` may then be incomplete. */
|
|
58
|
+
complete: boolean;
|
|
59
|
+
}
|
|
60
|
+
export interface PublishOptions {
|
|
61
|
+
/** Title when a pull request has to be opened. Default `Publish <branch>`. */
|
|
62
|
+
title?: string;
|
|
63
|
+
/**
|
|
64
|
+
* The head the editor reviewed (`compare().headOid`). When the pull request head differs, publish is `stale` and
|
|
65
|
+
* changes nothing (it neither marks the pull request ready nor merges).
|
|
66
|
+
*/
|
|
67
|
+
headOid: string;
|
|
68
|
+
mergeMethod?: 'merge' | 'squash' | 'rebase';
|
|
69
|
+
/** Default true. */
|
|
70
|
+
deleteBranch?: boolean;
|
|
71
|
+
deployHookUrl?: string;
|
|
72
|
+
}
|
|
73
|
+
export type PublishResult = {
|
|
74
|
+
status: 'merged';
|
|
75
|
+
pullRequest: PullRequestInfo;
|
|
76
|
+
mergeOid: string;
|
|
77
|
+
branchDeleted: boolean;
|
|
78
|
+
deployHook?: DeployHookResult;
|
|
79
|
+
} | {
|
|
80
|
+
status: 'blocked';
|
|
81
|
+
reason: 'protected' | 'behind' | 'conflict' | 'stale' | 'pending';
|
|
82
|
+
pullRequest: PullRequestInfo;
|
|
83
|
+
message?: string;
|
|
84
|
+
};
|
|
85
|
+
export interface DeploymentInfo {
|
|
86
|
+
id: number;
|
|
87
|
+
sha: string;
|
|
88
|
+
ref: string;
|
|
89
|
+
environment: string;
|
|
90
|
+
state: 'error' | 'failure' | 'inactive' | 'in_progress' | 'queued' | 'pending' | 'success' | 'waiting';
|
|
91
|
+
url: string | null;
|
|
92
|
+
logUrl: string | null;
|
|
93
|
+
createdAt: string;
|
|
94
|
+
updatedAt: string;
|
|
95
|
+
}
|