Skip to content

Commit 9337e14

Browse files
authored
Merge pull request #40 from ARYPROGRAMMER/contributors-page-fresh
fix: repo commit count via pagination
2 parents 32e49dd + e198e0e commit 9337e14

File tree

1 file changed

+37
-18
lines changed

1 file changed

+37
-18
lines changed

templates/index.html

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -551,39 +551,34 @@ <h1 class="text-5xl font-bold mb-4" id="mainTitle">Our Amazing Contributors</h1>
551551
timeout = setTimeout(() => func.apply(this, args), wait);
552552
};
553553
}
554-
554+
555555
async function fetchGitHubData() {
556556
const headers = {
557557
'Authorization': `token ${config.github.token}`,
558-
'Accept': 'application/vnd.github.v3+json'
558+
'Accept': 'application/vnd.github.inertia-preview+json'
559559
};
560560

561561
try {
562-
// Fetch data only for the specific repository
563-
const [repoData, contributors, prs] = await Promise.all([
562+
// Fetch data for the specific repository
563+
const [repoData, contributors, prs, projects] = await Promise.all([
564564
fetch(`${config.github.apiUrl}/repos/${config.github.owner}/${config.github.repo}`, { headers })
565565
.then(res => res.json()),
566566
fetch(`${config.github.apiUrl}/repos/${config.github.owner}/${config.github.repo}/contributors`, { headers })
567567
.then(res => res.json()),
568568
fetch(`${config.github.apiUrl}/repos/${config.github.owner}/${config.github.repo}/pulls?state=all`, { headers })
569+
.then(res => res.json()),
570+
fetch(`${config.github.apiUrl}/repos/${config.github.owner}/${config.github.repo}/projects`, { headers })
569571
.then(res => res.json())
570572
]);
571573

572574
// Check if the specific repository is archived or active
573-
const isActive = repoData.archived === false;
574-
const activeProjectsCount = isActive ? 1 : 0;
575-
576-
// Fetch commits from the default branch
577-
const defaultBranch = repoData.default_branch || 'main'; // Fallback to 'main' if not provided
578-
const commits = await fetch(
579-
`${config.github.apiUrl}/repos/${config.github.owner}/${config.github.repo}/commits?sha=${defaultBranch}`,
580-
{ headers }
581-
).then(res => res.json());
575+
const isActive = !repoData.archived;
582576

583-
const defaultBranchCommitsCount = Array.isArray(commits) ? commits.length : 0;
577+
// Fetch total commit count from the repoData
578+
const defaultBranchCommitsCount = repoData.commits || await getCommitCount(repoData.default_branch);
579+
// Count the number of projects associated with the repository
580+
const projectCount = Array.isArray(projects) ? projects.length : 0;
584581

585-
586-
587582
state.allContributors = contributors;
588583
updateFilterCounts(contributors);
589584

@@ -595,7 +590,7 @@ <h1 class="text-5xl font-bold mb-4" id="mainTitle">Our Amazing Contributors</h1>
595590
},
596591
contributors.length,
597592
prs.length,
598-
activeProjectsCount
593+
projectCount
599594
);
600595
renderFilterButtons(contributors.length);
601596
renderContributors(contributors);
@@ -605,6 +600,30 @@ <h1 class="text-5xl font-bold mb-4" id="mainTitle">Our Amazing Contributors</h1>
605600
}
606601
}
607602

603+
async function getCommitCount(branch) {
604+
const headers = {
605+
'Authorization': `token ${config.github.token}`,
606+
'Accept': 'application/vnd.github.v3+json'
607+
};
608+
const url = `${config.github.apiUrl}/repos/${config.github.owner}/${config.github.repo}/commits?sha=${branch}&per_page=100`;
609+
let page = 1;
610+
let totalCommits = 0;
611+
612+
while (true) {
613+
const response = await fetch(`${url}&page=${page}`, { headers });
614+
const commits = await response.json();
615+
616+
if (!Array.isArray(commits) || commits.length === 0) {
617+
break;
618+
}
619+
620+
totalCommits += commits.length;
621+
page += 1;
622+
}
623+
624+
return totalCommits;
625+
}
626+
608627
function renderStats(repoData, contributorsCount, prsCount, activeProjectsCount) {
609628
const stats = [
610629
{
@@ -634,7 +653,7 @@ <h1 class="text-5xl font-bold mb-4" id="mainTitle">Our Amazing Contributors</h1>
634653
if (!statsContainer) return;
635654

636655
statsContainer.innerHTML = stats.map(stat => createStatCard(stat)).join('');
637-
}
656+
}
638657

639658
function createStatCard(stat) {
640659
return `

0 commit comments

Comments
 (0)