Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: total commits count #15

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 55 additions & 2 deletions api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async function fetchStats(username) {
totalCount
}
contributionsCollection {
totalCommitContributions
contributionYears
}
pullRequests(first: 100) {
totalCount
Expand Down Expand Up @@ -53,9 +53,61 @@ async function fetchStats(username) {

const user = res.data.data.user;

// FETCH ALL COMMITS
let commitPromises = user.contributionsCollection.contributionYears.map(
async (year) => {
// Don't fetch contributions older than 2008 (Perf optimization)
if (year < 2008) {
return {
user: { contributionsCollection: { totalCommitContributions: 0 } },
};
}

const currentDate = new Date();
currentDate.setFullYear(year, 0, 0);

const res = await axios({
url: "https://api.github.com/graphql",
method: "post",
headers: {
Authorization: `bearer ${process.env.GITHUB_TOKEN}`,
},
data: {
query: `
query userInfo($login: String!, $from: DateTime!) {
user(login: $login) {
contributionsCollection(from: $from) {
totalCommitContributions
}
}
}
`,
variables: {
login: username,
from: currentDate.toISOString(),
},
},
});

console.log(
currentDate.toDateString(),
res.data.data.user.contributionsCollection.totalCommitContributions
);
return res.data.data;
}
);

const allCommits = await Promise.all(commitPromises);

const totalCommits = allCommits.reduce((preYear, currYear) => {
return (
preYear + currYear.user.contributionsCollection.totalCommitContributions
);
}, 0);

stats.name = user.name;
stats.totalIssues = user.issues.totalCount;
stats.totalCommits = user.contributionsCollection.totalCommitContributions;
stats.totalCommits = totalCommits;
stats.totalPRs = user.pullRequests.totalCount;
stats.contributedTo = user.repositoriesContributedTo.totalCount;

Expand Down Expand Up @@ -137,6 +189,7 @@ module.exports = async (req, res) => {

const stats = await fetchStats(username);

res.setHeader('Cache-Control', 'public, max-age=300');
res.setHeader("Content-Type", "image/svg+xml");
res.send(
renderSVG(stats, {
Expand Down
1 change: 1 addition & 0 deletions api/pin.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ module.exports = async (req, res) => {

const repoData = await fetchRepo(username, repo);

res.setHeader('Cache-Control', 'public, max-age=300');
res.setHeader("Content-Type", "image/svg+xml");
res.send(renderRepoCard(repoData));
};