chore: backport cleanup changes to 3-0-x (#16419)

* chore: backport cleanup changes to 3-1-x

* whoops

* minimize diff

* address review comments
This commit is contained in:
trop[bot] 2019-01-17 10:11:32 -08:00 committed by Shelley Vohr
parent cdc310ed94
commit d89d8f51b9

View file

@ -2,8 +2,11 @@
if (!process.env.CI) require('dotenv-safe').load() if (!process.env.CI) require('dotenv-safe').load()
require('colors') require('colors')
const pass = '\u2713'.green
const fail = '\u2717'.red
const args = require('minimist')(process.argv.slice(2), { const args = require('minimist')(process.argv.slice(2), {
string: ['tag'] string: ['tag', 'releaseID'],
default: { releaseID: '' }
}) })
const { execSync } = require('child_process') const { execSync } = require('child_process')
const { GitProcess } = require('dugite') const { GitProcess } = require('dugite')
@ -11,8 +14,8 @@ const { GitProcess } = require('dugite')
const GitHub = require('github') const GitHub = require('github')
const path = require('path') const path = require('path')
const github = new GitHub()
const gitDir = path.resolve(__dirname, '..') const gitDir = path.resolve(__dirname, '..')
const github = new GitHub()
github.authenticate({ github.authenticate({
type: 'token', type: 'token',
@ -42,36 +45,37 @@ async function revertBumpCommit (tag) {
await GitProcess.exec(['revert', commitToRevert], gitDir) await GitProcess.exec(['revert', commitToRevert], gitDir)
const pushDetails = await GitProcess.exec(['push', 'origin', `HEAD:${branch}`, '--follow-tags'], gitDir) const pushDetails = await GitProcess.exec(['push', 'origin', `HEAD:${branch}`, '--follow-tags'], gitDir)
if (pushDetails.exitCode === 0) { if (pushDetails.exitCode === 0) {
console.log(`Successfully reverted release commit.`) console.log(`${pass} successfully reverted release commit.`)
} else { } else {
const error = GitProcess.parseError(pushDetails.stderr) const error = GitProcess.parseError(pushDetails.stderr)
console.error(`Failed to push release commit: `, error) console.error(`${fail} could not push release commit: `, error)
process.exit(1) process.exit(1)
} }
} }
async function deleteDraft (releaseID, targetRepo) { async function deleteDraft (releaseId, targetRepo) {
try { try {
const result = await github.repos.getRelease({ const result = await github.repos.getRelease({
owner: 'electron', owner: 'electron',
repo: targetRepo, repo: targetRepo,
id: parseInt(releaseID, 10) id: parseInt(releaseId, 10)
}) })
console.log(result) console.log(result)
if (!result.draft) { if (!result.data.draft) {
console.log(`Published releases cannot be deleted.`) console.log(`${fail} published releases cannot be deleted.`)
process.exit(1) return false
} else { } else {
await github.repos.deleteRelease({ await github.repos.deleteRelease({
owner: 'electron', owner: 'electron',
repo: targetRepo, repo: targetRepo,
release_id: result.id release_id: result.data.id
}) })
} }
console.log(`Successfully deleted draft with id ${releaseID} from ${targetRepo}`) console.log(`${pass} successfully deleted draft with id ${releaseId} from ${targetRepo}`)
return true
} catch (err) { } catch (err) {
console.error(`Couldn't delete draft with id ${releaseID} from ${targetRepo}: `, err) console.error(`${fail} couldn't delete draft with id ${releaseId} from ${targetRepo}: `, err)
process.exit(1) return false
} }
} }
@ -82,29 +86,39 @@ async function deleteTag (tag, targetRepo) {
repo: targetRepo, repo: targetRepo,
ref: `tags/${tag}` ref: `tags/${tag}`
}) })
console.log(`Successfully deleted tag ${tag} from ${targetRepo}`) console.log(`${pass} successfully deleted tag ${tag} from ${targetRepo}`)
} catch (err) { } catch (err) {
console.log(`Couldn't delete tag ${tag} from ${targetRepo}: `, err) console.log(`${fail} couldn't delete tag ${tag} from ${targetRepo}: `, err)
process.exit(1)
} }
} }
async function cleanReleaseArtifacts () { async function cleanReleaseArtifacts () {
const releaseID = args.releaseID const releaseId = args.releaseID.length > 0 ? args.releaseID : null
const isNightly = args.tag.includes('nightly') const isNightly = args.tag.includes('nightly')
if (isNightly) { // try to revert commit regardless of tag and draft deletion status
await deleteDraft(releaseID, 'nightlies')
await deleteTag(args.tag, 'nightlies')
} else {
console.log('we are here')
await deleteDraft(releaseID, 'electron')
}
await deleteTag(args.tag, 'electron')
await revertBumpCommit(args.tag) await revertBumpCommit(args.tag)
console.log('Failed release artifact cleanup complete') if (releaseId) {
if (isNightly) {
const deletedNightlyDraft = await deleteDraft(releaseId, 'nightlies')
// 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')
}
}
}
console.log(`${pass} failed release artifact cleanup complete`)
} }
cleanReleaseArtifacts() cleanReleaseArtifacts()