Skip to content

Commit

Permalink
feat: add 'exclude_repo' option to stats card (#1450)
Browse files Browse the repository at this point in the history
This commit allows users to exclude repositories for the stats card
using the `exclude_repo` option.
  • Loading branch information
rickstaa authored Sep 16, 2022
1 parent dc8852d commit c1324b3
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 13 deletions.
2 changes: 2 additions & 0 deletions api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module.exports = async (req, res) => {
bg_color,
theme,
cache_seconds,
exclude_repo,
custom_title,
locale,
disable_animations,
Expand All @@ -50,6 +51,7 @@ module.exports = async (req, res) => {
username,
parseBoolean(count_private),
parseBoolean(include_all_commits),
parseArray(exclude_repo),
);

const cacheSeconds = clampValue(
Expand Down
11 changes: 6 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@
<img src="https://d2wvdrxmr8p0wf.cloudfront.net/static/giveindia.svg" alt="Give india logo" width="200" />
</a>

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:
</p>

Expand Down Expand Up @@ -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

Expand All @@ -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)_

Expand Down
22 changes: 19 additions & 3 deletions src/fetchers/stats-fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const fetcher = (variables, token) => {
repositories(first: 100, ownerAffiliations: OWNER, orderBy: {direction: DESC, field: STARGAZERS}) {
totalCount
nodes {
name
stargazers {
totalCount
}
Expand Down Expand Up @@ -108,6 +109,7 @@ async function fetchStats(
username,
count_private = false,
include_all_commits = false,
exclude_repo = [],
) {
if (!username) throw new MissingParamError(["username"]);

Expand All @@ -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;

Expand All @@ -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,
Expand Down
43 changes: 38 additions & 5 deletions tests/fetchStats.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 } },
],
},
},
Expand Down Expand Up @@ -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,
});
});
});

1 comment on commit c1324b3

@vercel
Copy link

@vercel vercel bot commented on c1324b3 Sep 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.