Skip to content

Commit 309cc7c

Browse files
committed
fetch branches
1 parent 13b4a80 commit 309cc7c

File tree

1 file changed

+44
-22
lines changed

1 file changed

+44
-22
lines changed

src/lib/sources/gitHubSource.ts

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -104,26 +104,19 @@ async function fetchFilesList(url: DirectoryUrl, options?: { requestInit?: Reque
104104
export function getGitHubSource(sourceId: string, options?: {requestInit?: RequestInit, accessToken?: string}): FileSource | DirSource | undefined {
105105
try {
106106
const url = parseGitHubUrl(sourceId)
107-
// async function fetchVersions() {
108-
// const refsList = await fetchRefsList(url, options)
109-
// return {
110-
// label: 'Branches',
111-
// versions: refsList.map(({ refType, name, ref }) => {
112-
// const label = refType === 'branches' ? name :
113-
// refType === 'converts' ? `[convert] ${name}` :
114-
// refType === 'tags' ? `[tag] ${name}` :
115-
// `[pr] ${name}`
116-
// // remove refs/heads/ from the ref name
117-
// // e.g. refs/heads/main -> main
118-
// const fixedRef = refType === 'branches' ? ref.replace(/refs\/heads\//, '') : ref
119-
// const branchSourceId = `${url.origin}/${getFullName(url)}/${url.kind === 'file' ? 'blob' : 'tree'}/${fixedRef}${url.path}`
120-
// return {
121-
// label,
122-
// sourceId: branchSourceId,
123-
// }
124-
// }),
125-
// }
126-
// }
107+
async function fetchVersions() {
108+
const branches = await fetchBranchesList(url, options)
109+
return {
110+
label: 'Branches',
111+
versions: branches.map((branch) => {
112+
const branchSourceId = `${url.origin}/${url.repo}/${url.kind === 'file' ? 'blob' : 'tree'}/${branch}${url.path}`
113+
return {
114+
label: branch,
115+
sourceId: branchSourceId,
116+
}
117+
}),
118+
}
119+
}
127120
if (url.kind === 'file') {
128121
return {
129122
kind: 'file',
@@ -132,7 +125,7 @@ export function getGitHubSource(sourceId: string, options?: {requestInit?: Reque
132125
fileName: getFileName(url.path),
133126
resolveUrl: url.resolveUrl,
134127
requestInit: options?.requestInit,
135-
// fetchVersions,
128+
fetchVersions,
136129
}
137130
} else {
138131
return {
@@ -141,7 +134,7 @@ export function getGitHubSource(sourceId: string, options?: {requestInit?: Reque
141134
sourceParts: getSourceParts(url),
142135
prefix: getPrefix(url),
143136
listFiles: () => fetchFilesList(url, options),
144-
// fetchVersions,
137+
fetchVersions,
145138
}
146139
}
147140
} catch {
@@ -260,3 +253,32 @@ export function parseGitHubUrl(url: string): GHUrl {
260253

261254
throw new Error('Unsupported GitHub URL')
262255
}
256+
257+
/**
258+
* List branches in a GitHub dataset repo
259+
*
260+
* Example API URL: https://api.github.com/repos/owner/repo/branches
261+
*
262+
* @param repo (namespace/repo)
263+
* @param [options]
264+
* @param [options.requestInit] - request init object to pass to fetch
265+
* @param [options.accessToken] - access token to use for authentication
266+
*
267+
* @returns the list of branch names
268+
*/
269+
async function fetchBranchesList(
270+
url: GHUrl,
271+
options?: {requestInit?: RequestInit, accessToken?: string}
272+
): Promise<string[]> {
273+
const headers = new Headers(options?.requestInit?.headers)
274+
headers.set('accept', 'application/vnd.github+json')
275+
if (options?.accessToken) {
276+
headers.set('Authorization', `Bearer ${options.accessToken}`)
277+
}
278+
const response = await fetch(`https://api.github.com/repos/${url.repo}/branches`, { ...options?.requestInit, headers })
279+
if (!response.ok) {
280+
throw new Error(`HTTP error ${response.status.toString()}`)
281+
}
282+
const branches = await response.json() as {name: string}[]
283+
return branches.map(({ name }) => name)
284+
}

0 commit comments

Comments
 (0)