feat: add giteaService for fetching files and content from GitHub repositories
This commit is contained in:
@@ -16,8 +16,8 @@ const GITHUB_RAW = 'https://raw.githubusercontent.com';
|
||||
* @returns {Promise<Array<{name, sha, path}>>}
|
||||
*/
|
||||
export async function getRepoFolder(owner, repo, folder = '', branch = 'main') {
|
||||
const path = folder ? folder.replace(/^\/|\/$/g, '') : '';
|
||||
const url = `${GITHUB_API}/repos/${owner}/${repo}/contents/${path}?ref=${branch}`;
|
||||
// Use the GitHub Git Trees API to fetch all files recursively in one request
|
||||
const url = `${GITHUB_API}/repos/${owner}/${repo}/git/trees/${branch}?recursive=1`;
|
||||
|
||||
const res = await fetch(url, {
|
||||
headers: { Accept: 'application/vnd.github+json' },
|
||||
@@ -28,11 +28,23 @@ export async function getRepoFolder(owner, repo, folder = '', branch = 'main') {
|
||||
throw new Error(`GitHub API ${res.status}: ${err.message || 'Could not list folder'}`);
|
||||
}
|
||||
|
||||
const items = await res.json();
|
||||
// Return only files (.md / .txt), not sub-directories
|
||||
return items.filter(
|
||||
item => item.type === 'file' && (item.name.endsWith('.md') || item.name.endsWith('.txt'))
|
||||
);
|
||||
const data = await res.json();
|
||||
|
||||
// If folder is 'docs', we only want paths starting with 'docs/'
|
||||
const folderPrefix = folder ? folder.replace(/^\/|\/$/g, '') + '/' : '';
|
||||
|
||||
// Return only files (blobs) that match the folder prefix and file extensions
|
||||
return data.tree
|
||||
.filter(item =>
|
||||
item.type === 'blob' &&
|
||||
item.path.startsWith(folderPrefix) &&
|
||||
(item.path.endsWith('.md') || item.path.endsWith('.txt'))
|
||||
)
|
||||
.map(item => ({
|
||||
name: item.path.split('/').pop(),
|
||||
path: item.path,
|
||||
sha: item.sha
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user