Skip to content

Commit a2ec8fb

Browse files
committed
feat(branch-utilities): support naming convention for github copilot branch names
BREAKING CHANGE REGEX_BRANCH_NAME is no longer exported. use the utility function `parseBranchName` instead
1 parent dd17d1b commit a2ec8fb

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

packages/branch-utilities/src/lib/base.utils.spec.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,21 @@ describe('base utils', () => {
8181
})
8282

8383
describe('parseBranchName', () => {
84+
8485
test('works when valid pattern', () => {
85-
expect(parseBranchName('#7-abc').branchId).toBe(7)
86+
expect(parseBranchName('#7-abc')).toEqual({ branchId: 7, branchName: 'abc' } satisfies ReturnType<typeof parseBranchName>)
87+
8688
expect(parseBranchName('#72-abc').branchId).toBe(72)
89+
expect(parseBranchName('#000-int').branchId).toBe(0)
90+
expect(parseBranchName('#001-test').branchId).toBe(1)
91+
8792
expect(parseBranchName('#72- whatever').branchId).toBe(72)
8893
expect(parseBranchName('feature/#72-ok').branchId).toBe(72)
8994
})
95+
96+
test('works for github copilot created branches', () => {
97+
expect(parseBranchName('copilot/fix-123')).toEqual({ branchId: 123, branchName: 'fix' } satisfies ReturnType<typeof parseBranchName>)
98+
})
9099
test('throws when invalid pattern', () => {
91100
expect(() => parseBranchName('whrjwe')).toThrow()
92101
})

packages/branch-utilities/src/lib/base.utils.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,17 @@ export const REGEX_MASTER = /^master$/
88
/** regex to match the main branch */
99
export const REGEX_MAIN = /^main$/
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 = /copilot\/(?<name>fix)-(?<id>\d+)/
1322

1423
export interface StageInfo {
1524
isProd: boolean
@@ -134,13 +143,11 @@ export function isMainBranch(branchName: string): boolean {
134143
* @throws Throws an error if given branchName does not match our convention
135144
*/
136145
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
146+
const matches = REGEX_BRANCH_NAME_DEFAULT.exec(branchName) || REGEX_BRANCH_NAME_COPILOT.exec(branchName)
147+
if (matches && matches.groups) {
141148
return {
142-
branchId: parseInt(branchId, 10),
143-
branchName: branchN,
149+
branchId: parseInt(matches.groups['id'], 10),
150+
branchName: matches.groups['name'],
144151
}
145152
} else {
146153
throw new Error(

0 commit comments

Comments
 (0)