Skip to content
Merged
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/branch-utilities/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@shiftcode/branch-utilities",
"version": "4.0.0",
"version": "5.0.0-pr58.3",
"description": "Utilities for local and ci to get branch name and stage",
"repository": "https://github.com/shiftcode/sc-commons-public",
"license": "MIT",
Expand Down
15 changes: 14 additions & 1 deletion packages/branch-utilities/src/lib/base.utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,27 @@ describe('base utils', () => {
})

describe('parseBranchName', () => {

test('works when valid pattern', () => {
expect(parseBranchName('#7-abc').branchId).toBe(7)
expect(parseBranchName('#7-abc')).toEqual({ branchId: 7, branchName: 'abc' } satisfies ReturnType<typeof parseBranchName>)

expect(parseBranchName('#72-abc').branchId).toBe(72)
expect(parseBranchName('#000-int').branchId).toBe(0)
expect(parseBranchName('#001-test').branchId).toBe(1)

expect(parseBranchName('#72- whatever').branchId).toBe(72)
expect(parseBranchName('feature/#72-ok').branchId).toBe(72)
})

test('works for github copilot created branches', () => {
expect(parseBranchName('copilot/fix-123')).toEqual({ branchId: 123, branchName: 'fix' } satisfies ReturnType<typeof parseBranchName>)
expect(parseBranchName('copilot/feat-123')).toEqual({ branchId: 123, branchName: 'feat' } satisfies ReturnType<typeof parseBranchName>)
})

test('throws when invalid pattern', () => {
expect(() => parseBranchName('whrjwe')).toThrow()
expect(() => parseBranchName('copilot/123-fix')).toThrow()
expect(() => parseBranchName('feat/copilot/fix-123')).toThrow()
})
})
})
25 changes: 16 additions & 9 deletions packages/branch-utilities/src/lib/base.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,17 @@ export const REGEX_MASTER = /^master$/
/** regex to match the main branch */
export const REGEX_MAIN = /^main$/

/** regex to match our branch conventions with the following capture groups: fullMatch / branch id / branch name */
export const REGEX_BRANCH_NAME = /^[a-z]*\/?#(\d+)-(.*)/
/**
* regex to match our branch conventions with the following named capture groups: id, name
* @example #123-my-feature -> { id: '123', name: 'my-feature' }
* @example feature/#456-yanr -> { id: '456', name: 'yanr' }
*/
const REGEX_BRANCH_NAME_DEFAULT = /^[a-z]*\/?#(?<id>\d+)-(?<name>.*)$/
/**
* regex to match the branch convention github copilot uses with the following named capture groups: id, name
* @example copilot/fix-789 -> { id: '789', name: 'fix' }
*/
const REGEX_BRANCH_NAME_COPILOT = /^copilot\/(?<name>.*)-(?<id>\d+)$/

export interface StageInfo {
isProd: boolean
Expand Down Expand Up @@ -134,13 +143,11 @@ export function isMainBranch(branchName: string): boolean {
* @throws Throws an error if given branchName does not match our convention
*/
export function parseBranchName(branchName: string): { branchId: number; branchName: string } {
const matches = branchName.match(REGEX_BRANCH_NAME)
if (matches) {
// [0] full match / [1] branch id / [2] branch name
const [, branchId, branchN] = matches
const matches = REGEX_BRANCH_NAME_DEFAULT.exec(branchName) ?? REGEX_BRANCH_NAME_COPILOT.exec(branchName)
if (matches?.groups) {
return {
branchId: parseInt(branchId, 10),
branchName: branchN,
branchId: parseInt(matches.groups['id'], 10),
branchName: matches.groups['name'],
}
} else {
throw new Error(
Expand All @@ -156,7 +163,7 @@ export function parseBranchName(branchName: string): { branchId: number; branchN
* @return returns true if the stage is 'master' or 'main', false if not
*/
export function isProduction(stageName: string): boolean {
return REGEX_MASTER.test(stageName) || REGEX_MAIN.test(stageName)
return REGEX_MASTER.test(stageName) ?? REGEX_MAIN.test(stageName)
}

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/publish-helper/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@shiftcode/publish-helper",
"version": "4.0.0",
"version": "4.1.0-pr58.2",
"description": "scripts for conventional (pre)releases",
"repository": "https://github.com/shiftcode/sc-commons-public",
"license": "MIT",
Expand Down Expand Up @@ -30,11 +30,11 @@
"yargs": "^17.7.2"
},
"devDependencies": {
"@shiftcode/branch-utilities": "^4.0.0",
"@shiftcode/branch-utilities": "^5.0.0-pr58.3",
"@types/yargs": "^17.0.32"
},
"peerDependencies": {
"@shiftcode/branch-utilities": "^4.0.0 || ^4.0.0-pr45",
"@shiftcode/branch-utilities": "^4.0.0 || ^5.0.0-pr58 || ^5.0.0",
"lerna": "^8.1.6",
"tslib": "^2.3.0"
},
Expand Down