1010// [end-readme]
1111
1212require ( 'dotenv' ) . config ( )
13- const { DOCUBOT_REPO_PAT , HEROKU_PRODUCTION_APP } = process . env
13+ const {
14+ DOCUBOT_REPO_PAT ,
15+ HEROKU_PRODUCTION_APP ,
16+ GIT_BRANCH
17+ } = process . env
1418
1519// Exit if PAT is not found
1620if ( ! DOCUBOT_REPO_PAT ) {
@@ -23,8 +27,22 @@ const rimraf = require('rimraf').sync
2327const fs = require ( 'fs' )
2428const path = require ( 'path' )
2529const os = require ( 'os' )
26- const yaml = require ( 'js-yaml' )
27- const eaConfig = yaml . load ( fs . readFileSync ( path . join ( process . cwd ( ) , 'ea-config.yml' ) , 'utf8' ) )
30+ const EA_PRODUCTION_BRANCH = 'main'
31+
32+ // If a branch name is not provided in the environment, attempt to get
33+ // the local branch name; or default to 'main'
34+ let currentBranch = ( GIT_BRANCH || '' ) . replace ( / ^ r e f s \/ h e a d s \/ / , '' )
35+ if ( ! currentBranch ) {
36+ try {
37+ currentBranch = execSync ( 'git branch --show-current' ) . toString ( )
38+ } catch ( err ) {
39+ // Ignore but log
40+ console . warn ( 'Error checking for local branch:' , err . message )
41+ }
42+ }
43+ if ( ! currentBranch ) {
44+ currentBranch = EA_PRODUCTION_BRANCH
45+ }
2846
2947// Early Access details
3048const earlyAccessOwner = 'github'
@@ -50,12 +68,24 @@ const destinationDirsMap = destinationDirNames
5068const environment = HEROKU_PRODUCTION_APP ? 'production' : 'staging'
5169
5270// Early access branch to clone
53- const earlyAccessBranch = HEROKU_PRODUCTION_APP ? eaConfig . EA_PRODUCTION_BRANCH : eaConfig . EA_STAGING_BRANCH
71+ let earlyAccessBranch = HEROKU_PRODUCTION_APP ? EA_PRODUCTION_BRANCH : currentBranch
5472
5573// Confirm that the branch exists in the remote
56- const branchExists = execSync ( `git ls-remote --heads ${ earlyAccessFullRepo } ${ earlyAccessBranch } ` ) . toString ( )
74+ let branchExists = execSync ( `git ls-remote --heads ${ earlyAccessFullRepo } ${ earlyAccessBranch } ` ) . toString ( )
75+
76+ // If the branch did NOT exist, try checking for the default branch instead
77+ if ( ! branchExists && earlyAccessBranch !== EA_PRODUCTION_BRANCH ) {
78+ console . warn ( `The branch '${ earlyAccessBranch } ' was not found in ${ earlyAccessOwner } /${ earlyAccessRepoName } !` )
79+ console . warn ( `Attempting the default branch ${ EA_PRODUCTION_BRANCH } instead...` )
80+
81+ earlyAccessBranch = EA_PRODUCTION_BRANCH
82+ branchExists = execSync ( `git ls-remote --heads ${ earlyAccessFullRepo } ${ earlyAccessBranch } ` ) . toString ( )
83+ }
84+
85+ // If no suitable branch was found, bail out now
5786if ( ! branchExists ) {
58- console . error ( `The branch '${ earlyAccessBranch } ' was not found in ${ earlyAccessOwner } /${ earlyAccessRepoName } . Exiting!` )
87+ console . error ( `The branch '${ earlyAccessBranch } ' was not found in ${ earlyAccessOwner } /${ earlyAccessRepoName } !` )
88+ console . error ( 'Exiting!' )
5989 process . exit ( 1 )
6090}
6191
0 commit comments