-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathgithubReleases.ts
More file actions
88 lines (76 loc) · 2.53 KB
/
Copy pathgithubReleases.ts
File metadata and controls
88 lines (76 loc) · 2.53 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
import { defineCollection } from "astro:content";
import { z } from "astro:schema";
const githubRelaseApiSchema = z.object({
id: z.number(),
name: z.string().nullable(),
tag_name: z.string(),
body: z.string().nullable(),
draft: z.boolean(),
prerelease: z.boolean(),
published_at: z.string().nullable(),
html_url: z.string(),
});
const githubReleaseCollectionSchema = z.object({
id: z.string(),
name: z.string(),
body: z.string(),
html_url: z.string(),
published_at: z.string(),
});
export const githubReleases = defineCollection({
loader: async () => {
if (
import.meta.env.VERCEL === undefined &&
import.meta.env.FORCE_GITHUB_RELEASES === undefined
) {
console.warn(`
GitHub releases collection not fetched, as this isn't running on vercel nor the fetch being forced.
Rerun with the env variable FORCE_GITHUB_RELEASES=1
`);
return [];
}
const endpoint =
"https://api.github.com/repos/NomicFoundation/hardhat/releases";
console.log("Fetching from GitHub releases endpoint:", endpoint);
const response = await fetch(endpoint);
if (!response.ok) {
console.error(
`GitHub API error: ${response.status} ${response.statusText}`,
);
const text = await response.text();
console.error("Response body:", text);
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
const releases = z.array(githubRelaseApiSchema).parse(data);
return (
releases
.filter((release) => !release.draft && !release.prerelease)
// We only show Hardhat 3 (core) releases in the landing, so we filter
// by tag_name, assuming that our release process is consistent with
// the `hardhat@3.x.y` format.
.filter((release) => release.tag_name.startsWith("hardhat@3."))
.slice(0, 3)
.map((release) => {
// We use the name if any, otherwise "hardhat x.y.z", based on the
// tag_name.
const name =
release.name != null && release.name !== ""
? release.name
: release.tag_name.replace("@", " ");
// We only keep the body until the first ### subtitle.
const body =
release.body != null && release.body !== ""
? release.body.split("###")[0]?.trim()
: "";
return {
...release,
id: `${release.id}`,
name,
body,
};
})
);
},
schema: githubReleaseCollectionSchema,
});