| 
									
										
										
										
											2018-08-29 11:13:22 -07:00
										 |  |  | #!/usr/bin/env node
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if (!process.env.CI) require('dotenv-safe').load() | 
					
						
							|  |  |  | const args = require('minimist')(process.argv.slice(2), { | 
					
						
							| 
									
										
										
										
											2018-12-14 15:15:16 -08:00
										 |  |  |   string: ['tag', 'releaseID'], | 
					
						
							|  |  |  |   default: { releaseID: '' } | 
					
						
							| 
									
										
										
										
											2018-08-29 11:13:22 -07:00
										 |  |  | }) | 
					
						
							| 
									
										
										
										
											2019-08-29 07:46:54 -07:00
										 |  |  | const path = require('path') | 
					
						
							| 
									
										
										
										
											2018-08-29 11:13:22 -07:00
										 |  |  | const { execSync } = require('child_process') | 
					
						
							|  |  |  | const { GitProcess } = require('dugite') | 
					
						
							| 
									
										
										
										
											2019-06-24 10:18:04 -07:00
										 |  |  | const { getCurrentBranch, ELECTRON_DIR } = require('../lib/utils.js') | 
					
						
							| 
									
										
										
										
											2018-08-29 11:13:22 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 18:48:40 -07:00
										 |  |  | const octokit = require('@octokit/rest')({ | 
					
						
							|  |  |  |   auth: process.env.ELECTRON_GITHUB_TOKEN | 
					
						
							|  |  |  | }) | 
					
						
							| 
									
										
										
										
											2018-08-29 11:13:22 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-29 07:46:54 -07:00
										 |  |  | require('colors') | 
					
						
							|  |  |  | const pass = '✓'.green | 
					
						
							|  |  |  | const fail = '✗'.red | 
					
						
							| 
									
										
										
										
											2018-08-29 11:13:22 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 08:54:53 -07:00
										 |  |  | function getLastBumpCommit (tag) { | 
					
						
							| 
									
										
										
										
											2018-10-18 22:20:43 -05:00
										 |  |  |   const data = execSync(`git log -n1 --grep "Bump ${tag}" --format='format:{"hash": "%H", "message": "%s"}'`).toString() | 
					
						
							| 
									
										
										
										
											2018-08-29 11:13:22 -07:00
										 |  |  |   return JSON.parse(data) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function revertBumpCommit (tag) { | 
					
						
							| 
									
										
										
										
											2018-09-19 09:38:35 -07:00
										 |  |  |   const branch = await getCurrentBranch() | 
					
						
							| 
									
										
										
										
											2018-09-04 08:54:53 -07:00
										 |  |  |   const commitToRevert = getLastBumpCommit(tag).hash | 
					
						
							| 
									
										
										
										
											2019-06-24 10:18:04 -07:00
										 |  |  |   await GitProcess.exec(['revert', commitToRevert], ELECTRON_DIR) | 
					
						
							|  |  |  |   const pushDetails = await GitProcess.exec(['push', 'origin', `HEAD:${branch}`, '--follow-tags'], ELECTRON_DIR) | 
					
						
							| 
									
										
										
										
											2018-08-29 11:13:22 -07:00
										 |  |  |   if (pushDetails.exitCode === 0) { | 
					
						
							| 
									
										
										
										
											2018-11-30 09:58:09 -08:00
										 |  |  |     console.log(`${pass} successfully reverted release commit.`) | 
					
						
							| 
									
										
										
										
											2018-08-29 11:13:22 -07:00
										 |  |  |   } else { | 
					
						
							|  |  |  |     const error = GitProcess.parseError(pushDetails.stderr) | 
					
						
							| 
									
										
										
										
											2018-11-30 09:58:09 -08:00
										 |  |  |     console.error(`${fail} could not push release commit: `, error) | 
					
						
							| 
									
										
										
										
											2018-08-29 11:13:22 -07:00
										 |  |  |     process.exit(1) | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-27 23:12:01 -05:00
										 |  |  | async function deleteDraft (releaseId, targetRepo) { | 
					
						
							| 
									
										
										
										
											2018-08-29 11:13:22 -07:00
										 |  |  |   try { | 
					
						
							| 
									
										
										
										
											2019-01-08 12:05:58 -08:00
										 |  |  |     const result = await octokit.repos.getRelease({ | 
					
						
							| 
									
										
										
										
											2018-08-29 11:13:22 -07:00
										 |  |  |       owner: 'electron', | 
					
						
							|  |  |  |       repo: targetRepo, | 
					
						
							| 
									
										
										
										
											2019-01-08 12:05:58 -08:00
										 |  |  |       release_id: parseInt(releaseId, 10) | 
					
						
							| 
									
										
										
										
											2018-08-29 11:13:22 -07:00
										 |  |  |     }) | 
					
						
							| 
									
										
										
										
											2018-11-27 23:12:01 -05:00
										 |  |  |     console.log(result) | 
					
						
							| 
									
										
										
										
											2019-01-04 11:26:14 -08:00
										 |  |  |     if (!result.data.draft) { | 
					
						
							| 
									
										
										
										
											2018-11-30 09:58:09 -08:00
										 |  |  |       console.log(`${fail} published releases cannot be deleted.`) | 
					
						
							|  |  |  |       return false | 
					
						
							| 
									
										
										
										
											2018-08-29 11:13:22 -07:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2019-01-08 12:05:58 -08:00
										 |  |  |       await octokit.repos.deleteRelease({ | 
					
						
							| 
									
										
										
										
											2018-08-29 11:13:22 -07:00
										 |  |  |         owner: 'electron', | 
					
						
							|  |  |  |         repo: targetRepo, | 
					
						
							| 
									
										
										
										
											2019-01-04 11:26:14 -08:00
										 |  |  |         release_id: result.data.id | 
					
						
							| 
									
										
										
										
											2018-08-29 11:13:22 -07:00
										 |  |  |       }) | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-11-30 09:58:09 -08:00
										 |  |  |     console.log(`${pass} successfully deleted draft with id ${releaseId} from ${targetRepo}`) | 
					
						
							|  |  |  |     return true | 
					
						
							| 
									
										
										
										
											2018-08-29 11:13:22 -07:00
										 |  |  |   } catch (err) { | 
					
						
							| 
									
										
										
										
											2018-11-30 09:58:09 -08:00
										 |  |  |     console.error(`${fail} couldn't delete draft with id ${releaseId} from ${targetRepo}: `, err) | 
					
						
							|  |  |  |     return false | 
					
						
							| 
									
										
										
										
											2018-08-29 11:13:22 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function deleteTag (tag, targetRepo) { | 
					
						
							|  |  |  |   try { | 
					
						
							| 
									
										
										
										
											2019-01-08 12:05:58 -08:00
										 |  |  |     await octokit.git.deleteRef({ | 
					
						
							| 
									
										
										
										
											2018-08-29 11:13:22 -07:00
										 |  |  |       owner: 'electron', | 
					
						
							|  |  |  |       repo: targetRepo, | 
					
						
							| 
									
										
										
										
											2019-01-10 13:42:42 -08:00
										 |  |  |       ref: `tags/${tag}` | 
					
						
							| 
									
										
										
										
											2018-08-29 11:13:22 -07:00
										 |  |  |     }) | 
					
						
							| 
									
										
										
										
											2018-11-30 09:58:09 -08:00
										 |  |  |     console.log(`${pass} successfully deleted tag ${tag} from ${targetRepo}`) | 
					
						
							| 
									
										
										
										
											2018-08-29 11:13:22 -07:00
										 |  |  |   } catch (err) { | 
					
						
							| 
									
										
										
										
											2018-11-30 09:58:09 -08:00
										 |  |  |     console.log(`${fail} couldn't delete tag ${tag} from ${targetRepo}: `, err) | 
					
						
							| 
									
										
										
										
											2018-08-29 11:13:22 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function cleanReleaseArtifacts () { | 
					
						
							| 
									
										
										
										
											2018-12-14 15:15:16 -08:00
										 |  |  |   const releaseId = args.releaseID.length > 0 ? args.releaseID : null | 
					
						
							| 
									
										
										
										
											2018-10-18 22:20:43 -05:00
										 |  |  |   const isNightly = args.tag.includes('nightly') | 
					
						
							| 
									
										
										
										
											2018-08-29 11:13:22 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-18 14:00:15 -08:00
										 |  |  |   // try to revert commit regardless of tag and draft deletion status
 | 
					
						
							|  |  |  |   await revertBumpCommit(args.tag) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-30 09:58:09 -08:00
										 |  |  |   if (releaseId) { | 
					
						
							|  |  |  |     if (isNightly) { | 
					
						
							|  |  |  |       const deletedNightlyDraft = await deleteDraft(releaseId, 'nightlies') | 
					
						
							| 
									
										
										
										
											2019-01-18 14:00:15 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-30 09:58:09 -08: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 14:00:15 -08:00
										 |  |  |   } else { | 
					
						
							|  |  |  |     await Promise.all([ | 
					
						
							|  |  |  |       deleteTag(args.tag, 'electron'), | 
					
						
							|  |  |  |       deleteTag(args.tag, 'nightlies') | 
					
						
							|  |  |  |     ]) | 
					
						
							| 
									
										
										
										
											2018-08-29 11:13:22 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-30 09:58:09 -08:00
										 |  |  |   console.log(`${pass} failed release artifact cleanup complete`) | 
					
						
							| 
									
										
										
										
											2018-08-29 11:13:22 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | cleanReleaseArtifacts() |