-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathbuildApi.ts
More file actions
223 lines (193 loc) · 4.83 KB
/
Copy pathbuildApi.ts
File metadata and controls
223 lines (193 loc) · 4.83 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
import { ApiClient, paths, handleApiError } from '../api'
import { BuildError, FileUploadError } from '../errors'
import { LogEntry } from './types'
import { tarFileStreamUpload } from './utils'
import stripAnsi from 'strip-ansi'
type RequestBuildInput = {
alias: string
cpuCount: number
memoryMB: number
}
type GetFileUploadLinkInput = {
templateID: string
filesHash: string
}
type TriggerBuildInput = {
templateID: string
buildID: string
template: TriggerBuildTemplate
}
type GetBuildStatusInput = {
templateID: string
buildID: string
logsOffset: number
}
export type GetBuildStatusResponse =
paths['/templates/{templateID}/builds/{buildID}/status']['get']['responses']['200']['content']['application/json']
export type TriggerBuildTemplate =
paths['/v2/templates/{templateID}/builds/{buildID}']['post']['requestBody']['content']['application/json']
export async function requestBuild(
client: ApiClient,
{ alias, cpuCount, memoryMB }: RequestBuildInput
) {
const requestBuildRes = await client.api.POST('/v2/templates', {
body: {
alias,
cpuCount,
memoryMB,
},
})
const error = handleApiError(requestBuildRes, BuildError)
if (error) {
throw error
}
if (!requestBuildRes.data) {
throw new BuildError('Failed to request build')
}
return requestBuildRes.data
}
export async function getFileUploadLink(
client: ApiClient,
{ templateID, filesHash }: GetFileUploadLinkInput
) {
const fileUploadLinkRes = await client.api.GET(
'/templates/{templateID}/files/{hash}',
{
params: {
path: {
templateID,
hash: filesHash,
},
},
}
)
const error = handleApiError(fileUploadLinkRes, FileUploadError)
if (error) {
throw error
}
if (!fileUploadLinkRes.data) {
throw new FileUploadError('Failed to get file upload link')
}
return fileUploadLinkRes.data
}
export async function uploadFile(options: {
fileName: string
fileContextPath: string
url: string
}) {
const { fileName, url, fileContextPath } = options
const { contentLength, uploadStream } = await tarFileStreamUpload(
fileName,
fileContextPath
)
// The compiler assumes this is Web fetch API, but it's actually Node.js fetch API
const res = await fetch(url, {
method: 'PUT',
// @ts-expect-error
body: uploadStream,
headers: {
'Content-Length': contentLength.toString(),
},
duplex: 'half',
})
if (!res.ok) {
throw new FileUploadError(
`Failed to upload file: ${res.statusText} ${res.status}`
)
}
}
export async function triggerBuild(
client: ApiClient,
{ templateID, buildID, template }: TriggerBuildInput
) {
const triggerBuildRes = await client.api.POST(
'/v2/templates/{templateID}/builds/{buildID}',
{
params: {
path: {
templateID,
buildID,
},
},
body: template,
}
)
const error = handleApiError(triggerBuildRes, BuildError)
if (error) {
throw error
}
}
export async function getBuildStatus(
client: ApiClient,
{ templateID, buildID, logsOffset }: GetBuildStatusInput
) {
const buildStatusRes = await client.api.GET(
'/templates/{templateID}/builds/{buildID}/status',
{
params: {
path: {
templateID,
buildID,
},
query: {
logsOffset,
},
},
}
)
const error = handleApiError(buildStatusRes, BuildError)
if (error) {
throw error
}
if (!buildStatusRes.data) {
throw new BuildError('Failed to get build status')
}
return buildStatusRes.data
}
export async function waitForBuildFinish(
client: ApiClient,
{
templateID,
buildID,
onBuildLogs,
logsRefreshFrequency,
}: {
templateID: string
buildID: string
onBuildLogs?: (logEntry: InstanceType<typeof LogEntry>) => void
logsRefreshFrequency: number
}
): Promise<void> {
let logsOffset = 0
let status: GetBuildStatusResponse['status'] = 'building'
while (status === 'building') {
const buildStatus = await getBuildStatus(client, {
templateID,
buildID,
logsOffset,
})
logsOffset += buildStatus.logEntries.length
buildStatus.logEntries.forEach(
(logEntry: GetBuildStatusResponse['logEntries'][number]) =>
onBuildLogs?.(
new LogEntry(
new Date(logEntry.timestamp),
logEntry.level,
stripAnsi(logEntry.message)
)
)
)
status = buildStatus.status
switch (status) {
case 'ready': {
return
}
case 'error': {
throw new BuildError(buildStatus?.reason?.message ?? 'Unknown error')
}
}
// Wait for a short period before checking the status again
await new Promise((resolve) => setTimeout(resolve, logsRefreshFrequency))
}
throw new BuildError('Unknown build error occurred.')
}