diff --git a/contributors/contributors.js b/contributors/contributors.js index c7af093..6ffb5f7 100644 --- a/contributors/contributors.js +++ b/contributors/contributors.js @@ -6,6 +6,20 @@ const repoUrl = `https://api.github.com/repos/${repoOwner}/${repoName}`; async function fetchContributorData() { try { + // Fetch all contributors using pagination + const contributors = await fetchAllContributors(); + + // Fetch repository data (stars, forks, etc.) + const repoRes = await fetch(repoUrl); + const repoData = await repoRes.json(); + + // Render stats + renderStats(repoData, contributors); + + // Render contributors + renderContributors(contributors); + + // Fetch repository data to get star and fork counts const repoRes = await fetch(repoUrl); const repoData = await repoRes.json(); @@ -65,6 +79,7 @@ async function fetchContributorData() { `).join(''); + } catch (error) { console.error("Error fetching data:", error); } @@ -72,7 +87,55 @@ async function fetchContributorData() { } +// Fetch all contributors across multiple pages +async function fetchAllContributors() { + let contributors = []; + let page = 1; + let response; + + do { + response = await fetch(`${contributorsUrl}?page=${page}&per_page=100`); + const contributorsData = await response.json(); + contributors.push(...contributorsData); + page++; + } while (response.headers.get('link') && response.headers.get('link').includes('rel="next"')); // Check for "next" link in the header + + return contributors; +} + +// Render stats like total contributions, stars, forks, etc. +function renderStats(repoData, contributors) { + const statsGrid = document.getElementById("statsGrid"); + + statsGrid.innerHTML = ` +

${contributors.length}

Contributors

+

${contributors.reduce((sum, { contributions }) => sum + contributions, 0)}

Total Contributions

+

${repoData.stargazers_count}

GitHub Stars

+

${repoData.forks_count}

Forks

+ `; +} + +// Render the list of contributors +function renderContributors(contributors) { + const contributorsContainer = document.getElementById("contributors"); + + contributorsContainer.innerHTML = contributors.map(({ login, contributions, avatar_url, html_url }) => ` +
+ ${login}'s avatar +

${login}

+

Contributions: ${contributions}

+ GitHub Profile +
+ `).join(''); +} + +// Call the function to fetch and display data fetchContributorData(); +// Initialize the page when the DOM is loaded +document.addEventListener('DOMContentLoaded', function () { + // Your initialization code here if needed +}); +fetchContributorData();