(
config: Config,
releaser: Releaser,
url: string,
path: string,
currentAssets: Array<{ id: number; name: string; label?: string | null }>,
)
| 302 | : `Cannot upload asset ${name} to an immutable release. GitHub only allows asset uploads before a release is published, so upload assets to a draft release before you publish it.`; |
| 303 | |
| 304 | export const upload = async ( |
| 305 | config: Config, |
| 306 | releaser: Releaser, |
| 307 | url: string, |
| 308 | path: string, |
| 309 | currentAssets: Array<{ id: number; name: string; label?: string | null }>, |
| 310 | ): Promise<any> => { |
| 311 | const [owner, repo] = config.github_repository.split('/'); |
| 312 | const { name, mime, size } = asset(path); |
| 313 | const releaseIdMatch = url.match(/\/releases\/(\d+)\/assets/); |
| 314 | const releaseId = releaseIdMatch ? Number(releaseIdMatch[1]) : undefined; |
| 315 | const currentAsset = currentAssets.find( |
| 316 | // GitHub can rewrite uploaded asset names, so compare against both the raw name |
| 317 | // GitHub returns and the restored label we set when available. |
| 318 | (currentAsset) => releaseAssetMatchesName(name, currentAsset), |
| 319 | ); |
| 320 | if (currentAsset) { |
| 321 | if (config.input_overwrite_files === false) { |
| 322 | console.log(`Asset ${name} already exists and overwrite_files is false...`); |
| 323 | return null; |
| 324 | } else { |
| 325 | console.log(`♻️ Deleting previously uploaded asset ${name}...`); |
| 326 | await releaser.deleteReleaseAsset({ |
| 327 | asset_id: currentAsset.id || 1, |
| 328 | owner, |
| 329 | repo, |
| 330 | }); |
| 331 | } |
| 332 | } |
| 333 | console.log(`⬆️ Uploading ${name}...`); |
| 334 | const endpoint = new URL(url); |
| 335 | endpoint.searchParams.append('name', name); |
| 336 | const findReleaseAsset = async ( |
| 337 | matches: (asset: { id: number; name: string; label?: string | null }) => boolean, |
| 338 | attempts: number = 3, |
| 339 | ) => { |
| 340 | if (releaseId === undefined) { |
| 341 | return undefined; |
| 342 | } |
| 343 | |
| 344 | for (let attempt = 1; attempt <= attempts; attempt++) { |
| 345 | const latestAssets = await releaser.listReleaseAssets({ |
| 346 | owner, |
| 347 | repo, |
| 348 | release_id: releaseId, |
| 349 | }); |
| 350 | const latestAsset = latestAssets.find(matches); |
| 351 | if (latestAsset) { |
| 352 | return latestAsset; |
| 353 | } |
| 354 | |
| 355 | if (attempt < attempts) { |
| 356 | await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | return undefined; |
| 361 | }; |
no test coverage detected