address conflicting uploads

This commit is contained in:
softprops 2021-08-08 00:19:31 -04:00
parent ea51250dd0
commit 6afa5259e8
3 changed files with 44 additions and 34 deletions

32
dist/index.js vendored
View file

@ -2147,8 +2147,9 @@ function run() {
if (files.length == 0) { if (files.length == 0) {
console.warn(`🤔 ${config.input_files} not include valid file.`); 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 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 => { }))).catch(error => {
throw error; throw error;
}); });
@ -2500,8 +2501,18 @@ exports.asset = (path) => {
exports.mimeOrDefault = (path) => { exports.mimeOrDefault = (path) => {
return mime_1.getType(path) || "application/octet-stream"; return mime_1.getType(path) || "application/octet-stream";
}; };
exports.upload = (ghToken, url, path) => __awaiter(void 0, void 0, void 0, function* () { exports.upload = (config, github, url, path, currentAssets) => __awaiter(void 0, void 0, void 0, function* () {
let { name, size, mime, data: body } = exports.asset(path); 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}...`); console.log(`⬆️ Uploading ${name}...`);
const endpoint = new URL(url); const endpoint = new URL(url);
endpoint.searchParams.append("name", name); endpoint.searchParams.append("name", name);
@ -2510,28 +2521,17 @@ exports.upload = (ghToken, url, path) => __awaiter(void 0, void 0, void 0, funct
headers: { headers: {
"content-length": `${size}`, "content-length": `${size}`,
"content-type": mime, "content-type": mime,
authorization: `token ${ghToken}` authorization: `token ${config.github_token}`
}, },
method: "POST", method: "POST",
body body
}); });
console.log(`resp headers`, resp.headers);
console.log(resp.status);
const json = yield resp.json(); const json = yield resp.json();
console.log(`body`, json); console.log(`body`, json);
if (resp.status !== 201) { 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 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* () { exports.release = (config, releaser, maxRetries = 3) => __awaiter(void 0, void 0, void 0, function* () {
var e_1, _a; var e_1, _a;

View file

@ -24,6 +24,7 @@ export interface Release {
target_commitish: string; target_commitish: string;
draft: boolean; draft: boolean;
prerelease: boolean; prerelease: boolean;
assets: Array<{ id: number; name: string }>;
} }
export interface Releaser { export interface Releaser {
@ -128,11 +129,25 @@ export const mimeOrDefault = (path: string): string => {
}; };
export const upload = async ( export const upload = async (
ghToken: string, config: Config,
github: GitHub,
url: string, url: string,
path: string path: string,
currentAssets: Array<{ id: number; name: string }>
): Promise<any> => { ): Promise<any> => {
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}...`); console.log(`⬆️ Uploading ${name}...`);
const endpoint = new URL(url); const endpoint = new URL(url);
endpoint.searchParams.append("name", name); endpoint.searchParams.append("name", name);
@ -141,31 +156,19 @@ export const upload = async (
headers: { headers: {
"content-length": `${size}`, "content-length": `${size}`,
"content-type": mime, "content-type": mime,
authorization: `token ${ghToken}` authorization: `token ${config.github_token}`
}, },
method: "POST", method: "POST",
body body
}); });
console.log(`resp headers`, resp.headers);
console.log(resp.status);
const json = await resp.json(); const json = await resp.json();
console.log(`body`, json); console.log(`body`, json);
if (resp.status !== 201) { if (resp.status !== 201) {
throw new Error( 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 json;
// return await gh.rest.repos.uploadReleaseAsset({
// url,
// headers: {
// "content-length": size,
// "content-type": mime,
// },
// name,
// file,
// });
}; };
export const release = async ( export const release = async (

View file

@ -65,9 +65,16 @@ async function run() {
if (files.length == 0) { if (files.length == 0) {
console.warn(`🤔 ${config.input_files} not include valid file.`); console.warn(`🤔 ${config.input_files} not include valid file.`);
} }
const currentAsserts = rel.assets;
await Promise.all( await Promise.all(
files.map(async path => { 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 => { ).catch(error => {
throw error; throw error;