@@ -8,8 +8,17 @@ export const REGEX_MASTER = /^master$/
88/** regex to match the main branch */
99export const REGEX_MAIN = / ^ m a i n $ /
1010
11- /** regex to match our branch conventions with the following capture groups: fullMatch / branch id / branch name */
12- export const REGEX_BRANCH_NAME = / ^ [ a - z ] * \/ ? # ( \d + ) - ( .* ) /
11+ /**
12+ * regex to match our branch conventions with the following named capture groups: id, name
13+ * @example #123-my-feature -> { id: '123', name: 'my-feature' }
14+ * @example feature/#456-yanr -> { id: '456', name: 'yanr' }
15+ */
16+ const REGEX_BRANCH_NAME_DEFAULT = / ^ [ a - z ] * \/ ? # (?< id > \d + ) - (?< name > .* ) $ /
17+ /**
18+ * regex to match the branch convention github copilot uses with the following named capture groups: id, name
19+ * @example copilot/fix-789 -> { id: '789', name: 'fix' }
20+ */
21+ const REGEX_BRANCH_NAME_COPILOT = / ^ c o p i l o t \/ (?< name > .* ) - (?< id > \d + ) $ /
1322
1423export interface StageInfo {
1524 isProd : boolean
@@ -81,9 +90,11 @@ export function getBranchInfo(env: unknown, branchName?: string): BranchInfo {
8190}
8291
8392export function isFullBranchOverrideDefined ( envVars : unknown ) : envVars is CustomScOverrideEnv {
84- return envVars !== null
85- && typeof ( envVars as CustomScOverrideEnv ) . SC_OVERRIDE_BRANCH_NAME === 'string'
86- && typeof ( envVars as CustomScOverrideEnv ) . SC_OVERRIDE_IS_PR === 'string'
93+ return (
94+ envVars !== null &&
95+ typeof ( envVars as CustomScOverrideEnv ) . SC_OVERRIDE_BRANCH_NAME === 'string' &&
96+ typeof ( envVars as CustomScOverrideEnv ) . SC_OVERRIDE_IS_PR === 'string'
97+ )
8798}
8899
89100export function getBranchNameOverride ( env : CustomScOverrideEnv ) : string {
@@ -134,13 +145,11 @@ export function isMainBranch(branchName: string): boolean {
134145 * @throws Throws an error if given branchName does not match our convention
135146 */
136147export function parseBranchName ( branchName : string ) : { branchId : number ; branchName : string } {
137- const matches = branchName . match ( REGEX_BRANCH_NAME )
138- if ( matches ) {
139- // [0] full match / [1] branch id / [2] branch name
140- const [ , branchId , branchN ] = matches
148+ const matches = REGEX_BRANCH_NAME_DEFAULT . exec ( branchName ) ?? REGEX_BRANCH_NAME_COPILOT . exec ( branchName )
149+ if ( matches ?. groups ) {
141150 return {
142- branchId : parseInt ( branchId , 10 ) ,
143- branchName : branchN ,
151+ branchId : parseInt ( matches . groups [ 'id' ] , 10 ) ,
152+ branchName : matches . groups [ 'name' ] ,
144153 }
145154 } else {
146155 throw new Error (
@@ -156,7 +165,7 @@ export function parseBranchName(branchName: string): { branchId: number; branchN
156165 * @return returns true if the stage is 'master' or 'main', false if not
157166 */
158167export function isProduction ( stageName : string ) : boolean {
159- return REGEX_MASTER . test ( stageName ) || REGEX_MAIN . test ( stageName )
168+ return REGEX_MASTER . test ( stageName ) ?? REGEX_MAIN . test ( stageName )
160169}
161170
162171/**
0 commit comments