feat: implement commits year option and display appropriate year label (#2107)

* add custom year

* edit fetcher

* add tests

* add documentation

* Update vercel.json

* Update fetchStats.test.js

* Update readme.md

* Update readme.md

Co-authored-by: Rick Staa <rick.staa@outlook.com>

* test: fix tests

* style: format code

* fix: fix commit stats time frame description #1053

This commit makes sure the last year label is shown when
`include_all_commits` or `year` is not provided. See #1053 for more
information.

* resolved merge conflict

* Update stats-fetcher.js

* resolve comments

* Update src/cards/types.d.ts

Co-authored-by: Alexandr Garbuzov <qwerty541zxc@gmail.com>

* Update src/fetchers/stats-fetcher.js

Co-authored-by: Alexandr Garbuzov <qwerty541zxc@gmail.com>

* graphQL year only to commit

* change argument name to commits_year

* Update readme.md

Co-authored-by: Alexandr Garbuzov <qwerty541zxc@gmail.com>

* Update readme.md

Co-authored-by: Alexandr Garbuzov <qwerty541zxc@gmail.com>

* Update readme.md

Co-authored-by: Alexandr Garbuzov <qwerty541zxc@gmail.com>

* wrap label into function

* review

* more review

* fix test

* Update readme.md

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* docs

---------

Co-authored-by: Touch Sungkawichai <talay@10ninox.com>
Co-authored-by: Rick Staa <rick.staa@outlook.com>
Co-authored-by: Alexandr Garbuzov <qwerty541zxc@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2025-09-17 22:01:37 +03:00
1 parent 63b402177d
commit b594b57b56
9 files changed
+118 -18

No files matched your search

+3 -1
View File
@@ -38,8 +38,10 @@ const data_stats = {
user: {
name: stats.name,
repositoriesContributedTo: { totalCount: stats.contributedTo },
contributionsCollection: {
commits: {
totalCommitContributions: stats.totalCommits,
},
reviews: {
totalPullRequestReviewContributions: stats.totalReviews,
},
pullRequests: { totalCount: stats.totalPRs },
+3 -1
View File
@@ -24,8 +24,10 @@ const data_stats = {
user: {
name: stats.name,
repositoriesContributedTo: { totalCount: stats.contributedTo },
contributionsCollection: {
commits: {
totalCommitContributions: stats.totalCommits,
},
reviews: {
totalPullRequestReviewContributions: stats.totalReviews,
},
pullRequests: { totalCount: stats.totalPRs },
+54 -2
View File
@@ -11,8 +11,10 @@ const data_stats = {
user: {
name: "Anurag Hazra",
repositoriesContributedTo: { totalCount: 61 },
contributionsCollection: {
commits: {
totalCommitContributions: 100,
},
reviews: {
totalPullRequestReviewContributions: 50,
},
pullRequests: { totalCount: 300 },
@@ -38,6 +40,9 @@ const data_stats = {
},
};
const data_year2003 = JSON.parse(JSON.stringify(data_stats));
data_year2003.data.user.commits.totalCommitContributions = 428;
const data_repo = {
data: {
user: {
@@ -91,9 +96,18 @@ const mock = new MockAdapter(axios);
beforeEach(() => {
process.env.FETCH_MULTI_PAGE_STARS = "false"; // Set to `false` to fetch only one page of stars.
mock.onPost("https://api.github.com/graphql").reply((cfg) => {
let req = JSON.parse(cfg.data);
if (
req.variables &&
req.variables.startTime &&
req.variables.startTime.startsWith("2003")
) {
return [200, data_year2003];
}
return [
200,
cfg.data.includes("contributionsCollection") ? data_stats : data_repo,
req.query.includes("totalCommitContributions") ? data_stats : data_repo,
];
});
});
@@ -409,4 +423,42 @@ describe("Test fetchStats", () => {
rank,
});
});
it("should get commits of provided year", async () => {
let stats = await fetchStats(
"anuraghazra",
false,
[],
false,
false,
false,
2003,
);
const rank = calculateRank({
all_commits: false,
commits: 428,
prs: 300,
reviews: 50,
issues: 200,
repos: 5,
stars: 300,
followers: 100,
});
expect(stats).toStrictEqual({
contributedTo: 61,
name: "Anurag Hazra",
totalCommits: 428,
totalIssues: 200,
totalPRs: 300,
totalPRsMerged: 0,
mergedPRsPercentage: 0,
totalReviews: 50,
totalStars: 300,
totalDiscussionsStarted: 0,
totalDiscussionsAnswered: 0,
rank,
});
});
});
+1 -1
View File
@@ -387,7 +387,7 @@ describe("Test renderStatsCard", () => {
document.querySelector(
'g[transform="translate(0, 25)"]>.stagger>.stat.bold',
).textContent,
).toMatchInlineSnapshot(`"累计提交总数 (${new Date().getFullYear()}):"`);
).toMatchInlineSnapshot(`"累计提交总数 (去年):"`);
expect(
document.querySelector(
'g[transform="translate(0, 50)"]>.stagger>.stat.bold',