@vite-hub/forge 0.0.0-stage → 0.1.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.
@@ -0,0 +1,343 @@
1
+ import { $ as toDate, d as NotFoundError, g as UnsupportedOperationError, tt as toMergeError } from "./utils-CA_4WJhJ-DNgT61X2.js";
2
+ import { S as toFileContent, p as fromBase64 } from "./webhooks-DgHwxxYz-D1V2LOr1.js";
3
+ import { t as FORGE } from "./tangled-DPt_0u21-zS41xdG5.js";
4
+ //#region ../../node_modules/.pnpm/forges@https+++pkg.pr.new+danielroe+forges@40d50b2f8bdc5585becfae405e1a3b8a325f7202/node_modules/forges/dist/knot-BpyTK9D7.mjs
5
+ const DEFAULT_PAGE = 30;
6
+ const TREE_TYPES = {
7
+ "0040000": "directory",
8
+ "0120000": "symlink",
9
+ "0160000": "submodule"
10
+ };
11
+ function hex(hash) {
12
+ if (typeof hash === "string" || hash === void 0) return hash ?? "";
13
+ return hash.map((byte) => byte.toString(16).padStart(2, "0")).join("");
14
+ }
15
+ function signature({ Name, Email, When }) {
16
+ return {
17
+ name: Name,
18
+ email: Email,
19
+ date: toDate(When)
20
+ };
21
+ }
22
+ function fragmentPatch(fragments) {
23
+ return fragments.map((fragment) => {
24
+ return `@@ -${fragment.OldPosition},${fragment.OldLines} +${fragment.NewPosition},${fragment.NewLines} @@${fragment.Comment ? ` ${fragment.Comment}` : ""}\n` + (fragment.Lines ?? []).map((line) => `${line.Op === 1 ? "-" : line.Op === 2 ? "+" : " "}${line.Line}`).join("");
25
+ }).join("");
26
+ }
27
+ function changedFile(file) {
28
+ const fragments = file.fragments ?? [];
29
+ const status = file.isNew ? "added" : file.isDelete ? "removed" : file.isRename ? "renamed" : file.isCopy ? "copied" : "modified";
30
+ return {
31
+ path: file.isDelete ? file.oldName : file.newName,
32
+ previousPath: file.isRename || file.isCopy ? file.oldName : void 0,
33
+ status,
34
+ additions: fragments.reduce((sum, fragment) => sum + fragment.LinesAdded, 0),
35
+ deletions: fragments.reduce((sum, fragment) => sum + fragment.LinesDeleted, 0),
36
+ patch: file.isBinary || !fragments.length ? void 0 : fragmentPatch(fragments),
37
+ sha: file.sha || void 0,
38
+ isBinary: file.isBinary || void 0
39
+ };
40
+ }
41
+ const fromDiff = (file) => changedFile({
42
+ oldName: file.name.old,
43
+ newName: file.name.new,
44
+ isNew: file.is_new,
45
+ isDelete: file.is_delete,
46
+ isCopy: file.is_copy,
47
+ isRename: file.is_rename,
48
+ isBinary: file.is_binary,
49
+ fragments: file.text_fragments
50
+ });
51
+ const fromPatch = (file) => changedFile({
52
+ oldName: file.OldName,
53
+ newName: file.NewName,
54
+ isNew: file.IsNew,
55
+ isDelete: file.IsDelete,
56
+ isCopy: file.IsCopy,
57
+ isRename: file.IsRename,
58
+ isBinary: file.IsBinary,
59
+ sha: file.NewOIDPrefix,
60
+ fragments: file.TextFragments
61
+ });
62
+ function stats(files) {
63
+ const additions = files.reduce((sum, file) => sum + (file.additions ?? 0), 0);
64
+ const deletions = files.reduce((sum, file) => sum + (file.deletions ?? 0), 0);
65
+ return {
66
+ additions,
67
+ deletions,
68
+ total: additions + deletions
69
+ };
70
+ }
71
+ /** Offset pagination, as knots page branches, tags and commits. */
72
+ function offsetPage(items, offset, limit) {
73
+ return {
74
+ items,
75
+ cursor: items.length >= limit ? { token: String(offset + items.length) } : void 0
76
+ };
77
+ }
78
+ function offsetOf(options) {
79
+ const offset = Number(options.cursor?.token ?? 0);
80
+ return Number.isInteger(offset) && offset > 0 ? offset : 0;
81
+ }
82
+ /** Reads git contents from the knot named in each repo record. */
83
+ function createTangledContents({ fetcher, instance, webUrl, context, canonicalRepo, serviceAuth }) {
84
+ async function knotFor(repo, signal) {
85
+ const { ref, record } = await canonicalRepo(repo, signal);
86
+ const { knot, repoDid, name } = record.value;
87
+ const origin = /^https?:\/\//.test(knot) ? knot.replace(/\/$/, "") : `https://${knot}`;
88
+ return {
89
+ ref: repoDid ? {
90
+ ...ref,
91
+ externalId: repoDid
92
+ } : ref,
93
+ record,
94
+ repo: repoDid ?? `${ref.owner}/${name ?? ref.name}`,
95
+ call: async (nsid, query) => (await fetcher.json(`${origin}/xrpc/${nsid}`, {
96
+ query,
97
+ signal
98
+ })).data,
99
+ /** Calls a knot procedure as the signed-in account. */
100
+ post: async (nsid, json, mapError) => {
101
+ const token = await serviceAuth(`did:web:${encodeURIComponent(new URL(origin).host)}`, nsid, signal);
102
+ await fetcher.raw(`${origin}/xrpc/${nsid}`, {
103
+ method: "POST",
104
+ json,
105
+ headers: { authorization: `Bearer ${token}` },
106
+ mapError,
107
+ signal
108
+ });
109
+ }
110
+ };
111
+ }
112
+ const pageUrl = (repo, section, ref, path) => `${webUrl}/${repo.owner}/${repo.name}/${section}/${encodeURIComponent(ref ?? "HEAD")}/${path.split("/").map(encodeURIComponent).join("/")}`;
113
+ function toCommit(repo, raw, files) {
114
+ const sha = raw.this ?? hex(raw.hash);
115
+ return {
116
+ ref: {
117
+ forge: FORGE,
118
+ instance,
119
+ repo,
120
+ sha
121
+ },
122
+ sha,
123
+ message: raw.message,
124
+ author: raw.author && signature(raw.author),
125
+ committer: raw.committer && signature(raw.committer),
126
+ parents: (raw.parent_hashes ?? []).map(hex),
127
+ url: `${webUrl}/${repo.owner}/${repo.name}/commit/${sha}`,
128
+ ...files ? {
129
+ files,
130
+ stats: stats(files)
131
+ } : {},
132
+ raw
133
+ };
134
+ }
135
+ function fromHeader(repo, header) {
136
+ const files = (header.Files ?? []).map(fromPatch);
137
+ const author = header.Author && {
138
+ ...signature(header.Author),
139
+ date: toDate(header.AuthorDate)
140
+ };
141
+ const committer = header.Committer && {
142
+ ...signature(header.Committer),
143
+ date: toDate(header.CommitterDate)
144
+ };
145
+ return {
146
+ ref: {
147
+ forge: FORGE,
148
+ instance,
149
+ repo,
150
+ sha: header.SHA
151
+ },
152
+ sha: header.SHA,
153
+ message: header.Body ? `${header.Title}\n\n${header.Body}` : header.Title,
154
+ author: author || void 0,
155
+ committer: committer || void 0,
156
+ parents: [],
157
+ url: `${webUrl}/${repo.owner}/${repo.name}/commit/${header.SHA}`,
158
+ files,
159
+ stats: stats(files),
160
+ raw: {
161
+ ...header,
162
+ Files: void 0
163
+ }
164
+ };
165
+ }
166
+ return {
167
+ async defaultBranch(repo, signal) {
168
+ const knot = await knotFor(repo, signal);
169
+ return (await knot.call("sh.tangled.repo.getDefaultBranch", { repo: knot.repo })).name || void 0;
170
+ },
171
+ async file(repo, path, options = {}) {
172
+ const knot = await knotFor(repo, options.signal);
173
+ const blob = await knot.call("sh.tangled.repo.blob", {
174
+ repo: knot.repo,
175
+ ref: options.ref,
176
+ path
177
+ });
178
+ if (blob.submodule || blob.content === void 0) throw new NotFoundError(`${path} is a submodule, not a file`, 404, "", context);
179
+ const bytes = blob.encoding === "base64" ? fromBase64(blob.content) : new TextEncoder().encode(blob.content);
180
+ return toFileContent(bytes, {
181
+ path,
182
+ size: blob.size,
183
+ url: pageUrl(knot.ref, "blob", options.ref, path)
184
+ }, options, context);
185
+ },
186
+ async treePage(repo, options = {}) {
187
+ const knot = await knotFor(repo, options.signal);
188
+ const parent = options.path?.replace(/^\/|\/$/g, "") ?? "";
189
+ const items = ((await knot.call("sh.tangled.repo.tree", {
190
+ repo: knot.repo,
191
+ ref: options.ref,
192
+ path: parent || void 0
193
+ })).files ?? []).map((entry) => {
194
+ const path = parent ? `${parent}/${entry.name}` : entry.name;
195
+ const type = TREE_TYPES[entry.mode] ?? "file";
196
+ return {
197
+ path,
198
+ type,
199
+ size: type === "file" ? entry.size : void 0,
200
+ mode: entry.mode.replace(/^0+(?=\d{6}$)/, ""),
201
+ url: pageUrl(knot.ref, type === "directory" ? "tree" : "blob", options.ref, path)
202
+ };
203
+ });
204
+ const warnings = options.recursive ? [{
205
+ code: "recursive_unsupported",
206
+ message: "Knots list one level of a tree; read subdirectories separately"
207
+ }] : [];
208
+ return warnings.length ? {
209
+ items,
210
+ warnings
211
+ } : { items };
212
+ },
213
+ async branchesPage(repo, options = {}) {
214
+ const knot = await knotFor(repo, options.signal);
215
+ const offset = offsetOf(options);
216
+ const limit = Math.min(options.perPage ?? DEFAULT_PAGE, 100);
217
+ return offsetPage(((await knot.call("sh.tangled.repo.branches", {
218
+ repo: knot.repo,
219
+ limit,
220
+ cursor: offset || void 0
221
+ })).branches ?? []).map((branch) => ({
222
+ name: branch.reference.name,
223
+ sha: branch.reference.hash,
224
+ isDefault: branch.is_default ?? false,
225
+ url: `${webUrl}/${knot.ref.owner}/${knot.ref.name}/tree/${encodeURIComponent(branch.reference.name)}`,
226
+ raw: {
227
+ ...branch,
228
+ commit: void 0
229
+ }
230
+ })), offset, limit);
231
+ },
232
+ async tagsPage(repo, options = {}) {
233
+ const knot = await knotFor(repo, options.signal);
234
+ const offset = offsetOf(options);
235
+ const limit = Math.min(options.perPage ?? DEFAULT_PAGE, 100);
236
+ return offsetPage(((await knot.call("sh.tangled.repo.tags", {
237
+ repo: knot.repo,
238
+ limit,
239
+ cursor: offset || void 0
240
+ })).tags ?? []).map((tag) => ({
241
+ name: tag.name,
242
+ sha: tag.tag ? hex(tag.tag.Target) : tag.hash,
243
+ url: `${webUrl}/${knot.ref.owner}/${knot.ref.name}/tree/${encodeURIComponent(tag.name)}`,
244
+ raw: tag
245
+ })), offset, limit);
246
+ },
247
+ async resolveRef(repo, ref, options = {}) {
248
+ const knot = await knotFor(repo, options.signal);
249
+ const commit = (await knot.call("sh.tangled.repo.log", {
250
+ repo: knot.repo,
251
+ ref,
252
+ limit: 1
253
+ })).commits?.[0];
254
+ if (!commit) throw new NotFoundError(`No commit at ${ref}`, 404, "", context);
255
+ return commit.this ?? hex(commit.hash);
256
+ },
257
+ async commitsPage(repo, query = {}) {
258
+ const knot = await knotFor(repo, query.signal);
259
+ const offset = offsetOf(query);
260
+ const limit = query.perPage ?? DEFAULT_PAGE;
261
+ const page = offsetPage(((await knot.call("sh.tangled.repo.log", {
262
+ repo: knot.repo,
263
+ ref: query.ref,
264
+ limit,
265
+ cursor: offset || void 0
266
+ })).commits ?? []).map((commit) => toCommit(knot.ref, commit)), offset, limit);
267
+ const ignored = [
268
+ "path",
269
+ "author",
270
+ "since",
271
+ "until"
272
+ ].filter((key) => query[key] !== void 0);
273
+ return ignored.length && !offset ? {
274
+ ...page,
275
+ warnings: [{
276
+ code: "filter_unsupported",
277
+ message: `Knots do not filter commits by ${ignored.join(", ")}; those filters were ignored`
278
+ }]
279
+ } : page;
280
+ },
281
+ async commit(repo, sha, options = {}) {
282
+ const knot = await knotFor(repo, options.signal);
283
+ const { diff } = await knot.call("sh.tangled.repo.diff", {
284
+ repo: knot.repo,
285
+ ref: sha
286
+ });
287
+ return toCommit(knot.ref, diff.commit, (diff.diff ?? []).map(fromDiff));
288
+ },
289
+ async compare(repo, base, head, options = {}) {
290
+ const knot = await knotFor(repo, options.signal);
291
+ const data = await knot.call("sh.tangled.repo.compare", {
292
+ repo: knot.repo,
293
+ rev1: base,
294
+ rev2: head
295
+ });
296
+ const commits = (data.format_patch ?? []).map((header) => fromHeader(knot.ref, header)).toReversed();
297
+ return {
298
+ base,
299
+ head,
300
+ aheadBy: commits.length,
301
+ commits,
302
+ files: (data.combined_patch ?? []).map(fromPatch),
303
+ mergeBaseSha: data.merge_base || void 0,
304
+ raw: {
305
+ rev1: data.rev1,
306
+ rev2: data.rev2,
307
+ merge_base: data.merge_base
308
+ }
309
+ };
310
+ },
311
+ /** The `git format-patch` of the commits `head` has and `base` lacks, empty when there are none. */
312
+ async formatPatch(repo, base, head, signal) {
313
+ const knot = await knotFor(repo, signal);
314
+ return (await knot.call("sh.tangled.repo.compare", {
315
+ repo: knot.repo,
316
+ rev1: base,
317
+ rev2: head
318
+ })).patch ?? "";
319
+ },
320
+ /** Applies `patch` to `branch` on the knot, as `git am` does for a format-patch. */
321
+ async merge(repo, branch, patch, signal) {
322
+ const knot = await knotFor(repo, signal);
323
+ if (!knot.ref.externalId) throw new UnsupportedOperationError(`${repo.owner}/${repo.name} has no repo DID, which merging needs`, context);
324
+ await knot.post("sh.tangled.repo.merge", {
325
+ did: knot.ref.owner,
326
+ name: knot.record.value.name ?? knot.ref.name,
327
+ repo: knot.ref.externalId,
328
+ branch,
329
+ patch
330
+ }, toMergeError);
331
+ },
332
+ async addCollaborator(repo, subject, signal) {
333
+ const knot = await knotFor(repo, signal);
334
+ if (!knot.ref.externalId) throw new UnsupportedOperationError(`${repo.owner}/${repo.name} has no repo DID, which managing collaborators needs`, context);
335
+ await knot.post("sh.tangled.repo.addCollaborator", {
336
+ repo: knot.ref.externalId,
337
+ subject
338
+ });
339
+ }
340
+ };
341
+ }
342
+ //#endregion
343
+ export { createTangledContents };
@@ -0,0 +1,43 @@
1
+ //#region ../../node_modules/.pnpm/forges@https+++pkg.pr.new+danielroe+forges@40d50b2f8bdc5585becfae405e1a3b8a325f7202/node_modules/forges/dist/tangled-DPt_0u21.mjs
2
+ const FORGE = "tangled";
3
+ const COLLECTIONS = {
4
+ repo: "sh.tangled.repo",
5
+ issue: "sh.tangled.repo.issue",
6
+ pull: "sh.tangled.repo.pull",
7
+ comment: "sh.tangled.feed.comment",
8
+ issueComment: "sh.tangled.repo.issue.comment",
9
+ pullComment: "sh.tangled.repo.pull.comment",
10
+ issueState: "sh.tangled.repo.issue.state",
11
+ pullStatus: "sh.tangled.repo.pull.status",
12
+ reaction: "sh.tangled.feed.reaction",
13
+ labelOp: "sh.tangled.label.op",
14
+ labelDefinition: "sh.tangled.label.definition",
15
+ subscription: "sh.tangled.feed.subscription",
16
+ profile: "sh.tangled.actor.profile"
17
+ };
18
+ COLLECTIONS.issue, COLLECTIONS.pull, COLLECTIONS.comment, COLLECTIONS.issueComment, COLLECTIONS.pullComment, COLLECTIONS.issueState, COLLECTIONS.pullStatus, COLLECTIONS.reaction, COLLECTIONS.labelOp;
19
+ function toActor(instance, identity, webUrl) {
20
+ const login = identity.handle ?? identity.did;
21
+ return {
22
+ forge: FORGE,
23
+ instance,
24
+ login,
25
+ id: identity.did,
26
+ url: `${webUrl}/${login}`,
27
+ isBotHint: false
28
+ };
29
+ }
30
+ Object.fromEntries(Object.entries({
31
+ "+1": "👍",
32
+ "-1": "👎",
33
+ "laugh": "😆",
34
+ "hooray": "🎉",
35
+ "confused": "🫤",
36
+ "heart": "❤️",
37
+ "rocket": "🚀",
38
+ "eyes": "👀"
39
+ }).map(([name, emoji]) => [emoji, name]));
40
+ COLLECTIONS.issueState, COLLECTIONS.pullStatus;
41
+ COLLECTIONS.comment, COLLECTIONS.issueComment, COLLECTIONS.comment, COLLECTIONS.pullComment;
42
+ //#endregion
43
+ export { toActor as n, FORGE as t };
@@ -0,0 +1,125 @@
1
+ import { I as mapConcurrent, d as NotFoundError } from "./utils-CA_4WJhJ-DNgT61X2.js";
2
+ //#region ../../node_modules/.pnpm/forges@https+++pkg.pr.new+danielroe+forges@40d50b2f8bdc5585becfae405e1a3b8a325f7202/node_modules/forges/dist/templates-Bn8a-pd2.mjs
3
+ /** Where GitHub looks for a pull request template and the legacy issue template, in its order of precedence. */
4
+ const LOCATIONS = [
5
+ ".github",
6
+ "",
7
+ "docs"
8
+ ];
9
+ const ISSUE_TEMPLATE = /\.(?:md|ya?ml)$/i;
10
+ const ISSUE_FORM = /\.ya?ml$/i;
11
+ const CHOOSER_CONFIG = /^config\.ya?ml$/i;
12
+ const CONTENTS_OWNER = /\/repos\/([^/]+)\/[^/]+\/contents(?:[/?]|$)/;
13
+ /** No REST endpoint for templates, and GraphQL's `issueTemplates` skips issue forms, so this walks the folders GitHub reads. */
14
+ async function readTemplates(repo, kinds, reader) {
15
+ const root = await rootOf(repo, reader);
16
+ const own = await templatesOf(repo, root.entries, kinds, reader);
17
+ const missing = kinds.filter((kind) => kind === "issue" ? !own.ownsIssues : !own.templates.some((template) => template.kind === kind));
18
+ if (!missing.length || repo.name.toLowerCase() === ".github") return own.templates;
19
+ const source = {
20
+ forge: repo.forge,
21
+ instance: repo.instance,
22
+ owner: root.owner,
23
+ name: ".github"
24
+ };
25
+ const fallback = await fallbackOf(source, missing, reader);
26
+ return [...own.templates, ...fallback.map((template) => ({
27
+ ...template,
28
+ source
29
+ }))];
30
+ }
31
+ /** The root of the default branch, and the owner the repository lives under now, which differs from `repo.owner` after a transfer. */
32
+ async function rootOf(repo, reader) {
33
+ try {
34
+ const entries = await reader.list(repo, "");
35
+ const owner = entries.map((entry) => entry.url && CONTENTS_OWNER.exec(new URL(entry.url).pathname)?.[1]).find(Boolean);
36
+ return {
37
+ entries,
38
+ owner: owner ? decodeURIComponent(owner) : repo.owner
39
+ };
40
+ } catch (error) {
41
+ if (!(error instanceof NotFoundError)) throw error;
42
+ return {
43
+ entries: [],
44
+ owner: (await reader.repository(repo)).owner?.login ?? repo.owner
45
+ };
46
+ }
47
+ }
48
+ /** The owner's `.github` repository serves defaults only while it is public, or internal in an enterprise. */
49
+ async function fallbackOf(source, kinds, reader) {
50
+ let detail;
51
+ try {
52
+ detail = await reader.repository(source);
53
+ } catch (error) {
54
+ if (error instanceof NotFoundError) return [];
55
+ throw error;
56
+ }
57
+ if (detail.visibility ? detail.visibility === "private" : detail.private) return [];
58
+ return (await templatesOf(source, await orEmpty(reader.list(source, "")), kinds, reader)).templates;
59
+ }
60
+ /** A chooser `config.yml` alone still makes issue templates the repository's own, while any other file there doesn't. */
61
+ async function templatesOf(repo, root, kinds, reader) {
62
+ const locations = await Promise.all(LOCATIONS.map((directory) => !directory ? root : root.some((entry) => entry.type === "dir" && entry.name === directory) ? orEmpty(reader.list(repo, directory)) : []));
63
+ const folder = async (entries, name) => {
64
+ const found = entries.find((entry) => entry.type === "dir" && entry.name.toLowerCase() === name);
65
+ return found ? (await orEmpty(reader.list(repo, found.path))).filter((entry) => entry.type === "file") : [];
66
+ };
67
+ const [issueFiles, pullFiles] = await Promise.all([kinds.includes("issue") ? folder(locations[0], "issue_template") : [], kinds.includes("pull_request") ? folder(locations.flat(), "pull_request_template") : []]);
68
+ const found = [];
69
+ let ownsIssues = false;
70
+ if (kinds.includes("issue")) {
71
+ const templates = issueFiles.filter((entry) => ISSUE_TEMPLATE.test(entry.name) && !CHOOSER_CONFIG.test(entry.name));
72
+ const legacy = firstFile(locations, "issue_template");
73
+ if (templates.length) {
74
+ const forms = templates.filter((entry) => ISSUE_FORM.test(entry.name));
75
+ found.push(...forms.map((entry) => ({
76
+ kind: "issue",
77
+ format: "form",
78
+ entry
79
+ })), ...templates.filter((entry) => !forms.includes(entry)).map((entry) => ({
80
+ kind: "issue",
81
+ format: "markdown",
82
+ entry
83
+ })));
84
+ } else if (legacy) found.push({
85
+ kind: "issue",
86
+ format: "markdown",
87
+ entry: legacy
88
+ });
89
+ ownsIssues = templates.length > 0 || issueFiles.some((entry) => CHOOSER_CONFIG.test(entry.name)) || legacy !== void 0;
90
+ }
91
+ if (kinds.includes("pull_request")) {
92
+ const single = firstFile(locations, "pull_request_template");
93
+ found.push(...[...single ? [single] : [], ...pullFiles].map((entry) => ({
94
+ kind: "pull_request",
95
+ format: "markdown",
96
+ entry
97
+ })));
98
+ }
99
+ return {
100
+ templates: await mapConcurrent(found, 4, async ({ kind, format, entry }) => ({
101
+ kind,
102
+ name: entry.name,
103
+ path: entry.path,
104
+ format,
105
+ body: await reader.read(repo, entry.path),
106
+ raw: entry
107
+ })),
108
+ ownsIssues
109
+ };
110
+ }
111
+ /** The first file named `name`, with any extension and in any case, across the locations in their order. */
112
+ function firstFile(locations, name) {
113
+ return locations.flat().find((entry) => entry.type === "file" && entry.name.toLowerCase().replace(/\.[^.]*$/, "") === name);
114
+ }
115
+ /** A listing of a directory that may not exist, or of a repository that is empty. */
116
+ async function orEmpty(listing) {
117
+ try {
118
+ return await listing;
119
+ } catch (error) {
120
+ if (error instanceof NotFoundError) return [];
121
+ throw error;
122
+ }
123
+ }
124
+ //#endregion
125
+ export { readTemplates };