From 93fe92ad9e69d974e13b621a1e6422fd38045647 Mon Sep 17 00:00:00 2001 From: Razon Yang Date: Mon, 25 Mar 2024 16:14:25 +0800 Subject: [PATCH] fix(git-repo): correct the humanize function for "m" unit. (#564) fix(git-org): correct the humanize function for `m` unit. fix(git-repo): replace `getJSON` with `resources.getRemote`. fix(git-org): replace `getJSON` with `resources.getRemote`. --- .../modules/git-org/functions/data.html | 26 ++++++++----- .../modules/git-org/functions/humanize.html | 2 +- .../modules/git-repo/functions/data.html | 37 +++++++++++++------ .../modules/git-repo/functions/humanize.html | 2 +- 4 files changed, 44 insertions(+), 23 deletions(-) diff --git a/modules/git-org/layouts/partials/hb/modules/header/modules/git-org/functions/data.html b/modules/git-org/layouts/partials/hb/modules/header/modules/git-org/functions/data.html index 36708e3c..30889f46 100644 --- a/modules/git-org/layouts/partials/hb/modules/header/modules/git-org/functions/data.html +++ b/modules/git-org/layouts/partials/hb/modules/header/modules/git-org/functions/data.html @@ -1,23 +1,29 @@ {{- $data := newScratch }} {{- $service := default "github" .service }} {{- if eq $service "github" }} - {{- $opts := dict }} + {{- $opts := newScratch }} {{- $ghToken := getenv "GITHUB_TOKEN" }} {{- with $ghToken }} - {{- $opts := dict "Authorization" (printf "Bearer %s" .) }} + {{- $opts.SetInMap "headers" "Authorization" (printf "Bearer %s" .) }} {{- end }} - {{- with getJSON (printf "https://api.github.com/users/%s" .name) $opts }} - {{- $data.Set "URL" .html_url }} - {{- $data.Set "Name" .name }} - {{- $data.Set "Repos" .public_repos }} - {{- $data.Set "Followers" .followers }} - {{- $data.Set "AvatarURL" (printf "%s&s=32" .avatar_url) }} + {{- with resources.GetRemote (printf "https://api.github.com/users/%s" .name) $opts.Values }} + {{- with .Err }} + {{- errorf "[git-org] unable to fetch org information from GitHub: %s." . }} + {{- else }} + {{- $org := .Content | transform.Unmarshal }} + {{- $data.Set "URL" $org.html_url }} + {{- $data.Set "Name" $org.name }} + {{- $data.Set "Repos" $org.public_repos }} + {{- $data.Set "Followers" $org.followers }} + {{- $data.Set "AvatarURL" (printf "%s&s=32" $org.avatar_url) }} + {{- end }} {{- end }} {{/* Get stars. */}} {{- $stars := 0 }} - {{- with getJSON (printf "https://api.github.com/users/%s/repos?per_page=100" .name) $opts }} + {{- with resources.GetRemote (printf "https://api.github.com/users/%s/repos?per_page=100" .name) $opts.Values }} {{- $repos := slice }} - {{- range . }} + {{- $reposResponse := .Content | transform.Unmarshal }} + {{- range $reposResponse }} {{- $repos = $repos | append (printf "repo:%s" .full_name) }} {{- end }} {{- $graphqlAPI := "https://api.github.com/graphql" }} diff --git a/modules/git-org/layouts/partials/hb/modules/header/modules/git-org/functions/humanize.html b/modules/git-org/layouts/partials/hb/modules/header/modules/git-org/functions/humanize.html index 57d2fa4b..76cec0ec 100644 --- a/modules/git-org/layouts/partials/hb/modules/header/modules/git-org/functions/humanize.html +++ b/modules/git-org/layouts/partials/hb/modules/header/modules/git-org/functions/humanize.html @@ -1,4 +1,4 @@ -{{- if gt . 1e5 -}} +{{- if gt . 1e6 -}} {{- printf "%.1fm" (div . 1e6) -}} {{- else if gt . 1e3 -}} {{- printf "%.1fk" (div . 1e3) -}} diff --git a/modules/git-repo/layouts/partials/hb/modules/header/modules/git-repo/functions/data.html b/modules/git-repo/layouts/partials/hb/modules/header/modules/git-repo/functions/data.html index 3f4e7c45..b17d05ee 100644 --- a/modules/git-repo/layouts/partials/hb/modules/header/modules/git-repo/functions/data.html +++ b/modules/git-repo/layouts/partials/hb/modules/header/modules/git-repo/functions/data.html @@ -2,24 +2,39 @@ {{- $service := default "github" .service }} {{- $params := default dict site.Params.hb.header.git_repo }} {{- if eq $service "github" }} - {{- $opts := dict }} + {{- $opts := newScratch }} {{- with getenv "GITHUB_TOKEN" }} - {{- $opts := dict "Authorization" (printf "Bearer %s" .) }} + {{- $opts.SetInMap "headers" "Authorization" (printf "Bearer %s" .) }} {{- end }} - {{- with getJSON (printf "https://api.github.com/repos/%s/%s" .owner .name) $opts }} - {{- $data.Set "Title" .full_name }} - {{- $data.Set "URL" .html_url }} - {{- $data.Set "Stars" .stargazers_count }} - {{- $data.Set "Forks" .forks_count }} + {{- with resources.GetRemote (printf "https://api.github.com/repos/%s/%s" .owner .name) $opts.Values }} + {{- with .Err }} + {{- errorf "[git-repo] unable to fetch repo information from GitHub: %s." . }} + {{- else }} + {{- $repo := .Content | transform.Unmarshal }} + {{- $data.Set "Title" $repo.full_name }} + {{- $data.Set "URL" $repo.html_url }} + {{- $data.Set "Stars" $repo.stargazers_count }} + {{- $data.Set "Forks" $repo.forks_count }} + {{- end }} {{- end }} {{- if default true $params.tag }} - {{- with getJSON (printf "https://api.github.com/repos/%s/%s/releases/latest" .owner .name) $opts }} - {{- $data.Set "Tag" .tag_name }} + {{- with resources.GetRemote (printf "https://api.github.com/repos/%s/%s/releases/latest" .owner .name) $opts.Values }} + {{- with .Err }} + {{- errorf "[git-repo] unable to fetch latest release from GitHub: %s." . }} + {{- else }} + {{- $release := .Content | transform.Unmarshal }} + {{- $data.Set "Tag" $release.tag_name }} + {{- end }} {{- end }} {{- end }} {{- with $params.docker }} - {{- with getJSON (printf "https://hub.docker.com/v2/repositories/%s/%s" .namespace .name) $opts }} - {{- $data.Set "DockerPulls" .pull_count }} + {{- with resources.GetRemote (printf "https://hub.docker.com/v2/repositories/%s/%s" .namespace .name) }} + {{- with .Err }} + {{- errorf "[git-repo] unable to fetch stats from DockerHub: %s." . }} + {{- else }} + {{- $dockerHubData := .Content | transform.Unmarshal }} + {{- $data.Set "DockerPulls" $dockerHubData.pull_count }} + {{- end }} {{- end }} {{- end }} {{- else }} diff --git a/modules/git-repo/layouts/partials/hb/modules/header/modules/git-repo/functions/humanize.html b/modules/git-repo/layouts/partials/hb/modules/header/modules/git-repo/functions/humanize.html index 57d2fa4b..76cec0ec 100644 --- a/modules/git-repo/layouts/partials/hb/modules/header/modules/git-repo/functions/humanize.html +++ b/modules/git-repo/layouts/partials/hb/modules/header/modules/git-repo/functions/humanize.html @@ -1,4 +1,4 @@ -{{- if gt . 1e5 -}} +{{- if gt . 1e6 -}} {{- printf "%.1fm" (div . 1e6) -}} {{- else if gt . 1e3 -}} {{- printf "%.1fk" (div . 1e3) -}}