From c1324b31bfc6da38c0f3c529b1562c80ec6ecd76 Mon Sep 17 00:00:00 2001 From: Rick Staa Date: Fri, 16 Sep 2022 13:09:19 +0200 Subject: [PATCH] feat: add 'exclude_repo' option to stats card (#1450) This commit allows users to exclude repositories for the stats card using the `exclude_repo` option. --- api/index.js | 2 ++ readme.md | 11 +++++---- src/fetchers/stats-fetcher.js | 22 +++++++++++++++--- tests/fetchStats.test.js | 43 +++++++++++++++++++++++++++++++---- 4 files changed, 65 insertions(+), 13 deletions(-) diff --git a/api/index.js b/api/index.js index b07464d..2075005 100644 --- a/api/index.js +++ b/api/index.js @@ -29,6 +29,7 @@ module.exports = async (req, res) => { bg_color, theme, cache_seconds, + exclude_repo, custom_title, locale, disable_animations, @@ -50,6 +51,7 @@ module.exports = async (req, res) => { username, parseBoolean(count_private), parseBoolean(include_all_commits), + parseArray(exclude_repo), ); const cacheSeconds = clampValue( diff --git a/readme.md b/readme.md index d8e4c7d..a4034ee 100644 --- a/readme.md +++ b/readme.md @@ -67,12 +67,12 @@ Give india logo -Are you considering supporting the project by donating? Please DON'T!! +Are you considering supporting the project by donating? Please DON'T!! -Instead, Help India fight the 2nd deadly wave of COVID-19. -Thousands of people are dying in India because of a lack of Oxygen & also COVID-related infrastructure. +Instead, Help India fight the 2nd deadly wave of COVID-19. +Thousands of people are dying in India because of a lack of Oxygen & also COVID-related infrastructure. -Visit [https://indiafightscorona.giveindia.org](https://indiafightscorona.giveindia.org) and make a small donation to help us fight COVID and overcome this crisis. +Visit [https://indiafightscorona.giveindia.org](https://indiafightscorona.giveindia.org) and make a small donation to help us fight COVID and overcome this crisis. A small donation goes a long way. :heart:

@@ -171,7 +171,7 @@ You can customize the appearance of your `Stats Card` or `Repo Card` however you - `locale` - set the language in the card _(e.g. cn, de, es, etc.)_ - `border_radius` - Corner rounding on the card -> Note: The minimum of cache_seconds is currently 4 hours as a temporary fix for PATs exhaustion. +> Note: The minimum of cache_seconds is currently 4 hours as a temporary fix for PATs exhaustion. ##### Gradient in bg_color @@ -193,6 +193,7 @@ You can provide multiple comma-separated values in the bg_color option to render - `include_all_commits` - Count total commits instead of just the current year commits _(boolean)_ - `count_private` - Count private commits _(boolean)_ - `line_height` - Sets the line-height between text _(number)_ +- `exclude_repo` - Exclude stars from specified repositories _(Comma-separated values)_ - `custom_title` - Sets a custom title for the card - `disable_animations` - Disables all animations in the card _(boolean)_ diff --git a/src/fetchers/stats-fetcher.js b/src/fetchers/stats-fetcher.js index 2617137..dc9be5b 100644 --- a/src/fetchers/stats-fetcher.js +++ b/src/fetchers/stats-fetcher.js @@ -47,6 +47,7 @@ const fetcher = (variables, token) => { repositories(first: 100, ownerAffiliations: OWNER, orderBy: {direction: DESC, field: STARGAZERS}) { totalCount nodes { + name stargazers { totalCount } @@ -108,6 +109,7 @@ async function fetchStats( username, count_private = false, include_all_commits = false, + exclude_repo = [], ) { if (!username) throw new MissingParamError(["username"]); @@ -133,6 +135,15 @@ async function fetchStats( const user = res.data.data.user; + // populate repoToHide map for quick lookup + // while filtering out + let repoToHide = {}; + if (exclude_repo) { + exclude_repo.forEach((repoName) => { + repoToHide[repoName] = true; + }); + } + stats.name = user.name || user.login; stats.totalIssues = user.openIssues.totalCount + user.closedIssues.totalCount; @@ -154,9 +165,14 @@ async function fetchStats( stats.totalPRs = user.pullRequests.totalCount; stats.contributedTo = user.repositoriesContributedTo.totalCount; - stats.totalStars = user.repositories.nodes.reduce((prev, curr) => { - return prev + curr.stargazers.totalCount; - }, 0); + // Retrieve stars while filtering out repositories to be hidden + stats.totalStars = user.repositories.nodes + .filter((data) => { + return !repoToHide[data.name]; + }) + .reduce((prev, curr) => { + return prev + curr.stargazers.totalCount; + }, 0); stats.rank = calculateRank({ totalCommits: stats.totalCommits, diff --git a/tests/fetchStats.test.js b/tests/fetchStats.test.js index f732182..fe2fd4d 100644 --- a/tests/fetchStats.test.js +++ b/tests/fetchStats.test.js @@ -20,11 +20,11 @@ const data = { repositories: { totalCount: 5, nodes: [ - { stargazers: { totalCount: 100 } }, - { stargazers: { totalCount: 100 } }, - { stargazers: { totalCount: 100 } }, - { stargazers: { totalCount: 50 } }, - { stargazers: { totalCount: 50 } }, + { name: "test-repo-1", stargazers: { totalCount: 100 } }, + { name: "test-repo-2", stargazers: { totalCount: 100 } }, + { name: "test-repo-3", stargazers: { totalCount: 100 } }, + { name: "test-repo-4", stargazers: { totalCount: 50 } }, + { name: "test-repo-5", stargazers: { totalCount: 50 } }, ], }, }, @@ -134,4 +134,37 @@ describe("Test fetchStats", () => { rank, }); }); + + it("should exclude stars of the `test-repo-1` repository", async () => { + mock.onPost("https://api.github.com/graphql").reply(200, data); + mock + .onGet("https://api.github.com/search/commits?q=author:anuraghazra") + .reply(200, { total_count: 1000 }); + + let stats = await fetchStats( + "anuraghazra", + true, + true, + (exclude_repo = ["test-repo-1"]), + ); + const rank = calculateRank({ + totalCommits: 1050, + totalRepos: 5, + followers: 100, + contributions: 61, + stargazers: 300, + prs: 300, + issues: 200, + }); + + expect(stats).toStrictEqual({ + contributedTo: 61, + name: "Anurag Hazra", + totalCommits: 1050, + totalIssues: 200, + totalPRs: 300, + totalStars: 300, + rank, + }); + }); });