diff --git a/api/github-stats.js b/api/github-stats.js new file mode 100644 index 00000000..db9e464b --- /dev/null +++ b/api/github-stats.js @@ -0,0 +1,42 @@ +import parse from "parse-link-header"; + +const options = { + headers: { + Authorization: `Bearer ${process.env.GITHUB_ACCESS_TOKEN}`, + }, +}; + +export default async function githubStats(request, response) { + response.setHeader( + "Cache-Control", + "s-maxage=3600, stale-while-revalidate=600" + ); + + if (!process.env.GITHUB_ACCESS_TOKEN) { + response.status(500).send({ message: "Github access token not set" }); + } + + try { + const [starsRes, contributorsRes] = await Promise.all([ + fetch("https://api.github.com/repos/deepset-ai/haystack", options), + fetch( + "https://api.github.com/repos/deepset-ai/haystack/contributors?per_page=1", + options + ), + ]); + + if (!starsRes.ok || !contributorsRes.ok) { + throw new Error("Failed to fetch data from GitHub"); + } + + const starsData = await starsRes.json(); + const stars = starsData.stargazers_count; + const contributorsLinkHeader = contributorsRes.headers.get("link"); + const parsed = parse(contributorsLinkHeader); // parse link header to get total pages + const contributors = parsed.last.page; + + response.status(200).json({ stars, contributors }); + } catch (e) { + response.status(500).json({ error: e.message }); + } +} diff --git a/config.toml b/config.toml index 8b02a2f9..26591285 100644 --- a/config.toml +++ b/config.toml @@ -29,7 +29,7 @@ paginatePath = "/" [security] [security.funcs] - getenv = ["^HUGO_", "GITHUB_ACCESS_TOKEN", "GOOGLE_TAG_ANALYTICS", "DC_API_KEY", "DC_WORKSPACE_NAME", "DC_PIPELINE_NAME"] + getenv = ["^HUGO_", "GOOGLE_TAG_ANALYTICS", "DC_API_KEY", "DC_WORKSPACE_NAME", "DC_PIPELINE_NAME"] [markup] # Code blocks diff --git a/themes/haystack/assets/js/github-stats.js b/themes/haystack/assets/js/github-stats.js new file mode 100644 index 00000000..50885dcf --- /dev/null +++ b/themes/haystack/assets/js/github-stats.js @@ -0,0 +1,32 @@ +import { kFormatter } from "./utils"; + +// Fetches github data from api and populates html with stars and contributors +export const githubStats = async () => { + const starContainers = document.querySelectorAll(".github-stars-js"); + const contributorContainers = document.querySelectorAll( + ".github-contributors-js" + ); + + if ([...starContainers, ...contributorContainers].length > 0) { + const response = await fetch("/api/github-stats", { + method: "GET", + headers: { + "Content-Type": "application/json", + }, + }); + + if (response.ok) { + const data = await response.json(); + + // Populate stars + starContainers.forEach((container) => { + container.innerHTML = kFormatter(data.stars); + }); + + // Populate contributors + contributorContainers.forEach((container) => { + container.innerHTML = data.contributors; + }); + } + } +}; diff --git a/themes/haystack/assets/js/script.js b/themes/haystack/assets/js/script.js index 0717b554..6e0c3db4 100644 --- a/themes/haystack/assets/js/script.js +++ b/themes/haystack/assets/js/script.js @@ -2,6 +2,7 @@ import { navigation } from "./navigation"; import { accordions } from "./accordions"; import { newsletters } from "./newsletters"; +import { githubStats } from "./github-stats"; const ready = (fn) => { if (document.readyState != "loading") { @@ -14,3 +15,4 @@ const ready = (fn) => { ready(navigation); ready(accordions); ready(newsletters); +ready(githubStats); diff --git a/themes/haystack/assets/sass/components/_buttons.scss b/themes/haystack/assets/sass/components/_buttons.scss index b0b59b06..27d8cd80 100644 --- a/themes/haystack/assets/sass/components/_buttons.scss +++ b/themes/haystack/assets/sass/components/_buttons.scss @@ -321,6 +321,10 @@ button.nav-toggle-close { .separator { font-weight: 400; } + + .github-stars-js { + min-width: 3.125rem; + } } .colab-btn, diff --git a/themes/haystack/assets/sass/pages/_community.scss b/themes/haystack/assets/sass/pages/_community.scss index f3c27506..d4e7f24a 100644 --- a/themes/haystack/assets/sass/pages/_community.scss +++ b/themes/haystack/assets/sass/pages/_community.scss @@ -94,6 +94,22 @@ font-size: var(--text-small); } } + + .github-stars-js { + min-width: 8.75rem; + min-height: 6rem; + display: flex; + align-items: center; + justify-content: center; + } + + .github-contributors-js { + min-width: 6.5rem; + min-height: 6rem; + display: flex; + align-items: center; + justify-content: center; + } } } } diff --git a/themes/haystack/layouts/partials/github-stars-button.html b/themes/haystack/layouts/partials/github-stars-button.html index 151eaeb3..bb7a5ab8 100644 --- a/themes/haystack/layouts/partials/github-stars-button.html +++ b/themes/haystack/layouts/partials/github-stars-button.html @@ -1,21 +1,7 @@ {{/* Github stars button */}} -{{ $accessToken := getenv "GITHUB_ACCESS_TOKEN" }} -{{ $starCount := 0 }} - -{{ with $accessToken }} -{{/* Authorization */}} -{{ $auth := (dict "Authorization" (printf "Bearer %s" $accessToken)) }} - -{{/* Fetch Stargazer count */}} -{{ $starsData := getJSON "https://api.github.com/repos/deepset-ai/haystack" $auth }} -{{/* Format number to K (2000 -> 2k) */}} -{{ $starCount = (div $starsData.stargazers_count 1000) | lang.FormatNumber 1 }} -{{ $starCount = printf (print $starCount "%s") "k"}} -{{ end }} - {{/* Button */}} - Stars |{{ $starCount }} \ No newline at end of file + Stars |{{ partial "loader" . }} \ No newline at end of file diff --git a/themes/haystack/layouts/partials/github-stats.html b/themes/haystack/layouts/partials/github-stats.html index 0a60286d..3add16c8 100644 --- a/themes/haystack/layouts/partials/github-stats.html +++ b/themes/haystack/layouts/partials/github-stats.html @@ -1,57 +1,17 @@ -{{/* Github star & contributor count */}} +{{/* Github star & contributor count */}} -{{ $accessToken := getenv "GITHUB_ACCESS_TOKEN" }} - -{{/* Placeholder values */}} -{{ $starCount := 0 }} -{{ $contributorCount := 0 }} - -{{ with $accessToken }} - {{/* Authorization */}} - {{ $auth := (dict "Authorization" (printf "Bearer %s" $accessToken)) }} - - {{/* Fetch Stargazer count */}} - {{ $starsData := getJSON "https://api.github.com/repos/deepset-ai/haystack" $auth }} - {{/* Format number to K (2000 -> 2k) */}} - {{ $starCount = (div $starsData.stargazers_count 1000) | lang.FormatNumber 1 }} - {{ $starCount = printf (print $starCount "%s") "k"}} - - - {{/* Fetch Contributor count */}} - {{ $contributorsURL := "https://api.github.com/repos/deepset-ai/haystack/contributors?per_page=100" }} - {{ $contributorsData := getJSON $contributorsURL $auth }} - {{ $contributorCount = len $contributorsData }} - - {{/* Save the amount of results we got */}} - {{ $lastFetchCount := $contributorCount }} - - {{/* Range 10 times - hardcoded since there is no proper loop in Hugo */}} - {{ range $i, $num := (seq 10) }} - {{/* If we got 100 results on last fetch, fetch again */}} - {{ if eq $lastFetchCount 100 }} - {{ $page := add $i 2 }} - {{ $contributorsURL = printf "https://api.github.com/repos/deepset-ai/haystack/contributors?per_page=100&page=%s" ( string $page ) }} - {{ $contributorsData = getJSON $contributorsURL $auth }} - {{ $lastFetchCount = (len $contributorsData) }} - {{ $contributorCount = add $contributorCount (len $contributorsData) }} - {{ end }} - {{ end }} - -{{ end }} - - -{{/* Stat boxes */}} +{{/* Stat boxes */}} + \ No newline at end of file diff --git a/themes/haystack/layouts/partials/github-top-contributors.html b/themes/haystack/layouts/partials/github-top-contributors.html deleted file mode 100644 index 22a1da58..00000000 --- a/themes/haystack/layouts/partials/github-top-contributors.html +++ /dev/null @@ -1,24 +0,0 @@ -{{/* Github most active contributors */}} - -{{ $accessToken := getenv "GITHUB_ACCESS_TOKEN" }} - -{{ with $accessToken }} - {{/* Authorization */}} - {{ $auth := (dict "Authorization" (printf "Bearer %s" $accessToken)) }} - - {{/* Fetch Top Contributors */}} - {{ $URL := "https://api.github.com/repos/deepset-ai/haystack/contributors?per_page=10" }} - {{ $data := getJSON $URL $auth }} - - - {{/* List contributors */}} - -{{ end }}