MCPcopy Create free account
hub / github.com/CodeGraphContext/CodeGraphContext / fetchGithubRepoFiles

Function fetchGithubRepoFiles

website/src/lib/parser.ts:125–153  ·  view source on GitHub ↗
(url: string, onProgress?: (msg: string) => void)

Source from the content-addressed store, hash-verified

123}
124
125export async function fetchGithubRepoFiles(url: string, onProgress?: (msg: string) => void) {
126 const match = url.match(/github\.com\/([^/]+)\/([^/]+)/);
127 if (!match) throw new Error("Invalid GitHub URL");
128 const [_, owner, repoName] = match;
129 let treeUrl = `https://api.github.com/repos/${owner}/${repoName}/git/trees/main?recursive=1`;
130 let res = await fetch(treeUrl);
131 if (!res.ok) {
132 treeUrl = `https://api.github.com/repos/${owner}/${repoName}/git/trees/master?recursive=1`;
133 res = await fetch(treeUrl);
134 }
135 if (!res.ok) throw new Error("Could not fetch repo.");
136 const data = await res.json();
137 const filePaths = data.tree
138 .filter((t: any) => t.type === "blob")
139 .map((t: any) => t.path)
140 .filter((p: string) => p.match(/\.(js|ts|jsx|tsx|py|c|h|cpp|hpp|cc|cs|go|rs|rb|php|swift|kt|kts|dart)$/) && !p.includes("node_modules") && !p.includes(".git"));
141 const files: { path: string, content: string }[] = [];
142 for (let i = 0; i < Math.min(filePaths.length, 150); i += 10) {
143 const batch = filePaths.slice(i, i + 10);
144 await Promise.all(batch.map(async (path: string) => {
145 try {
146 let r = await fetch(`https://raw.githubusercontent.com/${owner}/${repoName}/main/${path}`);
147 if (!r.ok) r = await fetch(`https://raw.githubusercontent.com/${owner}/${repoName}/master/${path}`);
148 if (r.ok) files.push({ path, content: await r.text() });
149 } catch (err) { }
150 }));
151 }
152 return files;
153}

Callers

nothing calls this directly

Calls 3

mapMethod · 0.65
pushMethod · 0.45
textMethod · 0.45

Tested by

no test coverage detected