Skip to content

Commit 18acd4f

Browse files
committed
Improve prTitle and prDescription templating
1 parent 471b401 commit 18acd4f

File tree

2 files changed

+87
-94
lines changed

2 files changed

+87
-94
lines changed

src/lib/github/v3/createPullRequest.test.ts

Lines changed: 83 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -431,102 +431,91 @@ describe('getPullRequestBody', () => {
431431
});
432432

433433
describe('getTitle', () => {
434+
const commits = [
435+
{
436+
author: {
437+
438+
name: 'Søren Louv-Jansen',
439+
},
440+
sourceBranch: 'main',
441+
sourcePullRequest: {
442+
labels: [],
443+
title: 'My PR Title',
444+
number: 55,
445+
url: 'https://github.com/backport-org/different-merge-strategies/pull/55',
446+
mergeCommit: {
447+
sha: 'abcdefghi',
448+
message: 'My commit message (#55)',
449+
},
450+
},
451+
suggestedTargetBranches: [],
452+
sourceCommit: {
453+
branchLabelMapping: {},
454+
committedDate: '2020',
455+
sha: 'abcdefghi',
456+
message: 'My commit message (#55)',
457+
},
458+
targetPullRequestStates: [],
459+
},
460+
{
461+
author: {
462+
463+
name: 'Søren Louv-Jansen',
464+
},
465+
sourcePullRequest: {
466+
labels: [],
467+
number: 56,
468+
title: 'My PR Title',
469+
url: 'https://github.com/backport-org/different-merge-strategies/pull/56',
470+
mergeCommit: {
471+
sha: 'jklmnopqr',
472+
message: 'Another commit message (#56)',
473+
},
474+
},
475+
suggestedTargetBranches: [],
476+
sourceCommit: {
477+
branchLabelMapping: {},
478+
committedDate: '2020',
479+
sha: 'jklmnopqr',
480+
message: 'Another commit message (#56)',
481+
},
482+
sourceBranch: 'main',
483+
targetPullRequestStates: [],
484+
},
485+
];
486+
434487
it('has the default title', () => {
435-
expect(
436-
getTitle({
437-
options: {} as ValidConfigOptions,
438-
commits: [
439-
{
440-
author: {
441-
442-
name: 'Søren Louv-Jansen',
443-
},
444-
sourceBranch: 'main',
445-
sourcePullRequest: {
446-
labels: [],
447-
title: 'My PR Title',
448-
number: 55,
449-
url: 'https://github.com/backport-org/different-merge-strategies/pull/55',
450-
mergeCommit: {
451-
sha: 'abcdefghi',
452-
message: 'My commit message (#55)',
453-
},
454-
},
455-
suggestedTargetBranches: [],
456-
sourceCommit: {
457-
branchLabelMapping: {},
458-
committedDate: '2020',
459-
sha: 'abcdefghi',
460-
message: 'My commit message (#55)',
461-
},
462-
targetPullRequestStates: [],
463-
},
464-
{
465-
author: {
466-
467-
name: 'Søren Louv-Jansen',
468-
},
469-
sourcePullRequest: {
470-
labels: [],
471-
number: 56,
472-
title: 'My PR Title',
473-
url: 'https://github.com/backport-org/different-merge-strategies/pull/56',
474-
mergeCommit: {
475-
sha: 'jklmnopqr',
476-
message: 'Another commit message (#56)',
477-
},
478-
},
479-
suggestedTargetBranches: [],
480-
sourceCommit: {
481-
branchLabelMapping: {},
482-
committedDate: '2020',
483-
sha: 'jklmnopqr',
484-
message: 'Another commit message (#56)',
485-
},
486-
sourceBranch: 'main',
487-
targetPullRequestStates: [],
488-
},
489-
],
490-
targetBranch: '7.x',
491-
}),
492-
).toEqual('[7.x] My commit message (#55) | Another commit message (#56)');
488+
const options = {} as ValidConfigOptions;
489+
expect(getTitle({ options, commits, targetBranch: '7.x' })).toEqual(
490+
'[7.x] My commit message (#55) | Another commit message (#56)',
491+
);
493492
});
494493

495-
it('replaces template variables in PR title', () => {
496-
expect(
497-
getTitle({
498-
options: {
499-
prTitle: 'Branch: "{{targetBranch}}". Messages: {{commitMessages}}',
500-
} as ValidConfigOptions,
501-
commits: [
502-
{
503-
author: {
504-
505-
name: 'Søren Louv-Jansen',
506-
},
507-
sourcePullRequest: {
508-
labels: [],
509-
number: 55,
510-
title: 'My PR Title',
511-
url: 'https://github.com/backport-org/different-merge-strategies/pull/55',
512-
mergeCommit: {
513-
sha: 'abcdefghi',
514-
message: 'My commit message (#55)',
515-
},
516-
},
517-
suggestedTargetBranches: [],
518-
sourceCommit: {
519-
branchLabelMapping: {},
520-
committedDate: '',
521-
sha: 'abcdefghi',
522-
message: 'My commit message (#55)',
523-
},
524-
sourceBranch: 'main',
525-
targetPullRequestStates: [],
526-
},
527-
],
528-
targetBranch: '7.x',
529-
}),
530-
).toEqual('Branch: "7.x". Messages: My commit message (#55)');
494+
it('renders title with the original PR title', () => {
495+
const options = {
496+
prTitle: '[{{targetBranch}}] {{sourcePullRequest.title}}',
497+
} as ValidConfigOptions;
498+
expect(getTitle({ options, commits, targetBranch: '7.x' })).toEqual(
499+
'[7.x] My PR Title',
500+
);
501+
});
502+
503+
it('renders title using a specific commit', () => {
504+
const options = {
505+
prTitle: '[{{targetBranch}}] {{commits.0.sourcePullRequest.title}}',
506+
} as ValidConfigOptions;
507+
expect(getTitle({ options, commits, targetBranch: '7.x' })).toEqual(
508+
'[7.x] My PR Title',
509+
);
510+
});
511+
512+
it('renders title using {{commitMessages}}', () => {
513+
const options = {
514+
prTitle: 'Branch: "{{targetBranch}}". Messages: {{commitMessages}}',
515+
} as ValidConfigOptions;
516+
517+
expect(getTitle({ options, commits, targetBranch: '7.x' })).toEqual(
518+
'Branch: "7.x". Messages: My commit message (#55) | Another commit message (#56)',
519+
);
531520
});
532521
});

src/lib/github/v3/createPullRequest.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ export function getPullRequestBody({
136136
);
137137

138138
return template({
139+
sourcePullRequest: commits[0].sourcePullRequest, // assume that all commits are from the same PR
139140
sourceBranch,
140141
targetBranch,
141142
commitMessages,
@@ -162,7 +163,10 @@ export function getTitle({
162163

163164
const template = Handlebars.compile(options.prTitle ?? defaultPrTitle);
164165

166+
commits[0].author.name;
167+
165168
return template({
169+
sourcePullRequest: commits[0].sourcePullRequest, // assume that all commits are from the same PR
166170
sourceBranch,
167171
targetBranch,
168172
commitMessages,

0 commit comments

Comments
 (0)