mirror of
https://github.com/Aviortheking/codestats-readme.git
synced 2025-04-22 10:42:08 +00:00
feat: Add count_private flag to count private contributions (#148)
* Add private contributions count * Remove unused var and add tests * Update readme * fix: tests & minor code formating * docs: updated docs Co-authored-by: anuraghazra <hazru.anurag@gmail.com>
This commit is contained in:
parent
3a9ded0a09
commit
4c0518616f
@ -17,6 +17,7 @@ module.exports = async (req, res) => {
|
|||||||
hide_border,
|
hide_border,
|
||||||
hide_rank,
|
hide_rank,
|
||||||
show_icons,
|
show_icons,
|
||||||
|
count_private,
|
||||||
line_height,
|
line_height,
|
||||||
title_color,
|
title_color,
|
||||||
icon_color,
|
icon_color,
|
||||||
@ -30,7 +31,7 @@ module.exports = async (req, res) => {
|
|||||||
res.setHeader("Content-Type", "image/svg+xml");
|
res.setHeader("Content-Type", "image/svg+xml");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
stats = await fetchStats(username);
|
stats = await fetchStats(username, parseBoolean(count_private));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return res.send(renderError(err.message));
|
return res.send(renderError(err.message));
|
||||||
}
|
}
|
||||||
|
43
readme.md
43
readme.md
@ -65,6 +65,18 @@ To hide any specific stats, you can pass a query parameter `?hide=` with comma s
|
|||||||

|

|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Adding private contributions count to total commits count
|
||||||
|
|
||||||
|
You can add the count of all your private contributions to the total commits count by using the query parameter `?count_private=true`.
|
||||||
|
|
||||||
|
_Note: If you are deploying this project yourself, the private contributions will be counted by default otherwise you need to chose to share your private contribution counts._
|
||||||
|
|
||||||
|
> Options: `&count_private=true`
|
||||||
|
|
||||||
|
```md
|
||||||
|

|
||||||
|
```
|
||||||
|
|
||||||
### Showing icons
|
### Showing icons
|
||||||
|
|
||||||
To enable icons, you can pass `show_icons=true` in the query param, like so:
|
To enable icons, you can pass `show_icons=true` in the query param, like so:
|
||||||
@ -97,21 +109,22 @@ You can customize the appearance of your `Stats Card` or `Repo Card` however you
|
|||||||
|
|
||||||
Customization Options:
|
Customization Options:
|
||||||
|
|
||||||
| Option | type | description | Stats Card (default) | Repo Card (default) | Top Lang Card (default) |
|
| Option | type | description | Stats Card (default) | Repo Card (default) | Top Lang Card (default) |
|
||||||
| ------------- | --------- | ------------------------------------ | -------------------- | ------------------- | ----------------------- |
|
| ------------- | --------- | ------------------------------------------- | -------------------- | ------------------- | ----------------------- |
|
||||||
| title_color | hex color | title color | 2f80ed | 2f80ed | 2f80ed |
|
| title_color | hex color | title color | 2f80ed | 2f80ed | 2f80ed |
|
||||||
| text_color | hex color | body color | 333 | 333 | 333 |
|
| text_color | hex color | body color | 333 | 333 | 333 |
|
||||||
| icon_color | hex color | icon color | 4c71f2 | 586069 | 586069 |
|
| icon_color | hex color | icon color | 4c71f2 | 586069 | 586069 |
|
||||||
| bg_color | hex color | card bg color | FFFEFE | FFFEFE | FFFEFE |
|
| bg_color | hex color | card bg color | FFFEFE | FFFEFE | FFFEFE |
|
||||||
| line_height | number | control the line-height between text | 30 | N/A | N/A |
|
| line_height | number | control the line-height between text | 30 | N/A | N/A |
|
||||||
| hide | CSV | hides the items specified | undefined | N/A | undefined |
|
| hide | CSV | hides the items specified | undefined | N/A | undefined |
|
||||||
| hide_rank | boolean | hides the ranking | false | N/A | N/A |
|
| hide_rank | boolean | hides the ranking | false | N/A | N/A |
|
||||||
| hide_title | boolean | hides the stats title | false | N/A | false |
|
| hide_title | boolean | hides the stats title | false | N/A | false |
|
||||||
| hide_border | boolean | hides the stats card border | false | N/A | N/A |
|
| hide_border | boolean | hides the stats card border | false | N/A | N/A |
|
||||||
| show_owner | boolean | shows owner name in repo card | N/A | false | N/A |
|
| show_owner | boolean | shows owner name in repo card | N/A | false | N/A |
|
||||||
| show_icons | boolean | shows icons | false | N/A | N/A |
|
| show_icons | boolean | shows icons | false | N/A | N/A |
|
||||||
| theme | string | sets inbuilt theme | 'default' | 'default_repocard' | 'default |
|
| theme | string | sets inbuilt theme | 'default' | 'default_repocard' | 'default |
|
||||||
| cache_seconds | number | manually set custom cache control | 1800 | 1800 | '1800' |
|
| cache_seconds | number | manually set custom cache control | 1800 | 1800 | '1800' |
|
||||||
|
| count_private | boolean | counts private contributions too if enabled | false | N/A | N/A |
|
||||||
|
|
||||||
> Note on cache: Repo cards have default cache of 30mins (1800 seconds) if the fork count & star count is less than 1k otherwise it's 2hours (7200). Also note that cache is clamped to minimum of 30min and maximum of 24hours
|
> Note on cache: Repo cards have default cache of 30mins (1800 seconds) if the fork count & star count is less than 1k otherwise it's 2hours (7200). Also note that cache is clamped to minimum of 30min and maximum of 24hours
|
||||||
|
|
||||||
|
@ -13,6 +13,7 @@ const fetcher = (variables, token) => {
|
|||||||
login
|
login
|
||||||
contributionsCollection {
|
contributionsCollection {
|
||||||
totalCommitContributions
|
totalCommitContributions
|
||||||
|
restrictedContributionsCount
|
||||||
}
|
}
|
||||||
repositoriesContributedTo(first: 1, contributionTypes: [COMMIT, ISSUE, PULL_REQUEST, REPOSITORY]) {
|
repositoriesContributedTo(first: 1, contributionTypes: [COMMIT, ISSUE, PULL_REQUEST, REPOSITORY]) {
|
||||||
totalCount
|
totalCount
|
||||||
@ -45,7 +46,7 @@ const fetcher = (variables, token) => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
async function fetchStats(username) {
|
async function fetchStats(username, count_private = false) {
|
||||||
if (!username) throw Error("Invalid username");
|
if (!username) throw Error("Invalid username");
|
||||||
|
|
||||||
const stats = {
|
const stats = {
|
||||||
@ -66,10 +67,18 @@ async function fetchStats(username) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const user = res.data.data.user;
|
const user = res.data.data.user;
|
||||||
|
const contributionCount = user.contributionsCollection;
|
||||||
|
|
||||||
stats.name = user.name || user.login;
|
stats.name = user.name || user.login;
|
||||||
stats.totalIssues = user.issues.totalCount;
|
stats.totalIssues = user.issues.totalCount;
|
||||||
stats.totalCommits = user.contributionsCollection.totalCommitContributions;
|
|
||||||
|
stats.totalCommits = contributionCount.totalCommitContributions;
|
||||||
|
if (count_private) {
|
||||||
|
stats.totalCommits =
|
||||||
|
contributionCount.totalCommitContributions +
|
||||||
|
contributionCount.restrictedContributionsCount;
|
||||||
|
}
|
||||||
|
|
||||||
stats.totalPRs = user.pullRequests.totalCount;
|
stats.totalPRs = user.pullRequests.totalCount;
|
||||||
stats.contributedTo = user.repositoriesContributedTo.totalCount;
|
stats.contributedTo = user.repositoriesContributedTo.totalCount;
|
||||||
|
|
||||||
|
@ -30,7 +30,10 @@ const data = {
|
|||||||
user: {
|
user: {
|
||||||
name: stats.name,
|
name: stats.name,
|
||||||
repositoriesContributedTo: { totalCount: stats.contributedTo },
|
repositoriesContributedTo: { totalCount: stats.contributedTo },
|
||||||
contributionsCollection: { totalCommitContributions: stats.totalCommits },
|
contributionsCollection: {
|
||||||
|
totalCommitContributions: stats.totalCommits,
|
||||||
|
restrictedContributionsCount: 100,
|
||||||
|
},
|
||||||
pullRequests: { totalCount: stats.totalPRs },
|
pullRequests: { totalCount: stats.totalPRs },
|
||||||
issues: { totalCount: stats.totalIssues },
|
issues: { totalCount: stats.totalIssues },
|
||||||
followers: { totalCount: 0 },
|
followers: { totalCount: 0 },
|
||||||
@ -181,4 +184,36 @@ describe("Test /api/", () => {
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should add private contributions", async () => {
|
||||||
|
const { req, res } = faker(
|
||||||
|
{
|
||||||
|
username: "anuraghazra",
|
||||||
|
count_private: true,
|
||||||
|
},
|
||||||
|
data
|
||||||
|
);
|
||||||
|
|
||||||
|
await api(req, res);
|
||||||
|
|
||||||
|
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
|
||||||
|
expect(res.send).toBeCalledWith(
|
||||||
|
renderStatsCard(
|
||||||
|
{
|
||||||
|
...stats,
|
||||||
|
totalCommits: stats.totalCommits + 100,
|
||||||
|
rank: calculateRank({
|
||||||
|
totalCommits: stats.totalCommits + 100,
|
||||||
|
totalRepos: 1,
|
||||||
|
followers: 0,
|
||||||
|
contributions: stats.contributedTo,
|
||||||
|
stargazers: stats.totalStars,
|
||||||
|
prs: stats.totalPRs,
|
||||||
|
issues: stats.totalIssues,
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
{}
|
||||||
|
)
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -9,7 +9,7 @@ const data = {
|
|||||||
user: {
|
user: {
|
||||||
name: "Anurag Hazra",
|
name: "Anurag Hazra",
|
||||||
repositoriesContributedTo: { totalCount: 61 },
|
repositoriesContributedTo: { totalCount: 61 },
|
||||||
contributionsCollection: { totalCommitContributions: 100 },
|
contributionsCollection: { totalCommitContributions: 100, restrictedContributionsCount: 50 },
|
||||||
pullRequests: { totalCount: 300 },
|
pullRequests: { totalCount: 300 },
|
||||||
issues: { totalCount: 200 },
|
issues: { totalCount: 200 },
|
||||||
followers: { totalCount: 100 },
|
followers: { totalCount: 100 },
|
||||||
@ -77,4 +77,29 @@ describe("Test fetchStats", () => {
|
|||||||
"Could not resolve to a User with the login of 'noname'."
|
"Could not resolve to a User with the login of 'noname'."
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should fetch and add private contributions", async () => {
|
||||||
|
mock.onPost("https://api.github.com/graphql").reply(200, data);
|
||||||
|
|
||||||
|
let stats = await fetchStats("anuraghazra", true);
|
||||||
|
const rank = calculateRank({
|
||||||
|
totalCommits: 150,
|
||||||
|
totalRepos: 5,
|
||||||
|
followers: 100,
|
||||||
|
contributions: 61,
|
||||||
|
stargazers: 400,
|
||||||
|
prs: 300,
|
||||||
|
issues: 200,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(stats).toStrictEqual({
|
||||||
|
contributedTo: 61,
|
||||||
|
name: "Anurag Hazra",
|
||||||
|
totalCommits: 150,
|
||||||
|
totalIssues: 200,
|
||||||
|
totalPRs: 300,
|
||||||
|
totalStars: 400,
|
||||||
|
rank,
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
Loading…
x
Reference in New Issue
Block a user