electron/script/release/uploaders/upload-to-github.js

88 lines
2.7 KiB
JavaScript
Raw Normal View History

2020-03-20 20:28:31 +00:00
if (!process.env.CI) require('dotenv-safe').load();
2018-07-11 18:02:03 +00:00
2020-03-20 20:28:31 +00:00
const fs = require('fs');
const { Octokit } = require('@octokit/rest');
const octokit = new Octokit({
auth: process.env.ELECTRON_GITHUB_TOKEN
2020-03-20 20:28:31 +00:00
});
if (process.argv.length < 6) {
2020-03-20 20:28:31 +00:00
console.log('Usage: upload-to-github filePath fileName releaseId');
process.exit(1);
}
2020-03-20 20:28:31 +00:00
const filePath = process.argv[2];
const fileName = process.argv[3];
const releaseId = process.argv[4];
const releaseVersion = process.argv[5];
const getHeaders = (filePath, fileName) => {
2020-03-20 20:28:31 +00:00
const extension = fileName.split('.').pop();
const size = fs.statSync(filePath).size;
const options = {
json: 'text/json',
zip: 'application/zip',
txt: 'text/plain',
ts: 'application/typescript'
2020-03-20 20:28:31 +00:00
};
return {
'content-type': options[extension],
'content-length': size
2020-03-20 20:28:31 +00:00
};
};
2020-03-20 20:28:31 +00:00
const targetRepo = releaseVersion.indexOf('nightly') > 0 ? 'nightlies' : 'electron';
const uploadUrl = `https://uploads.github.com/repos/electron/${targetRepo}/releases/${releaseId}/assets{?name,label}`;
let retry = 0;
function uploadToGitHub () {
octokit.repos.uploadReleaseAsset({
url: uploadUrl,
headers: getHeaders(filePath, fileName),
file: fs.createReadStream(filePath),
name: fileName
}).then(() => {
2020-03-20 20:28:31 +00:00
console.log(`Successfully uploaded ${fileName} to GitHub.`);
process.exit();
}).catch((err) => {
if (retry < 4) {
2020-03-20 20:28:31 +00:00
console.log(`Error uploading ${fileName} to GitHub, will retry. Error was:`, err);
retry++;
octokit.repos.listAssetsForRelease({
owner: 'electron',
repo: targetRepo,
release_id: releaseId,
per_page: 100
}).then(assets => {
2020-03-20 20:28:31 +00:00
console.log('Got list of assets for existing release:');
console.log(JSON.stringify(assets.data, null, ' '));
const existingAssets = assets.data.filter(asset => asset.name === fileName);
2017-10-18 20:49:32 +00:00
if (existingAssets.length > 0) {
2020-03-20 20:28:31 +00:00
console.log(`${fileName} already exists; will delete before retrying upload.`);
octokit.repos.deleteReleaseAsset({
owner: 'electron',
repo: targetRepo,
asset_id: existingAssets[0].id
}).catch((deleteErr) => {
2020-03-20 20:28:31 +00:00
console.log(`Failed to delete existing asset ${fileName}. Error was:`, deleteErr);
}).then(uploadToGitHub);
2017-10-18 20:49:32 +00:00
} else {
2020-03-20 20:28:31 +00:00
console.log(`Current asset ${fileName} not found in existing assets; retrying upload.`);
uploadToGitHub();
2017-10-18 20:49:32 +00:00
}
}).catch((getReleaseErr) => {
2020-03-20 20:28:31 +00:00
console.log('Fatal: Unable to get current release assets via getRelease! Error was:', getReleaseErr);
});
} else {
2020-03-20 20:28:31 +00:00
console.log(`Error retrying uploading ${fileName} to GitHub:`, err);
process.exitCode = 1;
}
2020-03-20 20:28:31 +00:00
});
}
2020-03-20 20:28:31 +00:00
uploadToGitHub();