diff --git a/src/fetchers/stats.js b/src/fetchers/stats.js index 56af97d89a041..c54cdd93ea81c 100644 --- a/src/fetchers/stats.js +++ b/src/fetchers/stats.js @@ -297,7 +297,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/tests/fetchStats.test.js b/tests/fetchStats.test.js index 73a4f28257d4e..e0b0a146bfc6c 100644 --- a/tests/fetchStats.test.js +++ b/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: { @@ -461,4 +471,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, + }); + }); });