Browse Source

chore: improve current branch fetching (#18837)

* chore: improve current branch fetching

* chore: fix current branch fetch on master (#18844)
Shelley Vohr 5 years ago
parent
commit
993b70ee9e
4 changed files with 35 additions and 49 deletions
  1. 32 1
      script/lib/utils.js
  2. 1 17
      script/prepare-release.js
  3. 1 19
      script/publish-to-npm.js
  4. 1 12
      script/release-artifact-cleanup.js

+ 32 - 1
script/lib/utils.js

@@ -1,6 +1,10 @@
+const { GitProcess } = require('dugite')
+const path = require('path')
+
 const OUT_DIR = process.env.ELECTRON_OUT_DIR || 'Debug'
 
-const path = require('path')
+require('colors')
+const fail = '\u2717'.red
 
 function getElectronExec () {
   switch (process.platform) {
@@ -19,7 +23,34 @@ function getAbsoluteElectronExec () {
   return path.resolve(__dirname, '../../..', getElectronExec())
 }
 
+async function handleGitCall (args, gitDir) {
+  const details = await GitProcess.exec(args, gitDir)
+  if (details.exitCode === 0) {
+    const output = details.stdout.replace(/^\*|\s+|\s+$/, '')
+    return output.trim()
+  } else {
+    const error = GitProcess.parseError(details.stderr)
+    console.log(`${fail} couldn't parse git process call: `, error)
+    process.exit(1)
+  }
+}
+
+async function getCurrentBranch (gitDir) {
+  let branch = await handleGitCall(['rev-parse', '--abbrev-ref', 'HEAD'], gitDir)
+  if (branch !== 'master' && !branch.match(/[0-9]+-[0-9]+-x/)) {
+    const lastCommit = await handleGitCall(['rev-parse', 'HEAD'], gitDir)
+    const branches = (await handleGitCall(['branch', '--contains', lastCommit], gitDir)).split('\n')
+    branch = branches.filter(b => b === 'master' || b.match(/[0-9]+-[0-9]+-x/))[0]
+    if (!branch) {
+      console.log(`${fail} no release branch exists for this ref`)
+      process.exit(1)
+    }
+  }
+  return branch
+}
+
 module.exports = {
+  getCurrentBranch,
   getElectronExec,
   getAbsoluteElectronExec,
   OUT_DIR

+ 1 - 17
script/prepare-release.js

@@ -14,6 +14,7 @@ const pass = '\u2713'.green
 const path = require('path')
 const readline = require('readline')
 const releaseNotesGenerator = require('./release-notes/index.js')
+const { getCurrentBranch } = require('./lib/utils.js')
 const versionType = args._[0]
 const targetRepo = versionType === 'nightly' ? 'nightlies' : 'electron'
 
@@ -53,23 +54,6 @@ async function getNewVersion (dryRun) {
   }
 }
 
-async function getCurrentBranch (gitDir) {
-  console.log(`Determining current git branch`)
-  const gitArgs = ['rev-parse', '--abbrev-ref', 'HEAD']
-  const branchDetails = await GitProcess.exec(gitArgs, gitDir)
-  if (branchDetails.exitCode === 0) {
-    const currentBranch = branchDetails.stdout.trim()
-    console.log(`${pass} Successfully determined current git branch is ` +
-      `${currentBranch}`)
-    return currentBranch
-  } else {
-    const error = GitProcess.parseError(branchDetails.stderr)
-    console.log(`${fail} Could not get details for the current branch,
-      error was ${branchDetails.stderr}`, error)
-    process.exit(1)
-  }
-}
-
 async function getReleaseNotes (currentBranch, newVersion) {
   if (versionType === 'nightly') {
     return 'Nightlies do not get release notes, please compare tags for info'

+ 1 - 19
script/publish-to-npm.js

@@ -2,8 +2,8 @@ const temp = require('temp')
 const fs = require('fs')
 const path = require('path')
 const childProcess = require('child_process')
+const { getCurrentBranch } = require('./lib/utils.js')
 const GitHubApi = require('github')
-const { GitProcess } = require('dugite')
 const request = require('request')
 const semver = require('semver')
 const rootPackageJson = require('../package.json')
@@ -176,21 +176,3 @@ new Promise((resolve, reject) => {
     console.error(`Error: ${err}`)
     process.exit(1)
   })
-
-async function getCurrentBranch () {
-  const gitDir = path.resolve(__dirname, '..')
-  console.log(`Determining current git branch`)
-  const gitArgs = ['rev-parse', '--abbrev-ref', 'HEAD']
-  const branchDetails = await GitProcess.exec(gitArgs, gitDir)
-  if (branchDetails.exitCode === 0) {
-    const currentBranch = branchDetails.stdout.trim()
-    console.log(`Successfully determined current git branch is ` +
-      `${currentBranch}`)
-    return currentBranch
-  } else {
-    const error = GitProcess.parseError(branchDetails.stderr)
-    console.log(`Could not get details for the current branch,
-      error was ${branchDetails.stderr}`, error)
-    process.exit(1)
-  }
-}

+ 1 - 12
script/release-artifact-cleanup.js

@@ -10,6 +10,7 @@ const args = require('minimist')(process.argv.slice(2), {
 })
 const { execSync } = require('child_process')
 const { GitProcess } = require('dugite')
+const { getCurrentBranch } = require('./lib/utils.js')
 
 const GitHub = require('github')
 const path = require('path')
@@ -27,18 +28,6 @@ function getLastBumpCommit (tag) {
   return JSON.parse(data)
 }
 
-async function getCurrentBranch (gitDir) {
-  const gitArgs = ['rev-parse', '--abbrev-ref', 'HEAD']
-  const branchDetails = await GitProcess.exec(gitArgs, gitDir)
-  if (branchDetails.exitCode === 0) {
-    return branchDetails.stdout.trim()
-  }
-
-  const error = GitProcess.parseError(branchDetails.stderr)
-  console.error(`${fail} couldn't get current branch: `, error)
-  process.exit(1)
-}
-
 async function revertBumpCommit (tag) {
   const branch = await getCurrentBranch()
   const commitToRevert = getLastBumpCommit(tag).hash