|
|
|
|
@ -18,6 +18,7 @@ export interface GitRepoInfo {
@@ -18,6 +18,7 @@ export interface GitRepoInfo {
|
|
|
|
|
content: string; |
|
|
|
|
format: 'markdown' | 'asciidoc'; |
|
|
|
|
}; |
|
|
|
|
usingGitHubToken?: boolean; // Indicates if GitHub API token was used
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export interface GitBranch { |
|
|
|
|
@ -89,20 +90,30 @@ function parseGitUrl(url: string): { platform: string; owner: string; repo: stri
@@ -89,20 +90,30 @@ function parseGitUrl(url: string): { platform: string; owner: string; repo: stri
|
|
|
|
|
*/ |
|
|
|
|
async function fetchFromGitHub(owner: string, repo: string): Promise<GitRepoInfo | null> { |
|
|
|
|
try { |
|
|
|
|
const repoResponse = await fetchGitHubApi(`https://api.github.com/repos/${owner}/${repo}`); |
|
|
|
|
if (!repoResponse.ok) { |
|
|
|
|
console.warn(`GitHub API error for repo ${owner}/${repo}: ${repoResponse.status} ${repoResponse.statusText}`); |
|
|
|
|
const repoApiResult = await fetchGitHubApi(`https://api.github.com/repos/${owner}/${repo}`); |
|
|
|
|
if (!repoApiResult.response.ok) { |
|
|
|
|
console.warn(`GitHub API error for repo ${owner}/${repo}: ${repoApiResult.response.status} ${repoApiResult.response.statusText}`); |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
const repoData = await repoResponse.json(); |
|
|
|
|
const repoData = await repoApiResult.response.json(); |
|
|
|
|
|
|
|
|
|
// Track if any request used a token
|
|
|
|
|
let usingToken = repoApiResult.usedToken; |
|
|
|
|
|
|
|
|
|
const defaultBranch = repoData.default_branch || 'main'; |
|
|
|
|
const [branchesResponse, commitsResponse, treeResponse] = await Promise.all([ |
|
|
|
|
const [branchesResult, commitsResult, treeResult] = await Promise.all([ |
|
|
|
|
fetchGitHubApi(`https://api.github.com/repos/${owner}/${repo}/branches`), |
|
|
|
|
fetchGitHubApi(`https://api.github.com/repos/${owner}/${repo}/commits?per_page=10`), |
|
|
|
|
fetchGitHubApi(`https://api.github.com/repos/${owner}/${repo}/git/trees/${defaultBranch}?recursive=1`).catch(() => null) |
|
|
|
|
fetchGitHubApi(`https://api.github.com/repos/${owner}/${repo}/git/trees/${defaultBranch}?recursive=1`).catch(() => ({ response: null as any, usedToken: false })) |
|
|
|
|
]); |
|
|
|
|
|
|
|
|
|
// Update token usage flag if any request used a token
|
|
|
|
|
usingToken = usingToken || branchesResult.usedToken || commitsResult.usedToken || (treeResult?.usedToken ?? false); |
|
|
|
|
|
|
|
|
|
const branchesResponse = branchesResult.response; |
|
|
|
|
const commitsResponse = commitsResult.response; |
|
|
|
|
const treeResponse = treeResult?.response; |
|
|
|
|
|
|
|
|
|
// Check if responses are OK and parse JSON
|
|
|
|
|
let branchesData: any[] = []; |
|
|
|
|
let commitsData: any[] = []; |
|
|
|
|
@ -206,10 +217,12 @@ async function fetchFromGitHub(owner: string, repo: string): Promise<GitRepoInfo
@@ -206,10 +217,12 @@ async function fetchFromGitHub(owner: string, repo: string): Promise<GitRepoInfo
|
|
|
|
|
const readmeFiles = ['README.adoc', 'README.md', 'README.rst', 'README.txt']; |
|
|
|
|
for (const readmeFile of readmeFiles) { |
|
|
|
|
try { |
|
|
|
|
const readmeData = await fetchGitHubApi(`https://api.github.com/repos/${owner}/${repo}/contents/${readmeFile}?ref=${defaultBranch}`).then(r => { |
|
|
|
|
if (!r.ok) throw new Error('Not found'); |
|
|
|
|
return r.json(); |
|
|
|
|
}); |
|
|
|
|
const readmeResult = await fetchGitHubApi(`https://api.github.com/repos/${owner}/${repo}/contents/${readmeFile}?ref=${defaultBranch}`); |
|
|
|
|
if (readmeResult.usedToken) { |
|
|
|
|
usingToken = true; |
|
|
|
|
} |
|
|
|
|
if (!readmeResult.response.ok) throw new Error('Not found'); |
|
|
|
|
const readmeData = await readmeResult.response.json(); |
|
|
|
|
if (readmeData.content) { |
|
|
|
|
const content = atob(readmeData.content.replace(/\s/g, '')); |
|
|
|
|
readme = { |
|
|
|
|
@ -244,10 +257,12 @@ async function fetchFromGitHub(owner: string, repo: string): Promise<GitRepoInfo
@@ -244,10 +257,12 @@ async function fetchFromGitHub(owner: string, repo: string): Promise<GitRepoInfo
|
|
|
|
|
// If found in tree, fetch it
|
|
|
|
|
if (readmePath) { |
|
|
|
|
try { |
|
|
|
|
const readmeData = await fetchGitHubApi(`https://api.github.com/repos/${owner}/${repo}/contents/${readmePath}?ref=${defaultBranch}`).then(r => { |
|
|
|
|
if (!r.ok) throw new Error('Not found'); |
|
|
|
|
return r.json(); |
|
|
|
|
}); |
|
|
|
|
const readmeResult = await fetchGitHubApi(`https://api.github.com/repos/${owner}/${repo}/contents/${readmePath}?ref=${defaultBranch}`); |
|
|
|
|
if (readmeResult.usedToken) { |
|
|
|
|
usingToken = true; |
|
|
|
|
} |
|
|
|
|
if (!readmeResult.response.ok) throw new Error('Not found'); |
|
|
|
|
const readmeData = await readmeResult.response.json(); |
|
|
|
|
if (readmeData.content) { |
|
|
|
|
const content = atob(readmeData.content.replace(/\s/g, '')); |
|
|
|
|
const format = readmePath.toLowerCase().endsWith('.adoc') ? 'asciidoc' : 'markdown'; |
|
|
|
|
@ -271,7 +286,8 @@ async function fetchFromGitHub(owner: string, repo: string): Promise<GitRepoInfo
@@ -271,7 +286,8 @@ async function fetchFromGitHub(owner: string, repo: string): Promise<GitRepoInfo
|
|
|
|
|
branches, |
|
|
|
|
commits, |
|
|
|
|
files, |
|
|
|
|
readme |
|
|
|
|
readme, |
|
|
|
|
usingGitHubToken: usingToken |
|
|
|
|
}; |
|
|
|
|
} catch (error) { |
|
|
|
|
console.error('Error fetching from GitHub:', error); |
|
|
|
|
|