2018-08-29 18:13:22 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
if (!process.env.CI) require('dotenv-safe').load()
|
|
|
|
require('colors')
|
2018-11-30 17:58:09 +00:00
|
|
|
const pass = '\u2713'.green
|
|
|
|
const fail = '\u2717'.red
|
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: '' }
|
2018-08-29 18:13:22 +00:00
|
|
|
})
|
|
|
|
const { execSync } = require('child_process')
|
|
|
|
const { GitProcess } = require('dugite')
|
2018-12-03 21:28:10 +00:00
|
|
|
const { getCurrentBranch } = require('./lib/utils.js')
|
2018-08-29 18:13:22 +00:00
|
|
|
|
2019-05-08 01:48:40 +00:00
|
|
|
const octokit = require('@octokit/rest')({
|
|
|
|
auth: process.env.ELECTRON_GITHUB_TOKEN
|
|
|
|
})
|
2018-08-29 18:13:22 +00:00
|
|
|
|
2019-05-08 01:48:40 +00:00
|
|
|
const path = require('path')
|
2018-08-29 18:13:22 +00:00
|
|
|
const gitDir = path.resolve(__dirname, '..')
|
|
|
|
|
2018-09-04 15:54:53 +00:00
|
|
|
function getLastBumpCommit (tag) {
|
2018-10-19 03:20:43 +00:00
|
|
|
const data = execSync(`git log -n1 --grep "Bump ${tag}" --format='format:{"hash": "%H", "message": "%s"}'`).toString()
|
2018-08-29 18:13:22 +00:00
|
|
|
return JSON.parse(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function revertBumpCommit (tag) {
|
2018-09-19 16:38:35 +00:00
|
|
|
const branch = await getCurrentBranch()
|
2018-09-04 15:54:53 +00:00
|
|
|
const commitToRevert = getLastBumpCommit(tag).hash
|
2018-08-29 18:13:22 +00:00
|
|
|
await GitProcess.exec(['revert', commitToRevert], gitDir)
|
|
|
|
const pushDetails = await GitProcess.exec(['push', 'origin', `HEAD:${branch}`, '--follow-tags'], gitDir)
|
|
|
|
if (pushDetails.exitCode === 0) {
|
2018-11-30 17:58:09 +00:00
|
|
|
console.log(`${pass} successfully reverted release commit.`)
|
2018-08-29 18:13:22 +00:00
|
|
|
} else {
|
|
|
|
const error = GitProcess.parseError(pushDetails.stderr)
|
2018-11-30 17:58:09 +00:00
|
|
|
console.error(`${fail} could not push release commit: `, error)
|
2018-08-29 18:13:22 +00:00
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
2018-08-29 18:13:22 +00:00
|
|
|
})
|
2018-11-28 04:12:01 +00:00
|
|
|
console.log(result)
|
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) {
|
|
|
|
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
|
|
|
|
2019-01-18 22:00:15 +00:00
|
|
|
// try to revert commit regardless of tag and draft deletion status
|
|
|
|
await revertBumpCommit(args.tag)
|
|
|
|
|
2018-11-30 17:58:09 +00:00
|
|
|
if (releaseId) {
|
|
|
|
if (isNightly) {
|
|
|
|
const deletedNightlyDraft = await deleteDraft(releaseId, 'nightlies')
|
2019-01-18 22:00:15 +00:00
|
|
|
|
2018-11-30 17:58:09 +00:00
|
|
|
// don't delete tag unless draft deleted successfully
|
|
|
|
if (deletedNightlyDraft) {
|
|
|
|
await Promise.all([
|
|
|
|
deleteTag(args.tag, 'electron'),
|
|
|
|
deleteTag(args.tag, 'nightlies')
|
|
|
|
])
|
|
|
|
}
|
|
|
|
} 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()
|