-
-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathsync-release.ts
More file actions
129 lines (109 loc) · 3.31 KB
/
Copy pathsync-release.ts
File metadata and controls
129 lines (109 loc) · 3.31 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
interface GitHubRelease {
tag_name: string;
assets: Array<{
name: string;
browser_download_url: string;
size: number;
}>;
}
interface Config {
githubToken: string;
githubRepo: string;
s3Bucket: string;
s3Endpoint: string;
awsAccessKeyId: string;
awsSecretAccessKey: string;
}
const config: Config = {
githubToken: process.env.GITHUB_TOKEN!,
githubRepo: process.env.GITHUB_REPO || "egoist/chatwise-releases",
s3Bucket: process.env.S3_BUCKET || "gh-releases",
s3Endpoint: process.env.S3_ENDPOINT!,
awsAccessKeyId: process.env.AWS_ACCESS_KEY_ID!,
awsSecretAccessKey: process.env.AWS_SECRET_ACCESS_KEY!,
};
const s3Client = new Bun.S3Client({
bucket: config.s3Bucket,
endpoint: config.s3Endpoint,
accessKeyId: config.awsAccessKeyId,
secretAccessKey: config.awsSecretAccessKey,
});
async function fetchReleases(): Promise<GitHubRelease[]> {
const response = await fetch(
`https://api.github.com/repos/${config.githubRepo}/releases`,
{
headers: {
Authorization: `Bearer ${config.githubToken}`,
"User-Agent": "sync-release",
},
}
);
if (!response.ok) {
throw new Error(
`GitHub API error: ${response.status} ${response.statusText}`
);
}
return (await response.json()) as any;
}
async function downloadAsset(url: string): Promise<ArrayBuffer> {
const response = await fetch(url, {
headers: {
Authorization: `Bearer ${config.githubToken}`,
"User-Agent": "sync-release",
},
});
if (!response.ok) {
throw new Error(
`Failed to download asset: ${response.status} ${response.statusText}`
);
}
return await response.arrayBuffer();
}
async function uploadToS3(
buffer: ArrayBufferLike,
key: string,
contentType?: string
): Promise<void> {
await s3Client.write(key, new Uint8Array(buffer), {
type: contentType || "application/octet-stream",
});
}
async function syncReleaseAssets(): Promise<void> {
try {
console.log("Fetching latest release...");
const releases = await fetchReleases();
const release = releases[0]!;
console.log(
`Found release: ${release.tag_name} with ${release.assets.length} assets`
);
await Promise.all(
release.assets.map(async (asset) => {
const s3Key = `${config.githubRepo}/${release.tag_name}/${asset.name}`;
console.log(`Downloading ${asset.name} (${asset.size} bytes)...`);
const buffer = await downloadAsset(asset.browser_download_url);
console.log(`Uploading to S3: ${s3Key}...`);
await uploadToS3(buffer, s3Key);
console.log(` Synced ${asset.name}`);
})
);
console.log(
`Successfully synced ${release.assets.length} assets from ${release.tag_name}`
);
async function syncReleaseJSON(type: "latest" | "all") {
const key = `${config.githubRepo}/${type}.json`;
console.log(`Uploading release metadata to S3: ${key}...`);
const buffer = new TextEncoder().encode(
JSON.stringify(type === "latest" ? release : releases)
);
await uploadToS3(buffer.buffer, key, "application/json");
console.log(`Synced ${key}`);
}
await Promise.all([syncReleaseJSON("all"), syncReleaseJSON("latest")]);
} catch (error) {
console.error("Sync failed:", error);
process.exit(1);
}
}
if (import.meta.main) {
syncReleaseAssets();
}