Skip to content

Commit 0bfd227

Browse files
authored
improve build errors' messages for stats fetching (#1608)
* improve build error for stars fetching * more * chore: prettier
1 parent 150fe31 commit 0bfd227

File tree

4 files changed

+131
-105
lines changed

4 files changed

+131
-105
lines changed

scripts/sort-libraries/get-gem-stats.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,22 @@ type GemStatsFetchRespone = {
3333
}
3434

3535
export async function getGemStats(packageName: string): Promise<number> {
36-
const response = await fetch(
37-
`https://rubygems.org/api/v1/gems/${encodeURIComponent(packageName)}.json`
38-
)
39-
if (!response.ok) {
40-
console.warn(`Get invalid response from GEM for ${packageName}:`, response)
41-
return 0
42-
}
43-
const responseJson: GemStatsFetchRespone = await response.json()
44-
if (!responseJson) {
45-
console.warn(
46-
`Get invalid response from GEM for ${packageName}:`,
47-
responseJson
36+
try {
37+
const response = await fetch(
38+
`https://rubygems.org/api/v1/gems/${encodeURIComponent(packageName)}.json`
4839
)
40+
41+
if (!response.ok) {
42+
console.warn(
43+
`Error fetching GEM stats for ${packageName}. Status: ${response.status}`
44+
)
45+
return 0
46+
}
47+
48+
const responseJson: GemStatsFetchRespone = await response.json()
49+
return responseJson.downloads ?? 0
50+
} catch (error) {
51+
console.error(`Exception fetching GEM stats for ${packageName}:`, error)
4952
return 0
5053
}
51-
return responseJson.downloads ?? 0
5254
}

scripts/sort-libraries/get-github-stats.ts

Lines changed: 73 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -168,86 +168,91 @@ export async function getGitHubStats(
168168
}
169169
}
170170
`
171-
const lastMonth = new Date()
172-
lastMonth.setMonth(lastMonth.getMonth() - 3)
173-
const response = await fetch("https://api.github.com/graphql", {
174-
method: "POST",
175-
body: JSON.stringify({
176-
query,
177-
variables: { owner, repoName, since: lastMonth },
178-
}),
179-
headers: {
180-
Authorization: `Bearer ${accessToken}`,
181-
"Content-Type": "application/json",
182-
},
183-
})
184-
if (!response.ok) {
185-
console.warn(
186-
`Get invalid response from GitHub for ${owner}/${repoName}. Status: ${response.status}`
187-
)
188-
}
189-
const responseJson: GitHubStatsFetchResponse = await response.json()
171+
const lastThreeMonths = new Date()
172+
lastThreeMonths.setMonth(lastThreeMonths.getMonth() - 3)
173+
174+
try {
175+
const response = await fetch("https://api.github.com/graphql", {
176+
method: "POST",
177+
body: JSON.stringify({
178+
query,
179+
variables: { owner, repoName, since: lastThreeMonths.toISOString() },
180+
}),
181+
headers: {
182+
Authorization: `Bearer ${accessToken}`,
183+
"Content-Type": "application/json",
184+
},
185+
})
186+
187+
if (!response.ok) {
188+
console.warn(
189+
`Error fetching GitHub stats for ${owner}/${repoName}. Status: ${response.status}`
190+
)
191+
return undefined
192+
}
193+
194+
const responseJson: GitHubStatsFetchResponse = await response.json()
190195

191-
if (responseJson && "data" in responseJson) {
192-
const repositoryOwner = responseJson.data.repositoryOwner
193-
if (!repositoryOwner) {
194-
throw `No GitHub user found for ${owner}/${repoName}`
196+
if ("errors" in responseJson) {
197+
console.warn(
198+
`GitHub GraphQL errors for ${owner}/${repoName}:`,
199+
responseJson.errors
200+
)
201+
return undefined
195202
}
196-
const { repository: repo } = repositoryOwner
197-
console.log("repo:", repo.tags)
203+
204+
const repo = responseJson.data.repositoryOwner?.repository
198205
if (!repo) {
199-
throw `No GitHub repo found ${owner}/${repoName}`
206+
console.warn(`No GitHub repository found for ${owner}/${repoName}`)
207+
return undefined
200208
}
201-
const stars = repo.stargazers.totalCount
202-
const commitHistory = repo.defaultBranchRef.target.history.edges
203209

204-
let hasCommitsInLast3Months = false
205-
commitHistory.forEach(commit => {
206-
if (!commit.node.author.name.match(/bot/i)) {
207-
hasCommitsInLast3Months = true
208-
}
209-
})
210-
const formattedStars = numbro(stars).format({
210+
const hasCommitsInLast3Months =
211+
repo.defaultBranchRef.target.history.edges.some(
212+
edge => new Date(edge.node.pushedDate) > lastThreeMonths
213+
)
214+
const formattedStars = numbro(repo.stargazers.totalCount).format({
211215
average: true,
212216
})
213-
const releases: Release[] = []
214-
if (
215-
repo.tags &&
216-
repo.tags.nodes &&
217-
repo.tags.nodes.length &&
218-
repo.tags.nodes[0].target.target &&
219-
repo.tags.nodes[0].target.target.pushedDate
220-
) {
217+
218+
const lastRelease = getLastRelease(repo)
219+
220+
return {
221+
hasCommitsInLast3Months,
222+
stars: repo.stargazers.totalCount,
223+
formattedStars,
224+
license: repo.licenseInfo?.name ?? "Unknown",
225+
lastRelease: lastRelease?.date ?? "",
226+
formattedLastRelease: lastRelease?.formattedDate ?? "",
227+
}
228+
} catch (error) {
229+
console.error(`Exception fetching GitHub stats for ${githubRepo}:`, error)
230+
return undefined
231+
}
232+
}
233+
234+
function getLastRelease(repo: any): Release | undefined {
235+
const releases: Release[] = []
236+
237+
repo.tags.nodes.forEach((node: any) => {
238+
if (node.target.target?.pushedDate) {
221239
releases.push({
222-
date: repo.tags.nodes[0].target.target.pushedDate,
223-
formattedDate: timeago(repo.tags.nodes[0].target.target.pushedDate),
240+
date: node.target.target.pushedDate,
241+
formattedDate: timeago(node.target.target.pushedDate),
224242
})
225243
}
226-
if (repo.releases && repo.releases.nodes && repo.releases.nodes.length) {
244+
})
245+
246+
repo.releases.nodes.forEach((node: any) => {
247+
if (node.publishedAt) {
227248
releases.push({
228-
date: repo.releases.nodes[0].publishedAt,
229-
formattedDate: timeago(repo.releases.nodes[0].publishedAt),
249+
date: node.publishedAt,
250+
formattedDate: timeago(node.publishedAt),
230251
})
231252
}
232-
if (owner.includes("graphql")) {
233-
console.log({ releases, repoName })
234-
}
235-
console.log("releases", releases)
253+
})
236254

237-
const lastRelease = releases.filter(Boolean).sort().reverse()[0]
238-
return {
239-
hasCommitsInLast3Months,
240-
stars,
241-
formattedStars,
242-
license: repo.licenseInfo && repo.licenseInfo.name,
243-
lastRelease: lastRelease ? lastRelease.date : "",
244-
formattedLastRelease: lastRelease ? lastRelease.formattedDate : "",
245-
}
246-
} else {
247-
console.warn(
248-
`Get invalid response from GitHub for ${owner}/${repoName}. Response: ${JSON.stringify(
249-
responseJson
250-
)}`
251-
)
252-
}
255+
return releases.sort(
256+
(a, b) => new Date(b.date).getTime() - new Date(a.date).getTime()
257+
)[0]
253258
}

scripts/sort-libraries/get-http-score.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,29 @@ type HttpScoreFetchResponse = {
66
}
77

88
export async function getHttpScore(packageName: string): Promise<number> {
9-
const response = await fetch(
10-
`https://raw.githubusercontent.com/graphql/graphql-http/main/implementations/${encodeURIComponent(
9+
try {
10+
const url = `https://raw.githubusercontent.com/graphql/graphql-http/main/implementations/${encodeURIComponent(
1111
packageName
1212
)}/report.json`
13-
)
14-
if (!response.ok) {
15-
console.warn(
16-
`Get invalid response from HTTP score for ${packageName}. Status: ${response.status}`
17-
)
18-
return 0
19-
}
20-
const responseJson: HttpScoreFetchResponse = await response.json()
21-
if (!responseJson) {
22-
console.warn(`Get invalid response from HTTP score for ${packageName}`)
13+
const response = await fetch(url)
14+
15+
if (!response.ok) {
16+
if (response.status === 404) {
17+
console.warn(
18+
`Resource not found for package ${packageName} at URL: ${url}`
19+
)
20+
} else {
21+
console.warn(
22+
`invalid response from HTTP score for ${packageName}. Status: ${response.status}`
23+
)
24+
}
25+
return 0
26+
}
27+
28+
const responseJson: HttpScoreFetchResponse = await response.json()
29+
return responseJson.total ?? 0
30+
} catch (error) {
31+
console.error(`Error fetching HTTP score for ${packageName}:`, error)
2332
return 0
2433
}
25-
return responseJson.total ?? 0
2634
}

scripts/sort-libraries/get-npm-stats.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,29 @@ type NpmStatsFetchResponse =
88
| { error: string }
99

1010
export async function getNpmStats(packageName: string): Promise<number> {
11-
const response = await fetch(
12-
`https://api.npmjs.org/downloads/point/last-week/${encodeURIComponent(
13-
packageName
14-
)}`
15-
)
16-
if (response.ok) {
11+
try {
12+
const response = await fetch(
13+
`https://api.npmjs.org/downloads/point/last-week/${encodeURIComponent(
14+
packageName
15+
)}`
16+
)
17+
18+
if (!response.ok) {
19+
console.warn(
20+
`Error fetching NPM stats for ${packageName}. Status: ${response.status}`
21+
)
22+
return 0
23+
}
24+
1725
const responseJson: NpmStatsFetchResponse = await response.json()
18-
if (responseJson && "downloads" in responseJson) {
19-
return responseJson.downloads ?? 0
20-
} else {
21-
console.warn(`Get invalid response from npm for ${packageName}`)
26+
if ("error" in responseJson) {
27+
console.warn(`NPM Stats error for ${packageName}: ${responseJson.error}`)
28+
return 0
2229
}
30+
31+
return responseJson.downloads ?? 0
32+
} catch (error) {
33+
console.error(`Exception fetching NPM stats for ${packageName}:`, error)
34+
return 0
2335
}
24-
return 0
2536
}

0 commit comments

Comments
 (0)