From 92518e375f99bf83b3e0113805ce80dae1f2fb72 Mon Sep 17 00:00:00 2001 From: Alexandr Garbuzov Date: Mon, 19 Jun 2023 06:51:58 +0300 Subject: [PATCH] Stats card: show started and answered discussions stats (#2849) * Stats card: show started and answered discussions stats * trans * dev --- readme.md | 8 ++--- src/cards/stats-card.js | 20 ++++++++++++ src/common/icons.js | 2 ++ src/fetchers/stats-fetcher.js | 10 ++++++ src/fetchers/types.d.ts | 2 ++ src/translations.js | 60 +++++++++++++++++++++++++++++++++++ tests/api.test.js | 8 +++++ tests/fetchStats.test.js | 16 ++++++++++ tests/renderStatsCard.test.js | 18 +++++++++-- 9 files changed, 137 insertions(+), 7 deletions(-) diff --git a/readme.md b/readme.md index 6b5fe93..6c37694 100644 --- a/readme.md +++ b/readme.md @@ -151,7 +151,7 @@ You can pass a query parameter `&hide=` to hide any specific stats with comma-se You can pass a query parameter `&show=` to show any specific additional stats with comma-separated values. -> Options: `&show=reviews` +> Options: `&show=reviews,discussions_started,discussions_answered` ```md ![Anurag's GitHub stats](https://github-readme-stats.vercel.app/api?username=anuraghazra&show=reviews) @@ -314,7 +314,7 @@ You can provide multiple comma-separated values in the bg\_color option to rende * `disable_animations` - Disables all animations in the card *(boolean)*. Default: `false`. * `ring_color` - Color of the rank circle *(hex color)*. Defaults to the theme ring color if it exists and otherwise the title color. * `number_format` - Switch between two available formats for displaying the card values `short` (i.e. `6.6k`) and `long` (i.e. `6626`). Default: `short`. -* `show` - Show [additional items](#showing-additional-individual-stats) on stats card (i.e. `reviews`) *(Comma-separated values)*. Default: `[] (blank array)`. +* `show` - Show [additional items](#showing-additional-individual-stats) on stats card (i.e. `reviews`, `discussions_started` or `discussions_answered`) *(Comma-separated values)*. Default: `[] (blank array)`. > **Note** > When hide\_rank=`true`, the minimum card width is 270 px + the title length and padding. @@ -541,9 +541,9 @@ Change the `?username=` value to your [Wakatime](https://wakatime.com) username. ![Anurag's GitHub stats](https://github-readme-stats.vercel.app/api?username=anuraghazra\&hide=contribs,issues) -* Showing addition stats +* Showing additional stats -![Anurag's GitHub stats](https://github-readme-stats.vercel.app/api?username=anuraghazra\&show=reviews) +![Anurag's GitHub stats](https://github-readme-stats.vercel.app/api?username=anuraghazra\&show=reviews,discussions_started,discussions_answered) * Showing icons diff --git a/src/cards/stats-card.js b/src/cards/stats-card.js index 034c45e..79b0076 100644 --- a/src/cards/stats-card.js +++ b/src/cards/stats-card.js @@ -86,6 +86,8 @@ const renderStatsCard = (stats = {}, options = {}) => { totalIssues, totalPRs, totalReviews, + totalDiscussionsStarted, + totalDiscussionsAnswered, contributedTo, rank, } = stats; @@ -176,6 +178,24 @@ const renderStatsCard = (stats = {}, options = {}) => { value: totalIssues, id: "issues", }; + + if (show.includes("discussions_started")) { + STATS.discussions_started = { + icon: icons.discussions_started, + label: i18n.t("statcard.discussions-started"), + value: totalDiscussionsStarted, + id: "discussions_started", + }; + } + if (show.includes("discussions_answered")) { + STATS.discussions_answered = { + icon: icons.discussions_answered, + label: i18n.t("statcard.discussions-answered"), + value: totalDiscussionsAnswered, + id: "discussions_answered", + }; + } + STATS.contribs = { icon: icons.contribs, label: i18n.t("statcard.contribs"), diff --git a/src/common/icons.js b/src/common/icons.js index 949ce65..4368375 100644 --- a/src/common/icons.js +++ b/src/common/icons.js @@ -7,6 +7,8 @@ const icons = { contribs: ``, fork: ``, reviews: ``, + discussions_started: ``, + discussions_answered: ``, }; /** diff --git a/src/fetchers/stats-fetcher.js b/src/fetchers/stats-fetcher.js index 931f3e2..21d2611 100644 --- a/src/fetchers/stats-fetcher.js +++ b/src/fetchers/stats-fetcher.js @@ -63,6 +63,12 @@ const GRAPHQL_STATS_QUERY = ` followers { totalCount } + repositoryDiscussions { + totalCount + } + repositoryDiscussionComments(onlyAnswers: true) { + totalCount + } ${GRAPHQL_REPOS_FIELD} } } @@ -190,6 +196,8 @@ const fetchStats = async ( totalCommits: 0, totalIssues: 0, totalStars: 0, + totalDiscussionsStarted: 0, + totalDiscussionsAnswered: 0, contributedTo: 0, rank: { level: "C", percentile: 100 }, }; @@ -232,6 +240,8 @@ const fetchStats = async ( stats.totalReviews = user.contributionsCollection.totalPullRequestReviewContributions; stats.totalIssues = user.openIssues.totalCount + user.closedIssues.totalCount; + stats.totalDiscussionsStarted = user.repositoryDiscussions.totalCount; + stats.totalDiscussionsAnswered = user.repositoryDiscussionComments.totalCount; stats.contributedTo = user.repositoriesContributedTo.totalCount; // Retrieve stars while filtering out repositories to be hidden. diff --git a/src/fetchers/types.d.ts b/src/fetchers/types.d.ts index a810c6c..3a86205 100644 --- a/src/fetchers/types.d.ts +++ b/src/fetchers/types.d.ts @@ -22,6 +22,8 @@ export type StatsData = { totalCommits: number; totalIssues: number; totalStars: number; + totalDiscussionsStarted: number; + totalDiscussionsAnswered: number; contributedTo: number; rank: { level: string; percentile: number }; }; diff --git a/src/translations.js b/src/translations.js index 9934946..1c66c28 100644 --- a/src/translations.js +++ b/src/translations.js @@ -228,6 +228,66 @@ const statCardLocales = ({ name, apostrophe }) => { vi: "Tổng Số PR Đã Xem Xét", se: "Totalt antal granskade PR", }, + "statcard.discussions-started": { + ar: "مجموع بدء المناقشات", + cn: "发起的讨论总数", + "zh-tw": "發起的討論總數", + cs: "Celkem zahájených diskusí", + de: "Gesamt gestartete Diskussionen", + en: "Total Discussions Started", + bn: "সর্বমোট আলোচনা শুরু", + es: "Discusiones totales iniciadas", + fr: "Nombre total de discussions lancées", + hu: "Összes megkezdett megbeszélés", + it: "Discussioni totali avviate", + ja: "開始されたディスカッションの総数", + kr: "시작된 토론 총 수", + nl: "Totaal gestarte discussies", + "pt-pt": "Total de Discussões Iniciadas", + "pt-br": "Total de Discussões Iniciadas", + np: "कुल चर्चा सुरु", + el: "Σύνολο Συζητήσεων που Ξεκίνησαν", + ru: "Всего начатых дискуссий", + "uk-ua": "Всього розпочатих дискусій", + id: "Total Diskusi Dimulai", + my: "Jumlah Perbincangan Bermula", + sk: "Celkový počet začatých diskusií", + tr: "Başlatılan Toplam Tartışma", + pl: "Łącznie rozpoczętych dyskusji", + uz: "Boshlangan muzokaralar soni", + vi: "Tổng Số Thảo Luận Bắt Đầu", + se: "Totalt antal diskussioner startade", + }, + "statcard.discussions-answered": { + ar: "مجموع الردود على المناقشات", + cn: "回复的讨论总数", + "zh-tw": "回覆的討論總數", + cs: "Celkem zodpovězených diskusí", + de: "Gesamt beantwortete Diskussionen", + en: "Total Discussions Answered", + bn: "সর্বমোট আলোচনা উত্তর", + es: "Discusiones totales respondidas", + fr: "Nombre total de discussions répondues", + hu: "Összes megválaszolt megbeszélés", + it: "Discussioni totali risposte", + ja: "回答されたディスカッションの総数", + kr: "답변된 토론 총 수", + nl: "Totaal beantwoorde discussies", + "pt-pt": "Total de Discussões Respondidas", + "pt-br": "Total de Discussões Respondidas", + np: "कुल चर्चा उत्तर", + el: "Σύνολο Συζητήσεων που Απαντήθηκαν", + ru: "Всего отвеченных дискуссий", + "uk-ua": "Всього відповідей на дискусії", + id: "Total Diskusi Dibalas", + my: "Jumlah Perbincangan Dijawab", + sk: "Celkový počet zodpovedaných diskusií", + tr: "Toplam Cevaplanan Tartışma", + pl: "Łącznie odpowiedzianych dyskusji", + uz: "Javob berilgan muzokaralar soni", + vi: "Tổng Số Thảo Luận Đã Trả Lời", + se: "Totalt antal diskussioner besvarade", + }, }; }; diff --git a/tests/api.test.js b/tests/api.test.js index 4fb965e..7155799 100644 --- a/tests/api.test.js +++ b/tests/api.test.js @@ -12,6 +12,9 @@ const stats = { totalCommits: 200, totalIssues: 300, totalPRs: 400, + totalReviews: 50, + totalDiscussionsStarted: 10, + totalDiscussionsAnswered: 40, contributedTo: 50, rank: null, }; @@ -33,11 +36,16 @@ const data_stats = { repositoriesContributedTo: { totalCount: stats.contributedTo }, contributionsCollection: { totalCommitContributions: stats.totalCommits, + totalPullRequestReviewContributions: stats.totalReviews, }, pullRequests: { totalCount: stats.totalPRs }, openIssues: { totalCount: stats.totalIssues }, closedIssues: { totalCount: 0 }, followers: { totalCount: 0 }, + repositoryDiscussions: { totalCount: stats.totalDiscussionsStarted }, + repositoryDiscussionComments: { + totalCount: stats.totalDiscussionsAnswered, + }, repositories: { totalCount: 1, nodes: [{ stargazers: { totalCount: 100 } }], diff --git a/tests/fetchStats.test.js b/tests/fetchStats.test.js index 3b0f915..3945d75 100644 --- a/tests/fetchStats.test.js +++ b/tests/fetchStats.test.js @@ -18,6 +18,8 @@ const data_stats = { openIssues: { totalCount: 100 }, closedIssues: { totalCount: 100 }, followers: { totalCount: 100 }, + repositoryDiscussions: { totalCount: 10 }, + repositoryDiscussionComments: { totalCount: 40 }, repositories: { totalCount: 5, nodes: [ @@ -119,6 +121,8 @@ describe("Test fetchStats", () => { totalPRs: 300, totalReviews: 50, totalStars: 300, + totalDiscussionsStarted: 10, + totalDiscussionsAnswered: 40, rank, }); }); @@ -150,6 +154,8 @@ describe("Test fetchStats", () => { totalPRs: 300, totalReviews: 50, totalStars: 300, + totalDiscussionsStarted: 10, + totalDiscussionsAnswered: 40, rank, }); }); @@ -187,6 +193,8 @@ describe("Test fetchStats", () => { totalPRs: 300, totalReviews: 50, totalStars: 300, + totalDiscussionsStarted: 10, + totalDiscussionsAnswered: 40, rank, }); }); @@ -215,6 +223,8 @@ describe("Test fetchStats", () => { totalPRs: 300, totalReviews: 50, totalStars: 200, + totalDiscussionsStarted: 10, + totalDiscussionsAnswered: 40, rank, }); }); @@ -241,6 +251,8 @@ describe("Test fetchStats", () => { totalPRs: 300, totalReviews: 50, totalStars: 400, + totalDiscussionsStarted: 10, + totalDiscussionsAnswered: 40, rank, }); }); @@ -267,6 +279,8 @@ describe("Test fetchStats", () => { totalPRs: 300, totalReviews: 50, totalStars: 300, + totalDiscussionsStarted: 10, + totalDiscussionsAnswered: 40, rank, }); }); @@ -293,6 +307,8 @@ describe("Test fetchStats", () => { totalPRs: 300, totalReviews: 50, totalStars: 300, + totalDiscussionsStarted: 10, + totalDiscussionsAnswered: 40, rank, }); }); diff --git a/tests/renderStatsCard.test.js b/tests/renderStatsCard.test.js index 6a47f94..a2fd5b6 100644 --- a/tests/renderStatsCard.test.js +++ b/tests/renderStatsCard.test.js @@ -17,6 +17,8 @@ const stats = { totalIssues: 300, totalPRs: 400, totalReviews: 50, + totalDiscussionsStarted: 10, + totalDiscussionsAnswered: 50, contributedTo: 500, rank: { level: "A+", score: 40 }, }; @@ -42,6 +44,12 @@ describe("Test renderStatsCard", () => { // Default hidden stats expect(queryByTestId(document.body, "reviews")).not.toBeInTheDocument(); + expect( + queryByTestId(document.body, "discussions_started"), + ).not.toBeInTheDocument(); + expect( + queryByTestId(document.body, "discussions_answered"), + ).not.toBeInTheDocument(); }); it("should have proper name apostrophe", () => { @@ -73,16 +81,18 @@ describe("Test renderStatsCard", () => { expect(queryByTestId(document.body, "prs")).toBeNull(); expect(queryByTestId(document.body, "contribs")).toBeNull(); expect(queryByTestId(document.body, "reviews")).toBeNull(); + expect(queryByTestId(document.body, "discussions_started")).toBeNull(); + expect(queryByTestId(document.body, "discussions_answered")).toBeNull(); }); - it("should show total reviews", () => { + it("should show additional stats", () => { document.body.innerHTML = renderStatsCard(stats, { - show: ["reviews"], + show: ["reviews", "discussions_started", "discussions_answered"], }); expect( document.body.getElementsByTagName("svg")[0].getAttribute("height"), - ).toBe("220"); + ).toBe("270"); expect(queryByTestId(document.body, "stars")).toBeDefined(); expect(queryByTestId(document.body, "commits")).toBeDefined(); @@ -90,6 +100,8 @@ describe("Test renderStatsCard", () => { expect(queryByTestId(document.body, "prs")).toBeDefined(); expect(queryByTestId(document.body, "contribs")).toBeDefined(); expect(queryByTestId(document.body, "reviews")).toBeDefined(); + expect(queryByTestId(document.body, "discussions_started")).toBeDefined(); + expect(queryByTestId(document.body, "discussions_answered")).toBeDefined(); }); it("should hide_rank", () => {