@fabrika/github-app 0.9.0-dev.2 → 0.9.0-dev.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -106,9 +106,10 @@ export interface DecodedTree {
|
|
|
106
106
|
readonly truncated: boolean
|
|
107
107
|
}
|
|
108
108
|
|
|
109
|
-
|
|
109
|
+
/** A tree read by `<commit>:<path>`, whose own sha the request did not name. */
|
|
110
|
+
export function decodeTreeListing(body: unknown): DecodedTree {
|
|
110
111
|
const result = treeSchema.safeParse(body)
|
|
111
|
-
if (!result.success
|
|
112
|
+
if (!result.success) throw responseError()
|
|
112
113
|
return { entries: result.data.tree, truncated: result.data.truncated }
|
|
113
114
|
}
|
|
114
115
|
|
package/src/repository.ts
CHANGED
|
@@ -21,11 +21,12 @@ import {
|
|
|
21
21
|
decodeCommit,
|
|
22
22
|
decodeComparison,
|
|
23
23
|
decodeCreatedTreeSha,
|
|
24
|
+
type DecodedTree,
|
|
24
25
|
decodePullRequest,
|
|
25
26
|
decodePullRequestList,
|
|
26
27
|
decodeRef,
|
|
27
28
|
decodeTextBlob,
|
|
28
|
-
|
|
29
|
+
decodeTreeListing,
|
|
29
30
|
encodeBranch,
|
|
30
31
|
MAX_BLOB_RESPONSE_BYTES,
|
|
31
32
|
MAX_CHANGED_BYTES,
|
|
@@ -105,41 +106,36 @@ class RepositoryClient implements GitHubRepositoryClient {
|
|
|
105
106
|
async readTree(commitSha: string, directory = '', signal?: AbortSignal): Promise<GitHubTreeListing> {
|
|
106
107
|
validateSha(commitSha)
|
|
107
108
|
if (directory !== '') validatePath(directory)
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
if (directory !== '') {
|
|
111
|
-
const entry = await this.#treeWalker(rootSha, signal).find(directory)
|
|
112
|
-
if (entry?.type !== 'tree') return { commitSha, directory, entries: [], truncated: false }
|
|
113
|
-
treeSha = entry.sha
|
|
114
|
-
}
|
|
115
|
-
const tree = decodeTree(await this.#readGitObject(`/git/trees/${treeSha}?recursive=1`, signal, MAX_TREE_RESPONSE_BYTES), treeSha)
|
|
109
|
+
const listing = await this.#readTreeAt(commitSha, directory, '?recursive=1', signal)
|
|
110
|
+
if (listing === null) return { commitSha, directory, entries: [], truncated: false }
|
|
116
111
|
const prefix = directory === '' ? '' : `${directory}/`
|
|
117
|
-
const entries =
|
|
118
|
-
return { commitSha, directory, entries, truncated:
|
|
112
|
+
const entries = listing.entries.slice(0, MAX_TREE_ENTRIES).map((entry) => ({ ...entry, path: `${prefix}${entry.path}` }))
|
|
113
|
+
return { commitSha, directory, entries, truncated: listing.truncated || listing.entries.length > MAX_TREE_ENTRIES }
|
|
119
114
|
}
|
|
120
115
|
|
|
121
116
|
async readFiles(commitSha: string, paths: readonly string[], signal?: AbortSignal): Promise<GitHubFileRead> {
|
|
122
117
|
validateSha(commitSha)
|
|
123
118
|
if (paths.length === 0 || paths.length > MAX_READ_PATHS || new Set(paths).size !== paths.length) throw configurationError()
|
|
124
119
|
for (const path of paths) validatePath(path)
|
|
125
|
-
const
|
|
120
|
+
const entries = await Promise.all(paths.map((path) => this.#find(commitSha, path, signal)))
|
|
121
|
+
const blobs: GitHubBlobRef[] = []
|
|
122
|
+
const notFiles = new Map<string, GitHubUnreadableReason>()
|
|
123
|
+
for (const [index, path] of paths.entries()) {
|
|
124
|
+
const entry = entries[index] ?? null
|
|
125
|
+
if (entry === null) notFiles.set(path, 'missing')
|
|
126
|
+
else if (entry.type !== 'blob' || entry.mode === '120000') notFiles.set(path, 'not_a_file')
|
|
127
|
+
else blobs.push({ path, sha: entry.sha, size: entry.size })
|
|
128
|
+
}
|
|
129
|
+
const read = blobs.length === 0 ? { files: [], unreadable: [] } : await this.readBlobTexts(blobs, signal)
|
|
130
|
+
const reasons = new Map([...notFiles, ...read.unreadable.map((entry): [string, GitHubUnreadableReason] => [entry.path, entry.reason])])
|
|
131
|
+
const contents = new Map(read.files.map((file) => [file.path, file.content]))
|
|
126
132
|
const files: GitHubTextFile[] = []
|
|
127
133
|
const unreadable: GitHubUnreadablePath[] = []
|
|
128
|
-
let remainingBytes = MAX_READ_BYTES
|
|
129
134
|
for (const path of paths) {
|
|
130
|
-
const
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
}
|
|
135
|
-
const body = await this.#readGitObject(`/git/blobs/${entry.sha}`, signal, MAX_BLOB_RESPONSE_BYTES)
|
|
136
|
-
const content = decodeTextBlob(body, entry.sha, entry.size)
|
|
137
|
-
if (content === null) {
|
|
138
|
-
unreadable.push({ path, reason: 'binary' })
|
|
139
|
-
continue
|
|
140
|
-
}
|
|
141
|
-
remainingBytes -= entry.size
|
|
142
|
-
files.push({ path, content })
|
|
135
|
+
const content = contents.get(path)
|
|
136
|
+
const reason = reasons.get(path)
|
|
137
|
+
if (content !== undefined) files.push({ path, content })
|
|
138
|
+
else if (reason !== undefined) unreadable.push({ path, reason })
|
|
143
139
|
}
|
|
144
140
|
return { commitSha, files, unreadable }
|
|
145
141
|
}
|
|
@@ -237,7 +233,7 @@ class RepositoryClient implements GitHubRepositoryClient {
|
|
|
237
233
|
const head = await this.readRef(input.branch, input.signal)
|
|
238
234
|
if (head?.sha !== input.expectedHeadSha) throw new GitHubStaleHeadError()
|
|
239
235
|
const parent = await parentRead
|
|
240
|
-
const tree = await this.#resolveModes(changes, parent.
|
|
236
|
+
const tree = await this.#resolveModes(changes, parent.sha, input.signal)
|
|
241
237
|
|
|
242
238
|
const createdTree = await this.#request('/git/trees', {
|
|
243
239
|
method: 'POST',
|
|
@@ -299,20 +295,40 @@ class RepositoryClient implements GitHubRepositoryClient {
|
|
|
299
295
|
}
|
|
300
296
|
|
|
301
297
|
/** Keeps an edited executable's mode, and refuses to delete anything but an existing file. */
|
|
302
|
-
async #resolveModes(changes: readonly TreeChange[],
|
|
303
|
-
const
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
const
|
|
307
|
-
const isFile = existing?.type === 'blob'
|
|
298
|
+
async #resolveModes(changes: readonly TreeChange[], commitSha: string, signal: AbortSignal | undefined): Promise<TreeChange[]> {
|
|
299
|
+
const existing = await Promise.all(changes.map((change) => this.#find(commitSha, change.path, signal)))
|
|
300
|
+
return changes.map((change, index) => {
|
|
301
|
+
const entry = existing[index] ?? null
|
|
302
|
+
const isFile = entry?.type === 'blob'
|
|
308
303
|
if (change.content === undefined && !isFile) throw configurationError()
|
|
309
|
-
|
|
310
|
-
}
|
|
311
|
-
return result
|
|
304
|
+
return isFile && entry.mode === '100755' ? { ...change, mode: '100755' } : change
|
|
305
|
+
})
|
|
312
306
|
}
|
|
313
307
|
|
|
314
|
-
|
|
315
|
-
|
|
308
|
+
/** The entry at `path` in `commitSha`, or null. One listing of its directory, which this client reads once. */
|
|
309
|
+
async #find(commitSha: string, path: string, signal: AbortSignal | undefined): Promise<GitHubTreeEntry | null> {
|
|
310
|
+
const slash = path.lastIndexOf('/')
|
|
311
|
+
const listing = await this.#readTreeAt(commitSha, slash === -1 ? '' : path.slice(0, slash), '', signal)
|
|
312
|
+
if (listing?.truncated === true) throw responseError()
|
|
313
|
+
return listing?.entries.find((entry) => entry.path === path.slice(slash + 1)) ?? null
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* The tree of `directory` ('' for the root) at `commitSha`, in one request: GitHub resolves `<commit>:<path>`. Null
|
|
318
|
+
* when the commit holds no such directory; an unknown commit is not found.
|
|
319
|
+
*/
|
|
320
|
+
async #readTreeAt(commitSha: string, directory: string, query: '' | '?recursive=1', signal: AbortSignal | undefined): Promise<DecodedTree | null> {
|
|
321
|
+
const path = `/git/trees/${encodeURIComponent(`${commitSha}:${directory}`)}${query}`
|
|
322
|
+
let body: unknown
|
|
323
|
+
try {
|
|
324
|
+
body = await this.#readGitObject(path, signal, MAX_TREE_RESPONSE_BYTES)
|
|
325
|
+
} catch (error) {
|
|
326
|
+
if (!(error instanceof GitHubNotFoundError)) throw error
|
|
327
|
+
// GitHub answers the same for a missing directory and an unknown commit; only the commit read tells them apart.
|
|
328
|
+
await this.#readCommit(commitSha, signal)
|
|
329
|
+
return null
|
|
330
|
+
}
|
|
331
|
+
return decodeTreeListing(body)
|
|
316
332
|
}
|
|
317
333
|
|
|
318
334
|
async #readCommit(sha: string, signal: AbortSignal | undefined): Promise<GitHubCommit> {
|
|
@@ -379,55 +395,6 @@ class RepositoryClient implements GitHubRepositoryClient {
|
|
|
379
395
|
}
|
|
380
396
|
|
|
381
397
|
/** Resolves repository paths through non-recursive tree reads, caching each directory once. */
|
|
382
|
-
class TreeWalker {
|
|
383
|
-
readonly #directories = new Map<string, Promise<ReadonlyMap<string, GitHubTreeEntry>>>()
|
|
384
|
-
|
|
385
|
-
constructor(
|
|
386
|
-
private readonly rootSha: string,
|
|
387
|
-
private readonly fetchTree: (treeSha: string) => Promise<unknown>,
|
|
388
|
-
) {}
|
|
389
|
-
|
|
390
|
-
async find(path: string): Promise<GitHubTreeEntry | null> {
|
|
391
|
-
let treeSha = this.rootSha
|
|
392
|
-
const parts = path.split('/')
|
|
393
|
-
for (const [index, part] of parts.entries()) {
|
|
394
|
-
const entry = (await this.#read(treeSha)).get(part)
|
|
395
|
-
if (entry === undefined) return null
|
|
396
|
-
if (index === parts.length - 1) return entry
|
|
397
|
-
if (entry.type !== 'tree') return null
|
|
398
|
-
treeSha = entry.sha
|
|
399
|
-
}
|
|
400
|
-
return null
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
#read(treeSha: string): Promise<ReadonlyMap<string, GitHubTreeEntry>> {
|
|
404
|
-
let directory = this.#directories.get(treeSha)
|
|
405
|
-
if (directory === undefined) {
|
|
406
|
-
directory = this.#load(treeSha)
|
|
407
|
-
this.#directories.set(treeSha, directory)
|
|
408
|
-
}
|
|
409
|
-
return directory
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
async #load(treeSha: string): Promise<ReadonlyMap<string, GitHubTreeEntry>> {
|
|
413
|
-
const tree = decodeTree(await this.fetchTree(treeSha), treeSha)
|
|
414
|
-
// A truncated directory listing cannot prove a path is absent.
|
|
415
|
-
if (tree.truncated) throw responseError()
|
|
416
|
-
return new Map(tree.entries.map((entry) => [entry.path, entry]))
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
type GitHubBlobEntry = Extract<GitHubTreeEntry, { readonly type: 'blob' }>
|
|
421
|
-
|
|
422
|
-
/** The entry when it is a regular file within both bounds, otherwise why it is not read. */
|
|
423
|
-
function readableBlob(entry: GitHubTreeEntry | null, remainingBytes: number): GitHubBlobEntry | GitHubUnreadableReason {
|
|
424
|
-
if (entry === null) return 'missing'
|
|
425
|
-
if (entry.type !== 'blob' || entry.mode === '120000') return 'not_a_file'
|
|
426
|
-
if (entry.size > MAX_FILE_BYTES) return 'too_large'
|
|
427
|
-
if (entry.size > remainingBytes) return 'budget'
|
|
428
|
-
return entry
|
|
429
|
-
}
|
|
430
|
-
|
|
431
398
|
function requireBranchAt(ref: GitHubRef, sha: string): GitHubRef {
|
|
432
399
|
if (ref.sha !== sha) throw new GitHubBranchConflictError()
|
|
433
400
|
return ref
|
|
@@ -30,7 +30,8 @@ export interface DecodedTree {
|
|
|
30
30
|
readonly entries: readonly GitHubTreeEntry[];
|
|
31
31
|
readonly truncated: boolean;
|
|
32
32
|
}
|
|
33
|
-
|
|
33
|
+
/** A tree read by `<commit>:<path>`, whose own sha the request did not name. */
|
|
34
|
+
export declare function decodeTreeListing(body: unknown): DecodedTree;
|
|
34
35
|
export declare function decodeCreatedTreeSha(body: unknown): string;
|
|
35
36
|
/** The blob's bytes as UTF-8 text, or null when they are not valid UTF-8. */
|
|
36
37
|
export declare function decodeTextBlob(body: unknown, expectedSha: string, expectedSize: number): string | null;
|