Skip to content

Commit

Permalink
Improve prTitle and prDescription templating
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenlouv committed Aug 30, 2023
1 parent 471b401 commit 18acd4f
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 94 deletions.
177 changes: 83 additions & 94 deletions src/lib/github/v3/createPullRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,102 +431,91 @@ describe('getPullRequestBody', () => {
});

describe('getTitle', () => {
const commits = [
{
author: {
email: '[email protected]',
name: 'Søren Louv-Jansen',
},
sourceBranch: 'main',
sourcePullRequest: {
labels: [],
title: 'My PR Title',
number: 55,
url: 'https://github.com/backport-org/different-merge-strategies/pull/55',
mergeCommit: {
sha: 'abcdefghi',
message: 'My commit message (#55)',
},
},
suggestedTargetBranches: [],
sourceCommit: {
branchLabelMapping: {},
committedDate: '2020',
sha: 'abcdefghi',
message: 'My commit message (#55)',
},
targetPullRequestStates: [],
},
{
author: {
email: '[email protected]',
name: 'Søren Louv-Jansen',
},
sourcePullRequest: {
labels: [],
number: 56,
title: 'My PR Title',
url: 'https://github.com/backport-org/different-merge-strategies/pull/56',
mergeCommit: {
sha: 'jklmnopqr',
message: 'Another commit message (#56)',
},
},
suggestedTargetBranches: [],
sourceCommit: {
branchLabelMapping: {},
committedDate: '2020',
sha: 'jklmnopqr',
message: 'Another commit message (#56)',
},
sourceBranch: 'main',
targetPullRequestStates: [],
},
];

it('has the default title', () => {
expect(
getTitle({
options: {} as ValidConfigOptions,
commits: [
{
author: {
email: '[email protected]',
name: 'Søren Louv-Jansen',
},
sourceBranch: 'main',
sourcePullRequest: {
labels: [],
title: 'My PR Title',
number: 55,
url: 'https://github.com/backport-org/different-merge-strategies/pull/55',
mergeCommit: {
sha: 'abcdefghi',
message: 'My commit message (#55)',
},
},
suggestedTargetBranches: [],
sourceCommit: {
branchLabelMapping: {},
committedDate: '2020',
sha: 'abcdefghi',
message: 'My commit message (#55)',
},
targetPullRequestStates: [],
},
{
author: {
email: '[email protected]',
name: 'Søren Louv-Jansen',
},
sourcePullRequest: {
labels: [],
number: 56,
title: 'My PR Title',
url: 'https://github.com/backport-org/different-merge-strategies/pull/56',
mergeCommit: {
sha: 'jklmnopqr',
message: 'Another commit message (#56)',
},
},
suggestedTargetBranches: [],
sourceCommit: {
branchLabelMapping: {},
committedDate: '2020',
sha: 'jklmnopqr',
message: 'Another commit message (#56)',
},
sourceBranch: 'main',
targetPullRequestStates: [],
},
],
targetBranch: '7.x',
}),
).toEqual('[7.x] My commit message (#55) | Another commit message (#56)');
const options = {} as ValidConfigOptions;
expect(getTitle({ options, commits, targetBranch: '7.x' })).toEqual(
'[7.x] My commit message (#55) | Another commit message (#56)',
);
});

it('replaces template variables in PR title', () => {
expect(
getTitle({
options: {
prTitle: 'Branch: "{{targetBranch}}". Messages: {{commitMessages}}',
} as ValidConfigOptions,
commits: [
{
author: {
email: '[email protected]',
name: 'Søren Louv-Jansen',
},
sourcePullRequest: {
labels: [],
number: 55,
title: 'My PR Title',
url: 'https://github.com/backport-org/different-merge-strategies/pull/55',
mergeCommit: {
sha: 'abcdefghi',
message: 'My commit message (#55)',
},
},
suggestedTargetBranches: [],
sourceCommit: {
branchLabelMapping: {},
committedDate: '',
sha: 'abcdefghi',
message: 'My commit message (#55)',
},
sourceBranch: 'main',
targetPullRequestStates: [],
},
],
targetBranch: '7.x',
}),
).toEqual('Branch: "7.x". Messages: My commit message (#55)');
it('renders title with the original PR title', () => {
const options = {
prTitle: '[{{targetBranch}}] {{sourcePullRequest.title}}',
} as ValidConfigOptions;
expect(getTitle({ options, commits, targetBranch: '7.x' })).toEqual(
'[7.x] My PR Title',
);
});

it('renders title using a specific commit', () => {
const options = {
prTitle: '[{{targetBranch}}] {{commits.0.sourcePullRequest.title}}',
} as ValidConfigOptions;
expect(getTitle({ options, commits, targetBranch: '7.x' })).toEqual(
'[7.x] My PR Title',
);
});

it('renders title using {{commitMessages}}', () => {
const options = {
prTitle: 'Branch: "{{targetBranch}}". Messages: {{commitMessages}}',
} as ValidConfigOptions;

expect(getTitle({ options, commits, targetBranch: '7.x' })).toEqual(
'Branch: "7.x". Messages: My commit message (#55) | Another commit message (#56)',
);
});
});
4 changes: 4 additions & 0 deletions src/lib/github/v3/createPullRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export function getPullRequestBody({
);

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

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

commits[0].author.name;

return template({
sourcePullRequest: commits[0].sourcePullRequest, // assume that all commits are from the same PR
sourceBranch,
targetBranch,
commitMessages,
Expand Down

0 comments on commit 18acd4f

Please sign in to comment.