Repository: anuraghazra/github-readme-stats
Pull request #3238 (2023-09-16 15:12:32)

refactor: change confusing behavior of showing 0 commints when upstream API fails

vercel[bot]: [vc]: #WxXwPZIcSvZTCsRo8364ccyQ6t61m7WMtNIy1yBrtn8=:eyJpc01vbm9yZXBvIjp0cnVlLCJ0eXBlIjoiZ2l0aHViIiwicHJvamVjdHMiOlt7Im5hbWUiOiJnaXRodWItcmVhZG1lLXN0YXRzIiwiaW5zcGVjdG9yVXJsIjoiaHR0cHM6Ly92ZXJjZWwuY29tL2dpdGh1Yi1yZWFkbWUtc3RhdHMtdGVhbS9naXRodWItcmVhZG1lLXN0YXRzLzZwYzNXb2czVzg0dkNkb1NVdWI4amEySzNDcGgiLCJwcmV2aWV3VXJsIjoiZ2l0aHViLXJlYWRtZS1zdGF0cy1naXQtcmVmYWN0by0xZjI0NTctZ2l0aHViLXJlYWRtZS1zdGF0cy10ZWFtLnZlcmNlbC5hcHAiLCJuZXh0Q29tbWl0U3RhdHVzIjoiREVQTE9ZRUQifV19
**The latest updates on your projects**. Learn more about [Vercel for Git ↗︎](https://vercel.link/github-learn-more)

| Name | Status | Preview | Updated (UTC) |
| :--- | :----- | :------ | :------ |
| **github-readme-stats** | ✅ Ready ([Inspect](https://vercel.com/github-readme-stats-team/github-readme-stats/6pc3Wog3W84vCdoSUub8ja2K3Cph)) | [Visit Preview](https://github-readme-stats-git-refacto-1f2457-github-readme-stats-team.vercel.app) | Sep 16, 2023 3:12pm |


codecov[bot]: ## [Codecov](https://app.codecov.io/gh/anuraghazra/github-readme-stats/pull/3238?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Anurag+Hazra) Report
Patch coverage: **`81.81%`** and project coverage change: **`-0.02%`** :warning:
> Comparison is base [(`518747e`)](https://app.codecov.io/gh/anuraghazra/github-readme-stats/commit/518747e99ded9b94d9cbf813148b3add995adc80?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Anurag+Hazra) 98.38% compared to head [(`e2f02f4`)](https://app.codecov.io/gh/anuraghazra/github-readme-stats/pull/3238?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Anurag+Hazra) 98.36%.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##           master    #3238      +/-   ##
==========================================
- Coverage   98.38%   98.36%   -0.02%     
==========================================
  Files          27       27              
  Lines        5884     5885       +1     
  Branches      525      526       +1     
==========================================
  Hits         5789     5789              
- Misses         92       93       +1     
  Partials        3        3              
```


| [Files Changed](https://app.codecov.io/gh/anuraghazra/github-readme-stats/pull/3238?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Anurag+Hazra) | Coverage Δ | |
|---|---|---|
| [src/fetchers/stats-fetcher.js](https://app.codecov.io/gh/anuraghazra/github-readme-stats/pull/3238?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Anurag+Hazra#diff-c3JjL2ZldGNoZXJzL3N0YXRzLWZldGNoZXIuanM=) | `94.57% <81.81%> (-0.33%)` | :arrow_down: |


</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/anuraghazra/github-readme-stats/pull/3238?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Anurag+Hazra).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Anurag+Hazra).


Patch:
diff --git a/src/fetchers/stats-fetcher.js b/src/fetchers/stats-fetcher.js
index dd62b46..f021a72 100644
--- a/src/fetchers/stats-fetcher.js
+++ b/src/fetchers/stats-fetcher.js
@@ -153,8 +153,8 @@ const statsFetcher = async (username) => {
  */
 const totalCommitsFetcher = async (username) => {
   if (!githubUsernameRegex.test(username)) {
-    logger.log("Invalid username");
-    return 0;
+    logger.log("Invalid username provided.");
+    throw new Error("Invalid username provided.");
   }
 
   // https://developer.github.com/v3/search/#search-commits
@@ -170,18 +170,19 @@ const totalCommitsFetcher = async (username) => {
     });
   };
 
+  let res;
   try {
-    let res = await retryer(fetchTotalCommits, { login: username });
-    let total_count = res.data.total_count;
-    if (!!total_count && !isNaN(total_count)) {
-      return res.data.total_count;
-    }
+    res = await retryer(fetchTotalCommits, { login: username });
   } catch (err) {
     logger.log(err);
+    throw new Error(err);
+  }
+
+  const totalCount = res.data.total_count;
+  if (!totalCount || isNaN(totalCount)) {
+    throw new Error("Could not fetch total commits.");
   }
-  // just return 0 if there is something wrong so that
-  // we don't break the whole app
-  return 0;
+  return totalCount;
 };
 
 /**


Test patch:
diff --git a/tests/fetchStats.test.js b/tests/fetchStats.test.js
index ac4d0a3..56125d8 100644
--- a/tests/fetchStats.test.js
+++ b/tests/fetchStats.test.js
@@ -210,62 +210,20 @@ describe("Test fetchStats", () => {
     });
   });
 
-  it("should return 0 commits when all_commits true and invalid username", async () => {
-    let stats = await fetchStats("asdf///---", true);
-    expect(stats).toStrictEqual({
-      contributedTo: 61,
-      name: "Anurag Hazra",
-      totalCommits: 0,
-      totalIssues: 200,
-      totalPRs: 300,
-      totalPRsMerged: 240,
-      mergedPRsPercentage: 80,
-      totalReviews: 50,
-      totalStars: 300,
-      totalDiscussionsStarted: 10,
-      totalDiscussionsAnswered: 40,
-      rank: calculateRank({
-        all_commits: true,
-        commits: 0,
-        prs: 300,
-        reviews: 50,
-        issues: 200,
-        repos: 5,
-        stars: 300,
-        followers: 100,
-      }),
-    });
+  it("should throw specific error when include_all_commits true and invalid username", async () => {
+    expect(fetchStats("asdf///---", true)).rejects.toThrow(
+      new Error("Invalid username provided."),
+    );
   });
 
-  it("should return 0 commits when all_commits true and API returns error", async () => {
+  it("should throw specific error when include_all_commits true and API returns error", async () => {
     mock
       .onGet("https://api.github.com/search/commits?q=author:anuraghazra")
       .reply(200, { error: "Some test error message" });
 
-    let stats = await fetchStats("anuraghazra", true);
-    expect(stats).toStrictEqual({
-      contributedTo: 61,
-      name: "Anurag Hazra",
-      totalCommits: 0,
-      totalIssues: 200,
-      totalPRs: 300,
-      totalPRsMerged: 240,
-      mergedPRsPercentage: 80,
-      totalReviews: 50,
-      totalStars: 300,
-      totalDiscussionsStarted: 10,
-      totalDiscussionsAnswered: 40,
-      rank: calculateRank({
-        all_commits: true,
-        commits: 0,
-        prs: 300,
-        reviews: 50,
-        issues: 200,
-        repos: 5,
-        stars: 300,
-        followers: 100,
-      }),
-    });
+    expect(fetchStats("anuraghazra", true)).rejects.toThrow(
+      new Error("Could not fetch total commits."),
+    );
   });
 
   it("should exclude stars of the `test-repo-1` repository", async () => {