2020-11-09 21:57:53 +00:00
|
|
|
import { Octokit } from '@octokit/rest';
|
2023-06-22 14:21:42 +00:00
|
|
|
import * as fs from 'node:fs';
|
2019-01-08 20:05:58 +00:00
|
|
|
|
2020-08-05 15:59:52 +00:00
|
|
|
const octokit = new Octokit({
|
2023-09-28 15:15:52 +00:00
|
|
|
auth: process.env.ELECTRON_GITHUB_TOKEN,
|
|
|
|
log: console
|
2019-05-08 01:48:40 +00:00
|
|
|
});
|
2017-09-23 21:26:04 +00:00
|
|
|
|
2020-11-09 21:57:53 +00:00
|
|
|
if (!process.env.CI) require('dotenv-safe').load();
|
|
|
|
|
2018-08-16 15:57:12 +00:00
|
|
|
if (process.argv.length < 6) {
|
2017-10-05 20:31:54 +00:00
|
|
|
console.log('Usage: upload-to-github filePath fileName releaseId');
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2019-01-08 20:05:58 +00:00
|
|
|
|
2018-10-02 01:56:31 +00:00
|
|
|
const filePath = process.argv[2];
|
|
|
|
const fileName = process.argv[3];
|
2020-11-09 21:57:53 +00:00
|
|
|
const releaseId = parseInt(process.argv[4], 10);
|
2018-10-02 01:56:31 +00:00
|
|
|
const releaseVersion = process.argv[5];
|
2018-08-16 15:57:12 +00:00
|
|
|
|
2020-11-09 21:57:53 +00:00
|
|
|
if (isNaN(releaseId)) {
|
2023-07-25 16:08:46 +00:00
|
|
|
throw new TypeError('Provided release ID was not a valid integer');
|
2020-11-09 21:57:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const getHeaders = (filePath: string, fileName: string) => {
|
2019-01-08 20:05:58 +00:00
|
|
|
const extension = fileName.split('.').pop();
|
2020-11-09 21:57:53 +00:00
|
|
|
if (!extension) {
|
|
|
|
throw new Error(`Failed to get headers for extensionless file: ${fileName}`);
|
|
|
|
}
|
2024-02-28 00:55:08 +00:00
|
|
|
console.log(`About to get size of ${filePath}`);
|
2019-01-08 20:05:58 +00:00
|
|
|
const size = fs.statSync(filePath).size;
|
2024-02-28 00:55:08 +00:00
|
|
|
console.log(`Got size of ${filePath}: ${size}`);
|
2020-11-09 21:57:53 +00:00
|
|
|
const options: Record<string, string> = {
|
2020-03-20 15:12:18 +00:00
|
|
|
json: 'text/json',
|
|
|
|
zip: 'application/zip',
|
|
|
|
txt: 'text/plain',
|
|
|
|
ts: 'application/typescript'
|
2019-01-08 20:05:58 +00:00
|
|
|
};
|
2017-09-23 21:26:04 +00:00
|
|
|
|
2019-01-08 20:05:58 +00:00
|
|
|
return {
|
|
|
|
'content-type': options[extension],
|
|
|
|
'content-length': size
|
|
|
|
};
|
2017-09-23 21:26:04 +00:00
|
|
|
};
|
2017-10-04 14:33:27 +00:00
|
|
|
|
2019-01-08 20:05:58 +00:00
|
|
|
const targetRepo = releaseVersion.indexOf('nightly') > 0 ? 'nightlies' : 'electron';
|
|
|
|
const uploadUrl = `https://uploads.github.com/repos/electron/${targetRepo}/releases/${releaseId}/assets{?name,label}`;
|
2017-10-05 20:31:54 +00:00
|
|
|
let retry = 0;
|
2017-10-04 14:33:27 +00:00
|
|
|
|
|
|
|
function uploadToGitHub () {
|
2024-02-28 00:55:08 +00:00
|
|
|
console.log(`in uploadToGitHub for ${filePath}, ${fileName}`);
|
|
|
|
const fileData = fs.createReadStream(filePath);
|
|
|
|
console.log(`in uploadToGitHub, created readstream for ${filePath}`);
|
2019-01-08 20:05:58 +00:00
|
|
|
octokit.repos.uploadReleaseAsset({
|
|
|
|
url: uploadUrl,
|
|
|
|
headers: getHeaders(filePath, fileName),
|
2024-02-28 00:55:08 +00:00
|
|
|
data: fileData as any,
|
2020-11-09 21:57:53 +00:00
|
|
|
name: fileName,
|
|
|
|
owner: 'electron',
|
|
|
|
repo: targetRepo,
|
|
|
|
release_id: releaseId
|
2019-02-07 00:51:45 +00:00
|
|
|
}).then(() => {
|
|
|
|
console.log(`Successfully uploaded ${fileName} to GitHub.`);
|
|
|
|
process.exit();
|
2017-10-04 14:33:27 +00:00
|
|
|
}).catch((err) => {
|
2017-10-05 20:31:54 +00:00
|
|
|
if (retry < 4) {
|
2019-02-07 00:51:45 +00:00
|
|
|
console.log(`Error uploading ${fileName} to GitHub, will retry. Error was:`, err);
|
2017-10-05 20:31:54 +00:00
|
|
|
retry++;
|
2019-01-08 20:05:58 +00:00
|
|
|
|
2020-11-09 20:30:43 +00:00
|
|
|
octokit.repos.listReleaseAssets({
|
2019-01-08 20:05:58 +00:00
|
|
|
owner: 'electron',
|
|
|
|
repo: targetRepo,
|
2019-09-07 21:48:35 +00:00
|
|
|
release_id: releaseId,
|
|
|
|
per_page: 100
|
2019-01-08 20:05:58 +00:00
|
|
|
}).then(assets => {
|
2019-01-04 21:14:41 +00:00
|
|
|
console.log('Got list of assets for existing release:');
|
2019-01-08 20:05:58 +00:00
|
|
|
console.log(JSON.stringify(assets.data, null, ' '));
|
2019-02-07 00:51:45 +00:00
|
|
|
const existingAssets = assets.data.filter(asset => asset.name === fileName);
|
2019-01-08 20:05:58 +00:00
|
|
|
|
2017-10-18 20:49:32 +00:00
|
|
|
if (existingAssets.length > 0) {
|
|
|
|
console.log(`${fileName} already exists; will delete before retrying upload.`);
|
2019-02-07 00:51:45 +00:00
|
|
|
octokit.repos.deleteReleaseAsset({
|
|
|
|
owner: 'electron',
|
|
|
|
repo: targetRepo,
|
|
|
|
asset_id: existingAssets[0].id
|
|
|
|
}).catch((deleteErr) => {
|
2019-01-04 21:14:41 +00:00
|
|
|
console.log(`Failed to delete existing asset ${fileName}. Error was:`, deleteErr);
|
|
|
|
}).then(uploadToGitHub);
|
2017-10-18 20:49:32 +00:00
|
|
|
} else {
|
2019-01-04 21:14:41 +00:00
|
|
|
console.log(`Current asset ${fileName} not found in existing assets; retrying upload.`);
|
2017-10-18 20:49:32 +00:00
|
|
|
uploadToGitHub();
|
|
|
|
}
|
2019-01-04 21:14:41 +00:00
|
|
|
}).catch((getReleaseErr) => {
|
2020-03-20 15:12:18 +00:00
|
|
|
console.log('Fatal: Unable to get current release assets via getRelease! Error was:', getReleaseErr);
|
2017-10-18 20:49:32 +00:00
|
|
|
});
|
2017-10-04 14:33:27 +00:00
|
|
|
} else {
|
|
|
|
console.log(`Error retrying uploading ${fileName} to GitHub:`, err);
|
|
|
|
process.exitCode = 1;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
uploadToGitHub();
|