Skip to content

Instantly share code, notes, and snippets.

@danielbodart
Created February 23, 2024 09:59
Show Gist options
  • Select an option

  • Save danielbodart/04c3c8f6a5975c5b8e5eaa21b9402ccb to your computer and use it in GitHub Desktop.

Select an option

Save danielbodart/04c3c8f6a5975c5b8e5eaa21b9402ccb to your computer and use it in GitHub Desktop.
// Function to fetch paginated GitHub API responses
async function fetchPaginatedGitHubData(url: string, headers: Headers): Promise<any[]> {
let results: any[] = [];
let nextPageUrl: string | null = url;
while (nextPageUrl) {
const response: Response = await fetch(nextPageUrl, { headers });
const data = await response.json();
results = results.concat(data);
const linkHeader = response.headers.get('Link');
const matches = linkHeader?.match(/<([^>]+)>; rel="next"/);
nextPageUrl = matches ? matches[1] : null;
}
return results;
}
// Define async function to fetch commit counts with pagination handling
async function getCommitCounts(access: string, org: string, since: string, until: string): Promise<number> {
const headers = new Headers({
"Authorization": "token "+ access,
"Accept": "application/vnd.github.v3+json"
});
let totalCommits = 0;
// Fetch all repositories for the organization with pagination
const repos = await fetchPaginatedGitHubData(`https://api.github.com/orgs/${org}/repos?type=all&per_page=100`, headers);
// Iterate through repositories and count commits with pagination
for (const repo of repos) {
const commits = await fetchPaginatedGitHubData(`https://api.github.com/repos/${org}/${repo.name}/commits?since=${since}&until=${until}&per_page=100`, headers);
totalCommits += commits.length;
console.log(`Repository: ${repo.name}, Commits: ${commits.length}`)
}
return totalCommits;
}
Deno.test("GitHub", async (context) => {
await context.step("calculate commits", async () => {
// Example usage
const access = 'REPLACE_ME';
const org = "REPLACE_ME";
const since = "2023-01-01T00:00:00Z"; // ISO 8601 format
const until = "2024-01-01T00:00:00Z"; // ISO 8601 format
const totalCommits = await getCommitCounts(access, org, since, until);
console.log(`Total commits: ${totalCommits}`)
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment