diff --git a/backend/src/fetchers/stats.js b/backend/src/fetchers/stats.js index 2467b5fa..2e642dc6 100644 --- a/backend/src/fetchers/stats.js +++ b/backend/src/fetchers/stats.js @@ -408,7 +408,8 @@ const fetchStats = async ( if (include_merged_pull_requests) { stats.totalPRsMerged = user.mergedPullRequests.totalCount; stats.mergedPRsPercentage = - (user.mergedPullRequests.totalCount / user.pullRequests.totalCount) * 100; + (user.mergedPullRequests.totalCount / user.pullRequests.totalCount) * + 100 || 0; } stats.totalReviews = user.reviews.totalPullRequestReviewContributions; stats.totalIssues = user.openIssues.totalCount + user.closedIssues.totalCount; diff --git a/backend/tests/fetchStats.test.js b/backend/tests/fetchStats.test.js index 0ef91113..6697e5c6 100644 --- a/backend/tests/fetchStats.test.js +++ b/backend/tests/fetchStats.test.js @@ -43,6 +43,16 @@ const data_stats = { const data_year2003 = JSON.parse(JSON.stringify(data_stats)); data_year2003.data.user.commits.totalCommitContributions = 428; +const data_without_pull_requests = { + data: { + user: { + ...data_stats.data.user, + pullRequests: { totalCount: 0 }, + mergedPullRequests: { totalCount: 0 }, + }, + }, +}; + const data_repo = { data: { user: { @@ -512,4 +522,37 @@ describe("Test fetchStats", () => { rank, }); }); + + it("should return correct data when user don't have any pull requests", async () => { + mock.reset(); + mock + .onPost("https://api.github.com/graphql") + .reply(200, data_without_pull_requests); + const stats = await fetchStats("anuraghazra", false, [], true); + const rank = calculateRank({ + all_commits: false, + commits: 100, + prs: 0, + reviews: 50, + issues: 200, + repos: 5, + stars: 300, + followers: 100, + }); + + expect(stats).toStrictEqual({ + contributedTo: 61, + name: "Anurag Hazra", + totalCommits: 100, + totalIssues: 200, + totalPRs: 0, + totalPRsMerged: 0, + mergedPRsPercentage: 0, + totalReviews: 50, + totalStars: 300, + totalDiscussionsStarted: 0, + totalDiscussionsAnswered: 0, + rank, + }); + }); });