feat: add giteaService for fetching files and content from GitHub repositories

This commit is contained in:
RaymondVerhoef
2026-05-18 21:25:18 +02:00
parent 7b84545dc5
commit f68d76e3d2

View File

@@ -16,8 +16,8 @@ const GITHUB_RAW = 'https://raw.githubusercontent.com';
* @returns {Promise<Array<{name, sha, path}>>} * @returns {Promise<Array<{name, sha, path}>>}
*/ */
export async function getRepoFolder(owner, repo, folder = '', branch = 'main') { export async function getRepoFolder(owner, repo, folder = '', branch = 'main') {
const path = folder ? folder.replace(/^\/|\/$/g, '') : ''; // Use the GitHub Git Trees API to fetch all files recursively in one request
const url = `${GITHUB_API}/repos/${owner}/${repo}/contents/${path}?ref=${branch}`; const url = `${GITHUB_API}/repos/${owner}/${repo}/git/trees/${branch}?recursive=1`;
const res = await fetch(url, { const res = await fetch(url, {
headers: { Accept: 'application/vnd.github+json' }, 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'}`); throw new Error(`GitHub API ${res.status}: ${err.message || 'Could not list folder'}`);
} }
const items = await res.json(); const data = await res.json();
// Return only files (.md / .txt), not sub-directories
return items.filter( // If folder is 'docs', we only want paths starting with 'docs/'
item => item.type === 'file' && (item.name.endsWith('.md') || item.name.endsWith('.txt')) 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
}));
} }
/** /**