editlet 0.0.0-stage → 0.1.0-alpha.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/LICENSE +21 -0
- package/README.md +140 -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,375 @@
|
|
|
1
|
+
import { GitHubError, encodeRef } from "./client.js";
|
|
2
|
+
import { assertDeployHookUrl, triggerDeployHook } from "./deploy.js";
|
|
3
|
+
import { isEditletBranchName, mergePullRequestBody } from "../core/gitStyle.js";
|
|
4
|
+
// github/repo.ts
|
|
5
|
+
function assertEditletBranch(name, prefix) {
|
|
6
|
+
if (!isEditletBranchName(name, prefix)) throw new GitHubError("invalid_branch", `Branch names must look like ${prefix}<name>: "${name}"`);
|
|
7
|
+
}
|
|
8
|
+
/** The prefix is inlined (a schema-checked ref path, so it cannot break the string). */
|
|
9
|
+
const branchesQuery = (prefix) => `query Branches($owner: String!, $name: String!, $base: String!, $after: String) {
|
|
10
|
+
repository(owner: $owner, name: $name) {
|
|
11
|
+
refs(refPrefix: "refs/heads/${prefix}", first: 100, after: $after) {
|
|
12
|
+
pageInfo { hasNextPage endCursor }
|
|
13
|
+
nodes {
|
|
14
|
+
name
|
|
15
|
+
target { oid ... on Commit { committedDate } }
|
|
16
|
+
associatedPullRequests(first: 1, states: [OPEN], baseRefName: $base) {
|
|
17
|
+
nodes { number url title isDraft headRefOid }
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}`;
|
|
23
|
+
const toPullRequest = (p) => ({
|
|
24
|
+
number: p.number,
|
|
25
|
+
url: p.html_url,
|
|
26
|
+
title: p.title,
|
|
27
|
+
isDraft: p.draft === true,
|
|
28
|
+
headOid: p.head.sha
|
|
29
|
+
});
|
|
30
|
+
const PULL_STATE_QUERY = `query PullState($owner: String!, $name: String!, $number: Int!) {
|
|
31
|
+
repository(owner: $owner, name: $name) {
|
|
32
|
+
pullRequest(number: $number) { id isDraft mergeable mergeStateStatus headRefOid }
|
|
33
|
+
}
|
|
34
|
+
}`;
|
|
35
|
+
const REVIEW_QUERY = `query PullReview($owner: String!, $name: String!, $number: Int!) {
|
|
36
|
+
repository(owner: $owner, name: $name) {
|
|
37
|
+
pullRequest(number: $number) {
|
|
38
|
+
reviewDecision
|
|
39
|
+
reviewRequests(first: 20) {
|
|
40
|
+
nodes { requestedReviewer { ... on User { login } ... on Team { slug organization { login } } } }
|
|
41
|
+
}
|
|
42
|
+
latestOpinionatedReviews(first: 20) { nodes { state author { login } } }
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}`;
|
|
46
|
+
const READY_MUTATION = `mutation Ready($id: ID!) {
|
|
47
|
+
markPullRequestReadyForReview(input: { pullRequestId: $id }) { pullRequest { isDraft } }
|
|
48
|
+
}`;
|
|
49
|
+
/**
|
|
50
|
+
* The only merge states in which the library merges. GitHub lets an admin token merge through the API even when
|
|
51
|
+
* branch protection blocks it, so the gate lives here. `viewerCanMergeAsAdmin` is never consulted.
|
|
52
|
+
*/
|
|
53
|
+
const MERGEABLE_STATES = /* @__PURE__ */ new Set([
|
|
54
|
+
"CLEAN",
|
|
55
|
+
"HAS_HOOKS",
|
|
56
|
+
"UNSTABLE"
|
|
57
|
+
]);
|
|
58
|
+
const MAX_UNKNOWN_POLLS = 5;
|
|
59
|
+
const is422 = (e) => e instanceof GitHubError && e.status === 422;
|
|
60
|
+
/** GitHub caps the compare file list; at this length it may be incomplete. */
|
|
61
|
+
const COMPARE_FILES_CAP = 300;
|
|
62
|
+
function otherFileStatus(status) {
|
|
63
|
+
if (status === "unchanged") return null;
|
|
64
|
+
if (status === "copied" || status === "changed") return "modified";
|
|
65
|
+
return status;
|
|
66
|
+
}
|
|
67
|
+
function makeRepoOps(deps) {
|
|
68
|
+
const { ctx, branch } = deps;
|
|
69
|
+
const repoPath = `/repos/${ctx.owner}/${ctx.repo}`;
|
|
70
|
+
/** The base branch, or `null` when it cannot be determined (broken schema); used by the safety refusals. */
|
|
71
|
+
async function tolerantBase() {
|
|
72
|
+
try {
|
|
73
|
+
return await deps.baseBranch();
|
|
74
|
+
} catch (error) {
|
|
75
|
+
if (error instanceof GitHubError && error.code === "not_found") return null;
|
|
76
|
+
throw error;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
async function findPull(base) {
|
|
80
|
+
return (await ctx.client.rest("GET", `${repoPath}/pulls`, {
|
|
81
|
+
query: {
|
|
82
|
+
head: `${ctx.owner}:${branch}`,
|
|
83
|
+
base,
|
|
84
|
+
state: "open"
|
|
85
|
+
},
|
|
86
|
+
fresh: true
|
|
87
|
+
}))[0] ?? null;
|
|
88
|
+
}
|
|
89
|
+
async function findPullRequest(base) {
|
|
90
|
+
const found = await findPull(base);
|
|
91
|
+
return found ? toPullRequest(found) : null;
|
|
92
|
+
}
|
|
93
|
+
const assertBranch = async (name) => assertEditletBranch(name, await deps.branchPrefix());
|
|
94
|
+
async function pullState(number) {
|
|
95
|
+
const repo = await deps.repoQuery(PULL_STATE_QUERY, { number });
|
|
96
|
+
if (!repo.pullRequest) throw new GitHubError("not_found", `Pull request not found: #${number}`);
|
|
97
|
+
return repo.pullRequest;
|
|
98
|
+
}
|
|
99
|
+
async function publish(options) {
|
|
100
|
+
if (options.deployHookUrl !== void 0) assertDeployHookUrl(options.deployHookUrl);
|
|
101
|
+
await assertBranch(branch);
|
|
102
|
+
const pullRequest = await ops.openPullRequest({ title: options.title ?? `Publish ${branch}` });
|
|
103
|
+
const blocked = (reason, message) => message === void 0 ? {
|
|
104
|
+
status: "blocked",
|
|
105
|
+
reason,
|
|
106
|
+
pullRequest
|
|
107
|
+
} : {
|
|
108
|
+
status: "blocked",
|
|
109
|
+
reason,
|
|
110
|
+
pullRequest,
|
|
111
|
+
message
|
|
112
|
+
};
|
|
113
|
+
let state = await pullState(pullRequest.number);
|
|
114
|
+
if (options.headOid !== state.headRefOid) return blocked("stale");
|
|
115
|
+
if (state.isDraft) {
|
|
116
|
+
await ctx.client.graphql(READY_MUTATION, { id: state.id });
|
|
117
|
+
state = await pullState(pullRequest.number);
|
|
118
|
+
}
|
|
119
|
+
for (let attempt = 1; state.mergeStateStatus === "UNKNOWN" && attempt <= MAX_UNKNOWN_POLLS; attempt++) {
|
|
120
|
+
await ctx.client.sleep(1e3 * attempt);
|
|
121
|
+
state = await pullState(pullRequest.number);
|
|
122
|
+
}
|
|
123
|
+
if (state.mergeStateStatus === "UNKNOWN") return blocked("pending");
|
|
124
|
+
if (options.headOid !== state.headRefOid) return blocked("stale");
|
|
125
|
+
if (!MERGEABLE_STATES.has(state.mergeStateStatus)) {
|
|
126
|
+
if (state.mergeStateStatus === "BEHIND") return blocked("behind");
|
|
127
|
+
if (state.mergeStateStatus === "DIRTY") return blocked("conflict");
|
|
128
|
+
return blocked("protected");
|
|
129
|
+
}
|
|
130
|
+
let mergeOid;
|
|
131
|
+
try {
|
|
132
|
+
mergeOid = (await ctx.client.rest("PUT", `${repoPath}/pulls/${pullRequest.number}/merge`, { body: {
|
|
133
|
+
merge_method: options.mergeMethod ?? "merge",
|
|
134
|
+
sha: state.headRefOid
|
|
135
|
+
} })).sha;
|
|
136
|
+
} catch (error) {
|
|
137
|
+
if (error instanceof GitHubError && error.status === 405) return blocked("protected", error.message);
|
|
138
|
+
if (error instanceof GitHubError && error.status === 409) return blocked("stale", error.message);
|
|
139
|
+
throw error;
|
|
140
|
+
}
|
|
141
|
+
let branchDeleted = false;
|
|
142
|
+
if (options.deleteBranch !== false) try {
|
|
143
|
+
branchDeleted = await ops.deleteBranch();
|
|
144
|
+
} catch {
|
|
145
|
+
branchDeleted = false;
|
|
146
|
+
}
|
|
147
|
+
const result = {
|
|
148
|
+
status: "merged",
|
|
149
|
+
pullRequest,
|
|
150
|
+
mergeOid,
|
|
151
|
+
branchDeleted
|
|
152
|
+
};
|
|
153
|
+
if (options.deployHookUrl !== void 0) result.deployHook = await triggerDeployHook(options.deployHookUrl);
|
|
154
|
+
return result;
|
|
155
|
+
}
|
|
156
|
+
async function latestDeployment(ref = branch) {
|
|
157
|
+
const head = await ctx.client.rest("GET", `${repoPath}/git/ref/heads/${encodeRef(ref)}`, { fresh: true });
|
|
158
|
+
const list = (query) => ctx.client.rest("GET", `${repoPath}/deployments`, {
|
|
159
|
+
query: {
|
|
160
|
+
...query,
|
|
161
|
+
per_page: 10
|
|
162
|
+
},
|
|
163
|
+
fresh: true
|
|
164
|
+
});
|
|
165
|
+
let found = await list({ sha: head.object.sha });
|
|
166
|
+
if (found.length === 0) found = await list({ ref });
|
|
167
|
+
const deployment = found[0];
|
|
168
|
+
if (!deployment) return null;
|
|
169
|
+
const status = (await ctx.client.rest("GET", `${repoPath}/deployments/${deployment.id}/statuses`, {
|
|
170
|
+
query: { per_page: 1 },
|
|
171
|
+
fresh: true
|
|
172
|
+
}))[0];
|
|
173
|
+
return {
|
|
174
|
+
id: deployment.id,
|
|
175
|
+
sha: deployment.sha,
|
|
176
|
+
ref: deployment.ref,
|
|
177
|
+
environment: deployment.environment,
|
|
178
|
+
state: status?.state ?? "pending",
|
|
179
|
+
url: status?.environment_url || null,
|
|
180
|
+
logUrl: status?.log_url || null,
|
|
181
|
+
createdAt: deployment.created_at,
|
|
182
|
+
updatedAt: deployment.updated_at
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
const ops = {
|
|
186
|
+
publish,
|
|
187
|
+
latestDeployment,
|
|
188
|
+
async listBranches() {
|
|
189
|
+
const [base, prefix] = await Promise.all([deps.baseBranch(), deps.branchPrefix()]);
|
|
190
|
+
const query = branchesQuery(prefix);
|
|
191
|
+
const out = [];
|
|
192
|
+
let after = null;
|
|
193
|
+
for (;;) {
|
|
194
|
+
const repo = await deps.repoQuery(query, {
|
|
195
|
+
base,
|
|
196
|
+
after
|
|
197
|
+
});
|
|
198
|
+
for (const node of repo.refs.nodes) {
|
|
199
|
+
const pr = node.associatedPullRequests.nodes[0];
|
|
200
|
+
out.push({
|
|
201
|
+
name: `${prefix}${node.name}`,
|
|
202
|
+
headOid: node.target.oid,
|
|
203
|
+
committedAt: node.target.committedDate ?? null,
|
|
204
|
+
pullRequest: pr ? {
|
|
205
|
+
number: pr.number,
|
|
206
|
+
url: pr.url,
|
|
207
|
+
title: pr.title,
|
|
208
|
+
isDraft: pr.isDraft,
|
|
209
|
+
headOid: pr.headRefOid
|
|
210
|
+
} : null
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
if (!repo.refs.pageInfo.hasNextPage) return out;
|
|
214
|
+
after = repo.refs.pageInfo.endCursor;
|
|
215
|
+
}
|
|
216
|
+
},
|
|
217
|
+
async createBranch(name) {
|
|
218
|
+
await assertBranch(name);
|
|
219
|
+
const base = await deps.baseBranch();
|
|
220
|
+
if (name === base) throw new GitHubError("invalid_branch", `"${name}" is the base branch`);
|
|
221
|
+
const ref = await ctx.client.rest("GET", `${repoPath}/git/ref/heads/${encodeRef(base)}`, { fresh: true });
|
|
222
|
+
try {
|
|
223
|
+
await ctx.client.rest("POST", `${repoPath}/git/refs`, { body: {
|
|
224
|
+
ref: `refs/heads/${name}`,
|
|
225
|
+
sha: ref.object.sha
|
|
226
|
+
} });
|
|
227
|
+
} catch (error) {
|
|
228
|
+
if (is422(error) && /already exists/i.test(error.message)) throw new GitHubError("branch_exists", `Branch already exists: ${name}`, { status: 422 });
|
|
229
|
+
throw error;
|
|
230
|
+
}
|
|
231
|
+
return deps.withBranch(name);
|
|
232
|
+
},
|
|
233
|
+
async renameBranch(name) {
|
|
234
|
+
await assertBranch(branch);
|
|
235
|
+
await assertBranch(name);
|
|
236
|
+
if (name === await tolerantBase()) throw new GitHubError("invalid_branch", `"${name}" is the base branch`);
|
|
237
|
+
try {
|
|
238
|
+
await ctx.client.rest("POST", `${repoPath}/branches/${encodeRef(branch)}/rename`, { body: { new_name: name } });
|
|
239
|
+
} catch (error) {
|
|
240
|
+
if (is422(error) && /already exists/i.test(error.message)) throw new GitHubError("branch_exists", `Branch already exists: ${name}`, { status: 422 });
|
|
241
|
+
throw error;
|
|
242
|
+
}
|
|
243
|
+
return deps.withBranch(name);
|
|
244
|
+
},
|
|
245
|
+
async deleteBranch() {
|
|
246
|
+
await assertBranch(branch);
|
|
247
|
+
if (branch === await tolerantBase()) throw new GitHubError("invalid_branch", `"${branch}" is the base branch`);
|
|
248
|
+
try {
|
|
249
|
+
await ctx.client.rest("DELETE", `${repoPath}/git/refs/heads/${encodeRef(branch)}`);
|
|
250
|
+
return true;
|
|
251
|
+
} catch (error) {
|
|
252
|
+
if (error instanceof GitHubError && error.code === "not_found") return false;
|
|
253
|
+
if (is422(error) && /does not exist/i.test(error.message)) return false;
|
|
254
|
+
throw error;
|
|
255
|
+
}
|
|
256
|
+
},
|
|
257
|
+
async pullRequest() {
|
|
258
|
+
return findPullRequest(await deps.baseBranch());
|
|
259
|
+
},
|
|
260
|
+
async openPullRequest({ title, body, draft, labels }) {
|
|
261
|
+
await assertBranch(branch);
|
|
262
|
+
const base = await deps.baseBranch();
|
|
263
|
+
const existing = await findPullRequest(base);
|
|
264
|
+
if (existing) return existing;
|
|
265
|
+
let created;
|
|
266
|
+
try {
|
|
267
|
+
const pull = await ctx.client.rest("POST", `${repoPath}/pulls`, { body: {
|
|
268
|
+
title,
|
|
269
|
+
head: branch,
|
|
270
|
+
base,
|
|
271
|
+
...body === void 0 ? {} : { body },
|
|
272
|
+
...draft ? { draft: true } : {}
|
|
273
|
+
} });
|
|
274
|
+
created = toPullRequest(pull);
|
|
275
|
+
} catch (error) {
|
|
276
|
+
if (!is422(error)) throw error;
|
|
277
|
+
if (/already exists/i.test(error.message)) {
|
|
278
|
+
const raced = await findPullRequest(base);
|
|
279
|
+
if (raced) return raced;
|
|
280
|
+
} else if (/no commits between/i.test(error.message)) throw new GitHubError("no_changes", `No changes between ${base} and ${branch}`, { status: 422 });
|
|
281
|
+
throw error;
|
|
282
|
+
}
|
|
283
|
+
if (!labels || labels.length === 0) return created;
|
|
284
|
+
try {
|
|
285
|
+
await ctx.client.rest("POST", `${repoPath}/issues/${created.number}/labels`, { body: { labels } });
|
|
286
|
+
return created;
|
|
287
|
+
} catch (error) {
|
|
288
|
+
return {
|
|
289
|
+
...created,
|
|
290
|
+
labelError: error instanceof Error ? error.message : String(error)
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
},
|
|
294
|
+
async refreshPullRequestBody(block) {
|
|
295
|
+
const found = await findPull(await deps.baseBranch());
|
|
296
|
+
if (!found) return "none";
|
|
297
|
+
const next = mergePullRequestBody(found.body ?? null, block);
|
|
298
|
+
if (next === null || next === (found.body ?? "")) return "unchanged";
|
|
299
|
+
await ctx.client.rest("PATCH", `${repoPath}/pulls/${found.number}`, { body: { body: next } });
|
|
300
|
+
return "updated";
|
|
301
|
+
},
|
|
302
|
+
async pullRequestReview(number) {
|
|
303
|
+
const pr = (await deps.repoQuery(REVIEW_QUERY, { number })).pullRequest;
|
|
304
|
+
if (!pr) return null;
|
|
305
|
+
const requested = pr.reviewRequests.nodes.flatMap(({ requestedReviewer: r }) => {
|
|
306
|
+
if (r?.login) return [`@${r.login}`];
|
|
307
|
+
if (r?.slug && r.organization) return [`@${r.organization.login}/${r.slug}`];
|
|
308
|
+
return [];
|
|
309
|
+
});
|
|
310
|
+
const by = (state) => pr.latestOpinionatedReviews.nodes.flatMap((n) => n.state === state && n.author ? [n.author.login] : []);
|
|
311
|
+
return {
|
|
312
|
+
decision: pr.reviewDecision,
|
|
313
|
+
requested,
|
|
314
|
+
approvedBy: by("APPROVED"),
|
|
315
|
+
changesRequestedBy: by("CHANGES_REQUESTED")
|
|
316
|
+
};
|
|
317
|
+
},
|
|
318
|
+
async compare() {
|
|
319
|
+
const base = await deps.baseBranch();
|
|
320
|
+
const cmp = await ctx.client.rest("GET", `${repoPath}/compare/${encodeRef(base)}...${encodeRef(branch)}`, {
|
|
321
|
+
query: { per_page: 1 },
|
|
322
|
+
fresh: true
|
|
323
|
+
});
|
|
324
|
+
const mergeBaseOid = cmp.merge_base_commit.sha;
|
|
325
|
+
const [head, baseSnap] = await Promise.all([deps.loadSnapshot(branch), deps.loadSnapshotAt(mergeBaseOid)]);
|
|
326
|
+
const files = [];
|
|
327
|
+
for (const path of /* @__PURE__ */ new Set([...head.files.keys(), ...baseSnap.files.keys()])) {
|
|
328
|
+
const sha = visibleSha(head, path);
|
|
329
|
+
const baseSha = visibleSha(baseSnap, path);
|
|
330
|
+
if (sha === baseSha) continue;
|
|
331
|
+
files.push({
|
|
332
|
+
path,
|
|
333
|
+
status: baseSha === null ? "added" : sha === null ? "removed" : "modified",
|
|
334
|
+
sha,
|
|
335
|
+
baseSha
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
files.sort((a, b) => a.path < b.path ? -1 : a.path > b.path ? 1 : 0);
|
|
339
|
+
const cmsPaths = new Set(files.map((f) => f.path));
|
|
340
|
+
const otherFiles = [];
|
|
341
|
+
for (const f of cmp.files ?? []) {
|
|
342
|
+
const status = otherFileStatus(f.status);
|
|
343
|
+
if (status !== null && !cmsPaths.has(f.filename)) otherFiles.push({
|
|
344
|
+
path: f.filename,
|
|
345
|
+
status
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
otherFiles.sort((a, b) => a.path < b.path ? -1 : a.path > b.path ? 1 : 0);
|
|
349
|
+
return {
|
|
350
|
+
status: cmp.status,
|
|
351
|
+
aheadBy: cmp.ahead_by,
|
|
352
|
+
behindBy: cmp.behind_by,
|
|
353
|
+
mergeBaseOid,
|
|
354
|
+
headOid: head.headOid,
|
|
355
|
+
files,
|
|
356
|
+
otherFiles,
|
|
357
|
+
complete: (cmp.files ?? []).length < COMPARE_FILES_CAP
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
};
|
|
361
|
+
return ops;
|
|
362
|
+
}
|
|
363
|
+
/** A path below `contentDir` with a dot segment (same rule as `core/paths.ts`). */
|
|
364
|
+
function hidden(snap, path) {
|
|
365
|
+
const prefix = `${snap.contentDir}/`;
|
|
366
|
+
return (path.startsWith(prefix) ? path.slice(prefix.length) : path).split("/").some((seg) => seg.startsWith("."));
|
|
367
|
+
}
|
|
368
|
+
/** The blob sha of a CMS file in `snap`, or `null` when absent or hidden. */
|
|
369
|
+
function visibleSha(snap, path) {
|
|
370
|
+
const sha = snap.files.get(path);
|
|
371
|
+
if (sha === void 0) return null;
|
|
372
|
+
if (path === snap.schemaPath) return sha;
|
|
373
|
+
return snap.schema === null || hidden(snap, path) ? null : sha;
|
|
374
|
+
}
|
|
375
|
+
export { makeRepoOps };
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { GitHubCache, PendingQueue } from "./cache.js";
|
|
2
|
+
import { GitHubClient, GitHubClientOptions } from "./client.js";
|
|
3
|
+
import { BranchDiff, BranchDiffFile, BranchDiffOtherFile, CmsBranch, DeploymentInfo, OpenedPullRequest, PublishOptions, PublishResult, PullRequestInfo, PullRequestReview } from "./repo.js";
|
|
4
|
+
import { Issue } from "../core/issues.js";
|
|
5
|
+
import { ContentSource, FileChange, FileHistory, FileRef } from "../core/source.js";
|
|
6
|
+
export interface GitHubSourceOptions {
|
|
7
|
+
owner: string;
|
|
8
|
+
repo: string;
|
|
9
|
+
branch: string;
|
|
10
|
+
/** Exactly one of `token` or `client`. */
|
|
11
|
+
token?: GitHubClientOptions['token'];
|
|
12
|
+
/** Share one client (and its rate limits) across sources. */
|
|
13
|
+
client?: GitHubClient;
|
|
14
|
+
/** Default: `schema.git.baseBranch` on `branch`. */
|
|
15
|
+
baseBranch?: string;
|
|
16
|
+
/** Default: `schema.git.branchPrefix` on `branch`, else `editlet/`. */
|
|
17
|
+
branchPrefix?: string;
|
|
18
|
+
/** Default `cms/schema.json`. */
|
|
19
|
+
schemaPath?: string;
|
|
20
|
+
/** Default `openGitHubCache()`. */
|
|
21
|
+
cache?: GitHubCache;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* A change plus the blob sha it is based on (`null` = the file must not exist). Default: the sha this source last
|
|
25
|
+
* served for the path (`readFile` / `readFiles`, or its own last commit), else the source's snapshot.
|
|
26
|
+
*/
|
|
27
|
+
export type GitHubFileChange = FileChange & {
|
|
28
|
+
sha?: string | null;
|
|
29
|
+
};
|
|
30
|
+
export interface GitHubSource extends ContentSource {
|
|
31
|
+
readonly kind: 'github';
|
|
32
|
+
readonly owner: string;
|
|
33
|
+
readonly repo: string;
|
|
34
|
+
readonly branch: string;
|
|
35
|
+
/** Unsaved edits for this repo + branch. */
|
|
36
|
+
readonly pending: PendingQueue;
|
|
37
|
+
/** Refreshes the snapshot (head oid + tree). Non-hidden `contentDir` files plus the schema file, sorted by path. */
|
|
38
|
+
listFiles(): Promise<FileRef[]>;
|
|
39
|
+
/** From the current snapshot (loaded if none). A missing path rejects with an `EditletError`. */
|
|
40
|
+
readFile(path: string): Promise<Uint8Array>;
|
|
41
|
+
/** Batched; in input order. Any missing path rejects with an `EditletError` listing all of them. */
|
|
42
|
+
readFiles(paths: string[]): Promise<Map<string, Uint8Array>>;
|
|
43
|
+
/** Any blob by sha (e.g. the base side of a diff). */
|
|
44
|
+
readBlob(sha: string): Promise<Uint8Array>;
|
|
45
|
+
/** Head commit oid of the current snapshot (loaded if none). */
|
|
46
|
+
head(): Promise<string>;
|
|
47
|
+
/**
|
|
48
|
+
* One atomic `createCommitOnBranch` on this (`<prefix>*`) branch. A path whose sha on GitHub is neither its expected
|
|
49
|
+
* sha nor the sha of the new content is a conflict issue; nothing is written then. The expected sha is
|
|
50
|
+
* `change.sha` when given, else the sha this source last served for the path (read or committed), else the
|
|
51
|
+
* snapshot's (so `listFiles` between a read and a save never hides a foreign edit). Concurrent commits to other
|
|
52
|
+
* paths are retried (3 attempts). Changes already on GitHub are skipped; `commit` is absent when nothing was left.
|
|
53
|
+
*/
|
|
54
|
+
commit(changes: GitHubFileChange[], message: string): Promise<{
|
|
55
|
+
issues: Issue[];
|
|
56
|
+
commit?: {
|
|
57
|
+
oid: string;
|
|
58
|
+
url: string;
|
|
59
|
+
};
|
|
60
|
+
}>;
|
|
61
|
+
/** The commits of this branch that touched `path`: newest, oldest and the count (all `null` / 0 for a new file). */
|
|
62
|
+
history(path: string): Promise<FileHistory>;
|
|
63
|
+
baseBranch(): Promise<string>;
|
|
64
|
+
/** The branch prefix: the option, else `git.branchPrefix` of the schema on this branch, else `editlet/`. */
|
|
65
|
+
branchPrefix(): Promise<string>;
|
|
66
|
+
/** Same client, cache and options; its own snapshot. */
|
|
67
|
+
withBranch(branch: string): GitHubSource;
|
|
68
|
+
/** `refs/heads/<prefix>*`, each with its open pull request into the base branch. */
|
|
69
|
+
listBranches(): Promise<CmsBranch[]>;
|
|
70
|
+
/** A new `<prefix><x>` branch from the head of the base branch; `branch_exists` when taken. Returns its source. */
|
|
71
|
+
createBranch(name: string): Promise<GitHubSource>;
|
|
72
|
+
/** Renames this branch (both names must have the prefix); returns the source for the new name. The queue stays. */
|
|
73
|
+
renameBranch(name: string): Promise<GitHubSource>;
|
|
74
|
+
/**
|
|
75
|
+
* Deletes this branch; `false` when it is already gone. Only prefixed branches, never the base. Does not clear the
|
|
76
|
+
* pending queue.
|
|
77
|
+
*/
|
|
78
|
+
deleteBranch(): Promise<boolean>;
|
|
79
|
+
/** The open pull request from this branch into the base branch. */
|
|
80
|
+
pullRequest(): Promise<PullRequestInfo | null>;
|
|
81
|
+
/**
|
|
82
|
+
* Finds the open pull request or creates one (only from a prefixed branch, else `invalid_branch`); `no_changes` when
|
|
83
|
+
* the branch has no commits over the base. A new one gets `draft` and then `labels`; when adding the labels fails,
|
|
84
|
+
* the pull request is still returned, with `labelError`. A found one is returned as it is.
|
|
85
|
+
*/
|
|
86
|
+
openPullRequest(options: {
|
|
87
|
+
title: string;
|
|
88
|
+
body?: string;
|
|
89
|
+
draft?: boolean;
|
|
90
|
+
labels?: string[];
|
|
91
|
+
}): Promise<OpenedPullRequest>;
|
|
92
|
+
/**
|
|
93
|
+
* Replaces the Editlet part (between the markers) of the open pull request's body with `block`: `none` without a
|
|
94
|
+
* pull request, `unchanged` when nothing changed or the markers are gone, else `updated` (one `PATCH`).
|
|
95
|
+
*/
|
|
96
|
+
refreshPullRequestBody(block: string): Promise<'none' | 'unchanged' | 'updated'>;
|
|
97
|
+
/** Review state of pull request `number` (decision, requested reviewers, latest approvals); `null` if not found. */
|
|
98
|
+
pullRequestReview(number: number): Promise<PullRequestReview | null>;
|
|
99
|
+
/**
|
|
100
|
+
* This branch against the base branch: counts, merge base, the changed CMS files (hidden paths excluded) and the
|
|
101
|
+
* other changed paths a merge would bring in (`otherFiles`; `complete: false` when GitHub's list was capped).
|
|
102
|
+
*/
|
|
103
|
+
compare(): Promise<BranchDiff>;
|
|
104
|
+
/**
|
|
105
|
+
* Only from a prefixed branch (else `invalid_branch`). Opens (or finds) the pull request; when its head is not the
|
|
106
|
+
* reviewed `headOid`, returns `stale` without touching it. Otherwise marks it ready and merges it into the base
|
|
107
|
+
* branch, but only when GitHub reports `CLEAN`, `HAS_HOOKS` or `UNSTABLE` and the head is still `headOid`;
|
|
108
|
+
* otherwise a `blocked` result with the PR url. Never bypasses branch protection. The merge carries the reviewed
|
|
109
|
+
* head `sha`. Then deletes the branch and calls the deploy hook. Does not touch the pending queue.
|
|
110
|
+
*/
|
|
111
|
+
publish(options: PublishOptions): Promise<PublishResult>;
|
|
112
|
+
/** The newest GitHub Deployment for the head of `ref` (default: this branch) with its latest status; `null` if none. */
|
|
113
|
+
latestDeployment(ref?: string): Promise<DeploymentInfo | null>;
|
|
114
|
+
}
|
|
115
|
+
export declare function createGitHubSource(options: GitHubSourceOptions): GitHubSource;
|