electron/script/release/release-artifact-cleanup.js

110 lines
3.3 KiB
JavaScript
Raw Normal View History

#!/usr/bin/env node
2020-03-20 20:28:31 +00:00
if (!process.env.CI) require('dotenv-safe').load();
const args = require('minimist')(process.argv.slice(2), {
string: ['tag', 'releaseID'],
default: { releaseID: '' }
2020-03-20 20:28:31 +00:00
});
const { execSync } = require('child_process');
const { GitProcess } = require('dugite');
const { getCurrentBranch, ELECTRON_DIR } = require('../lib/utils.js');
const { Octokit } = require('@octokit/rest');
const octokit = new Octokit({
auth: process.env.ELECTRON_GITHUB_TOKEN
2020-03-20 20:28:31 +00:00
});
2020-03-20 20:28:31 +00:00
require('colors');
const pass = '✓'.green;
const fail = '✗'.red;
function getLastBumpCommit (tag) {
2020-03-20 20:28:31 +00:00
const data = execSync(`git log -n1 --grep "Bump ${tag}" --format='format:{"hash": "%H", "message": "%s"}'`).toString();
return JSON.parse(data);
}
async function revertBumpCommit (tag) {
2020-03-20 20:28:31 +00:00
const branch = await getCurrentBranch();
const commitToRevert = getLastBumpCommit(tag).hash;
await GitProcess.exec(['revert', commitToRevert], ELECTRON_DIR);
const pushDetails = await GitProcess.exec(['push', 'origin', `HEAD:${branch}`, '--follow-tags'], ELECTRON_DIR);
if (pushDetails.exitCode === 0) {
2020-03-20 20:28:31 +00:00
console.log(`${pass} successfully reverted release commit.`);
} else {
2020-03-20 20:28:31 +00:00
const error = GitProcess.parseError(pushDetails.stderr);
console.error(`${fail} could not push release commit: `, error);
process.exit(1);
}
}
async function deleteDraft (releaseId, targetRepo) {
try {
const result = await octokit.repos.getRelease({
owner: 'electron',
repo: targetRepo,
release_id: parseInt(releaseId, 10)
2020-03-20 20:28:31 +00:00
});
if (!result.data.draft) {
2020-03-20 20:28:31 +00:00
console.log(`${fail} published releases cannot be deleted.`);
return false;
} else {
await octokit.repos.deleteRelease({
owner: 'electron',
repo: targetRepo,
release_id: result.data.id
2020-03-20 20:28:31 +00:00
});
}
2020-03-20 20:28:31 +00:00
console.log(`${pass} successfully deleted draft with id ${releaseId} from ${targetRepo}`);
return true;
} 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;
}
}
async function deleteTag (tag, targetRepo) {
try {
await octokit.git.deleteRef({
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}`);
} catch (err) {
2020-03-20 20:28:31 +00:00
console.log(`${fail} couldn't delete tag ${tag} from ${targetRepo}: `, err);
}
}
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');
// try to revert commit regardless of tag and draft deletion status
2020-03-20 20:28:31 +00:00
await revertBumpCommit(args.tag);
if (releaseId) {
if (isNightly) {
2020-03-20 20:28:31 +00:00
await deleteDraft(releaseId, 'nightlies');
// 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');
} else {
2020-03-20 20:28:31 +00:00
const deletedElectronDraft = await deleteDraft(releaseId, 'electron');
// don't delete tag unless draft deleted successfully
if (deletedElectronDraft) {
2020-03-20 20:28:31 +00:00
await deleteTag(args.tag, 'electron');
}
}
} else {
await Promise.all([
deleteTag(args.tag, 'electron'),
deleteTag(args.tag, 'nightlies')
2020-03-20 20:28:31 +00:00
]);
}
2020-03-20 20:28:31 +00:00
console.log(`${pass} failed release artifact cleanup complete`);
}
2020-03-20 20:28:31 +00:00
cleanReleaseArtifacts();