From 51717e99a0e77c8c6f1d2e9d09a0f7c6a8e7bc7f Mon Sep 17 00:00:00 2001 From: r-freeman Date: Tue, 21 Feb 2023 21:21:11 +0000 Subject: [PATCH] Added top repo metric to dashboard --- lib/dashboard.ts | 15 ++++++++++++++- lib/github.ts | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/lib/dashboard.ts b/lib/dashboard.ts index f4a04ec..b39b589 100644 --- a/lib/dashboard.ts +++ b/lib/dashboard.ts @@ -1,4 +1,10 @@ -import {getTotalFollowers, getTotalForks, getTotalRepos, getTotalStars} from '@/lib/github' +import { + getTopRepo, + getTotalFollowers, + getTotalForks, + getTotalRepos, + getTotalStars +} from '@/lib/github' import {getAllArticles} from '@/lib/getAllArticles' import {getTopArtist, getTopGenre} from '@/lib/spotify' import {getViews} from '@/lib/views' @@ -11,6 +17,7 @@ export async function getDashboardData() { getTotalFollowers() ]) + const topRepo = await getTopRepo() const totalStars = await getTotalStars(totalRepos) const totalForks = await getTotalForks(totalRepos) const totalArticles = (await getAllArticles()).length @@ -56,6 +63,12 @@ export async function getDashboardData() { group: "GitHub", href: "https://github.com/r-freeman?tab=repositories" }, + { + title: "Top repo", + value: topRepo.name, + group: "GitHub", + href: topRepo.url + }, { title: "Followers", value: +totalFollowers, diff --git a/lib/github.ts b/lib/github.ts index e90b1bc..6aeb1d1 100644 --- a/lib/github.ts +++ b/lib/github.ts @@ -67,6 +67,21 @@ type TotalForksResponse = { } } +type TopRepoResponse = { + data: { + user: { + repositories: { + nodes: [ + { + name: string + url: string + } + ] + } + } + } +} + export async function getPinnedRepos() { const response = await fetcher(GITHUB_GRAPHQL, { method: 'POST', @@ -192,4 +207,33 @@ export async function getTotalForks(totalRepos: number) { return response.data.user.repositories.nodes .reduce((acc, node) => acc + node.forks.totalCount, 0) +} + +export async function getTopRepo() { + 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: 1, orderBy: {field: STARGAZERS, direction: DESC}) { + nodes { + name + url + } + } + } + }` + }) + }) as TopRepoResponse + + const {name, url} = response.data.user.repositories.nodes[0] + + return { + name, + url + } } \ No newline at end of file