Created
April 28, 2025 16:21
-
-
Save lynsei/6eeb4b42914e7cde2a165f3494801da6 to your computer and use it in GitHub Desktop.
[cruft-code] - template parsing repo data #typescript #deno
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export async function fetchAllRepos(): Promise<Type.RepoData[]> { | |
const repos: Type.RepoData[] = []; | |
let page = 1; | |
const perPage = 500; | |
while (true) { | |
console.log(blue(`Fetching page ${page}...`)); | |
const response = await fetch( | |
`https://api.github.com/orgs/${ORG_NAME}/repos?per_page=${perPage}&page=${page}`, | |
{ headers: GITHUB_HEADERS } | |
); | |
if (!response.ok) { | |
console.error(`Failed to fetch repositories: ${response.status} ${response.statusText}`); | |
Deno.exit(1); | |
} | |
const data = await response.json(); | |
if (data.length === 0) { | |
break; | |
} | |
for (const repo of data) { | |
// Fetch topics for each repository | |
const topicsResponse = await fetch( | |
`https://api.github.com/repos/${ORG_NAME}/${repo.name}/topics`, | |
{ headers: GITHUB_HEADERS } | |
); | |
let topics: string[] = []; | |
if (topicsResponse.ok) { | |
const topicsData = await topicsResponse.json(); | |
topics = topicsData.names; | |
} else { | |
console.warn(`Failed to fetch topics for ${repo.name}: ${topicsResponse.status} ${topicsResponse.statusText}`); | |
} | |
repos.push({ | |
url: repo.clone_url, | |
name: repo.name, | |
topics, | |
}); | |
} | |
page += 1; | |
} | |
return repos; | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export async function findTopics() { | |
const repos = await fetchAllRepos(); | |
const yamlData = stringify({ repos }); | |
await Deno.writeTextFile("./downloads.yml", yamlData); | |
console.log("./downloads.yml has been created successfully."); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @function getOrgRepos | |
* @description Fetches all repositories for a given organization from the GitHub API. | |
* @param org [string] The name of the organization. | |
* @param verbose [boolean] If true, logs additional information. | |
* @returns {Promise<Type.RepoData[]>} A promise that resolves to an array of repository data. | |
*/ | |
export async function getOrgRepos(org: string, verbose = false): Promise<Type.RepoData[]> { | |
const response = await fetch(`https://api.github.com/orgs/${org}/repos?per_page=100000`, { | |
headers: GITHUB_HEADERS, | |
}); | |
if (!response.ok) throw new Error(`Failed to fetch repos for ${org}`); | |
const repos = await response.json(); | |
const results: Type.RepoData[] = []; | |
for (const repo of repos) { | |
const topicsResp = await fetch(`https://api.github.com/repos/${org}/${repo.name}/topics`, { | |
headers: GITHUB_HEADERS, | |
}); | |
const topicData = await topicsResp.json(); | |
if (verbose) { | |
const loggable: Type.ColoredString<"red"> = { | |
text: `Fetched ${repo.name} with topics: ${topicData.names.join(", ")}`, | |
color: "red", | |
}; | |
logColor("red", loggable); | |
} | |
results.push({ | |
url: repo.html_url, | |
name: repo.name, | |
topics: topicData.names ?? [], | |
}); | |
} | |
return results; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment