Skip to content

Commit 813d738

Browse files
committed
fix(github): authenticate API calls to fix rate-limited star count showing 0
Unauthenticated GitHub API calls are capped at 60 req/hr per IP — easily exhausted in production. Add optional GITHUB_TOKEN support via Astro's env schema to raise the limit to 5000/hr. Also fix the error fallthrough: on non-404 errors the handler logged but continued, risking stale/zero data; now returns defaultValues immediately. Add GITHUB_TOKEN as a secret env var in Cloudflare Pages → kestra-io project → Settings → Environment variables. Use the value of GH_READ_ORG_TOKEN.
1 parent 089f04d commit 813d738

2 files changed

Lines changed: 13 additions & 7 deletions

File tree

astro.config.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,11 @@ export default defineConfig({
197197
optional: true,
198198
default: false,
199199
}),
200+
GITHUB_TOKEN: envField.string({
201+
context: "server",
202+
access: "secret",
203+
optional: true,
204+
}),
200205
NO_RANDOM_ORDER: envField.boolean({
201206
context: "client",
202207
access: "public",

src/pages/api/github.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { $fetchCached, $fetchCachedRaw } from "~/utils/fetch"
2-
import { DISABLE_GITHUB } from "astro:env/server"
2+
import { DISABLE_GITHUB, GITHUB_TOKEN } from "astro:env/server"
33

44
const defaultValues = {
55
stargazers: 0,
@@ -16,27 +16,28 @@ export async function getValues() {
1616
if (DISABLE_GITHUB) {
1717
return defaultValues
1818
}
19+
const authHeaders: Record<string, string> = GITHUB_TOKEN
20+
? { "User-Agent": "request", Authorization: `Bearer ${GITHUB_TOKEN}` }
21+
: { "User-Agent": "request" }
22+
1923
let contribCountRes: any
2024
try {
2125
contribCountRes = await $fetchCachedRaw(
2226
"https://api.github.com/repos/kestra-io/kestra/contributors?anon=true&per_page=1",
23-
{ headers: { "User-Agent": "request" } },
27+
{ headers: authHeaders },
2428
)
2529
} catch (error) {
2630
console.error("Error fetching contributors count:", error)
2731
return defaultValues
2832
}
2933

3034
if (!contribCountRes.ok) {
31-
if (contribCountRes.status === 404) {
32-
return defaultValues
33-
}
34-
// Handle other errors
3535
console.error(
3636
"Error fetching contributors count:",
3737
contribCountRes.status,
3838
contribCountRes.statusText,
3939
)
40+
return defaultValues
4041
}
4142

4243
const contribStr =
@@ -53,7 +54,7 @@ export async function getValues() {
5354
}
5455

5556
return await $fetchCached("https://api.github.com/repos/kestra-io/kestra", {
56-
headers: { "User-Agent": "request" },
57+
headers: authHeaders,
5758
}).then((value) => {
5859
return {
5960
stargazers: value.stargazers_count,

0 commit comments

Comments
 (0)