Skip to content

Commit a060990

Browse files
authored
Merge pull request #61 from shiftcode/#58-branch-name-convention
feat(branch-utilities): support naming convention for github copilot …
2 parents fc2a0fc + c079195 commit a060990

File tree

5 files changed

+38
-18
lines changed

5 files changed

+38
-18
lines changed

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/branch-utilities/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@shiftcode/branch-utilities",
3-
"version": "4.0.0",
3+
"version": "5.0.0-pr58.3",
44
"description": "Utilities for local and ci to get branch name and stage",
55
"repository": "https://github.com/shiftcode/sc-commons-public",
66
"license": "MIT",

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,27 @@ 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+
expect(parseBranchName('copilot/feat-123')).toEqual({ branchId: 123, branchName: 'feat' } satisfies ReturnType<typeof parseBranchName>)
99+
})
100+
90101
test('throws when invalid pattern', () => {
91102
expect(() => parseBranchName('whrjwe')).toThrow()
103+
expect(() => parseBranchName('copilot/123-fix')).toThrow()
104+
expect(() => parseBranchName('feat/copilot/fix-123')).toThrow()
92105
})
93106
})
94107
})

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

Lines changed: 16 additions & 9 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>.*)-(?<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?.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(
@@ -156,7 +163,7 @@ export function parseBranchName(branchName: string): { branchId: number; branchN
156163
* @return returns true if the stage is 'master' or 'main', false if not
157164
*/
158165
export function isProduction(stageName: string): boolean {
159-
return REGEX_MASTER.test(stageName) || REGEX_MAIN.test(stageName)
166+
return REGEX_MASTER.test(stageName) ?? REGEX_MAIN.test(stageName)
160167
}
161168

162169
/**

packages/publish-helper/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@shiftcode/publish-helper",
3-
"version": "4.0.0",
3+
"version": "4.1.0-pr58.2",
44
"description": "scripts for conventional (pre)releases",
55
"repository": "https://github.com/shiftcode/sc-commons-public",
66
"license": "MIT",
@@ -30,11 +30,11 @@
3030
"yargs": "^17.7.2"
3131
},
3232
"devDependencies": {
33-
"@shiftcode/branch-utilities": "^4.0.0",
33+
"@shiftcode/branch-utilities": "^5.0.0-pr58.3",
3434
"@types/yargs": "^17.0.32"
3535
},
3636
"peerDependencies": {
37-
"@shiftcode/branch-utilities": "^4.0.0 || ^4.0.0-pr45",
37+
"@shiftcode/branch-utilities": "^4.0.0 || ^5.0.0-pr58 || ^5.0.0",
3838
"lerna": "^8.1.6",
3939
"tslib": "^2.3.0"
4040
},

0 commit comments

Comments
 (0)