prepare-release.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #!/usr/bin/env node
  2. require('colors')
  3. const args = require('minimist')(process.argv.slice(2), {
  4. boolean: ['automaticRelease', 'notesOnly', 'stable']
  5. })
  6. const assert = require('assert')
  7. const ciReleaseBuild = require('./ci-release-build')
  8. const { execSync } = require('child_process')
  9. const fail = '\u2717'.red
  10. const { GitProcess } = require('dugite')
  11. const GitHub = require('github')
  12. const pass = '\u2713'.green
  13. const path = require('path')
  14. const pkg = require('../package.json')
  15. const readline = require('readline')
  16. const versionType = args._[0]
  17. // TODO (future) automatically determine version based on conventional commits
  18. // via conventional-recommended-bump
  19. assert(process.env.ELECTRON_GITHUB_TOKEN, 'ELECTRON_GITHUB_TOKEN not found in environment')
  20. if (!versionType && !args.notesOnly) {
  21. console.log(`Usage: prepare-release versionType [major | minor | patch | beta]` +
  22. ` (--stable) (--notesOnly) (--automaticRelease) (--branch)`)
  23. process.exit(1)
  24. }
  25. const github = new GitHub()
  26. const gitDir = path.resolve(__dirname, '..')
  27. github.authenticate({type: 'token', token: process.env.ELECTRON_GITHUB_TOKEN})
  28. function getNewVersion (dryRun) {
  29. console.log(`Bumping for new "${versionType}" version.`)
  30. let bumpScript = path.join(__dirname, 'bump-version.py')
  31. let scriptArgs = [bumpScript, `--bump ${versionType}`]
  32. if (args.stable) {
  33. scriptArgs.push('--stable')
  34. }
  35. if (dryRun) {
  36. scriptArgs.push('--dry-run')
  37. }
  38. try {
  39. let bumpVersion = execSync(scriptArgs.join(' '), {encoding: 'UTF-8'})
  40. bumpVersion = bumpVersion.substr(bumpVersion.indexOf(':') + 1).trim()
  41. let newVersion = `v${bumpVersion}`
  42. if (!dryRun) {
  43. console.log(`${pass} Successfully bumped version to ${newVersion}`)
  44. }
  45. return newVersion
  46. } catch (err) {
  47. console.log(`${fail} Could not bump version, error was:`, err)
  48. }
  49. }
  50. async function getCurrentBranch (gitDir) {
  51. console.log(`Determining current git branch`)
  52. let gitArgs = ['rev-parse', '--abbrev-ref', 'HEAD']
  53. let branchDetails = await GitProcess.exec(gitArgs, gitDir)
  54. if (branchDetails.exitCode === 0) {
  55. let currentBranch = branchDetails.stdout.trim()
  56. console.log(`${pass} Successfully determined current git branch is ` +
  57. `${currentBranch}`)
  58. return currentBranch
  59. } else {
  60. let error = GitProcess.parseError(branchDetails.stderr)
  61. console.log(`${fail} Could not get details for the current branch,
  62. error was ${branchDetails.stderr}`, error)
  63. process.exit(1)
  64. }
  65. }
  66. async function getReleaseNotes (currentBranch) {
  67. console.log(`Generating release notes for ${currentBranch}.`)
  68. let githubOpts = {
  69. owner: 'electron',
  70. repo: 'electron',
  71. base: `v${pkg.version}`,
  72. head: currentBranch
  73. }
  74. let releaseNotes
  75. if (args.automaticRelease) {
  76. releaseNotes = '## Bug Fixes/Changes \n\n'
  77. } else {
  78. releaseNotes = '(placeholder)\n'
  79. }
  80. console.log(`Checking for commits from ${pkg.version} to ${currentBranch}`)
  81. let commitComparison = await github.repos.compareCommits(githubOpts)
  82. .catch(err => {
  83. console.log(`${fail} Error checking for commits from ${pkg.version} to ` +
  84. `${currentBranch}`, err)
  85. process.exit(1)
  86. })
  87. if (commitComparison.data.commits.length === 0) {
  88. console.log(`${pass} There are no commits from ${pkg.version} to ` +
  89. `${currentBranch}, skipping release.`)
  90. process.exit(0)
  91. }
  92. let prCount = 0
  93. const mergeRE = /Merge pull request #(\d+) from .*\n/
  94. const newlineRE = /(.*)\n*.*/
  95. const prRE = /(.* )\(#(\d+)\)(?:.*)/
  96. commitComparison.data.commits.forEach(commitEntry => {
  97. let commitMessage = commitEntry.commit.message
  98. if (commitMessage.indexOf('#') > -1) {
  99. let prMatch = commitMessage.match(mergeRE)
  100. let prNumber
  101. if (prMatch) {
  102. commitMessage = commitMessage.replace(mergeRE, '').replace('\n', '')
  103. let newlineMatch = commitMessage.match(newlineRE)
  104. if (newlineMatch) {
  105. commitMessage = newlineMatch[1]
  106. }
  107. prNumber = prMatch[1]
  108. } else {
  109. prMatch = commitMessage.match(prRE)
  110. if (prMatch) {
  111. commitMessage = prMatch[1].trim()
  112. prNumber = prMatch[2]
  113. }
  114. }
  115. if (prMatch) {
  116. if (commitMessage.substr(commitMessage.length - 1, commitMessage.length) !== '.') {
  117. commitMessage += '.'
  118. }
  119. releaseNotes += `* ${commitMessage} #${prNumber} \n\n`
  120. prCount++
  121. }
  122. }
  123. })
  124. console.log(`${pass} Done generating release notes for ${currentBranch}. Found ${prCount} PRs.`)
  125. return releaseNotes
  126. }
  127. async function createRelease (branchToTarget, isBeta) {
  128. let releaseNotes = await getReleaseNotes(branchToTarget)
  129. let newVersion = getNewVersion()
  130. await tagRelease(newVersion)
  131. const githubOpts = {
  132. owner: 'electron',
  133. repo: 'electron'
  134. }
  135. console.log(`Checking for existing draft release.`)
  136. let releases = await github.repos.getReleases(githubOpts)
  137. .catch(err => {
  138. console.log('$fail} Could not get releases. Error was', err)
  139. })
  140. let drafts = releases.data.filter(release => release.draft &&
  141. release.tag_name === newVersion)
  142. if (drafts.length > 0) {
  143. console.log(`${fail} Aborting because draft release for
  144. ${drafts[0].tag_name} already exists.`)
  145. process.exit(1)
  146. }
  147. console.log(`${pass} A draft release does not exist; creating one.`)
  148. githubOpts.draft = true
  149. githubOpts.name = `electron ${newVersion}`
  150. if (isBeta) {
  151. githubOpts.body = `Note: This is a beta release. Please file new issues ` +
  152. `for any bugs you find in it.\n \n This release is published to npm ` +
  153. `under the beta tag and can be installed via npm install electron@beta, ` +
  154. `or npm i electron@${newVersion.substr(1)}.\n \n ${releaseNotes}`
  155. githubOpts.name = `${githubOpts.name}`
  156. githubOpts.prerelease = true
  157. } else {
  158. githubOpts.body = releaseNotes
  159. }
  160. githubOpts.tag_name = newVersion
  161. githubOpts.target_commitish = branchToTarget
  162. await github.repos.createRelease(githubOpts)
  163. .catch(err => {
  164. console.log(`${fail} Error creating new release: `, err)
  165. process.exit(1)
  166. })
  167. console.log(`${pass} Draft release for ${newVersion} has been created.`)
  168. }
  169. async function pushRelease (branch) {
  170. let pushDetails = await GitProcess.exec(['push', 'origin', `HEAD:${branch}`, '--follow-tags'], gitDir)
  171. if (pushDetails.exitCode === 0) {
  172. console.log(`${pass} Successfully pushed the release. Wait for ` +
  173. `release builds to finish before running "npm run release".`)
  174. } else {
  175. console.log(`${fail} Error pushing the release: ` +
  176. `${pushDetails.stderr}`)
  177. process.exit(1)
  178. }
  179. }
  180. async function runReleaseBuilds (branch) {
  181. await ciReleaseBuild(branch, {
  182. ghRelease: true,
  183. automaticRelease: args.automaticRelease
  184. })
  185. }
  186. async function tagRelease (version) {
  187. console.log(`Tagging release ${version}.`)
  188. let checkoutDetails = await GitProcess.exec([ 'tag', '-a', '-m', version, version ], gitDir)
  189. if (checkoutDetails.exitCode === 0) {
  190. console.log(`${pass} Successfully tagged ${version}.`)
  191. } else {
  192. console.log(`${fail} Error tagging ${version}: ` +
  193. `${checkoutDetails.stderr}`)
  194. process.exit(1)
  195. }
  196. }
  197. async function verifyNewVersion () {
  198. let newVersion = getNewVersion(true)
  199. let response
  200. if (args.automaticRelease) {
  201. response = 'y'
  202. } else {
  203. response = await promptForVersion(newVersion)
  204. }
  205. if (response.match(/^y/i)) {
  206. console.log(`${pass} Starting release of ${newVersion}`)
  207. } else {
  208. console.log(`${fail} Aborting release of ${newVersion}`)
  209. process.exit()
  210. }
  211. }
  212. async function promptForVersion (version) {
  213. return new Promise((resolve, reject) => {
  214. const rl = readline.createInterface({
  215. input: process.stdin,
  216. output: process.stdout
  217. })
  218. rl.question(`Do you want to create the release ${version.green} (y/N)? `, (answer) => {
  219. rl.close()
  220. resolve(answer)
  221. })
  222. })
  223. }
  224. async function prepareRelease (isBeta, notesOnly) {
  225. if (args.automaticRelease && (pkg.version.indexOf('beta') === -1 ||
  226. versionType !== 'beta')) {
  227. console.log(`${fail} Automatic release is only supported for beta releases`)
  228. process.exit(1)
  229. }
  230. let currentBranch
  231. if (args.branch) {
  232. currentBranch = args.branch
  233. } else {
  234. currentBranch = await getCurrentBranch(gitDir)
  235. }
  236. if (notesOnly) {
  237. let releaseNotes = await getReleaseNotes(currentBranch)
  238. console.log(`Draft release notes are: \n${releaseNotes}`)
  239. } else {
  240. await verifyNewVersion()
  241. await createRelease(currentBranch, isBeta)
  242. await pushRelease(currentBranch)
  243. await runReleaseBuilds(currentBranch)
  244. }
  245. }
  246. prepareRelease(!args.stable, args.notesOnly)