@@ -8,8 +8,17 @@ export const REGEX_MASTER = /^master$/
8
8
/** regex to match the main branch */
9
9
export const REGEX_MAIN = / ^ m a i n $ /
10
10
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 + ) $ /
13
22
14
23
export interface StageInfo {
15
24
isProd : boolean
@@ -81,9 +90,11 @@ export function getBranchInfo(env: unknown, branchName?: string): BranchInfo {
81
90
}
82
91
83
92
export 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
+ )
87
98
}
88
99
89
100
export function getBranchNameOverride ( env : CustomScOverrideEnv ) : string {
@@ -134,13 +145,11 @@ export function isMainBranch(branchName: string): boolean {
134
145
* @throws Throws an error if given branchName does not match our convention
135
146
*/
136
147
export 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 ) {
141
150
return {
142
- branchId : parseInt ( branchId , 10 ) ,
143
- branchName : branchN ,
151
+ branchId : parseInt ( matches . groups [ 'id' ] , 10 ) ,
152
+ branchName : matches . groups [ 'name' ] ,
144
153
}
145
154
} else {
146
155
throw new Error (
@@ -156,7 +165,7 @@ export function parseBranchName(branchName: string): { branchId: number; branchN
156
165
* @return returns true if the stage is 'master' or 'main', false if not
157
166
*/
158
167
export 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 )
160
169
}
161
170
162
171
/**
0 commit comments