-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathgithub.ts
More file actions
382 lines (323 loc) · 11.6 KB
/
github.ts
File metadata and controls
382 lines (323 loc) · 11.6 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import { mkdtemp, readFile, writeFile } from "fs/promises";
import { createWriteStream } from "fs";
import { join } from "path";
import { pipeline } from "stream/promises";
import nodefetch from "node-fetch";
import { fetch } from "./fetch";
import { exec } from "./shell";
export interface GitHubReleaseAsset {
id: string;
name: string;
browser_download_url: string;
size: number;
content_type: string;
digest: string;
}
export interface GitHubDraftRelease {
id: string;
tag_name: string;
name: string;
draft: boolean;
created_at: string;
published_at: string | null;
assets: GitHubReleaseAsset[];
}
interface GitHubReleaseInfo {
title: string;
tag: string;
target: string;
notes:
| {
text: string;
}
| {
file: string;
};
filesToRelease?: string;
isDraft?: boolean;
repo?: string;
}
interface GitHubPRInfo {
title: string;
body: string;
head: string;
base: string;
repo?: string;
}
export class GitHub {
authSet = false;
tmpPrefix = "gh-";
authToken: string = "";
owner = "mendix";
repo = "web-widgets";
setRepo(repo: string): void {
this.repo = repo;
}
getRepo(): string {
return this.repo;
}
async ensureAuth(): Promise<void> {
if (!this.authSet) {
if (process.env.GH_PAT) {
await exec(`echo "${process.env.GH_PAT}" | gh auth login --with-token`);
} else {
// No environment variables set, check if already authenticated
if (!(await this.isAuthenticated())) {
throw new Error(
"GitHub CLI is not authenticated. Please set GITHUB_TOKEN or GH_PAT environment variable, or run 'gh auth login'."
);
}
}
this.authSet = true;
}
}
private async isAuthenticated(): Promise<boolean> {
try {
// Try to run 'gh auth status' to check if authenticated
await exec("gh auth status", { stdio: "pipe" });
const { stdout: token } = await exec(`gh auth token`, { stdio: "pipe" });
this.authToken = token.trim();
return true;
} catch (_error: unknown) {
// If the command fails, the user is not authenticated
return false;
}
}
async createTempFile(): Promise<string> {
const dir = await mkdtemp(this.tmpPrefix);
return join(dir, "release-notes.txt");
}
async createGithubPRFrom(pr: GitHubPRInfo): Promise<void> {
await this.ensureAuth();
const repoArgument = pr.repo ? `--repo '${pr.repo}'` : "";
const command = [
`gh pr create`,
`--title '${pr.title}'`,
`--body '${pr.body}'`,
`--base '${pr.base}'`,
`--head '${pr.head}'`,
repoArgument
].join(" ");
await exec(command);
}
async createGithubReleaseFrom(params: GitHubReleaseInfo): Promise<void> {
const { notes, title, tag, filesToRelease = "", target, isDraft = false, repo } = params;
await this.ensureAuth();
const notesFilePath = "file" in notes ? notes.file : await this.createReleaseNotesFile(notes.text);
const targetHash = (await exec(`git rev-parse --verify ${target}`, { stdio: "pipe" })).stdout.trim();
const command = [
`gh release create`,
`--title '${title}'`,
`--notes-file '${notesFilePath}'`,
isDraft ? `--draft` : "",
repo ? `-R '${repo}'` : "",
`'${tag}'`,
`--target '${targetHash}'`,
filesToRelease ? `'${filesToRelease}'` : ""
]
.filter(str => str !== "")
.join(" ");
await exec(command);
}
get ghAPIHeaders(): Record<string, string> {
return {
"X-GitHub-Api-Version": "2022-11-28",
Authorization: `Bearer ${this.authToken || process.env.GH_PAT}`
};
}
async getReleaseIdByReleaseTag(releaseTag: string): Promise<string | undefined> {
console.log(`Searching for release from Github tag '${releaseTag}'`);
try {
const release =
(await fetch<{ id: string }>(
"GET",
`https://api.github.com/repos/${this.owner}/${this.repo}/releases/tags/${releaseTag}`,
undefined,
{ ...this.ghAPIHeaders }
)) ?? [];
if (!release) {
return undefined;
}
return release.id;
} catch (e) {
if (e instanceof Error && e.message.includes("404")) {
return undefined;
}
throw e;
}
}
async getMPKReleaseAssetUrl(releaseTag: string): Promise<string> {
const releaseId = await this.getReleaseIdByReleaseTag(releaseTag);
if (!releaseId) {
throw new Error(`Could not find release with tag '${releaseTag}' on GitHub`);
}
const artifacts = await this.listReleaseAssets(releaseId);
const downloadUrl = artifacts.find(asset => asset.name.endsWith(".mpk"))?.browser_download_url;
if (!downloadUrl) {
throw new Error(`Could not retrieve MPK url from GitHub release with tag ${process.env.TAG}`);
}
return downloadUrl;
}
async getDraftReleases(): Promise<GitHubDraftRelease[]> {
const releases = await fetch<GitHubDraftRelease[]>(
"GET",
`https://api.github.com/repos/${this.owner}/${this.repo}/releases`,
undefined,
{
...this.ghAPIHeaders
}
);
// Filter only draft releases
return releases.filter(release => release.draft);
}
async createReleaseNotesFile(releaseNotesText: string): Promise<string> {
const filePath = await this.createTempFile();
await writeFile(filePath, releaseNotesText);
return filePath;
}
private async triggerGithubWorkflow(params: {
workflowId: string;
ref: string;
inputs: Record<string, string>;
owner?: string;
repo?: string;
}): Promise<void> {
await this.ensureAuth();
const { workflowId, ref, inputs } = params;
// Convert inputs object to CLI parameters
const inputParams = Object.entries(inputs)
.map(([key, value]) => `-f ${key}=${value}`)
.join(" ");
const repoParam = `${this.owner}/${this.repo}`;
const command = [`gh workflow run`, `"${workflowId}"`, `--ref "${ref}"`, inputParams, `-R "${repoParam}"`]
.filter(Boolean)
.join(" ");
try {
await exec(command);
console.log(`Successfully triggered workflow '${workflowId}'`);
} catch (error) {
throw new Error(`Failed to trigger workflow '${workflowId}': ${error}`);
}
}
async triggerCreateReleaseWorkflow(packageName: string, ref = "main"): Promise<void> {
return this.triggerGithubWorkflow({
workflowId: "CreateGitHubRelease.yml",
ref,
inputs: {
package: packageName
}
});
}
async listReleaseAssets(releaseId: string): Promise<GitHubReleaseAsset[]> {
return fetch<GitHubReleaseAsset[]>(
"GET",
`https://api.github.com/repos/${this.owner}/${this.repo}/releases/${releaseId}/assets`,
undefined,
{
...this.ghAPIHeaders
}
);
}
async downloadReleaseAsset(assetId: string, destinationPath: string): Promise<void> {
await this.ensureAuth();
const url = `https://api.github.com/repos/${this.owner}/${this.repo}/releases/assets/${assetId}`;
try {
const response = await nodefetch(url, {
method: "GET",
headers: {
Accept: "application/octet-stream",
...this.ghAPIHeaders
},
redirect: "follow"
});
if (!response.ok) {
throw new Error(`Failed to download asset ${assetId}: ${response.status} ${response.statusText}`);
}
if (!response.body) {
throw new Error(`No response body received for asset ${assetId}`);
}
// Stream the response body to the file
const fileStream = createWriteStream(destinationPath);
await pipeline(response.body, fileStream);
} catch (error) {
throw new Error(
`Failed to download release asset ${assetId}: ${error instanceof Error ? error.message : String(error)}`
);
}
}
/**
* Delete a release asset by ID
*/
async deleteReleaseAsset(assetId: string): Promise<void> {
await this.ensureAuth();
const response = await nodefetch(
`https://api.github.com/repos/${this.owner}/${this.repo}/releases/assets/${assetId}`,
{
method: "DELETE",
headers: this.ghAPIHeaders
}
);
if (!response.ok) {
throw new Error(`Failed to delete asset ${assetId}: ${response.status} ${response.statusText}`);
}
}
/**
* Upload a new asset to a release
*/
async uploadReleaseAsset(releaseId: string, filePath: string, assetName: string): Promise<GitHubReleaseAsset> {
await this.ensureAuth();
// Get release info to get upload URL
const release = await fetch<{ upload_url: string }>(
"GET",
`https://api.github.com/repos/${this.owner}/${this.repo}/releases/${releaseId}`,
undefined,
this.ghAPIHeaders
);
// The upload_url comes with {?name,label} template, we need to replace it
const uploadUrl = release.upload_url.replace(/\{[^}]+}/g, "") + `?name=${encodeURIComponent(assetName)}`;
// Read the file
const fileBuffer = await readFile(filePath);
// Upload the file
const response = await nodefetch(uploadUrl, {
method: "POST",
headers: {
...this.ghAPIHeaders,
"Content-Type": "application/octet-stream",
"Content-Length": fileBuffer.length.toString()
},
body: fileBuffer
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(
`Failed to upload asset ${assetName}: ${response.status} ${response.statusText} - ${errorText}`
);
}
return (await response.json()) as GitHubReleaseAsset;
}
/**
* Update a release asset's name
*/
async updateReleaseAsset(assetId: string, newName: string): Promise<GitHubReleaseAsset> {
await this.ensureAuth();
const response = await nodefetch(
`https://api.github.com/repos/${this.owner}/${this.repo}/releases/assets/${assetId}`,
{
method: "PATCH",
headers: {
...this.ghAPIHeaders,
"Content-Type": "application/json"
},
body: JSON.stringify({ name: newName })
}
);
if (!response.ok) {
const errorText = await response.text();
throw new Error(
`Failed to update asset ${assetId}: ${response.status} ${response.statusText} - ${errorText}`
);
}
return (await response.json()) as GitHubReleaseAsset;
}
}
export const gh = new GitHub();