diff --git a/src/util/pull-request-title.ts b/src/util/pull-request-title.ts index ce5756d1f..a75d74b79 100644 --- a/src/util/pull-request-title.ts +++ b/src/util/pull-request-title.ts @@ -39,10 +39,12 @@ export function generateMatchPattern(pullRequestTitlePattern?: string): RegExp { logger.warn("pullRequestTitlePattern miss the part of '${version}'"); return new RegExp( `^${(pullRequestTitlePattern || DEFAULT_PR_TITLE_PATTERN) - .replace('${scope}', '(\\((?[\\w-.]+)\\))?') + .replace('[', '\\[') // TODO: handle all regex escaping + .replace(']', '\\]') + .replace('${scope}', '(\\((?[\\w-./]+)\\))?') .replace('${component}', ' ?(?[\\w-.]*)?') .replace('${version}', 'v?(?[0-9].*)') - .replace('${branch}', '(?[\\w-.]+)?')}$` + .replace('${branch}', '(?[\\w-./]+)?')}$` ); } @@ -80,7 +82,7 @@ export class PullRequestTitle { : undefined, component: match.groups['component'], targetBranch: match.groups['branch'], - pullRequestTitlePattern: pullRequestTitlePattern, + pullRequestTitlePattern, }); } return undefined; diff --git a/test/manifest.ts b/test/manifest.ts index abade1aee..401d8ac34 100644 --- a/test/manifest.ts +++ b/test/manifest.ts @@ -4532,6 +4532,48 @@ describe('Manifest', () => { const releases = await manifest.buildReleases(); expect(releases).lengthOf(0); }); + + it('should handle complex title and base branch', async () => { + mockPullRequests( + github, + [], + [ + { + headBranchName: + 'release-please--branches--hotfix/v3.1.0-bug--components--my-package-name', + baseBranchName: 'hotfix/v3.1.0-bug', + number: 1234, + title: '[HOTFIX] - chore(hotfix/v3.1.0-bug): release 3.1.0-hotfix1', + body: pullRequestBody('release-notes/single.txt'), + labels: ['autorelease: pending'], + files: [], + sha: 'abc123', + }, + ] + ); + const manifest = new Manifest( + github, + 'hotfix/v3.1.0-bug', + { + '.': { + releaseType: 'simple', + pullRequestTitlePattern: + '[HOTFIX] - chore${scope}: release${component} ${version}', + packageName: 'my-package-name', + includeComponentInTag: false, + }, + }, + { + '.': Version.parse('3.1.0'), + } + ); + const releases = await manifest.buildReleases(); + expect(releases).lengthOf(1); + expect(releases[0].tag.toString()).to.eql('v3.1.0-hotfix1'); + expect(releases[0].sha).to.eql('abc123'); + expect(releases[0].notes).to.be.a('string'); + expect(releases[0].path).to.eql('.'); + }); }); describe('createReleases', () => { diff --git a/test/util/branch-name.ts b/test/util/branch-name.ts index b1805d639..84bee93aa 100644 --- a/test/util/branch-name.ts +++ b/test/util/branch-name.ts @@ -106,6 +106,16 @@ describe('BranchName', () => { expect(branchName?.toString()).to.eql(name); }); + it('parses a target branch that has a /', () => { + const name = 'release-please--branches--hotfix/3.3.x'; + const branchName = BranchName.parse(name); + expect(branchName).to.not.be.undefined; + expect(branchName?.getTargetBranch()).to.eql('hotfix/3.3.x'); + expect(branchName?.getComponent()).to.be.undefined; + expect(branchName?.getVersion()).to.be.undefined; + expect(branchName?.toString()).to.eql(name); + }); + it('fails to parse', () => { const branchName = BranchName.parse('release-foo'); expect(branchName).to.be.undefined; diff --git a/test/util/pull-request-title.ts b/test/util/pull-request-title.ts index c6b77f299..561018329 100644 --- a/test/util/pull-request-title.ts +++ b/test/util/pull-request-title.ts @@ -124,7 +124,7 @@ describe('PullRequestTitle', () => { it('return matchPattern with default Pattern', () => { const matchPattern = generateMatchPattern(); expect(matchPattern).to.eql( - /^chore(\((?[\w-.]+)\))?: release ?(?[\w-.]*)? v?(?[0-9].*)$/ + /^chore(\((?[\w-./]+)\))?: release ?(?[\w-.]*)? v?(?[0-9].*)$/ ); }); }); @@ -212,6 +212,19 @@ describe('PullRequestTitle with custom pullRequestTitlePattern', () => { expect(pullRequestTitle?.getComponent()).to.be.undefined; expect(pullRequestTitle?.getVersion()).to.be.undefined; }); + + it('parses a complex title and pattern', () => { + const pullRequestTitle = PullRequestTitle.parse( + '[HOTFIX] - chore(hotfix/v3.1.0-bug): release 3.1.0-hotfix1', + '[HOTFIX] - chore${scope}: release${component} ${version}' + ); + expect(pullRequestTitle).to.not.be.undefined; + expect(pullRequestTitle?.getTargetBranch()).to.eql('hotfix/v3.1.0-bug'); + expect(pullRequestTitle?.getVersion()?.toString()).to.eql( + '3.1.0-hotfix1' + ); + expect(pullRequestTitle?.getComponent()).to.be.undefined; + }); }); describe('ofVersion', () => { it('builds the autorelease versioned branch name', () => { @@ -265,7 +278,7 @@ describe('PullRequestTitle with custom pullRequestTitlePattern', () => { 'chore${scope}: 🔖 release${component} ${version}' ); expect(matchPattern).to.eql( - /^chore(\((?[\w-.]+)\))?: 🔖 release ?(?[\w-.]*)? v?(?[0-9].*)$/ + /^chore(\((?[\w-./]+)\))?: 🔖 release ?(?[\w-.]*)? v?(?[0-9].*)$/ ); });