-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgitHubSource.ts
More file actions
283 lines (266 loc) · 9.29 KB
/
gitHubSource.ts
File metadata and controls
283 lines (266 loc) · 9.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import type { DirSource, FileMetadata, FileSource, SourcePart } from './types.js'
import { getFileName } from './utils.js'
interface BaseUrl {
source: string
origin: string
repo: string
branch: string
path: string
}
interface DirectoryUrl extends BaseUrl {
kind: 'directory'
action: 'tree'
}
interface FileUrl extends BaseUrl {
kind: 'file'
action?: 'blob' | 'raw' | 'raw/refs/heads'
resolveUrl: string
}
interface RawFileUrl extends BaseUrl {
kind: 'file'
action: undefined
resolveUrl: string
}
type GHUrl = DirectoryUrl | FileUrl | RawFileUrl
const baseUrl = 'https://github.com'
const baseRawUrl = 'https://raw.githubusercontent.com'
function getSourceParts(url: GHUrl): SourcePart[] {
const sourceParts: SourcePart[] = [{
sourceId: `${baseUrl}/${url.repo}/tree/${url.branch}/`,
text: `${baseUrl}/${url.repo}/tree/${url.branch}/`,
}]
const pathParts = url.path.split('/').filter(d => d.length > 0)
const lastPart = pathParts.at(-1)
if (lastPart) {
for (const [i, part] of pathParts.slice(0, -1).entries()) {
sourceParts.push({
sourceId: `${baseUrl}/${url.repo}/tree/${url.branch}/${pathParts.slice(0, i + 1).join('/')}`,
text: part + '/',
})
}
sourceParts.push({
sourceId: `${baseUrl}/${url.repo}/${url.action === 'tree' ? 'tree/' : 'blob/'}${url.branch}${url.path}`,
text: lastPart,
})
}
return sourceParts
}
function getPrefix(url: DirectoryUrl): string {
return `${baseUrl}/${url.repo}/tree/${url.branch}${url.path}`.replace(/\/$/, '')
}
async function fetchFilesList(url: DirectoryUrl, options?: { requestInit?: RequestInit, accessToken?: string }): Promise<FileMetadata[]> {
const apiURL = `https://api.github.com/repos/${url.repo}/contents${url.path}?ref=${url.branch}`
const headers = new Headers(options?.requestInit?.headers)
headers.set('Accept', 'application/vnd.github+json')
if (options?.accessToken) {
headers.set('Authorization', `Bearer ${options.accessToken}`)
}
const response = await fetch(apiURL, {
...options?.requestInit,
method: 'GET',
headers,
})
if (!response.ok) {
throw new Error(`GitHub API error: ${response.status} ${response.statusText} - ${await response.text()}`)
}
try {
const data: unknown = await response.json()
const isDirectory = Array.isArray(data)
if (!isDirectory) {
throw new Error('Not a directory')
}
const files: FileMetadata[] = []
for (const file of data as unknown[]) {
if (typeof file !== 'object' || file === null || !('name' in file) || !('path' in file) || !('type' in file) || !('size' in file)) {
throw new Error('Invalid file metadata')
}
if (file.type !== 'file' && file.type !== 'dir') {
throw new Error('Unsupported file type')
}
if (typeof file.name !== 'string' || typeof file.path !== 'string' || typeof file.size !== 'number') {
throw new Error('Invalid file metadata types')
}
files.push({
name: getFileName(file.path),
fileSize: file.size,
sourceId: `${url.origin}/${url.repo}/${file.type === 'file' ? 'blob' : 'tree'}/${url.branch}/${file.path}`.replace(/\/$/, ''),
kind: file.type === 'file' ? 'file' : 'directory', // 'unknown' is considered as a directory
})
}
return files
} catch (error) {
throw new Error(`Failed to parse GitHub API response: ${error instanceof Error ? error.message : String(error)}`)
}
}
export function getGitHubSource(sourceId: string, options?: {requestInit?: RequestInit, accessToken?: string}): FileSource | DirSource | undefined {
try {
const url = parseGitHubUrl(sourceId)
async function fetchVersions() {
const branches = await fetchBranchesList(url, options)
return {
label: 'Branches',
versions: branches.map((branch) => {
const branchSourceId = `${baseUrl}/${url.repo}/${url.kind === 'file' ? 'blob' : 'tree'}/${branch}${url.path}`
return {
label: branch,
sourceId: branchSourceId,
}
}),
}
}
if (url.kind === 'file') {
return {
kind: 'file',
sourceId,
sourceParts: getSourceParts(url),
fileName: getFileName(url.path),
resolveUrl: url.resolveUrl,
requestInit: options?.requestInit,
fetchVersions,
}
} else {
return {
kind: 'directory',
sourceId,
sourceParts: getSourceParts(url),
prefix: getPrefix(url),
listFiles: () => fetchFilesList(url, options),
fetchVersions,
}
}
} catch {
return undefined
}
}
export function parseGitHubUrl(url: string): GHUrl {
const urlObject = new URL(url)
// ^ throws 'TypeError: URL constructor: {url} is not a valid URL.' if url is not a valid URL
if (
urlObject.protocol !== 'https:' ||
![
'github.co', 'github.com', 'www.github.com', 'raw.githubusercontent.com',
].includes(urlObject.host)
) {
throw new Error('Not a GitHub URL')
}
const { pathname } = urlObject
if (urlObject.host === 'raw.githubusercontent.com') {
// https://raw.githubusercontent.com/apache/parquet-testing/refs/heads/master/variant/README.md
const rawFileGroups =
/^\/(?<owner>[^/]+)\/(?<repo>[^/]+)\/(?<action>(refs\/heads\/)?)(?<branch>[^/]+)(?<path>(\/[^/]+)+)$/.exec(
pathname
)?.groups
if (
rawFileGroups?.owner !== undefined &&
rawFileGroups.repo !== undefined &&
rawFileGroups.branch !== undefined &&
rawFileGroups.path !== undefined
) {
const branch = rawFileGroups.branch.replace(/\//g, '%2F')
const source = `${urlObject.origin}/${rawFileGroups.owner}/${rawFileGroups.repo}/${branch}${rawFileGroups.path}`
return {
kind: 'file',
source,
origin: urlObject.origin,
repo: rawFileGroups.owner + '/' + rawFileGroups.repo,
branch,
path: rawFileGroups.path,
resolveUrl: source,
}
} else {
throw new Error('Unsupported GitHub URL')
}
}
const repoGroups = /^\/(?<owner>[^/]+)\/(?<repo>[^/]+)\/?$/.exec(
pathname
)?.groups
if (repoGroups?.owner !== undefined && repoGroups.repo !== undefined) {
return {
kind: 'directory',
source: url,
origin: urlObject.origin,
repo: repoGroups.owner + '/' + repoGroups.repo,
action: 'tree',
branch: 'main', // hardcode the default branch
path: '',
}
}
const folderGroups =
/^\/(?<owner>[^/]+)\/(?<repo>[^/]+)\/(?<action>tree)\/(?<branch>[^/]+)(?<path>(\/[^/]+)*)\/?$/.exec(
pathname
)?.groups
if (
folderGroups?.owner !== undefined &&
folderGroups.repo !== undefined &&
folderGroups.action !== undefined &&
folderGroups.branch !== undefined &&
folderGroups.path !== undefined
) {
const branch = folderGroups.branch.replace(/\//g, '%2F')
const source = `${urlObject.origin}/${folderGroups.owner}/${folderGroups.repo}/${folderGroups.action}/${branch}${folderGroups.path}`
return {
kind: 'directory',
source,
origin: urlObject.origin,
repo: folderGroups.owner + '/' + folderGroups.repo,
action: 'tree',
branch,
path: folderGroups.path,
}
}
// https://github.com/apache/parquet-testing/blob/master/variant/README.md
// https://github.com/apache/parquet-testing/raw/refs/heads/master/variant/README.md
const fileGroups =
/^\/(?<owner>[^/]+)\/(?<repo>[^/]+)\/(?<action>blob|raw|raw\/refs\/heads)\/(?<branch>[^/]+)(?<path>(\/[^/]+)+)$/.exec(
pathname
)?.groups
if (
fileGroups?.owner !== undefined &&
fileGroups.repo !== undefined &&
fileGroups.action !== undefined &&
fileGroups.branch !== undefined &&
fileGroups.path !== undefined
) {
const branch = fileGroups.branch.replace(/\//g, '%2F')
const source = `${urlObject.origin}/${fileGroups.owner}/${fileGroups.repo}/${fileGroups.action}/${branch}${fileGroups.path}`
return {
kind: 'file',
source,
origin: urlObject.origin,
repo: fileGroups.owner + '/' + fileGroups.repo,
action: fileGroups.action === 'blob' ? 'blob' : fileGroups.action === 'raw' ? 'raw' : 'raw/refs/heads',
branch,
path: fileGroups.path,
resolveUrl: `${baseRawUrl}/${fileGroups.owner}/${fileGroups.repo}/${branch}${fileGroups.path}`,
}
}
throw new Error('Unsupported GitHub URL')
}
/**
* List branches in a GitHub dataset repo
*
* Example API URL: https://api.github.com/repos/owner/repo/branches
*
* @param repo (namespace/repo)
* @param [options]
* @param [options.requestInit] - request init object to pass to fetch
* @param [options.accessToken] - access token to use for authentication
*
* @returns the list of branch names
*/
async function fetchBranchesList(
url: GHUrl,
options?: {requestInit?: RequestInit, accessToken?: string}
): Promise<string[]> {
const headers = new Headers(options?.requestInit?.headers)
headers.set('accept', 'application/vnd.github+json')
if (options?.accessToken) {
headers.set('Authorization', `Bearer ${options.accessToken}`)
}
const response = await fetch(`https://api.github.com/repos/${url.repo}/branches`, { ...options?.requestInit, headers })
if (!response.ok) {
throw new Error(`HTTP error ${response.status.toString()}`)
}
const branches = await response.json() as {name: string}[]
return branches.map(({ name }) => name)
}