From ecfd9484df51200ad61ac949ccbad3c1d5ce7e16 Mon Sep 17 00:00:00 2001 From: Touch Sungkawichai Date: Tue, 4 Oct 2022 16:19:47 +0900 Subject: [PATCH 01/22] add custom year --- api/index.js | 3 +++ src/cards/stats-card.js | 7 ++++++- src/cards/types.d.ts | 1 + src/fetchers/stats-fetcher.js | 8 +++++++- 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/api/index.js b/api/index.js index 10dd48478ec6b..663130de42dc3 100644 --- a/api/index.js +++ b/api/index.js @@ -24,6 +24,7 @@ export default async (req, res) => { show_icons, count_private, include_all_commits, + year, line_height, title_color, icon_color, @@ -55,6 +56,7 @@ export default async (req, res) => { parseBoolean(count_private), parseBoolean(include_all_commits), parseArray(exclude_repo), + parseInt(year), ); const cacheSeconds = clampValue( @@ -74,6 +76,7 @@ export default async (req, res) => { card_width: parseInt(card_width, 10), hide_rank: parseBoolean(hide_rank), include_all_commits: parseBoolean(include_all_commits), + year: parseInt(year), line_height, title_color, icon_color, diff --git a/src/cards/stats-card.js b/src/cards/stats-card.js index 4b0cd33a49a21..88e17030073a1 100644 --- a/src/cards/stats-card.js +++ b/src/cards/stats-card.js @@ -72,6 +72,7 @@ const renderStatsCard = (stats = {}, options = { hide: [] }) => { card_width, hide_rank = false, include_all_commits = false, + year, line_height = 25, title_color, icon_color, @@ -118,7 +119,11 @@ const renderStatsCard = (stats = {}, options = { hide: [] }) => { commits: { icon: icons.commits, label: `${i18n.t("statcard.commits")}${ - include_all_commits ? "" : ` (${new Date().getFullYear()})` + include_all_commits + ? "" + : year + ? ` (${year})` + : ` (${new Date().getFullYear()})` }`, value: totalCommits, id: "commits", diff --git a/src/cards/types.d.ts b/src/cards/types.d.ts index 502314c41fa92..4076c0527d224 100644 --- a/src/cards/types.d.ts +++ b/src/cards/types.d.ts @@ -19,6 +19,7 @@ export type StatCardOptions = CommonOptions & { card_width: number; hide_rank: boolean; include_all_commits: boolean; + year?: number; line_height: number | string; custom_title: string; disable_animations: boolean; diff --git a/src/fetchers/stats-fetcher.js b/src/fetchers/stats-fetcher.js index b9493adfdbb43..0d50dff534b2d 100644 --- a/src/fetchers/stats-fetcher.js +++ b/src/fetchers/stats-fetcher.js @@ -41,6 +41,7 @@ const fetcher = (variables, token) => { closedIssues: issues(states: CLOSED) { totalCount } + followers { totalCount } @@ -103,6 +104,7 @@ const totalCommitsFetcher = async (username) => { * @param {string} username * @param {boolean} count_private * @param {boolean} include_all_commits + * @param {number|undefined} year * @returns {Promise} */ async function fetchStats( @@ -110,6 +112,7 @@ async function fetchStats( count_private = false, include_all_commits = false, exclude_repo = [], + year, ) { if (!username) throw new MissingParamError(["username"]); @@ -123,7 +126,10 @@ async function fetchStats( rank: { level: "C", score: 0 }, }; - let res = await retryer(fetcher, { login: username }); + let res = await retryer(fetcher, { + login: username, + starttime: year ? `${year}-01-01T00:00:00Z` : undefined + }); if (res.data.errors) { logger.error(res.data.errors); From d2c4dfdb01959b74e87e349b535a1e838d695542 Mon Sep 17 00:00:00 2001 From: Touch Sungkawichai Date: Tue, 11 Oct 2022 02:54:15 +0900 Subject: [PATCH 02/22] edit fetcher --- src/fetchers/stats-fetcher.js | 5 ++--- vercel.json | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/fetchers/stats-fetcher.js b/src/fetchers/stats-fetcher.js index 0d50dff534b2d..d1c02adabbac2 100644 --- a/src/fetchers/stats-fetcher.js +++ b/src/fetchers/stats-fetcher.js @@ -21,11 +21,11 @@ const fetcher = (variables, token) => { return request( { query: ` - query userInfo($login: String!) { + query userInfo($login: String!, $starttime: DateTime!) { user(login: $login) { name login - contributionsCollection { + contributionsCollection(from: $starttime) { totalCommitContributions restrictedContributionsCount } @@ -41,7 +41,6 @@ const fetcher = (variables, token) => { closedIssues: issues(states: CLOSED) { totalCount } - followers { totalCount } diff --git a/vercel.json b/vercel.json index aee61ce054fd0..ddf82eb15666f 100644 --- a/vercel.json +++ b/vercel.json @@ -2,7 +2,7 @@ "functions": { "api/*.js": { "memory": 128, - "maxDuration": 30 + "maxDuration": 10 } }, "redirects": [ From 4b15721f691d097919f56494d988034532da0790 Mon Sep 17 00:00:00 2001 From: Touch Sungkawichai Date: Tue, 11 Oct 2022 03:22:51 +0900 Subject: [PATCH 03/22] add tests --- tests/fetchStats.test.js | 97 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/tests/fetchStats.test.js b/tests/fetchStats.test.js index f8eae98139442..3a629496d7429 100644 --- a/tests/fetchStats.test.js +++ b/tests/fetchStats.test.js @@ -31,6 +31,33 @@ const data = { }, }; +const data2 = { + data: { + user: { + name: "Anurag Hazra", + repositoriesContributedTo: { totalCount: 61 }, + contributionsCollection: { + totalCommitContributions: 1000, + restrictedContributionsCount: 50, + }, + pullRequests: { totalCount: 300 }, + openIssues: { totalCount: 100 }, + closedIssues: { totalCount: 100 }, + followers: { totalCount: 100 }, + repositories: { + totalCount: 5, + nodes: [ + { 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 } }, + ], + }, + }, + }, +}; + const error = { errors: [ { @@ -162,4 +189,74 @@ describe("Test fetchStats", () => { rank, }); }); + + it("should get present year commits when provide no year", async () => { + const data2003 = {...data, data: + {...data.data, user: + {...data.data.user, contributionsCollection: { + totalCommitContributions: 2003, + restrictedContributionsCount: 3, + }}}} + mock.onPost("https://api.github.com/graphql").reply((cfg) => { + if (cfg.data.includes("contributionsCollection(from: 2003-01-01T00:00:00Z)")) + return [200, data2003]; + return [200, data]; + }); + + let stats = await fetchStats("anuraghazra", true, false, []); + 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, + }); + }); + + it("should get commits of provided year", async () => { + const data2003 = {...data, data: + {...data.data, user: + {...data.data.user, contributionsCollection: { + totalCommitContributions: 2003, + restrictedContributionsCount: 3, + }}}} + mock.onPost("https://api.github.com/graphql").reply((cfg) => { + if (cfg.data.includes(`"starttime":"2003-01-01T00:00:00Z"`)) + return [200, data2003]; + return [200, data]; + }); + + let stats = await fetchStats("anuraghazra", true, false, [], 2003); + const rank = calculateRank({ + totalCommits: 2006, + totalRepos: 5, + followers: 100, + contributions: 61, + stargazers: 400, + prs: 300, + issues: 200, + }); + + expect(stats).toStrictEqual({ + contributedTo: 61, + name: "Anurag Hazra", + totalCommits: 2006, + totalIssues: 200, + totalPRs: 300, + totalStars: 400, + rank, + }); + }); }); From b74d5be855002fbf9348921bb17dfc69b216c00b Mon Sep 17 00:00:00 2001 From: Touch Sungkawichai Date: Tue, 11 Oct 2022 03:29:00 +0900 Subject: [PATCH 04/22] add documentation --- readme.md | 51 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/readme.md b/readme.md index 6f741b745b102..5190111591c70 100644 --- a/readme.md +++ b/readme.md @@ -79,18 +79,38 @@ Visit and make a small donation to hel # Features -- [GitHub Stats Card](#github-stats-card) -- [GitHub Extra Pins](#github-extra-pins) -- [Top Languages Card](#top-languages-card) -- [Wakatime Week Stats](#wakatime-week-stats) -- [Themes](#themes) -- [Customization](#customization) - - [Common Options](#common-options) - - [Stats Card Exclusive Options](#stats-card-exclusive-options) - - [Repo Card Exclusive Options](#repo-card-exclusive-options) - - [Language Card Exclusive Options](#language-card-exclusive-options) - - [Wakatime Card Exclusive Option](#wakatime-card-exclusive-options) -- [Deploy Yourself](#deploy-on-your-own-vercel-instance) +- [Features](#features) +- [GitHub Stats Card](#github-stats-card) + - [Hiding individual stats](#hiding-individual-stats) + - [Adding private contributions count to total commits count](#adding-private-contributions-count-to-total-commits-count) + - [Specifying Year](#specifying-year) + - [Showing icons](#showing-icons) + - [Themes](#themes) + - [All inbuilt themes](#all-inbuilt-themes) + - [Customization](#customization) + - [Common Options](#common-options) + - [Gradient in bg_color](#gradient-in-bg_color) + - [Stats Card Exclusive Options](#stats-card-exclusive-options) + - [Repo Card Exclusive Options](#repo-card-exclusive-options) + - [Language Card Exclusive Options](#language-card-exclusive-options) + - [Wakatime Card Exclusive Options](#wakatime-card-exclusive-options) +- [GitHub Extra Pins](#github-extra-pins) + - [Usage](#usage) + - [Demo](#demo) +- [Top Languages Card](#top-languages-card) + - [Usage](#usage-1) + - [Exclude individual repositories](#exclude-individual-repositories) + - [Hide individual languages](#hide-individual-languages) + - [Show more languages](#show-more-languages) + - [Compact Language Card Layout](#compact-language-card-layout) + - [Demo](#demo-1) +- [Wakatime Week Stats](#wakatime-week-stats) + - [Demo](#demo-2) + - [All Demos](#all-demos) + - [Quick Tip (Align The Repo Cards)](#quick-tip-align-the-repo-cards) + - [Deploy on your own Vercel instance](#deploy-on-your-own-vercel-instance) + - [Check Out Step By Step Video Tutorial By @codeSTACKr](#check-out-step-by-step-video-tutorial-by-codestackr) + - [:sparkling_heart: Support the project](#sparkling_heart-support-the-project) # GitHub Stats Card @@ -128,6 +148,13 @@ _Note: If you are deploying this project yourself, the private contributions wil ![Anurag's GitHub stats](https://github-readme-stats.vercel.app/api?username=anuraghazra&count_private=true) ``` +### Specifying Year + +You can specify a year and fetch only the commits that were made in that year by passing `&year=YYYY` to the parameter. + +```md +![Anurag's GitHub stats](https://github-readme-stats.vercel.app/api?username=anuraghazra&year=2020) +``` ### Showing icons To enable icons, you can pass `show_icons=true` in the query param, like so: From 6fa57592d8027382caf9d68bd6b53bf29ffb9283 Mon Sep 17 00:00:00 2001 From: my99N Date: Tue, 11 Oct 2022 10:27:52 +0900 Subject: [PATCH 05/22] Update vercel.json --- vercel.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vercel.json b/vercel.json index ddf82eb15666f..aee61ce054fd0 100644 --- a/vercel.json +++ b/vercel.json @@ -2,7 +2,7 @@ "functions": { "api/*.js": { "memory": 128, - "maxDuration": 10 + "maxDuration": 30 } }, "redirects": [ From 150e13a786bf9f661c3236ca2ec5eb19ca5d69f4 Mon Sep 17 00:00:00 2001 From: my99N Date: Tue, 11 Oct 2022 10:29:09 +0900 Subject: [PATCH 06/22] Update fetchStats.test.js --- tests/fetchStats.test.js | 27 --------------------------- 1 file changed, 27 deletions(-) diff --git a/tests/fetchStats.test.js b/tests/fetchStats.test.js index 38e33b0308d4e..c5b5e9237a69a 100644 --- a/tests/fetchStats.test.js +++ b/tests/fetchStats.test.js @@ -79,33 +79,6 @@ const repositoriesWithZeroStarsData = { }, }; -const data2 = { - data: { - user: { - name: "Anurag Hazra", - repositoriesContributedTo: { totalCount: 61 }, - contributionsCollection: { - totalCommitContributions: 1000, - restrictedContributionsCount: 50, - }, - pullRequests: { totalCount: 300 }, - openIssues: { totalCount: 100 }, - closedIssues: { totalCount: 100 }, - followers: { totalCount: 100 }, - repositories: { - totalCount: 5, - nodes: [ - { 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 } }, - ], - }, - }, - }, -}; - const error = { errors: [ { From 15f930324ad66fcaf3f293c8cf1b07544044f667 Mon Sep 17 00:00:00 2001 From: my99N Date: Thu, 13 Oct 2022 21:06:05 +0900 Subject: [PATCH 07/22] Update readme.md --- readme.md | 1 + 1 file changed, 1 insertion(+) diff --git a/readme.md b/readme.md index 3c9aad8a1e8c8..f199d9a2542b3 100644 --- a/readme.md +++ b/readme.md @@ -203,6 +203,7 @@ You can provide multiple comma-separated values in the bg_color option to render - `custom_title` - Sets a custom title for the card. Default: ` Github Stats`. - `text_bold` - Use bold text _(boolean)_. Default: `true`. - `disable_animations` - Disables all animations in the card _(boolean)_. Default: `false`. +- `year` - Count commit of the entire year _(YYYY)_. Default: ` (one year to date)` > **Note** > When hide_rank=`true`, the minimum card width is 270 px + the title length and padding. From afb417b6d48a1d4cc09afea7e594728b803a4d67 Mon Sep 17 00:00:00 2001 From: my99N Date: Thu, 13 Oct 2022 21:45:47 +0900 Subject: [PATCH 08/22] Update readme.md Co-authored-by: Rick Staa --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index f199d9a2542b3..e9dc327eeda3c 100644 --- a/readme.md +++ b/readme.md @@ -203,7 +203,7 @@ You can provide multiple comma-separated values in the bg_color option to render - `custom_title` - Sets a custom title for the card. Default: ` Github Stats`. - `text_bold` - Use bold text _(boolean)_. Default: `true`. - `disable_animations` - Disables all animations in the card _(boolean)_. Default: `false`. -- `year` - Count commit of the entire year _(YYYY)_. Default: ` (one year to date)` +- `year` - Count commits of the entire year _(YYYY)_. Default: ` (one year to date)`. > **Note** > When hide_rank=`true`, the minimum card width is 270 px + the title length and padding. From eb445fa860405054213b9101e54ec3af1f813cbe Mon Sep 17 00:00:00 2001 From: rickstaa Date: Sat, 15 Oct 2022 09:32:32 +0200 Subject: [PATCH 09/22] test: fix tests --- readme.md | 1 + src/fetchers/stats-fetcher.js | 14 ++++--- tests/fetchStats.test.js | 72 +++++++++++------------------------ 3 files changed, 32 insertions(+), 55 deletions(-) diff --git a/readme.md b/readme.md index e9dc327eeda3c..a696d9e2777fe 100644 --- a/readme.md +++ b/readme.md @@ -137,6 +137,7 @@ You can specify a year and fetch only the commits that were made in that year by ```md ![Anurag's GitHub stats](https://github-readme-stats.vercel.app/api?username=anuraghazra&year=2020) ``` + ### Showing icons To enable icons, you can pass `show_icons=true` in the query param, like so: diff --git a/src/fetchers/stats-fetcher.js b/src/fetchers/stats-fetcher.js index 805bb8dc06c0d..abef78151fa89 100644 --- a/src/fetchers/stats-fetcher.js +++ b/src/fetchers/stats-fetcher.js @@ -25,11 +25,15 @@ const fetcher = (variables, token) => { return request( { query: ` - query userInfo($login: String!, $starttime: DateTime!) { + query userInfo($login: String!${ + variables.starttime ? ", $starttime: DateTime!" : "" + }) { user(login: $login) { name login - contributionsCollection(from: $starttime) { + contributionsCollection${ + variables.starttime ? "(from: $starttime)" : "" + } { totalCommitContributions restrictedContributionsCount } @@ -208,9 +212,9 @@ async function fetchStats( rank: { level: "C", score: 0 }, }; - let res = await retryer(fetcher, { - login: username, - starttime: year ? `${year}-01-01T00:00:00Z` : undefined + let res = await retryer(fetcher, { + login: username, + starttime: year ? `${year}-01-01T00:00:00Z` : undefined, }); // Catch GraphQL errors. diff --git a/tests/fetchStats.test.js b/tests/fetchStats.test.js index c5b5e9237a69a..cf55d36914bca 100644 --- a/tests/fetchStats.test.js +++ b/tests/fetchStats.test.js @@ -24,6 +24,10 @@ const data = { }, }; +const dataYear = JSON.parse(JSON.stringify(data)); +dataYear.data.user.contributionsCollection.totalCommitContributions = 2003; +dataYear.data.user.contributionsCollection.restrictedContributionsCount = 3; + const firstRepositoriesData = { data: { user: { @@ -92,10 +96,23 @@ const error = { const mock = new MockAdapter(axios); +/** + * Mocks the GraphQL API based on certain conditions. + * + * @param {*} config Axios config object. + * @returns Axios response object. + */ +const mockData = (config) => { + // If year is 2003, return dataYear. + if (config.data.includes("contributionsCollection(from: $starttime)")) + return [200, dataYear]; + return [200, data]; +}; + beforeEach(() => { mock .onPost("https://api.github.com/graphql") - .replyOnce(200, data) + .replyOnce(mockData) .onPost("https://api.github.com/graphql") .replyOnce(200, firstRepositoriesData); // .onPost("https://api.github.com/graphql") // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130. @@ -254,61 +271,15 @@ describe("Test fetchStats", () => { }); }); - it("should get present year commits when provide no year", async () => { - const data2003 = {...data, data: - {...data.data, user: - {...data.data.user, contributionsCollection: { - totalCommitContributions: 2003, - restrictedContributionsCount: 3, - }}}} - mock.onPost("https://api.github.com/graphql").reply((cfg) => { - if (cfg.data.includes("contributionsCollection(from: 2003-01-01T00:00:00Z)")) - return [200, data2003]; - return [200, data]; - }); - - let stats = await fetchStats("anuraghazra", true, false, []); - 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, - }); - }); - it("should get commits of provided year", async () => { - const data2003 = {...data, data: - {...data.data, user: - {...data.data.user, contributionsCollection: { - totalCommitContributions: 2003, - restrictedContributionsCount: 3, - }}}} - mock.onPost("https://api.github.com/graphql").reply((cfg) => { - if (cfg.data.includes(`"starttime":"2003-01-01T00:00:00Z"`)) - return [200, data2003]; - return [200, data]; - }); - let stats = await fetchStats("anuraghazra", true, false, [], 2003); const rank = calculateRank({ totalCommits: 2006, totalRepos: 5, followers: 100, contributions: 61, - stargazers: 400, + // stargazers: 400, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130. + stargazers: 300, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130. prs: 300, issues: 200, }); @@ -319,7 +290,8 @@ describe("Test fetchStats", () => { totalCommits: 2006, totalIssues: 200, totalPRs: 300, - totalStars: 400, + // totalStars: 400, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130. + totalStars: 300, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130. rank, }); }); From af6553ecf550fcbd6259b05294381f6cf2e126a2 Mon Sep 17 00:00:00 2001 From: rickstaa Date: Sat, 15 Oct 2022 09:34:02 +0200 Subject: [PATCH 10/22] style: format code --- src/cards/stats-card.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cards/stats-card.js b/src/cards/stats-card.js index 2bec67360894a..9a3edb5b57c24 100644 --- a/src/cards/stats-card.js +++ b/src/cards/stats-card.js @@ -134,11 +134,11 @@ const renderStatsCard = (stats = {}, options = { hide: [] }) => { commits: { icon: icons.commits, label: `${i18n.t("statcard.commits")}${ - include_all_commits - ? "" + include_all_commits + ? "" : year - ? ` (${year})` - : ` (${new Date().getFullYear()})` + ? ` (${year})` + : ` (${new Date().getFullYear()})` }`, value: totalCommits, id: "commits", From de4cefa0ad6826f1a378104b908dadf6c2f218c6 Mon Sep 17 00:00:00 2001 From: rickstaa Date: Sun, 16 Oct 2022 11:06:29 +0200 Subject: [PATCH 11/22] 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. --- src/cards/stats-card.js | 11 ++++------- tests/renderStatsCard.test.js | 2 +- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/cards/stats-card.js b/src/cards/stats-card.js index 9a3edb5b57c24..4e2b61648aa76 100644 --- a/src/cards/stats-card.js +++ b/src/cards/stats-card.js @@ -134,11 +134,7 @@ const renderStatsCard = (stats = {}, options = { hide: [] }) => { commits: { icon: icons.commits, label: `${i18n.t("statcard.commits")}${ - include_all_commits - ? "" - : year - ? ` (${year})` - : ` (${new Date().getFullYear()})` + include_all_commits ? "" : year ? ` (${year})` : ` (last year)` }`, value: totalCommits, id: "commits", @@ -188,7 +184,8 @@ const renderStatsCard = (stats = {}, options = { hide: [] }) => { index, showIcons: show_icons, shiftValuePos: - (!include_all_commits ? 50 : 35) + (isLongLocale ? 50 : 0), + (include_all_commits ? 35 : year ? 50 : 80.22) + + (isLongLocale ? 50 : 0), bold: text_bold, }), ); @@ -296,7 +293,7 @@ const renderStatsCard = (stats = {}, options = { hide: [] }) => { .map((key) => { if (key === "commits") { return `${i18n.t("statcard.commits")} ${ - include_all_commits ? "" : `in ${new Date().getFullYear()}` + include_all_commits ? "" : year ? ` (${year})` : ` (last year)` } : ${totalStars}`; } return `${STATS[key].label}: ${STATS[key].value}`; diff --git a/tests/renderStatsCard.test.js b/tests/renderStatsCard.test.js index 69708137b334c..31268cac794a1 100644 --- a/tests/renderStatsCard.test.js +++ b/tests/renderStatsCard.test.js @@ -296,7 +296,7 @@ describe("Test renderStatsCard", () => { document.querySelector( 'g[transform="translate(0, 25)"]>.stagger>.stat.bold', ).textContent, - ).toMatchInlineSnapshot(`"累计提交数(commit) (2022):"`); + ).toMatchInlineSnapshot(`"累计提交数(commit) (last year):"`); expect( document.querySelector( 'g[transform="translate(0, 50)"]>.stagger>.stat.bold', From 573af5dffbe0929450e7c2666be258ac06599400 Mon Sep 17 00:00:00 2001 From: vzsky Date: Fri, 5 Jan 2024 00:28:25 +0900 Subject: [PATCH 12/22] resolved merge conflict --- api/index.js | 4 +- api/index_BACKUP_82111.js | 146 ---------------------------------- api/index_BASE_82111.js | 95 ---------------------- api/index_LOCAL_82111.js | 98 ----------------------- api/index_REMOTE_82111.js | 140 -------------------------------- src/fetchers/stats-fetcher.js | 17 ++-- tests/fetchStats.test.js | 59 ++++++++++---- 7 files changed, 55 insertions(+), 504 deletions(-) delete mode 100644 api/index_BACKUP_82111.js delete mode 100644 api/index_BASE_82111.js delete mode 100644 api/index_LOCAL_82111.js delete mode 100644 api/index_REMOTE_82111.js diff --git a/api/index.js b/api/index.js index 8f61e30955bbc..263497dfdb158 100644 --- a/api/index.js +++ b/api/index.js @@ -72,11 +72,11 @@ export default async (req, res) => { username, parseBoolean(include_all_commits), parseArray(exclude_repo), - parseInt(year), showStats.includes("prs_merged") || showStats.includes("prs_merged_percentage"), showStats.includes("discussions_started"), showStats.includes("discussions_answered"), + parseInt(year, 10), ); let cacheSeconds = clampValue( @@ -104,7 +104,7 @@ export default async (req, res) => { card_width: parseInt(card_width, 10), hide_rank: parseBoolean(hide_rank), include_all_commits: parseBoolean(include_all_commits), - year: parseInt(year), + year: parseInt(year, 10), line_height, title_color, ring_color, diff --git a/api/index_BACKUP_82111.js b/api/index_BACKUP_82111.js deleted file mode 100644 index 9c4d94afad681..0000000000000 --- a/api/index_BACKUP_82111.js +++ /dev/null @@ -1,146 +0,0 @@ -import { renderStatsCard } from "../src/cards/stats-card.js"; -import { blacklist } from "../src/common/blacklist.js"; -import { - clampValue, - CONSTANTS, - parseArray, - parseBoolean, - renderError, -} from "../src/common/utils.js"; -import { fetchStats } from "../src/fetchers/stats-fetcher.js"; -import { isLocaleAvailable } from "../src/translations.js"; - -export default async (req, res) => { - const { - username, - hide, - hide_title, - hide_border, - card_width, - hide_rank, - show_icons, - include_all_commits, - year, - line_height, - title_color, - ring_color, - icon_color, - text_color, - text_bold, - bg_color, - theme, - cache_seconds, - exclude_repo, - custom_title, - locale, - disable_animations, - border_radius, - number_format, - border_color, - rank_icon, - show, - } = req.query; - res.setHeader("Content-Type", "image/svg+xml"); - - if (blacklist.includes(username)) { - return res.send( - renderError("Something went wrong", "This username is blacklisted", { - title_color, - text_color, - bg_color, - border_color, - theme, - }), - ); - } - - if (locale && !isLocaleAvailable(locale)) { - return res.send( - renderError("Something went wrong", "Language not found", { - title_color, - text_color, - bg_color, - border_color, - theme, - }), - ); - } - - try { - const showStats = parseArray(show); - const stats = await fetchStats( - username, - parseBoolean(include_all_commits), - parseArray(exclude_repo), -<<<<<<< HEAD - parseInt(year), -======= - showStats.includes("prs_merged") || - showStats.includes("prs_merged_percentage"), - showStats.includes("discussions_started"), - showStats.includes("discussions_answered"), ->>>>>>> 7d4c49b7f74118efbb2ebb239887a195682d4a75 - ); - - let cacheSeconds = clampValue( - parseInt(cache_seconds || CONSTANTS.CARD_CACHE_SECONDS, 10), - CONSTANTS.SIX_HOURS, - CONSTANTS.ONE_DAY, - ); - cacheSeconds = process.env.CACHE_SECONDS - ? parseInt(process.env.CACHE_SECONDS, 10) || cacheSeconds - : cacheSeconds; - - res.setHeader( - "Cache-Control", - `max-age=${ - cacheSeconds / 2 - }, s-maxage=${cacheSeconds}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`, - ); - - return res.send( - renderStatsCard(stats, { - hide: parseArray(hide), - show_icons: parseBoolean(show_icons), - hide_title: parseBoolean(hide_title), - hide_border: parseBoolean(hide_border), - card_width: parseInt(card_width, 10), - hide_rank: parseBoolean(hide_rank), - include_all_commits: parseBoolean(include_all_commits), - year: parseInt(year), - line_height, - title_color, - ring_color, - icon_color, - text_color, - text_bold: parseBoolean(text_bold), - bg_color, - theme, - custom_title, - border_radius, - border_color, - number_format, - locale: locale ? locale.toLowerCase() : null, - disable_animations: parseBoolean(disable_animations), - rank_icon, - show: showStats, - }), - ); - } catch (err) { - res.setHeader( - "Cache-Control", - `max-age=${CONSTANTS.ERROR_CACHE_SECONDS / 2}, s-maxage=${ - CONSTANTS.ERROR_CACHE_SECONDS - }, stale-while-revalidate=${CONSTANTS.ONE_DAY}`, - ); // Use lower cache period for errors. - return res.send( - renderError(err.message, err.secondaryMessage, { - title_color, - text_color, - bg_color, - border_color, - theme, - }), - ); - } -}; diff --git a/api/index_BASE_82111.js b/api/index_BASE_82111.js deleted file mode 100644 index 3b5e949d71d36..0000000000000 --- a/api/index_BASE_82111.js +++ /dev/null @@ -1,95 +0,0 @@ -import * as dotenv from "dotenv"; -import { renderStatsCard } from "../src/cards/stats-card.js"; -import { blacklist } from "../src/common/blacklist.js"; -import { - clampValue, - CONSTANTS, - parseArray, - parseBoolean, - renderError, -} from "../src/common/utils.js"; -import { fetchStats } from "../src/fetchers/stats-fetcher.js"; -import { isLocaleAvailable } from "../src/translations.js"; - -dotenv.config(); - -export default async (req, res) => { - const { - username, - hide, - hide_title, - hide_border, - card_width, - hide_rank, - show_icons, - count_private, - include_all_commits, - line_height, - title_color, - icon_color, - text_color, - text_bold, - bg_color, - theme, - cache_seconds, - exclude_repo, - custom_title, - locale, - disable_animations, - border_radius, - border_color, - } = req.query; - res.setHeader("Content-Type", "image/svg+xml"); - - if (blacklist.includes(username)) { - return res.send(renderError("Something went wrong")); - } - - if (locale && !isLocaleAvailable(locale)) { - return res.send(renderError("Something went wrong", "Language not found")); - } - - try { - const stats = await fetchStats( - username, - parseBoolean(count_private), - parseBoolean(include_all_commits), - parseArray(exclude_repo), - ); - - const cacheSeconds = clampValue( - parseInt(cache_seconds || CONSTANTS.FOUR_HOURS, 10), - CONSTANTS.FOUR_HOURS, - CONSTANTS.ONE_DAY, - ); - - res.setHeader("Cache-Control", `public, max-age=${cacheSeconds}`); - - return res.send( - renderStatsCard(stats, { - hide: parseArray(hide), - show_icons: parseBoolean(show_icons), - hide_title: parseBoolean(hide_title), - hide_border: parseBoolean(hide_border), - card_width: parseInt(card_width, 10), - hide_rank: parseBoolean(hide_rank), - include_all_commits: parseBoolean(include_all_commits), - line_height, - title_color, - icon_color, - text_color, - text_bold: parseBoolean(text_bold), - bg_color, - theme, - custom_title, - border_radius, - border_color, - locale: locale ? locale.toLowerCase() : null, - disable_animations: parseBoolean(disable_animations), - }), - ); - } catch (err) { - res.setHeader("Cache-Control", `no-store`); // Don't cache error responses. - return res.send(renderError(err.message, err.secondaryMessage)); - } -}; diff --git a/api/index_LOCAL_82111.js b/api/index_LOCAL_82111.js deleted file mode 100644 index 6845ebce79622..0000000000000 --- a/api/index_LOCAL_82111.js +++ /dev/null @@ -1,98 +0,0 @@ -import * as dotenv from "dotenv"; -import { renderStatsCard } from "../src/cards/stats-card.js"; -import { blacklist } from "../src/common/blacklist.js"; -import { - clampValue, - CONSTANTS, - parseArray, - parseBoolean, - renderError, -} from "../src/common/utils.js"; -import { fetchStats } from "../src/fetchers/stats-fetcher.js"; -import { isLocaleAvailable } from "../src/translations.js"; - -dotenv.config(); - -export default async (req, res) => { - const { - username, - hide, - hide_title, - hide_border, - card_width, - hide_rank, - show_icons, - count_private, - include_all_commits, - year, - line_height, - title_color, - icon_color, - text_color, - text_bold, - bg_color, - theme, - cache_seconds, - exclude_repo, - custom_title, - locale, - disable_animations, - border_radius, - border_color, - } = req.query; - res.setHeader("Content-Type", "image/svg+xml"); - - if (blacklist.includes(username)) { - return res.send(renderError("Something went wrong")); - } - - if (locale && !isLocaleAvailable(locale)) { - return res.send(renderError("Something went wrong", "Language not found")); - } - - try { - const stats = await fetchStats( - username, - parseBoolean(count_private), - parseBoolean(include_all_commits), - parseArray(exclude_repo), - parseInt(year), - ); - - const cacheSeconds = clampValue( - parseInt(cache_seconds || CONSTANTS.FOUR_HOURS, 10), - CONSTANTS.FOUR_HOURS, - CONSTANTS.ONE_DAY, - ); - - res.setHeader("Cache-Control", `public, max-age=${cacheSeconds}`); - - return res.send( - renderStatsCard(stats, { - hide: parseArray(hide), - show_icons: parseBoolean(show_icons), - hide_title: parseBoolean(hide_title), - hide_border: parseBoolean(hide_border), - card_width: parseInt(card_width, 10), - hide_rank: parseBoolean(hide_rank), - include_all_commits: parseBoolean(include_all_commits), - year: parseInt(year), - line_height, - title_color, - icon_color, - text_color, - text_bold: parseBoolean(text_bold), - bg_color, - theme, - custom_title, - border_radius, - border_color, - locale: locale ? locale.toLowerCase() : null, - disable_animations: parseBoolean(disable_animations), - }), - ); - } catch (err) { - res.setHeader("Cache-Control", `no-store`); // Don't cache error responses. - return res.send(renderError(err.message, err.secondaryMessage)); - } -}; diff --git a/api/index_REMOTE_82111.js b/api/index_REMOTE_82111.js deleted file mode 100644 index 2029367ca3eb9..0000000000000 --- a/api/index_REMOTE_82111.js +++ /dev/null @@ -1,140 +0,0 @@ -import { renderStatsCard } from "../src/cards/stats-card.js"; -import { blacklist } from "../src/common/blacklist.js"; -import { - clampValue, - CONSTANTS, - parseArray, - parseBoolean, - renderError, -} from "../src/common/utils.js"; -import { fetchStats } from "../src/fetchers/stats-fetcher.js"; -import { isLocaleAvailable } from "../src/translations.js"; - -export default async (req, res) => { - const { - username, - hide, - hide_title, - hide_border, - card_width, - hide_rank, - show_icons, - include_all_commits, - line_height, - title_color, - ring_color, - icon_color, - text_color, - text_bold, - bg_color, - theme, - cache_seconds, - exclude_repo, - custom_title, - locale, - disable_animations, - border_radius, - number_format, - border_color, - rank_icon, - show, - } = req.query; - res.setHeader("Content-Type", "image/svg+xml"); - - if (blacklist.includes(username)) { - return res.send( - renderError("Something went wrong", "This username is blacklisted", { - title_color, - text_color, - bg_color, - border_color, - theme, - }), - ); - } - - if (locale && !isLocaleAvailable(locale)) { - return res.send( - renderError("Something went wrong", "Language not found", { - title_color, - text_color, - bg_color, - border_color, - theme, - }), - ); - } - - try { - const showStats = parseArray(show); - const stats = await fetchStats( - username, - parseBoolean(include_all_commits), - parseArray(exclude_repo), - showStats.includes("prs_merged") || - showStats.includes("prs_merged_percentage"), - showStats.includes("discussions_started"), - showStats.includes("discussions_answered"), - ); - - let cacheSeconds = clampValue( - parseInt(cache_seconds || CONSTANTS.CARD_CACHE_SECONDS, 10), - CONSTANTS.SIX_HOURS, - CONSTANTS.ONE_DAY, - ); - cacheSeconds = process.env.CACHE_SECONDS - ? parseInt(process.env.CACHE_SECONDS, 10) || cacheSeconds - : cacheSeconds; - - res.setHeader( - "Cache-Control", - `max-age=${ - cacheSeconds / 2 - }, s-maxage=${cacheSeconds}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`, - ); - - return res.send( - renderStatsCard(stats, { - hide: parseArray(hide), - show_icons: parseBoolean(show_icons), - hide_title: parseBoolean(hide_title), - hide_border: parseBoolean(hide_border), - card_width: parseInt(card_width, 10), - hide_rank: parseBoolean(hide_rank), - include_all_commits: parseBoolean(include_all_commits), - line_height, - title_color, - ring_color, - icon_color, - text_color, - text_bold: parseBoolean(text_bold), - bg_color, - theme, - custom_title, - border_radius, - border_color, - number_format, - locale: locale ? locale.toLowerCase() : null, - disable_animations: parseBoolean(disable_animations), - rank_icon, - show: showStats, - }), - ); - } catch (err) { - res.setHeader( - "Cache-Control", - `max-age=${CONSTANTS.ERROR_CACHE_SECONDS / 2}, s-maxage=${ - CONSTANTS.ERROR_CACHE_SECONDS - }, stale-while-revalidate=${CONSTANTS.ONE_DAY}`, - ); // Use lower cache period for errors. - return res.send( - renderError(err.message, err.secondaryMessage, { - title_color, - text_color, - bg_color, - border_color, - theme, - }), - ); - } -}; diff --git a/src/fetchers/stats-fetcher.js b/src/fetchers/stats-fetcher.js index 0038b2e9b81f1..78c83458ec001 100644 --- a/src/fetchers/stats-fetcher.js +++ b/src/fetchers/stats-fetcher.js @@ -40,11 +40,11 @@ const GRAPHQL_REPOS_QUERY = ` `; const GRAPHQL_STATS_QUERY = ` - query userInfo($login: String!, $after: String, $includeMergedPullRequests: Boolean!, $includeDiscussions: Boolean!, $includeDiscussionsAnswers: Boolean!, $starttime: DateTime) { + query userInfo($login: String!, $after: String, $includeMergedPullRequests: Boolean!, $includeDiscussions: Boolean!, $includeDiscussionsAnswers: Boolean!, $startTime: DateTime) { user(login: $login) { name login - contributionsCollection (from:$starttime) { + contributionsCollection (from: $startTime) { totalCommitContributions, totalPullRequestReviewContributions } @@ -110,7 +110,7 @@ const fetcher = (variables, token) => { * @param {boolean} variables.includeMergedPullRequests Include merged pull requests. * @param {boolean} variables.includeDiscussions Include discussions. * @param {boolean} variables.includeDiscussionsAnswers Include discussions answers. - * @param {string|undefined} variables.starttime Time to start the count of total commits. + * @param {string|undefined} variables.startTime Time to start the count of total commits. * @returns {Promise} Axios response. * * @description This function supports multi-page fetching if the 'FETCH_MULTI_PAGE_STARS' environment variable is set to true. @@ -120,7 +120,7 @@ const statsFetcher = async ({ includeMergedPullRequests, includeDiscussions, includeDiscussionsAnswers, - starttime, + startTime, }) => { let stats; let hasNextPage = true; @@ -133,11 +133,10 @@ const statsFetcher = async ({ includeMergedPullRequests, includeDiscussions, includeDiscussionsAnswers, - starttime, + startTime, }; let res = await retryer(fetcher, variables); - if (res.data.errors) { return res; } @@ -200,6 +199,8 @@ const totalCommitsFetcher = async (username) => { throw new Error(err); } + console.log(res); + const totalCount = res.data.total_count; if (!totalCount || isNaN(totalCount)) { throw new CustomError( @@ -230,10 +231,10 @@ const fetchStats = async ( username, include_all_commits = false, exclude_repo = [], - year, include_merged_pull_requests = false, include_discussions = false, include_discussions_answers = false, + year = undefined, ) => { if (!username) { throw new MissingParamError(["username"]); @@ -256,10 +257,10 @@ const fetchStats = async ( let res = await statsFetcher({ username, - starttime: year ? `${year}-01-01T00:00:00Z` : undefined, includeMergedPullRequests: include_merged_pull_requests, includeDiscussions: include_discussions, includeDiscussionsAnswers: include_discussions_answers, + startTime: year ? `${year}-01-01T00:00:00Z` : undefined, }); // Catch GraphQL errors. diff --git a/tests/fetchStats.test.js b/tests/fetchStats.test.js index bd5d7cc596408..659ebdda9355c 100644 --- a/tests/fetchStats.test.js +++ b/tests/fetchStats.test.js @@ -24,14 +24,22 @@ const data_stats = { repositoryDiscussionComments: { totalCount: 40 }, repositories: { totalCount: 5, + nodes: [ + { name: "test-repo-1", stargazers: { totalCount: 100 } }, + { name: "test-repo-2", stargazers: { totalCount: 100 } }, + { name: "test-repo-3", stargazers: { totalCount: 100 } }, + ], + pageInfo: { + hasNextPage: true, + endCursor: "cursor", + }, }, }, }, }; -const data_for_year_test = JSON.parse(JSON.stringify(data_stats)); -data_for_year_test.data.user.contributionsCollection.totalCommitContributions = 2003; -data_for_year_test.data.user.contributionsCollection.restrictedContributionsCount = 3; +const data_year2003 = JSON.parse(JSON.stringify(data_stats)); +data_year2003.data.user.contributionsCollection.totalCommitContributions = 428; const data_repo = { data: { @@ -93,10 +101,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) => { - console.log(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("contributionsCollection") ? data_stats : data_repo, ]; }); }); @@ -414,26 +430,39 @@ describe("Test fetchStats", () => { }); it("should get commits of provided year", async () => { - let stats = await fetchStats("anuraghazra", true, false, [], 2003); + let stats = await fetchStats( + "anuraghazra", + false, + [], + false, + false, + false, + 2003, + ); + const rank = calculateRank({ - totalCommits: 2006, - totalRepos: 5, - followers: 100, - contributions: 61, - // stargazers: 400, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130. - stargazers: 300, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130. + 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: 2006, + totalCommits: 428, totalIssues: 200, totalPRs: 300, - // totalStars: 400, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130. - totalStars: 300, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130. + totalPRsMerged: 0, + mergedPRsPercentage: 0, + totalReviews: 50, + totalStars: 300, + totalDiscussionsStarted: 0, + totalDiscussionsAnswered: 0, rank, }); }); From 5b65614650d22b4764fdbcca6c83f9964047117b Mon Sep 17 00:00:00 2001 From: my99N Date: Tue, 16 Jan 2024 01:36:51 +0900 Subject: [PATCH 13/22] Update stats-fetcher.js From 03366e50b6f5f523332d1eb99bc6afa96be1326c Mon Sep 17 00:00:00 2001 From: vzsky Date: Tue, 16 Jan 2024 01:39:34 +0900 Subject: [PATCH 14/22] resolve comments --- src/fetchers/stats-fetcher.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/fetchers/stats-fetcher.js b/src/fetchers/stats-fetcher.js index 78c83458ec001..f28dbadf5bf99 100644 --- a/src/fetchers/stats-fetcher.js +++ b/src/fetchers/stats-fetcher.js @@ -88,7 +88,6 @@ const GRAPHQL_STATS_QUERY = ` * @param {string} token GitHub token. * @returns {Promise} Axios response. */ - const fetcher = (variables, token) => { const query = variables.after ? GRAPHQL_REPOS_QUERY : GRAPHQL_STATS_QUERY; return request( @@ -199,8 +198,6 @@ const totalCommitsFetcher = async (username) => { throw new Error(err); } - console.log(res); - const totalCount = res.data.total_count; if (!totalCount || isNaN(totalCount)) { throw new CustomError( From b24733a111ea4e0990f892f60bdab50f8aa4ace6 Mon Sep 17 00:00:00 2001 From: my99N Date: Tue, 23 Jan 2024 19:58:08 +0900 Subject: [PATCH 15/22] Update src/cards/types.d.ts Co-authored-by: Alexandr Garbuzov --- src/cards/types.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cards/types.d.ts b/src/cards/types.d.ts index aa89d43bca4de..5b773baff5b22 100644 --- a/src/cards/types.d.ts +++ b/src/cards/types.d.ts @@ -20,7 +20,7 @@ export type StatCardOptions = CommonOptions & { card_width: number; hide_rank: boolean; include_all_commits: boolean; - year?: number; + year: number; line_height: number | string; custom_title: string; disable_animations: boolean; From 4adc788434c57cb7d46fdf84c42f7392c9515a3d Mon Sep 17 00:00:00 2001 From: my99N Date: Tue, 23 Jan 2024 19:58:30 +0900 Subject: [PATCH 16/22] Update src/fetchers/stats-fetcher.js Co-authored-by: Alexandr Garbuzov --- src/fetchers/stats-fetcher.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/fetchers/stats-fetcher.js b/src/fetchers/stats-fetcher.js index f28dbadf5bf99..620b33d732857 100644 --- a/src/fetchers/stats-fetcher.js +++ b/src/fetchers/stats-fetcher.js @@ -135,7 +135,6 @@ const statsFetcher = async ({ startTime, }; let res = await retryer(fetcher, variables); - if (res.data.errors) { return res; } From e729141553e0ce4d71883cfdc39b0f0e6030e41d Mon Sep 17 00:00:00 2001 From: vzsky Date: Tue, 23 Jan 2024 20:21:32 +0900 Subject: [PATCH 17/22] graphQL year only to commit --- src/fetchers/stats-fetcher.js | 9 +++++---- tests/api.test.js | 4 +++- tests/bench/api.bench.js | 4 +++- tests/fetchStats.test.js | 8 +++++--- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/fetchers/stats-fetcher.js b/src/fetchers/stats-fetcher.js index 620b33d732857..964c81cd58cb9 100644 --- a/src/fetchers/stats-fetcher.js +++ b/src/fetchers/stats-fetcher.js @@ -44,8 +44,10 @@ const GRAPHQL_STATS_QUERY = ` user(login: $login) { name login - contributionsCollection (from: $startTime) { + commits: contributionsCollection (from: $startTime) { totalCommitContributions, + } + reviews: contributionsCollection { totalPullRequestReviewContributions } repositoriesContributedTo(first: 1, contributionTypes: [COMMIT, ISSUE, PULL_REQUEST, REPOSITORY]) { @@ -288,7 +290,7 @@ const fetchStats = async ( if (include_all_commits) { stats.totalCommits = await totalCommitsFetcher(username); } else { - stats.totalCommits = user.contributionsCollection.totalCommitContributions; + stats.totalCommits = user.commits.totalCommitContributions; } stats.totalPRs = user.pullRequests.totalCount; @@ -297,8 +299,7 @@ const fetchStats = async ( stats.mergedPRsPercentage = (user.mergedPullRequests.totalCount / user.pullRequests.totalCount) * 100; } - stats.totalReviews = - user.contributionsCollection.totalPullRequestReviewContributions; + stats.totalReviews = user.reviews.totalPullRequestReviewContributions; stats.totalIssues = user.openIssues.totalCount + user.closedIssues.totalCount; if (include_discussions) { stats.totalDiscussionsStarted = user.repositoryDiscussions.totalCount; diff --git a/tests/api.test.js b/tests/api.test.js index eee9a1a0a61af..a0eb1be5f37c7 100644 --- a/tests/api.test.js +++ b/tests/api.test.js @@ -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 }, diff --git a/tests/bench/api.bench.js b/tests/bench/api.bench.js index 4796b64306e24..f5f192d97e131 100644 --- a/tests/bench/api.bench.js +++ b/tests/bench/api.bench.js @@ -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 }, diff --git a/tests/fetchStats.test.js b/tests/fetchStats.test.js index 659ebdda9355c..fb94c285f59ec 100644 --- a/tests/fetchStats.test.js +++ b/tests/fetchStats.test.js @@ -11,8 +11,10 @@ const data_stats = { user: { name: "Anurag Hazra", repositoriesContributedTo: { totalCount: 61 }, - contributionsCollection: { + commits: { totalCommitContributions: 100, + }, + reviews: { totalPullRequestReviewContributions: 50, }, pullRequests: { totalCount: 300 }, @@ -39,7 +41,7 @@ const data_stats = { }; const data_year2003 = JSON.parse(JSON.stringify(data_stats)); -data_year2003.data.user.contributionsCollection.totalCommitContributions = 428; +data_year2003.data.user.commits.totalCommitContributions = 428; const data_repo = { data: { @@ -112,7 +114,7 @@ beforeEach(() => { } return [ 200, - req.query.includes("contributionsCollection") ? data_stats : data_repo, + req.query.includes("totalCommitContributions") ? data_stats : data_repo, ]; }); }); From 7c30597f1ab13a46851a507b05c54962ab265a77 Mon Sep 17 00:00:00 2001 From: vzsky Date: Tue, 23 Jan 2024 20:36:17 +0900 Subject: [PATCH 18/22] change argument name to commits_year --- api/index.js | 6 +++--- readme.md | 2 +- src/cards/stats-card.js | 18 +++++++++++------- src/cards/types.d.ts | 2 +- src/fetchers/stats-fetcher.js | 6 +++--- tests/fetchStats.test.js | 7 ------- tests/renderStatsCard.test.js | 4 +--- 7 files changed, 20 insertions(+), 25 deletions(-) diff --git a/api/index.js b/api/index.js index 263497dfdb158..61910c6d81627 100644 --- a/api/index.js +++ b/api/index.js @@ -20,7 +20,7 @@ export default async (req, res) => { hide_rank, show_icons, include_all_commits, - year, + commits_year, line_height, title_color, ring_color, @@ -76,7 +76,7 @@ export default async (req, res) => { showStats.includes("prs_merged_percentage"), showStats.includes("discussions_started"), showStats.includes("discussions_answered"), - parseInt(year, 10), + parseInt(commits_year, 10), ); let cacheSeconds = clampValue( @@ -104,7 +104,7 @@ export default async (req, res) => { card_width: parseInt(card_width, 10), hide_rank: parseBoolean(hide_rank), include_all_commits: parseBoolean(include_all_commits), - year: parseInt(year, 10), + commits_year: parseInt(commits_year, 10), line_height, title_color, ring_color, diff --git a/readme.md b/readme.md index 9c77490b9fac6..fc5c29c3e6b8d 100644 --- a/readme.md +++ b/readme.md @@ -391,7 +391,7 @@ If we don't support your language, please consider contributing! You can find mo | `ring_color` | Color of the rank circle. | string (hex color) | `2f80ed` | | `number_format` | Switches between two available formats for displaying the card values `short` (i.e. `6.6k`) and `long` (i.e. `6626`). | enum | `short` | | `show` | Shows [additional items](#showing-additional-individual-stats) on stats card (i.e. `reviews`, `discussions_started`, `discussions_answered`, `prs_merged` or `prs_merged_percentage`). | string (comma-separated values) | `null` | -| `year` | Count commits of the entire year | interger _(YYYY)_ | ` (one year to date)`. +| `commits_year` | Count commits of the entire year | integer _(YYYY)_ | ` (one year to date)`. > [!NOTE]\ > When hide\_rank=`true`, the minimum card width is 270 px + the title length and padding. diff --git a/src/cards/stats-card.js b/src/cards/stats-card.js index 5cc5dd706a1a4..860f3566d534c 100644 --- a/src/cards/stats-card.js +++ b/src/cards/stats-card.js @@ -222,7 +222,7 @@ const renderStatsCard = (stats, options = {}) => { card_width, hide_rank = false, include_all_commits = false, - year, + commits_year, line_height = 25, title_color, ring_color, @@ -275,11 +275,11 @@ const renderStatsCard = (stats, options = {}) => { STATS.commits = { icon: icons.commits, label: `${i18n.t("statcard.commits")}${ - include_all_commits - ? "" - : year - ? ` (${year})` - : ` (${new Date().getFullYear()})` + include_all_commits + ? "" + : commits_year + ? ` (${commits_year})` + : ` (last year)` }`, value: totalCommits, id: "commits", @@ -522,7 +522,11 @@ const renderStatsCard = (stats, options = {}) => { .map((key) => { if (key === "commits") { return `${i18n.t("statcard.commits")} ${ - include_all_commits ? "" : year ? ` (${year})` : ` (last year)` + include_all_commits + ? "" + : commits_year + ? ` (${commits_year})` + : ` (last year)` } : ${totalStars}`; } return `${STATS[key].label}: ${STATS[key].value}`; diff --git a/src/cards/types.d.ts b/src/cards/types.d.ts index 5b773baff5b22..dbd9f0ae5ff67 100644 --- a/src/cards/types.d.ts +++ b/src/cards/types.d.ts @@ -20,7 +20,7 @@ export type StatCardOptions = CommonOptions & { card_width: number; hide_rank: boolean; include_all_commits: boolean; - year: number; + commits_year: number; line_height: number | string; custom_title: string; disable_animations: boolean; diff --git a/src/fetchers/stats-fetcher.js b/src/fetchers/stats-fetcher.js index 964c81cd58cb9..f4d94480e93b0 100644 --- a/src/fetchers/stats-fetcher.js +++ b/src/fetchers/stats-fetcher.js @@ -222,7 +222,7 @@ const totalCommitsFetcher = async (username) => { * @param {boolean} include_merged_pull_requests Include merged pull requests. * @param {boolean} include_discussions Include discussions. * @param {boolean} include_discussions_answers Include discussions answers. - * @param {number|undefined} year Year to count total commits + * @param {number|undefined} commits_year Year to count total commits * @returns {Promise} Stats data. */ const fetchStats = async ( @@ -232,7 +232,7 @@ const fetchStats = async ( include_merged_pull_requests = false, include_discussions = false, include_discussions_answers = false, - year = undefined, + commits_year, ) => { if (!username) { throw new MissingParamError(["username"]); @@ -258,7 +258,7 @@ const fetchStats = async ( includeMergedPullRequests: include_merged_pull_requests, includeDiscussions: include_discussions, includeDiscussionsAnswers: include_discussions_answers, - startTime: year ? `${year}-01-01T00:00:00Z` : undefined, + startTime: commits_year ? `${commits_year}-01-01T00:00:00Z` : undefined, }); // Catch GraphQL errors. diff --git a/tests/fetchStats.test.js b/tests/fetchStats.test.js index fb94c285f59ec..663ee9f17e6e1 100644 --- a/tests/fetchStats.test.js +++ b/tests/fetchStats.test.js @@ -93,13 +93,6 @@ const error = { const mock = new MockAdapter(axios); -/** - * Mocks the GraphQL API based on certain conditions. - * - * @param {*} config Axios config object. - * @returns Axios response object. - */ - 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) => { diff --git a/tests/renderStatsCard.test.js b/tests/renderStatsCard.test.js index 973ee0a5a5db6..b83286a4a029d 100644 --- a/tests/renderStatsCard.test.js +++ b/tests/renderStatsCard.test.js @@ -387,9 +387,7 @@ describe("Test renderStatsCard", () => { document.querySelector( 'g[transform="translate(0, 25)"]>.stagger>.stat.bold', ).textContent, - ).toMatchInlineSnapshot( - `"累计提交数(commit) (${new Date().getFullYear()}):"`, - ); + ).toMatchInlineSnapshot(`"累计提交数(commit) (last year):"`); expect( document.querySelector( 'g[transform="translate(0, 50)"]>.stagger>.stat.bold', From 94d961262906bb613b1fd2a87f25d1240ef65370 Mon Sep 17 00:00:00 2001 From: my99N Date: Thu, 25 Jan 2024 23:11:31 +0900 Subject: [PATCH 19/22] Update readme.md Co-authored-by: Alexandr Garbuzov --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index fc5c29c3e6b8d..72808a0b9c9ae 100644 --- a/readme.md +++ b/readme.md @@ -169,7 +169,7 @@ You can pass a query parameter `&show=` to show any specific additional stats wi ### Specifying Year -You can specify a year and fetch only the commits that were made in that year by passing `&year=YYYY` to the parameter. +You can specify a year and fetch only the commits that were made in that year by passing `&commits_year=YYYY` to the parameter. ```md ![Anurag's GitHub stats](https://github-readme-stats.vercel.app/api?username=anuraghazra&year=2020) From e53a04e4cbb36f26bbfe42a0eec1dfa776f0aa1c Mon Sep 17 00:00:00 2001 From: my99N Date: Thu, 25 Jan 2024 23:12:03 +0900 Subject: [PATCH 20/22] Update readme.md Co-authored-by: Alexandr Garbuzov --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 72808a0b9c9ae..ff85fb31f571c 100644 --- a/readme.md +++ b/readme.md @@ -167,7 +167,7 @@ You can pass a query parameter `&show=` to show any specific additional stats wi ![Anurag's GitHub stats](https://github-readme-stats.vercel.app/api?username=anuraghazra&show=reviews,discussions_started,discussions_answered,prs_merged,prs_merged_percentage) ``` -### Specifying Year +### Showing commits count for specified year You can specify a year and fetch only the commits that were made in that year by passing `&commits_year=YYYY` to the parameter. From 4c3629b1c832f85bafc05607c87f8c03f0c6b162 Mon Sep 17 00:00:00 2001 From: my99N Date: Thu, 25 Jan 2024 23:12:30 +0900 Subject: [PATCH 21/22] Update readme.md Co-authored-by: Alexandr Garbuzov --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index ff85fb31f571c..17266e682019b 100644 --- a/readme.md +++ b/readme.md @@ -172,7 +172,7 @@ You can pass a query parameter `&show=` to show any specific additional stats wi You can specify a year and fetch only the commits that were made in that year by passing `&commits_year=YYYY` to the parameter. ```md -![Anurag's GitHub stats](https://github-readme-stats.vercel.app/api?username=anuraghazra&year=2020) +![Anurag's GitHub stats](https://github-readme-stats.vercel.app/api?username=anuraghazra&commits_year=2020) ``` ### Showing icons From b60aa8fdcf727686c19341ddae9ad3e480935ded Mon Sep 17 00:00:00 2001 From: vzsky Date: Thu, 25 Jan 2024 23:23:23 +0900 Subject: [PATCH 22/22] wrap label into function --- src/cards/stats-card.js | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/src/cards/stats-card.js b/src/cards/stats-card.js index 860f3566d534c..b1215694ea785 100644 --- a/src/cards/stats-card.js +++ b/src/cards/stats-card.js @@ -187,6 +187,20 @@ const getStyles = ({ `; }; +/** + * Return the label for commits according to the selected options + * + * @param {boolean} include_all_commits Option to include all years + * @param {number|undefined} commits_year Option to include only selected year + * @returns {string} The label corresponding to the options. + */ +const getTotalCommitsYearLabel = (include_all_commits, commits_year) => + include_all_commits + ? "" + : commits_year + ? ` (${commits_year})` + : ` (last year)`; + /** * @typedef {import('../fetchers/types').StatsData} StatsData * @typedef {import('./types').StatCardOptions} StatCardOptions @@ -274,13 +288,10 @@ const renderStatsCard = (stats, options = {}) => { }; STATS.commits = { icon: icons.commits, - label: `${i18n.t("statcard.commits")}${ - include_all_commits - ? "" - : commits_year - ? ` (${commits_year})` - : ` (last year)` - }`, + label: `${i18n.t("statcard.commits")}${getTotalCommitsYearLabel( + include_all_commits, + commits_year, + )}`, value: totalCommits, id: "commits", }; @@ -521,13 +532,10 @@ const renderStatsCard = (stats, options = {}) => { .filter((key) => !hide.includes(key)) .map((key) => { if (key === "commits") { - return `${i18n.t("statcard.commits")} ${ - include_all_commits - ? "" - : commits_year - ? ` (${commits_year})` - : ` (last year)` - } : ${totalStars}`; + return `${i18n.t("statcard.commits")} ${getTotalCommitsYearLabel( + include_all_commits, + commits_year, + )} : ${totalStars}`; } return `${STATS[key].label}: ${STATS[key].value}`; })