2018-08-29 18:13:22 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
if (!process.env.CI) require('dotenv-safe').load();
|
|
|
|
const args = require('minimist')(process.argv.slice(2), {
|
2018-12-14 23:15:16 +00:00
|
|
|
string: ['tag', 'releaseID'],
|
|
|
|
default: { releaseID: '' }
|
2018-08-29 18:13:22 +00:00
|
|
|
});
|
2020-08-05 15:59:52 +00:00
|
|
|
const { Octokit } = require('@octokit/rest');
|
2024-09-23 20:56:29 +00:00
|
|
|
const { createGitHubTokenStrategy } = require('./github-token');
|
2018-08-29 18:13:22 +00:00
|
|
|
|
2019-08-29 14:46:54 +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) {
|
2024-09-23 20:56:29 +00:00
|
|
|
const octokit = new Octokit({
|
|
|
|
authStrategy: createGitHubTokenStrategy(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)
|
2018-08-29 18:13:22 +00:00
|
|
|
});
|
2019-01-04 19:26:14 +00:00
|
|
|
if (!result.data.draft) {
|
2018-11-30 17:58:09 +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
|
2018-08-29 18:13:22 +00:00
|
|
|
});
|
|
|
|
}
|
2018-11-30 17:58:09 +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) {
|
2018-11-30 17:58:09 +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) {
|
2024-09-23 20:56:29 +00:00
|
|
|
const octokit = new Octokit({
|
|
|
|
authStrategy: createGitHubTokenStrategy(targetRepo)
|
|
|
|
});
|
|
|
|
|
2018-08-29 18:13:22 +00:00
|
|
|
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}`
|
2018-08-29 18:13:22 +00:00
|
|
|
});
|
2018-11-30 17:58:09 +00:00
|
|
|
console.log(`${pass} successfully deleted tag ${tag} from ${targetRepo}`);
|
2018-08-29 18:13:22 +00:00
|
|
|
} catch (err) {
|
2018-11-30 17:58:09 +00:00
|
|
|
console.log(`${fail} couldn't delete tag ${tag} from ${targetRepo}: `, err);
|
2018-08-29 18:13:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function cleanReleaseArtifacts () {
|
2018-12-14 23:15:16 +00:00
|
|
|
const releaseId = args.releaseID.length > 0 ? args.releaseID : null;
|
2018-10-19 03:20:43 +00:00
|
|
|
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-10 03:13:10 +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.
|
|
|
|
await deleteTag(args.tag, 'electron');
|
2018-11-30 17:58:09 +00:00
|
|
|
} else {
|
|
|
|
const deletedElectronDraft = await deleteDraft(releaseId, 'electron');
|
|
|
|
// don't delete tag unless draft deleted successfully
|
|
|
|
if (deletedElectronDraft) {
|
|
|
|
await deleteTag(args.tag, 'electron');
|
|
|
|
}
|
|
|
|
}
|
2019-01-18 22:00:15 +00:00
|
|
|
} else {
|
|
|
|
await Promise.all([
|
|
|
|
deleteTag(args.tag, 'electron'),
|
|
|
|
deleteTag(args.tag, 'nightlies')
|
|
|
|
]);
|
2018-08-29 18:13:22 +00:00
|
|
|
}
|
|
|
|
|
2018-11-30 17:58:09 +00:00
|
|
|
console.log(`${pass} failed release artifact cleanup complete`);
|
2018-08-29 18:13:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanReleaseArtifacts();
|