Skip to content

Commit

Permalink
Make the backport branch name used configurable (#478)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenlouv authored Sep 19, 2023
1 parent a195a78 commit 0baf729
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/cli-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ The CLI options will override the [config-file-options.md](/docs/config-file-opt
| --signoff | -s | Pass the --signoff option to the cherry-pick command | false |
| --copySourcePRLabels | | Copy labels from source PR to the target PR | false |
| --copySourcePRReviewers | | Copy reviewers from source PR to the target PR | false |
| --backportBranchName | | Name template to use for the branch name of the backport | |
| --source-branch | | Specify a non-default branch to backport from | |
| --source-pr-label | | Labels added to the source PR | |
| --target-branch | -b | Target branch(es) to backport to | |
Expand Down
21 changes: 21 additions & 0 deletions docs/config-file-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -518,3 +518,24 @@ Labels that will be added to the target (backport) pull request. This can be use
"targetPRLabels": ["backport", "apm-team"]
}
```

#### `backportBranchName`

Branch name to use for the backport PR
Template values:

- `{{targetBranch}}`: Branch the backport PR will be targeting
- `{{sourcePullRequest}}`: Original pull request object (see [interface](https://github.com/sqren/backport/blob/9e42503a7d0e06e60c575ed2c3b7dc3e5df0dd5c/src/lib/sourceCommit/parseSourceCommit.ts#L23-L31))
- `{{refValues}}`: Name representing the original commit/PR, `commit-<hash>` or `pr-<pr number>` respectively.

Default: `backport/{{targetBranch}}/{{refValues}}`

**Example**

```json
{
"backportBranchName": "{{targetBranch}}-{{refValues}}-backport"
}
```

See [source code](https://github.com/sqren/backport/blob/main/src/lib/cherrypickAndCreateTargetPullRequest/getBackportBranchName.ts#L14).
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ export async function cherrypickAndCreateTargetPullRequest({
commits: Commit[];
targetBranch: string;
}): Promise<{ url: string; number: number; didUpdate: boolean }> {
const backportBranch = getBackportBranchName(targetBranch, commits);
const backportBranch = getBackportBranchName({
options,
targetBranch,
commits,
});
const repoForkOwner = getRepoForkOwner(options);
consoleLog(`\n${chalk.bold(`Backporting to ${targetBranch}:`)}`);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Commit } from '../../entrypoint.api';
import { ValidConfigOptions } from '../../options/options';
import { getBackportBranchName } from './getBackportBranchName';

const commit = { sourcePullRequest: { number: 1234 } } as Commit;

describe('getBackportBranchName', () => {
it('returns the default name', () => {
const name = getBackportBranchName({
options: {
backportBranchName: undefined,
} as ValidConfigOptions,
targetBranch: '7.x',
commits: [commit],
});
expect(name).toBe('backport/7.x/pr-1234');
});

it('returns a custom name', () => {
const name = getBackportBranchName({
options: {
backportBranchName: 'bp/pull-{{sourcePullRequest.number}}',
} as ValidConfigOptions,
targetBranch: '7.x',
commits: [commit],
});
expect(name).toBe('bp/pull-1234');
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import Handlebars from 'handlebars';
import { Commit } from '../../entrypoint.api';
import { ValidConfigOptions } from '../../options/options';
import { getShortSha } from '../github/commitFormatters';

/*
Expand All @@ -9,7 +11,15 @@ import { getShortSha } from '../github/commitFormatters';
* For a single commit: `backport/7.x/commit-abcdef`
* For multiple: `backport/7.x/pr-1234_commit-abcdef`
*/
export function getBackportBranchName(targetBranch: string, commits: Commit[]) {
export function getBackportBranchName({
options,
targetBranch,
commits,
}: {
options: ValidConfigOptions;
targetBranch: string;
commits: Commit[];
}) {
const refValues = commits
.map((commit) =>
commit.sourcePullRequest
Expand All @@ -18,5 +28,14 @@ export function getBackportBranchName(targetBranch: string, commits: Commit[]) {
)
.join('_')
.slice(0, 200);
return `backport/${targetBranch}/${refValues}`;
const defaultBackportBranchName = 'backport/{{targetBranch}}/{{refValues}}';
const template = Handlebars.compile(
options.backportBranchName ?? defaultBackportBranchName,
);

return template({
sourcePullRequest: commits[0].sourcePullRequest, // assume that all commits are from the same PR
targetBranch,
refValues,
});
}
1 change: 1 addition & 0 deletions src/options/ConfigOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Options = Partial<{
autoMerge: boolean;
autoMergeMethod: string;
backportBinary: string;
backportBranchName: string;
cherrypickRef: boolean;
commitConflicts: boolean;
commitPaths: string[];
Expand Down
5 changes: 5 additions & 0 deletions src/options/cliArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,11 @@ export function getOptionsFromCliArgs(processArgs: readonly string[]) {
string: true,
})

.option('backportBranchName', {
description: 'Name template to use for the branch name of the backport',
type: 'string',
})

// cli-only
.option('verify', {
description: `Opposite of no-verify`,
Expand Down
1 change: 1 addition & 0 deletions src/options/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ function throwForRequiredOptions(
'author',
'autoMergeMethod',
'backportBinary',
'backportBranchName',
'dir',
'editor',
'gitHostname',
Expand Down
2 changes: 2 additions & 0 deletions src/test/e2e/cli/entrypoint.cli.private.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ Options:
-b, --targetBranch, --branch Branch(es) to backport to [array]
--targetBranchChoice List branches to backport to [array]
-l, --targetPRLabel, --label Add labels to the target (backport) PR [array]
--backportBranchName Name template to use for the branch name of
the backport [string]
--verify Opposite of no-verify [boolean]
--help Show help [boolean]
Expand Down

0 comments on commit 0baf729

Please sign in to comment.