Skip to content

Commit 5cdbc5b

Browse files
authored
fix: allow brackets in custom pull request title (#1445)
Fixes #1444
1 parent 8473c99 commit 5cdbc5b

4 files changed

Lines changed: 72 additions & 5 deletions

File tree

src/util/pull-request-title.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ export function generateMatchPattern(pullRequestTitlePattern?: string): RegExp {
3939
logger.warn("pullRequestTitlePattern miss the part of '${version}'");
4040
return new RegExp(
4141
`^${(pullRequestTitlePattern || DEFAULT_PR_TITLE_PATTERN)
42-
.replace('${scope}', '(\\((?<branch>[\\w-.]+)\\))?')
42+
.replace('[', '\\[') // TODO: handle all regex escaping
43+
.replace(']', '\\]')
44+
.replace('${scope}', '(\\((?<branch>[\\w-./]+)\\))?')
4345
.replace('${component}', ' ?(?<component>[\\w-.]*)?')
4446
.replace('${version}', 'v?(?<version>[0-9].*)')
45-
.replace('${branch}', '(?<branch>[\\w-.]+)?')}$`
47+
.replace('${branch}', '(?<branch>[\\w-./]+)?')}$`
4648
);
4749
}
4850

@@ -80,7 +82,7 @@ export class PullRequestTitle {
8082
: undefined,
8183
component: match.groups['component'],
8284
targetBranch: match.groups['branch'],
83-
pullRequestTitlePattern: pullRequestTitlePattern,
85+
pullRequestTitlePattern,
8486
});
8587
}
8688
return undefined;

test/manifest.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4532,6 +4532,48 @@ describe('Manifest', () => {
45324532
const releases = await manifest.buildReleases();
45334533
expect(releases).lengthOf(0);
45344534
});
4535+
4536+
it('should handle complex title and base branch', async () => {
4537+
mockPullRequests(
4538+
github,
4539+
[],
4540+
[
4541+
{
4542+
headBranchName:
4543+
'release-please--branches--hotfix/v3.1.0-bug--components--my-package-name',
4544+
baseBranchName: 'hotfix/v3.1.0-bug',
4545+
number: 1234,
4546+
title: '[HOTFIX] - chore(hotfix/v3.1.0-bug): release 3.1.0-hotfix1',
4547+
body: pullRequestBody('release-notes/single.txt'),
4548+
labels: ['autorelease: pending'],
4549+
files: [],
4550+
sha: 'abc123',
4551+
},
4552+
]
4553+
);
4554+
const manifest = new Manifest(
4555+
github,
4556+
'hotfix/v3.1.0-bug',
4557+
{
4558+
'.': {
4559+
releaseType: 'simple',
4560+
pullRequestTitlePattern:
4561+
'[HOTFIX] - chore${scope}: release${component} ${version}',
4562+
packageName: 'my-package-name',
4563+
includeComponentInTag: false,
4564+
},
4565+
},
4566+
{
4567+
'.': Version.parse('3.1.0'),
4568+
}
4569+
);
4570+
const releases = await manifest.buildReleases();
4571+
expect(releases).lengthOf(1);
4572+
expect(releases[0].tag.toString()).to.eql('v3.1.0-hotfix1');
4573+
expect(releases[0].sha).to.eql('abc123');
4574+
expect(releases[0].notes).to.be.a('string');
4575+
expect(releases[0].path).to.eql('.');
4576+
});
45354577
});
45364578

45374579
describe('createReleases', () => {

test/util/branch-name.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,16 @@ describe('BranchName', () => {
106106
expect(branchName?.toString()).to.eql(name);
107107
});
108108

109+
it('parses a target branch that has a /', () => {
110+
const name = 'release-please--branches--hotfix/3.3.x';
111+
const branchName = BranchName.parse(name);
112+
expect(branchName).to.not.be.undefined;
113+
expect(branchName?.getTargetBranch()).to.eql('hotfix/3.3.x');
114+
expect(branchName?.getComponent()).to.be.undefined;
115+
expect(branchName?.getVersion()).to.be.undefined;
116+
expect(branchName?.toString()).to.eql(name);
117+
});
118+
109119
it('fails to parse', () => {
110120
const branchName = BranchName.parse('release-foo');
111121
expect(branchName).to.be.undefined;

test/util/pull-request-title.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ describe('PullRequestTitle', () => {
124124
it('return matchPattern with default Pattern', () => {
125125
const matchPattern = generateMatchPattern();
126126
expect(matchPattern).to.eql(
127-
/^chore(\((?<branch>[\w-.]+)\))?: release ?(?<component>[\w-.]*)? v?(?<version>[0-9].*)$/
127+
/^chore(\((?<branch>[\w-./]+)\))?: release ?(?<component>[\w-.]*)? v?(?<version>[0-9].*)$/
128128
);
129129
});
130130
});
@@ -212,6 +212,19 @@ describe('PullRequestTitle with custom pullRequestTitlePattern', () => {
212212
expect(pullRequestTitle?.getComponent()).to.be.undefined;
213213
expect(pullRequestTitle?.getVersion()).to.be.undefined;
214214
});
215+
216+
it('parses a complex title and pattern', () => {
217+
const pullRequestTitle = PullRequestTitle.parse(
218+
'[HOTFIX] - chore(hotfix/v3.1.0-bug): release 3.1.0-hotfix1',
219+
'[HOTFIX] - chore${scope}: release${component} ${version}'
220+
);
221+
expect(pullRequestTitle).to.not.be.undefined;
222+
expect(pullRequestTitle?.getTargetBranch()).to.eql('hotfix/v3.1.0-bug');
223+
expect(pullRequestTitle?.getVersion()?.toString()).to.eql(
224+
'3.1.0-hotfix1'
225+
);
226+
expect(pullRequestTitle?.getComponent()).to.be.undefined;
227+
});
215228
});
216229
describe('ofVersion', () => {
217230
it('builds the autorelease versioned branch name', () => {
@@ -265,7 +278,7 @@ describe('PullRequestTitle with custom pullRequestTitlePattern', () => {
265278
'chore${scope}: 🔖 release${component} ${version}'
266279
);
267280
expect(matchPattern).to.eql(
268-
/^chore(\((?<branch>[\w-.]+)\))?: 🔖 release ?(?<component>[\w-.]*)? v?(?<version>[0-9].*)$/
281+
/^chore(\((?<branch>[\w-./]+)\))?: 🔖 release ?(?<component>[\w-.]*)? v?(?<version>[0-9].*)$/
269282
);
270283
});
271284

0 commit comments

Comments
 (0)