-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.js
More file actions
150 lines (121 loc) · 3.66 KB
/
Copy pathgithub.js
File metadata and controls
150 lines (121 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const GITHUB_API_BASE = "https://api.github.com"
const ORG_NAME = "boringcode-dev"
const FEATURED_REPOS_LIMIT = 9
const REPOS_PER_PAGE = 100
const ORG_CACHE_TTL_SECONDS = 60 * 60
const REPOS_CACHE_TTL_SECONDS = 30 * 60
const FALLBACK_ORG = {
login: ORG_NAME,
name: "BoringCode.dev",
description: "We write the boring stuff, so you don't have to.",
avatar_url: "/logo.png",
html_url: `https://github.com/${ORG_NAME}`,
public_repos: 0,
}
const FALLBACK_REPOS = {
repos: [],
totalCount: 0,
}
export async function onRequestGet(context) {
const url = new URL(context.request.url)
const type = url.searchParams.get("type")
if (type !== "org" && type !== "repos") {
return jsonResponse({ error: "Invalid type parameter" }, 400, {
"Cache-Control": "no-store",
})
}
const cache = caches.default
const cacheKey = new Request(url.toString(), {
method: "GET",
headers: context.request.headers,
})
const cachedResponse = await cache.match(cacheKey)
if (cachedResponse) {
return cachedResponse
}
try {
const responseData = type === "org" ? await fetchOrg(context.env.GITHUB_TOKEN) : await fetchFeaturedRepos(context.env.GITHUB_TOKEN)
const ttl = type === "org" ? ORG_CACHE_TTL_SECONDS : REPOS_CACHE_TTL_SECONDS
const response = jsonResponse(responseData, 200, cacheHeaders(ttl))
context.waitUntil(cache.put(cacheKey, response.clone()))
return response
} catch (error) {
console.error("GitHub API Error:", error)
const fallbackData = type === "org" ? FALLBACK_ORG : FALLBACK_REPOS
return jsonResponse(fallbackData, 200, {
...cacheHeaders(60),
"X-Data-Source": "fallback",
})
}
}
async function fetchOrg(token) {
const orgResponse = await fetchGitHub(`/orgs/${ORG_NAME}`, token)
if (orgResponse.status === 404) {
return FALLBACK_ORG
}
if (!orgResponse.ok) {
throw new Error(`Failed to fetch organization: ${orgResponse.status}`)
}
const orgData = await orgResponse.json()
return {
...orgData,
avatar_url: "/logo.png",
}
}
async function fetchFeaturedRepos(token) {
const repos = await fetchAllRepos(token)
const filteredRepos = repos
.filter((repo) => !repo.name.includes(".github") && !repo.name.toLowerCase().includes("test"))
.sort((a, b) => b.stargazers_count - a.stargazers_count)
return {
repos: filteredRepos.slice(0, FEATURED_REPOS_LIMIT),
totalCount: filteredRepos.length,
}
}
async function fetchAllRepos(token) {
const repos = []
for (let page = 1; page <= 50; page += 1) {
const reposResponse = await fetchGitHub(
`/orgs/${ORG_NAME}/repos?type=public&sort=updated&per_page=${REPOS_PER_PAGE}&page=${page}`,
token,
)
if (reposResponse.status === 404) {
return []
}
if (!reposResponse.ok) {
throw new Error(`Failed to fetch repositories: ${reposResponse.status}`)
}
const pageRepos = await reposResponse.json()
repos.push(...pageRepos)
if (pageRepos.length < REPOS_PER_PAGE) {
break
}
}
return repos
}
function fetchGitHub(path, token) {
const headers = new Headers({
Accept: "application/vnd.github+json",
"User-Agent": "BoringCode-Landing-Page",
})
if (token) {
headers.set("Authorization", `Bearer ${token}`)
}
return fetch(`${GITHUB_API_BASE}${path}`, {
headers,
})
}
function cacheHeaders(ttlSeconds) {
return {
"Cache-Control": `public, max-age=0, s-maxage=${ttlSeconds}, stale-while-revalidate=86400`,
}
}
function jsonResponse(body, status = 200, extraHeaders = {}) {
return new Response(JSON.stringify(body), {
status,
headers: {
"Content-Type": "application/json; charset=utf-8",
...extraHeaders,
},
})
}