( owner: string, repo: string, filename: string, branch: string, env: Env, ctx: ExecutionContext, )
| 33 | |
| 34 | // Helper: search for a file in a GitHub repository using the GitHub Search API |
| 35 | export async function searchGitHubRepo( |
| 36 | owner: string, |
| 37 | repo: string, |
| 38 | filename: string, |
| 39 | branch: string, |
| 40 | env: Env, |
| 41 | ctx: ExecutionContext, |
| 42 | ): Promise<GitHubFile | null> { |
| 43 | try { |
| 44 | const cachedFile = await getCachedFilePath(owner, repo, env); |
| 45 | let filePath = cachedFile?.path || ""; |
| 46 | |
| 47 | if (!filePath) { |
| 48 | // Use the centralized GitHub client to search for the file |
| 49 | const data = await searchFileByName(filename, owner, repo, env); |
| 50 | |
| 51 | // Handle search failure |
| 52 | if (!data) { |
| 53 | return null; |
| 54 | } |
| 55 | |
| 56 | // Check if we found any matches |
| 57 | if (data.total_count === 0 || !data.items || data.items.length === 0) { |
| 58 | return null; |
| 59 | } |
| 60 | |
| 61 | // Get the first matching file's path |
| 62 | filePath = data.items[0]?.path; |
| 63 | } |
| 64 | |
| 65 | const content = await fetchFileFromGitHub( |
| 66 | owner, |
| 67 | repo, |
| 68 | branch, |
| 69 | filePath, |
| 70 | env, |
| 71 | ); |
| 72 | |
| 73 | if (content) { |
| 74 | ctx.waitUntil( |
| 75 | cacheFilePath(owner, repo, filename, filePath, branch, env), |
| 76 | ); |
| 77 | return { content, path: filePath }; |
| 78 | } |
| 79 | |
| 80 | return null; |
| 81 | } catch (error) { |
| 82 | console.error( |
| 83 | `Error searching GitHub repo ${owner}/${repo} for ${filename}:`, |
| 84 | error, |
| 85 | ); |
| 86 | return null; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | export function constructGithubUrl( |
| 91 | owner: string, |
no test coverage detected