From 6afa5259e837d84fc4612bb1abde4623627dc757 Mon Sep 17 00:00:00 2001 From: softprops Date: Sun, 8 Aug 2021 00:19:31 -0400 Subject: [PATCH] address conflicting uploads --- dist/index.js | 32 ++++++++++++++++---------------- src/github.ts | 37 ++++++++++++++++++++----------------- src/main.ts | 9 ++++++++- 3 files changed, 44 insertions(+), 34 deletions(-) diff --git a/dist/index.js b/dist/index.js index 9661a4c..b106c10 100644 --- a/dist/index.js +++ b/dist/index.js @@ -2147,8 +2147,9 @@ function run() { if (files.length == 0) { console.warn(`🤔 ${config.input_files} not include valid file.`); } + const currentAsserts = rel.assets; yield Promise.all(files.map((path) => __awaiter(this, void 0, void 0, function* () { - yield github_1.upload(config.github_token, util_1.uploadUrl(rel.upload_url), path); + yield github_1.upload(config, gh, util_1.uploadUrl(rel.upload_url), path, currentAsserts); }))).catch(error => { throw error; }); @@ -2500,8 +2501,18 @@ exports.asset = (path) => { exports.mimeOrDefault = (path) => { return mime_1.getType(path) || "application/octet-stream"; }; -exports.upload = (ghToken, url, path) => __awaiter(void 0, void 0, void 0, function* () { - let { name, size, mime, data: body } = exports.asset(path); +exports.upload = (config, github, url, path, currentAssets) => __awaiter(void 0, void 0, void 0, function* () { + const [owner, repo] = config.github_repository.split("/"); + const { name, size, mime, data: body } = exports.asset(path); + const currentAsset = currentAssets.find(({ name: currentName }) => currentName == name); + if (currentAsset) { + console.log(`Deleting previously uploadeed asset ${name}...`); + yield github.rest.repos.deleteReleaseAsset({ + asset_id: currentAsset.id || 1, + owner, + repo + }); + } console.log(`⬆️ Uploading ${name}...`); const endpoint = new URL(url); endpoint.searchParams.append("name", name); @@ -2510,28 +2521,17 @@ exports.upload = (ghToken, url, path) => __awaiter(void 0, void 0, void 0, funct headers: { "content-length": `${size}`, "content-type": mime, - authorization: `token ${ghToken}` + authorization: `token ${config.github_token}` }, method: "POST", body }); - console.log(`resp headers`, resp.headers); - console.log(resp.status); const json = yield resp.json(); console.log(`body`, json); if (resp.status !== 201) { - throw new Error("failed to upload release asset ${name}. recieved status code ${resp.status}\n${json}"); + throw new Error("Failed to upload release asset ${name}. recieved status code ${resp.status}\n${json.message}\n${json.errors}"); } return json; - // return await gh.rest.repos.uploadReleaseAsset({ - // url, - // headers: { - // "content-length": size, - // "content-type": mime, - // }, - // name, - // file, - // }); }); exports.release = (config, releaser, maxRetries = 3) => __awaiter(void 0, void 0, void 0, function* () { var e_1, _a; diff --git a/src/github.ts b/src/github.ts index be66323..db537e7 100644 --- a/src/github.ts +++ b/src/github.ts @@ -24,6 +24,7 @@ export interface Release { target_commitish: string; draft: boolean; prerelease: boolean; + assets: Array<{ id: number; name: string }>; } export interface Releaser { @@ -128,11 +129,25 @@ export const mimeOrDefault = (path: string): string => { }; export const upload = async ( - ghToken: string, + config: Config, + github: GitHub, url: string, - path: string + path: string, + currentAssets: Array<{ id: number; name: string }> ): Promise => { - let { name, size, mime, data: body } = asset(path); + const [owner, repo] = config.github_repository.split("/"); + const { name, size, mime, data: body } = asset(path); + const currentAsset = currentAssets.find( + ({ name: currentName }) => currentName == name + ); + if (currentAsset) { + console.log(`Deleting previously uploadeed asset ${name}...`); + await github.rest.repos.deleteReleaseAsset({ + asset_id: currentAsset.id || 1, + owner, + repo + }); + } console.log(`⬆️ Uploading ${name}...`); const endpoint = new URL(url); endpoint.searchParams.append("name", name); @@ -141,31 +156,19 @@ export const upload = async ( headers: { "content-length": `${size}`, "content-type": mime, - authorization: `token ${ghToken}` + authorization: `token ${config.github_token}` }, method: "POST", body }); - console.log(`resp headers`, resp.headers); - console.log(resp.status); const json = await resp.json(); console.log(`body`, json); if (resp.status !== 201) { throw new Error( - "failed to upload release asset ${name}. recieved status code ${resp.status}\n${json}" + "Failed to upload release asset ${name}. recieved status code ${resp.status}\n${json.message}\n${json.errors}" ); } return json; - - // return await gh.rest.repos.uploadReleaseAsset({ - // url, - // headers: { - // "content-length": size, - // "content-type": mime, - // }, - // name, - // file, - // }); }; export const release = async ( diff --git a/src/main.ts b/src/main.ts index 1003994..df3d703 100644 --- a/src/main.ts +++ b/src/main.ts @@ -65,9 +65,16 @@ async function run() { if (files.length == 0) { console.warn(`🤔 ${config.input_files} not include valid file.`); } + const currentAsserts = rel.assets; await Promise.all( files.map(async path => { - await upload(config.github_token, uploadUrl(rel.upload_url), path); + await upload( + config, + gh, + uploadUrl(rel.upload_url), + path, + currentAsserts + ); }) ).catch(error => { throw error;