Skip to content

Commit d1fce01

Browse files
committed
remove concept of directory prefix, remove default branch in github source
1 parent b364346 commit d1fce01

8 files changed

Lines changed: 36 additions & 66 deletions

File tree

src/components/Folder/Folder.test.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ describe('Folder Component', () => {
9595
sourceId: 'test-source',
9696
sourceParts: [{ text: 'test-source', sourceId: 'test-source' }],
9797
kind: 'directory',
98-
prefix: '',
9998
listFiles: () => Promise.resolve(mockFiles),
10099
}
101100
const { getByPlaceholderText, findByText, getByText, queryByText } = render(<Folder source={dirSource} />)
@@ -133,7 +132,6 @@ describe('Folder Component', () => {
133132
sourceId: 'test-source',
134133
sourceParts: [{ text: 'test-source', sourceId: 'test-source' }],
135134
kind: 'directory',
136-
prefix: '',
137135
listFiles: () => Promise.resolve(mockFiles),
138136
}
139137
const { getByPlaceholderText, findByText } = render(<Folder source={dirSource} />)
@@ -153,7 +151,6 @@ describe('Folder Component', () => {
153151
sourceId: 'test-source',
154152
sourceParts: [{ text: 'test-source', sourceId: 'test-source' }],
155153
kind: 'directory',
156-
prefix: '',
157154
listFiles: async () => {
158155
await fetch('something') // to ensure we wait for loading
159156
return []

src/components/Folder/Folder.tsx

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ export default function Folder({ source }: FolderProps) {
6363
} else if (e.key === 'Enter') {
6464
// if there is only one result, view it
6565
if (filtered?.length === 1 && 0 in filtered) {
66-
const key = join(source.prefix, filtered[0].name)
67-
if (key.endsWith('/')) {
66+
const file = filtered[0]
67+
if (file.kind === 'directory') {
6868
// clear search because we're about to change folder
6969
if (searchRef.current) {
7070
searchRef.current.value = ''
7171
}
7272
setSearchQuery('')
7373
}
74-
location.href = `/files?key=${key}`
74+
location.href = routes?.getSourceRouteUrl?.({ sourceId: file.sourceId }) ?? `/files?key=${file.sourceId}`
7575
}
7676
} else if (e.key === 'ArrowDown') {
7777
// move focus to first list item
@@ -81,7 +81,7 @@ export default function Folder({ source }: FolderProps) {
8181
searchElement?.addEventListener('keyup', handleKeyup)
8282
// Clean up event listener
8383
return () => searchElement?.removeEventListener('keyup', handleKeyup)
84-
}, [filtered, source.prefix])
84+
}, [filtered, routes])
8585

8686
// Jump to search box if user types '/'
8787
useEffect(() => {
@@ -97,7 +97,7 @@ export default function Folder({ source }: FolderProps) {
9797
return () => { document.removeEventListener('keydown', handleKeydown) }
9898
}, [])
9999

100-
return <Layout error={error} title={source.prefix}>
100+
return <Layout error={error} title={source.sourceId}>
101101
<Breadcrumb source={source}>
102102
<input autoFocus className={cn(styles.search, customClass?.search)} placeholder='Search...' ref={searchRef} />
103103
<Dropdown className={styles.settings} label={gearIcon} align='right'>
@@ -114,7 +114,7 @@ export default function Folder({ source }: FolderProps) {
114114
<ul className={cn(styles.fileList, customClass?.fileList)} ref={listRef}>
115115
{filtered.map((file, index) =>
116116
<li key={index}>
117-
<a href={routes?.getSourceRouteUrl?.({ sourceId: file.sourceId }) ?? location.href}>
117+
<a href={routes?.getSourceRouteUrl?.({ sourceId: file.sourceId }) ?? `/files?key=${file.sourceId}`}>
118118
<span data-file-kind={file.kind}>
119119
{file.name}
120120
</span>
@@ -133,7 +133,3 @@ export default function Folder({ source }: FolderProps) {
133133
}
134134
</Layout>
135135
}
136-
137-
function join(prefix: string, file: string) {
138-
return prefix ? prefix + '/' + file : file
139-
}

src/lib/sources/gitHubSource.ts

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,47 @@ interface BaseUrl {
77
source: string
88
origin: string
99
repo: string
10+
}
11+
12+
interface RepoUrl extends BaseUrl {
13+
kind: 'repo'
14+
}
15+
16+
interface PathUrl extends BaseUrl {
1017
branch: string
1118
path: string
1219
}
1320

14-
interface DirectoryUrl extends BaseUrl {
21+
interface DirectoryUrl extends PathUrl {
1522
kind: 'directory'
1623
action: 'tree'
1724
}
1825

19-
interface FileUrl extends BaseUrl {
26+
interface FileUrl extends PathUrl {
2027
kind: 'file'
2128
action?: 'blob' | 'raw' | 'raw/refs/heads'
2229
resolveUrl: string
2330
}
2431

25-
interface RawFileUrl extends BaseUrl {
32+
interface RawFileUrl extends PathUrl {
2633
kind: 'file'
2734
action: undefined
2835
resolveUrl: string
2936
}
3037

31-
type GHUrl = DirectoryUrl | FileUrl | RawFileUrl
38+
type GHUrl = RepoUrl | DirectoryUrl | FileUrl | RawFileUrl
3239

3340
const baseUrl = 'https://github.com'
3441
const baseRawUrl = 'https://raw.githubusercontent.com'
3542

3643
function getSourceParts(url: GHUrl): SourcePart[] {
44+
if (url.kind === 'repo') {
45+
return [{
46+
sourceId: `${baseUrl}/${url.repo}`,
47+
text: `${baseUrl}/${url.repo}`,
48+
}]
49+
}
50+
3751
const sourceParts: SourcePart[] = [{
3852
sourceId: `${baseUrl}/${url.repo}/tree/${url.branch}/`,
3953
text: `${baseUrl}/${url.repo}/tree/${url.branch}/`,
@@ -55,11 +69,10 @@ function getSourceParts(url: GHUrl): SourcePart[] {
5569
}
5670
return sourceParts
5771
}
58-
function getPrefix(url: DirectoryUrl): string {
59-
return `${baseUrl}/${url.repo}/tree/${url.branch}${url.path}`.replace(/\/$/, '')
60-
}
61-
async function fetchFilesList(url: DirectoryUrl, options?: { requestInit?: RequestInit, accessToken?: string }): Promise<FileMetadata[]> {
62-
const apiURL = `https://api.github.com/repos/${url.repo}/contents${url.path}?ref=${url.branch}`
72+
async function fetchFilesList(url: DirectoryUrl | RepoUrl, options?: { requestInit?: RequestInit, accessToken?: string }): Promise<FileMetadata[]> {
73+
const path = url.kind === 'repo' ? '/' : url.path
74+
const branchParam = url.kind === 'repo' ? '' : `?ref=${url.branch}`
75+
const apiURL = `https://api.github.com/repos/${url.repo}/contents${path}${branchParam}`
6376
const headers = new Headers(options?.requestInit?.headers)
6477
headers.set('Accept', 'application/vnd.github+json')
6578
if (options?.accessToken) {
@@ -74,11 +87,11 @@ async function fetchFilesList(url: DirectoryUrl, options?: { requestInit?: Reque
7487
throw new Error(`GitHub API error: ${response.status} ${response.statusText} - ${await response.text()}`)
7588
}
7689
try {
77-
const data = await response.json() as {name: string, path: string, type: 'file' | 'dir', size: number}[]
90+
const data = await response.json() as {html_url: string, path: string, type: 'file' | 'dir', size: number}[]
7891
return data.map((file) => ({
7992
name: getFileName(file.path),
8093
fileSize: file.size,
81-
sourceId: `${url.origin}/${url.repo}/${file.type === 'file' ? 'blob' : 'tree'}/${url.branch}/${file.path}`.replace(/\/$/, ''),
94+
sourceId: file.html_url,
8295
kind: file.type === 'file' ? 'file' : 'directory',
8396
}))
8497
} catch (error) {
@@ -88,12 +101,13 @@ async function fetchFilesList(url: DirectoryUrl, options?: { requestInit?: Reque
88101
export function getGitHubSource(sourceId: string, options?: {requestInit?: RequestInit, accessToken?: string}): FileSource | DirSource | undefined {
89102
try {
90103
const url = parseGitHubUrl(sourceId)
104+
const path = url.kind === 'repo' ? '/' : url.path
91105
async function fetchVersions() {
92106
const branches = await fetchBranchesList(url, options)
93107
return {
94108
label: 'Branches',
95109
versions: branches.map((branch) => {
96-
const branchSourceId = `${baseUrl}/${url.repo}/${url.kind === 'file' ? 'blob' : 'tree'}/${branch}${url.path}`
110+
const branchSourceId = `${baseUrl}/${url.repo}/${url.kind === 'file' ? 'blob' : 'tree'}/${branch}${path}`
97111
return {
98112
label: branch,
99113
sourceId: branchSourceId,
@@ -116,7 +130,6 @@ export function getGitHubSource(sourceId: string, options?: {requestInit?: Reque
116130
kind: 'directory',
117131
sourceId,
118132
sourceParts: getSourceParts(url),
119-
prefix: getPrefix(url),
120133
listFiles: () => fetchFilesList(url, options),
121134
fetchVersions,
122135
}
@@ -174,13 +187,10 @@ export function parseGitHubUrl(url: string): GHUrl {
174187
)?.groups
175188
if (repoGroups?.owner !== undefined && repoGroups.repo !== undefined) {
176189
return {
177-
kind: 'directory',
190+
kind: 'repo',
178191
source: url,
179192
origin: urlObject.origin,
180193
repo: repoGroups.owner + '/' + repoGroups.repo,
181-
action: 'tree',
182-
branch: 'main', // hardcode the default branch
183-
path: '',
184194
}
185195
}
186196

src/lib/sources/httpSource.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ export function getHttpSource(sourceId: string, options?: {requestInit?: Request
9898
kind: 'directory',
9999
sourceId,
100100
sourceParts,
101-
prefix,
102101
listFiles: () => s3list(bucket, prefix).then(items =>
103102
items
104103
// skip s3 directory placeholder

src/lib/sources/huggingFaceSource.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,6 @@ function getSourceParts(url: HFUrl): SourcePart[] {
7272
}
7373
return sourceParts
7474
}
75-
function getPrefix(url: DirectoryUrl): string {
76-
return `${url.origin}/${getFullName(url)}/tree/${url.branch}${url.path}`.replace(/\/$/, '')
77-
}
7875
async function fetchFilesList(url: DirectoryUrl, options?: { requestInit?: RequestInit, accessToken?: string }): Promise<FileMetadata[]> {
7976
const repoFullName = getFullName(url)
8077
const filesIterator = listFiles({
@@ -134,7 +131,6 @@ export function getHuggingFaceSource(sourceId: string, options?: {requestInit?:
134131
kind: 'directory',
135132
sourceId,
136133
sourceParts: getSourceParts(url),
137-
prefix: getPrefix(url),
138134
listFiles: () => fetchFilesList(url, options),
139135
fetchVersions,
140136
}

src/lib/sources/hyperparamSource.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ export function getHyperparamSource(sourceId: string, { endpoint, requestInit }:
8080
kind: 'directory',
8181
sourceId,
8282
sourceParts,
83-
prefix,
8483
listFiles: () => listFiles(prefix, { endpoint, requestInit }),
8584
}
8685
}

src/lib/sources/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ export interface FileSource extends BaseSource {
3939

4040
export interface DirSource extends BaseSource {
4141
kind: 'directory'
42-
prefix: string,
4342
listFiles: () => Promise<FileMetadata[]>
4443
}
4544

test/lib/sources/gitHubSource.test.ts

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@ describe('parseGitHubUrl', () => {
1010
const origin = `https://${domain}`
1111
const url = `${origin}/owner/repo`
1212
expect(parseGitHubUrl(url)).toEqual({
13-
kind: 'directory',
13+
kind: 'repo',
1414
origin,
1515
repo: 'owner/repo',
1616
source: url,
17-
action: 'tree',
18-
branch: 'main',
19-
path: '',
2017
})
2118
})
2219

@@ -46,29 +43,6 @@ describe('parseGitHubUrl', () => {
4643
})
4744

4845
test.for([
49-
// Root directory
50-
[
51-
'https://github.com/owner/repo',
52-
'https://github.com/owner/repo',
53-
'owner/repo',
54-
'main',
55-
'',
56-
],
57-
[
58-
'https://github.com/owner/repo/',
59-
'https://github.com/owner/repo/',
60-
'owner/repo',
61-
'main',
62-
'',
63-
],
64-
// all-number identifier is not a valid GitHub repo name, but we accept any string
65-
[
66-
'https://github.com/owner/123',
67-
'https://github.com/owner/123',
68-
'owner/123',
69-
'main',
70-
'',
71-
],
7246
// Branches
7347
[
7448
'https://github.com/owner/repo/tree/branch',
@@ -148,8 +122,8 @@ describe('getGitHubSource', () => {
148122
it('returns the URL for a repository URL', () => {
149123
const url = 'https://github.com/owner/repo'
150124
expect(getGitHubSource(url)?.sourceParts).toEqual([{
151-
sourceId: 'https://github.com/owner/repo/tree/main/',
152-
text: 'https://github.com/owner/repo/tree/main/',
125+
sourceId: 'https://github.com/owner/repo',
126+
text: 'https://github.com/owner/repo',
153127
}])
154128
})
155129
it('returns the URL for a branch root URL', () => {

0 commit comments

Comments
 (0)