2018-08-29 18:13:22 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
if (!process.env.CI) require('dotenv-safe').load();
|
2018-08-29 18:13:22 +00:00
|
|
|
const args = require('minimist')(process.argv.slice(2), {
|
2018-12-14 23:15:16 +00:00
|
|
|
string: ['tag', 'releaseID'],
|
|
|
|
default: { releaseID: '' }
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2020-08-05 15:59:52 +00:00
|
|
|
const { Octokit } = require('@octokit/rest');
|
2018-08-29 18:13:22 +00:00
|
|
|
|
2020-08-05 15:59:52 +00:00
|
|
|
const octokit = new Octokit({
|
2019-05-08 01:48:40 +00:00
|
|
|
auth: process.env.ELECTRON_GITHUB_TOKEN
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2018-08-29 18:13:22 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
require('colors');
|
|
|
|
const pass = '✓'.green;
|
|
|
|
const fail = '✗'.red;
|
2018-08-29 18:13:22 +00:00
|
|
|
|
2018-11-28 04:12:01 +00:00
|
|
|
async function deleteDraft (releaseId, targetRepo) {
|
2018-08-29 18:13:22 +00:00
|
|
|
try {
|
2019-01-08 20:05:58 +00:00
|
|
|
const result = await octokit.repos.getRelease({
|
2018-08-29 18:13:22 +00:00
|
|
|
owner: 'electron',
|
|
|
|
repo: targetRepo,
|
2019-01-08 20:05:58 +00:00
|
|
|
release_id: parseInt(releaseId, 10)
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-01-04 19:26:14 +00:00
|
|
|
if (!result.data.draft) {
|
2020-03-20 20:28:31 +00:00
|
|
|
console.log(`${fail} published releases cannot be deleted.`);
|
|
|
|
return false;
|
2018-08-29 18:13:22 +00:00
|
|
|
} else {
|
2019-01-08 20:05:58 +00:00
|
|
|
await octokit.repos.deleteRelease({
|
2018-08-29 18:13:22 +00:00
|
|
|
owner: 'electron',
|
|
|
|
repo: targetRepo,
|
2019-01-04 19:26:14 +00:00
|
|
|
release_id: result.data.id
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2018-08-29 18:13:22 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
console.log(`${pass} successfully deleted draft with id ${releaseId} from ${targetRepo}`);
|
|
|
|
return true;
|
2018-08-29 18:13:22 +00:00
|
|
|
} catch (err) {
|
2020-03-20 20:28:31 +00:00
|
|
|
console.error(`${fail} couldn't delete draft with id ${releaseId} from ${targetRepo}: `, err);
|
|
|
|
return false;
|
2018-08-29 18:13:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function deleteTag (tag, targetRepo) {
|
|
|
|
try {
|
2019-01-08 20:05:58 +00:00
|
|
|
await octokit.git.deleteRef({
|
2018-08-29 18:13:22 +00:00
|
|
|
owner: 'electron',
|
|
|
|
repo: targetRepo,
|
2019-01-10 21:42:42 +00:00
|
|
|
ref: `tags/${tag}`
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
console.log(`${pass} successfully deleted tag ${tag} from ${targetRepo}`);
|
2018-08-29 18:13:22 +00:00
|
|
|
} catch (err) {
|
2020-03-20 20:28:31 +00:00
|
|
|
console.log(`${fail} couldn't delete tag ${tag} from ${targetRepo}: `, err);
|
2018-08-29 18:13:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function cleanReleaseArtifacts () {
|
2020-03-20 20:28:31 +00:00
|
|
|
const releaseId = args.releaseID.length > 0 ? args.releaseID : null;
|
|
|
|
const isNightly = args.tag.includes('nightly');
|
2018-08-29 18:13:22 +00:00
|
|
|
|
2018-11-30 17:58:09 +00:00
|
|
|
if (releaseId) {
|
|
|
|
if (isNightly) {
|
2020-03-20 20:28:31 +00:00
|
|
|
await deleteDraft(releaseId, 'nightlies');
|
2019-01-18 22:00:15 +00:00
|
|
|
|
2020-03-10 03:13:10 +00:00
|
|
|
// We only need to delete the Electron tag since the
|
|
|
|
// nightly tag is only created at publish-time.
|
2020-03-20 20:28:31 +00:00
|
|
|
await deleteTag(args.tag, 'electron');
|
2018-11-30 17:58:09 +00:00
|
|
|
} else {
|
2020-03-20 20:28:31 +00:00
|
|
|
const deletedElectronDraft = await deleteDraft(releaseId, 'electron');
|
2018-11-30 17:58:09 +00:00
|
|
|
// don't delete tag unless draft deleted successfully
|
|
|
|
if (deletedElectronDraft) {
|
2020-03-20 20:28:31 +00:00
|
|
|
await deleteTag(args.tag, 'electron');
|
2018-11-30 17:58:09 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-18 22:00:15 +00:00
|
|
|
} else {
|
|
|
|
await Promise.all([
|
|
|
|
deleteTag(args.tag, 'electron'),
|
|
|
|
deleteTag(args.tag, 'nightlies')
|
2020-03-20 20:28:31 +00:00
|
|
|
]);
|
2018-08-29 18:13:22 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
console.log(`${pass} failed release artifact cleanup complete`);
|
2018-08-29 18:13:22 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
cleanReleaseArtifacts();
|