Skip to content

Commit 25c13c6

Browse files
authored
Handle uncaught handlebars exception (#526)
Resolves handlebars errors like: ``` runSequentially failed Parse error on line 11: ```metadata={{ extraA -----------------------^\nExpecting 'ID', 'STRING', 'NUMBER', 'BOOLEAN', 'UNDEFINED', 'NULL', 'DATA', got 'INVALID' ```
1 parent 15c1eac commit 25c13c6

25 files changed

+970
-851
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"github-schema.graphql": true,
1414
"LICENSE.txt": true
1515
},
16-
"editor.formatOnSave": true,
16+
"editor.formatOnSave": false,
1717
"editor.codeActionsOnSave": {
1818
"source.fixAll.eslint": "explicit"
1919
},

docs/config-file-options.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,8 @@ Template values:
342342
- `{{targetBranch}}`: Branch the backport PR will be targeting
343343
- `{{sourcePullRequest}}`: Original pull request object (see [`Commit` interface](https://github.com/sorenlouv/backport/blob/main/src/lib/sourceCommit/parseSourceCommit.ts))
344344
- `{{commitMessages}}`: Message of backported commit. For multiple commits the messages will be separated by pipes (`|`).
345-
- `{{commits}}`: A list of commits ([`Commit` interface](https://github.com/sorenlouv/backport/blob/main/src/lib/sourceCommit/parseSourceCommit.ts))
345+
- `{{commits}}`: An array of selected commits ([`Commit` interface](https://github.com/sorenlouv/backport/blob/main/src/lib/sourceCommit/parseSourceCommit.ts))
346+
- `{{commitsStringified}}`: Array of commits stringified for easier output
346347

347348
**Default**
348349

src/backportRun.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { createStatusComment } from './lib/github/v3/createStatusComment';
99
import { GithubV4Exception } from './lib/github/v4/apiRequestV4';
1010
import { consoleLog, initLogger } from './lib/logger';
1111
import { ora } from './lib/ora';
12+
import { registerHandlebarsHelpers } from './lib/registerHandlebarsHelpers';
13+
import { runSequentially, Result } from './lib/runSequentially';
1214
import { setupRepo } from './lib/setupRepo';
1315
import { Commit } from './lib/sourceCommit/parseSourceCommit';
1416
import { ConfigFileOptions } from './options/ConfigOptions';
@@ -22,7 +24,8 @@ import {
2224
getOptions,
2325
ValidConfigOptions,
2426
} from './options/options';
25-
import { runSequentially, Result } from './runSequentially';
27+
28+
registerHandlebarsHelpers();
2629

2730
export type BackportAbortResponse = {
2831
status: 'aborted';

src/entrypoint.api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export type {
1818
HandledErrorResult,
1919
SuccessResult,
2020
UnhandledErrorResult,
21-
} from './runSequentially';
21+
} from './lib/runSequentially';
2222
export type {
2323
BackportAbortResponse,
2424
BackportFailureResponse,

src/lib/cherrypickAndCreateTargetPullRequest/cherrypickAndCreateTargetPullRequest.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ import { addLabelsToPullRequest } from '../github/v3/addLabelsToPullRequest';
1313
import { addReviewersToPullRequest } from '../github/v3/addReviewersToPullRequest';
1414
import {
1515
createPullRequest,
16-
getTitle,
17-
getPullRequestBody,
1816
PullRequestPayload,
19-
} from '../github/v3/createPullRequest';
17+
} from '../github/v3/getPullRequest/createPullRequest';
18+
import { getPullRequestBody } from '../github/v3/getPullRequest/getPullRequestBody';
19+
import { getTitle } from '../github/v3/getPullRequest/getTitle';
2020
import { validateTargetBranch } from '../github/v4/validateTargetBranch';
2121
import { consoleLog } from '../logger';
2222
import { sequentially } from '../sequentially';

src/lib/cherrypickAndCreateTargetPullRequest/getBackportBranchName.test.ts

Lines changed: 68 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,78 @@ import { Commit } from '../../entrypoint.api';
22
import { ValidConfigOptions } from '../../options/options';
33
import { getBackportBranchName } from './getBackportBranchName';
44

5-
const commit = { sourcePullRequest: { number: 1234 } } as Commit;
6-
75
describe('getBackportBranchName', () => {
8-
it('returns the default name', () => {
9-
const name = getBackportBranchName({
10-
options: {
11-
backportBranchName: undefined,
12-
} as ValidConfigOptions,
13-
targetBranch: '7.x',
14-
commits: [commit],
6+
describe('when options.backportBranchName is not set', () => {
7+
it('returns the default name with PR number', () => {
8+
const commits = [{ sourcePullRequest: { number: 1234 } }] as Commit[];
9+
const name = getBackportBranchName({
10+
options: { backportBranchName: undefined } as ValidConfigOptions,
11+
targetBranch: '7.x',
12+
commits,
13+
});
14+
expect(name).toBe('backport/7.x/pr-1234');
15+
});
16+
17+
it('returns the default name with commit sha', () => {
18+
const commits = [{ sourceCommit: { sha: 'abcde' } }] as Commit[];
19+
const name = getBackportBranchName({
20+
options: { backportBranchName: undefined } as ValidConfigOptions,
21+
targetBranch: '7.x',
22+
commits,
23+
});
24+
expect(name).toBe('backport/7.x/commit-abcde');
1525
});
16-
expect(name).toBe('backport/7.x/pr-1234');
1726
});
1827

19-
it('returns a custom name', () => {
20-
const name = getBackportBranchName({
21-
options: {
22-
backportBranchName: 'bp/pull-{{sourcePullRequest.number}}',
23-
} as ValidConfigOptions,
24-
targetBranch: '7.x',
25-
commits: [commit],
28+
describe('template variables are supported when using options.backportBranchName', () => {
29+
it('{{targetBranch}}', () => {
30+
const commits = [{ sourcePullRequest: { number: 1234 } }] as Commit[];
31+
const name = getBackportBranchName({
32+
options: {
33+
backportBranchName: 'bp/target-{{targetBranch}}',
34+
} as ValidConfigOptions,
35+
targetBranch: '7.x',
36+
commits,
37+
});
38+
expect(name).toBe('bp/target-7.x');
39+
});
40+
41+
it('{{refValues}}', () => {
42+
const commits = [{ sourcePullRequest: { number: 1234 } }] as Commit[];
43+
const name = getBackportBranchName({
44+
options: {
45+
backportBranchName: 'bp/ref-{{refValues}}',
46+
} as ValidConfigOptions,
47+
targetBranch: '7.x',
48+
commits,
49+
});
50+
expect(name).toBe('bp/ref-pr-1234');
51+
});
52+
53+
it('{{sourcePullRequest.number}}', () => {
54+
const commits = [{ sourcePullRequest: { number: 1234 } }] as Commit[];
55+
const name = getBackportBranchName({
56+
options: {
57+
backportBranchName: 'bp/pr-{{sourcePullRequest.number}}',
58+
} as ValidConfigOptions,
59+
targetBranch: '7.x',
60+
commits,
61+
});
62+
expect(name).toBe('bp/pr-1234');
63+
});
64+
65+
it('{{sourcePullRequest.title}}', () => {
66+
const commits = [
67+
{ sourcePullRequest: { title: 'My PR title', number: 1234 } },
68+
] as Commit[];
69+
const name = getBackportBranchName({
70+
options: {
71+
backportBranchName: 'bp/pr-{{sourcePullRequest.title}}',
72+
} as ValidConfigOptions,
73+
targetBranch: '7.x',
74+
commits,
75+
});
76+
expect(name).toBe('bp/pr-My PR title');
2677
});
27-
expect(name).toBe('bp/pull-1234');
2878
});
2979
});

src/lib/cherrypickAndCreateTargetPullRequest/getBackportBranchName.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@ export function getBackportBranchName({
2828
)
2929
.join('_')
3030
.slice(0, 200);
31+
32+
const sourcePullRequest = commits[0].sourcePullRequest; // assume that all commits are from the same PR
3133
const defaultBackportBranchName = 'backport/{{targetBranch}}/{{refValues}}';
32-
const template = Handlebars.compile(
33-
options.backportBranchName ?? defaultBackportBranchName,
34-
);
3534

35+
const backportBranchName =
36+
options.backportBranchName ?? defaultBackportBranchName;
37+
38+
const template = Handlebars.compile(backportBranchName);
3639
return template({
37-
sourcePullRequest: commits[0].sourcePullRequest, // assume that all commits are from the same PR
40+
sourcePullRequest,
3841
targetBranch,
3942
refValues,
4043
});

0 commit comments

Comments
 (0)