From ad39e0f4fe2c5aec1d159ad6f3df3c2bf678d6cb Mon Sep 17 00:00:00 2001 From: r-freeman Date: Tue, 21 Feb 2023 17:03:38 +0000 Subject: [PATCH] Added github forks metric to dashboard --- lib/dashboard.ts | 9 ++++++++- lib/github.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/lib/dashboard.ts b/lib/dashboard.ts index 5c4434f..f4a04ec 100644 --- a/lib/dashboard.ts +++ b/lib/dashboard.ts @@ -1,4 +1,4 @@ -import {getTotalFollowers, getTotalRepos, getTotalStars} from '@/lib/github' +import {getTotalFollowers, getTotalForks, getTotalRepos, getTotalStars} from '@/lib/github' import {getAllArticles} from '@/lib/getAllArticles' import {getTopArtist, getTopGenre} from '@/lib/spotify' import {getViews} from '@/lib/views' @@ -12,6 +12,7 @@ export async function getDashboardData() { ]) const totalStars = await getTotalStars(totalRepos) + const totalForks = await getTotalForks(totalRepos) const totalArticles = (await getAllArticles()).length const totalArticleViews = (await getViews()).views const topArtist = await getTopArtist() @@ -67,6 +68,12 @@ export async function getDashboardData() { group: "GitHub", href: "https://github.com/r-freeman/" }, + { + title: "Forks", + value: +totalForks, + group: "GitHub", + href: "https://github.com/r-freeman/" + }, { title: "Total articles", value: +totalArticles, diff --git a/lib/github.ts b/lib/github.ts index 7da09b8..e90b1bc 100644 --- a/lib/github.ts +++ b/lib/github.ts @@ -51,6 +51,22 @@ type TotalStarsResponse = { } } +type TotalForksResponse = { + data: { + user: { + repositories: { + nodes: [ + { + forks: { + totalCount: number + } + } + ] + } + } + } +} + export async function getPinnedRepos() { const response = await fetcher(GITHUB_GRAPHQL, { method: 'POST', @@ -150,4 +166,30 @@ export async function getTotalStars(totalRepos: number) { return response.data.user.repositories.nodes .reduce((acc, node) => acc + node.stargazers.totalCount, 0) +} + +export async function getTotalForks(totalRepos: number) { + const response = await fetcher(GITHUB_GRAPHQL, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${GITHUB_ACCESS_TOKEN}` + }, + body: JSON.stringify({ + query: `{ + user(login: "${GITHUB_USERNAME}") { + repositories(first: ${totalRepos}) { + nodes { + forks { + totalCount + } + } + } + } + }` + }) + }) as TotalForksResponse + + return response.data.user.repositories.nodes + .reduce((acc, node) => acc + node.forks.totalCount, 0) } \ No newline at end of file