diff --git a/package-lock.json b/package-lock.json index e5924ce..974aa77 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14836,7 +14836,7 @@ }, "packages/branch-utilities": { "name": "@shiftcode/branch-utilities", - "version": "4.0.0", + "version": "5.0.0-pr58.3", "license": "MIT", "engines": { "node": "^20.0.0 || ^22.0.0" @@ -14921,7 +14921,7 @@ }, "packages/publish-helper": { "name": "@shiftcode/publish-helper", - "version": "4.0.0", + "version": "4.1.0-pr58.2", "license": "MIT", "dependencies": { "conventional-changelog-angular": "^8.0.0", @@ -14932,14 +14932,14 @@ "publish-lib": "dist/publish-lib.js" }, "devDependencies": { - "@shiftcode/branch-utilities": "^4.0.0", + "@shiftcode/branch-utilities": "^5.0.0-pr58.3", "@types/yargs": "^17.0.32" }, "engines": { "node": "^20.0.0 || ^22.0.0" }, "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" } diff --git a/packages/branch-utilities/package.json b/packages/branch-utilities/package.json index 9074363..fefc5bd 100644 --- a/packages/branch-utilities/package.json +++ b/packages/branch-utilities/package.json @@ -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", diff --git a/packages/branch-utilities/src/lib/base.utils.spec.ts b/packages/branch-utilities/src/lib/base.utils.spec.ts index fdaf49b..e57ca14 100644 --- a/packages/branch-utilities/src/lib/base.utils.spec.ts +++ b/packages/branch-utilities/src/lib/base.utils.spec.ts @@ -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) + 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) + expect(parseBranchName('copilot/feat-123')).toEqual({ branchId: 123, branchName: 'feat' } satisfies ReturnType) + }) + test('throws when invalid pattern', () => { expect(() => parseBranchName('whrjwe')).toThrow() + expect(() => parseBranchName('copilot/123-fix')).toThrow() + expect(() => parseBranchName('feat/copilot/fix-123')).toThrow() }) }) }) diff --git a/packages/branch-utilities/src/lib/base.utils.ts b/packages/branch-utilities/src/lib/base.utils.ts index df60bf8..5f39748 100644 --- a/packages/branch-utilities/src/lib/base.utils.ts +++ b/packages/branch-utilities/src/lib/base.utils.ts @@ -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]*\/?#(?\d+)-(?.*)$/ +/** + * 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\/(?.*)-(?\d+)$/ export interface StageInfo { isProd: boolean @@ -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( @@ -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) } /** diff --git a/packages/publish-helper/package.json b/packages/publish-helper/package.json index b846663..2f45e6b 100644 --- a/packages/publish-helper/package.json +++ b/packages/publish-helper/package.json @@ -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", @@ -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" },