From f68d76e3d2c91c09a4c535cf298cc3f121713798 Mon Sep 17 00:00:00 2001 From: RaymondVerhoef Date: Mon, 18 May 2026 21:25:18 +0200 Subject: [PATCH] feat: add giteaService for fetching files and content from GitHub repositories --- src/lib/giteaService.js | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/lib/giteaService.js b/src/lib/giteaService.js index 1b60767..9da13e9 100644 --- a/src/lib/giteaService.js +++ b/src/lib/giteaService.js @@ -16,8 +16,8 @@ const GITHUB_RAW = 'https://raw.githubusercontent.com'; * @returns {Promise>} */ 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 + })); } /**