Skip to content

Commit

Permalink
Merge cfdbaab into 24be079
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenlouv committed Apr 15, 2020
2 parents 24be079 + cfdbaab commit 7cdd72a
Show file tree
Hide file tree
Showing 44 changed files with 577 additions and 201 deletions.
30 changes: 24 additions & 6 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,27 @@ Config:

#### `prTitle`

Pull request title pattern. You can access the base branch (`{baseBranch}`) and commit message (`{commitMessages}`) via the special accessors in quotes.
Multiple commits will be concatenated and separated by pipes.
Pull request title pattern.
Template values:

Default: `"[{baseBranch}] {commitMessages}"`
- `{targetBranch}`: Branch the backport PR will be targeting
- `{commitMessages}`: Multiple commits will be concatenated and separated by pipes (`|`).

CLI: `--pr-title "{commitMessages} backport for {baseBranch}"`
Default: `"[{targetBranch}] {commitMessages}"`

CLI: `--pr-title "{commitMessages} backport for {targetBranch}"`

Config:

```json
{
"prTitle": "{commitMessages} backport for {targetBranch}"
}
```

#### `prDescription`

Pull request description.
Will be added to the end of the pull request description.
Text that will be appended to the pull request description.

For people who often need to add the same description to PRs they can create a bash alias:

Expand All @@ -178,6 +188,14 @@ alias backport-skip-ci='backport --prDescription "[skip-ci]"'

CLI: `--pr-description "skip-ci"`

Config:

```json
{
"prDescription": "skip-ci"
}
```

#### `sourceBranch`

By default the list of commits will be sourced from the repository's default branch (mostly "master"). Use `sourceBranch` to list and backport commits from other branches than the default.
Expand Down
5 changes: 1 addition & 4 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
module.exports = {
snapshotSerializers: ['jest-snapshot-serializer-ansi'],
setupFiles: ['./src/test/setupFiles/automatic-mocks.ts'],
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
preset: 'ts-jest',
testRegex: '(test|src)/.*test.ts$',
moduleFileExtensions: ['ts', 'js', 'json'],
globals: {
'ts-jest': {
diagnostics: false,
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@
"node": ">=8.0.0"
},
"dependencies": {
"@types/lodash.flatmap": "^4.5.6",
"axios": "^0.19.2",
"dedent": "^0.7.0",
"del": "^5.1.0",
"find-up": "^4.1.0",
"inquirer": "^7.1.0",
"lodash.flatmap": "^4.5.0",
"lodash.isempty": "^4.4.0",
"lodash.isstring": "^4.0.1",
"lodash.uniq": "^4.5.0",
Expand Down
26 changes: 20 additions & 6 deletions src/__snapshots__/runWithOptions.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -88,29 +88,30 @@ Array [
"name": "1. Add 👻 (2e63475c) ",
"short": "Add 👻 (2e63475c)",
"value": Object {
"branch": "mySourceBranch",
"existingBackports": Array [],
"formattedMessage": "Add 👻 (2e63475c)",
"pullNumber": undefined,
"sha": "2e63475c483f7844b0f2833bc57fdee32095bacb",
"sourceBranch": "mySourceBranch",
"targetBranches": Array [],
},
},
Object {
"name": "2. Add witch (#85) ",
"short": "Add witch (#85)",
"value": Object {
"branch": "mySourceBranch",
"existingBackports": Array [],
"formattedMessage": "Add witch (#85)",
"pullNumber": 85,
"sha": "f3b618b9421fdecdb36862f907afbdd6344b361d",
"sourceBranch": "mySourceBranch",
"targetBranches": Array [],
},
},
Object {
"name": "3. Add SF mention (#80) 6.3",
"short": "Add SF mention (#80)",
"value": Object {
"branch": "mySourceBranch",
"existingBackports": Array [
Object {
"branch": "6.3",
Expand All @@ -120,34 +121,42 @@ Array [
"formattedMessage": "Add SF mention (#80)",
"pullNumber": 80,
"sha": "79cf18453ec32a4677009dcbab1c9c8c73fc14fe",
"sourceBranch": "mySourceBranch",
"targetBranches": Array [],
},
},
Object {
"name": "4. Add backport config (3827bbba) ",
"short": "Add backport config (3827bbba)",
"value": Object {
"branch": "mySourceBranch",
"existingBackports": Array [],
"formattedMessage": "Add backport config (3827bbba)",
"pullNumber": undefined,
"sha": "3827bbbaf39914eda4f02f6940189844375fd097",
"sourceBranch": "mySourceBranch",
"targetBranches": Array [],
},
},
Object {
"name": "5. Initial commit (5ea0da55) ",
"short": "Initial commit (5ea0da55)",
"value": Object {
"branch": "mySourceBranch",
"existingBackports": Array [],
"formattedMessage": "Initial commit (5ea0da55)",
"pullNumber": undefined,
"sha": "5ea0da550ac191029459289d67f99ad7d310812b",
"sourceBranch": "mySourceBranch",
"targetBranches": Array [],
},
},
Separator {
"line": "──────────────",
"type": "separator",
},
],
"message": "Select commit to backport",
"name": "promptResult",
"pageSize": 5,
"pageSize": 15,
"type": "list",
},
],
Expand All @@ -171,9 +180,14 @@ Array [
Object {
"name": "5.4",
},
Separator {
"line": "──────────────",
"type": "separator",
},
],
"message": "Select branch to backport to",
"name": "promptResult",
"pageSize": 15,
"type": "list",
},
],
Expand Down
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env node

import { runWithArgs } from './runWithArgs';
const args = process.argv.slice(2);

Expand Down
22 changes: 11 additions & 11 deletions src/options/__snapshots__/options.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,27 @@ It must contain a valid \\"username\\" and \\"accessToken\\".
Read more: https://github.com/sqren/backport/blob/434a28b431bb58c9a014d4489a95f561e6bb2769/docs/configuration.md#global-config-backportconfigjson"
`;

exports[`validateRequiredOptions should throw when both branches and branchChoices are missing 1`] = `
"Invalid option \\"branches\\"
exports[`validateRequiredOptions should throw when both branches and targetBranchChoices are missing 1`] = `
"You must specify a target branch
You can add it with either:
- Config file: \\".backportrc.json\\". Read more: https://github.com/sqren/backport/blob/434a28b431bb58c9a014d4489a95f561e6bb2769/docs/configuration.md#project-config-backportrcjson
- CLI: \\"--branches 6.1\\""
You can specify it via either:
- Config file (recommended): \\".backportrc.json\\". Read more: https://github.com/sqren/backport/blob/434a28b431bb58c9a014d4489a95f561e6bb2769/docs/configuration.md#project-config-backportrcjson
- CLI: \\"--branch 6.1\\""
`;

exports[`validateRequiredOptions should throw when upstream is empty 1`] = `
"Invalid option \\"upstream\\"
"You must specify a valid Github repository
You can add it with either:
- Config file: \\".backportrc.json\\". Read more: https://github.com/sqren/backport/blob/434a28b431bb58c9a014d4489a95f561e6bb2769/docs/configuration.md#project-config-backportrcjson
You can specify it via either:
- Config file (recommended): \\".backportrc.json\\". Read more: https://github.com/sqren/backport/blob/434a28b431bb58c9a014d4489a95f561e6bb2769/docs/configuration.md#project-config-backportrcjson
- CLI: \\"--upstream elastic/kibana\\""
`;

exports[`validateRequiredOptions should throw when upstream is missing 1`] = `
"Invalid option \\"upstream\\"
"You must specify a valid Github repository
You can add it with either:
- Config file: \\".backportrc.json\\". Read more: https://github.com/sqren/backport/blob/434a28b431bb58c9a014d4489a95f561e6bb2769/docs/configuration.md#project-config-backportrcjson
You can specify it via either:
- Config file (recommended): \\".backportrc.json\\". Read more: https://github.com/sqren/backport/blob/434a28b431bb58c9a014d4489a95f561e6bb2769/docs/configuration.md#project-config-backportrcjson
- CLI: \\"--upstream elastic/kibana\\""
`;

Expand Down
6 changes: 3 additions & 3 deletions src/options/cliArgs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('getOptionsFromCliArgs', () => {
githubApiBaseUrlV3: 'https://api.github.com',
githubApiBaseUrlV4: 'https://api.github.com/graphql',
backportCreatedLabels: [],
branchChoices: [],
targetBranchChoices: [],
fork: true,
gitHostname: 'github.com',
labels: [],
Expand Down Expand Up @@ -41,8 +41,8 @@ describe('getOptionsFromCliArgs', () => {
githubApiBaseUrlV3: 'https://api.github.com',
githubApiBaseUrlV4: 'https://api.github.com/graphql',
backportCreatedLabels: [],
branches: ['6.0', '6.1'],
branchChoices: [],
targetBranches: ['6.0', '6.1'],
targetBranchChoices: [],
fork: true,
gitHostname: 'github.com',
labels: [],
Expand Down
6 changes: 4 additions & 2 deletions src/options/cliArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function getOptionsFromCliArgs(
description: 'Pull request labels for the original PR',
type: 'array',
})
.option('branches', {
.option('targetBranches', {
default: [] as string[],
description: 'Branch(es) to backport to',
type: 'array',
Expand All @@ -43,6 +43,7 @@ export function getOptionsFromCliArgs(
.option('commitsCount', {
default: configOptions.commitsCount,
description: 'Number of commits to choose from',
alias: 'count',
type: 'number',
})
.option('editor', {
Expand Down Expand Up @@ -170,7 +171,8 @@ export function getOptionsFromCliArgs(
return {
...rest,
accessToken: cliArgs.accessToken || configOptions.accessToken,
branchChoices: configOptions.branchChoices,
targetBranchChoices: configOptions.targetBranchChoices, // not available as cli argument
branchLabelMapping: configOptions.branchLabelMapping, // not available as cli argument
multipleBranches: cliArgs.multipleBranches || cliArgs.multiple,
multipleCommits: cliArgs.multipleCommits || cliArgs.multiple,
};
Expand Down
4 changes: 2 additions & 2 deletions src/options/config/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('getOptionsFromConfigFiles', () => {
githubApiBaseUrlV3: 'https://api.github.com',
githubApiBaseUrlV4: 'https://api.github.com/graphql',
backportCreatedLabels: [],
branchChoices: [
targetBranchChoices: [
{ checked: false, name: '6.0' },
{ checked: false, name: '5.9' },
],
Expand All @@ -25,7 +25,7 @@ describe('getOptionsFromConfigFiles', () => {
multiple: false,
multipleBranches: true,
multipleCommits: false,
prTitle: '[{baseBranch}] {commitMessages}',
prTitle: '[{targetBranch}] {commitMessages}',
upstream: 'elastic/backport-demo',
username: 'sqren',
});
Expand Down
8 changes: 4 additions & 4 deletions src/options/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ export async function getOptionsFromConfigFiles() {
multipleBranches: true,
all: false,
labels: [] as string[],
prTitle: '[{baseBranch}] {commitMessages}',
prTitle: '[{targetBranch}] {commitMessages}',
gitHostname: 'github.com',
githubApiBaseUrlV3: 'https://api.github.com',
githubApiBaseUrlV4: 'https://api.github.com/graphql',
branchChoices: getBranchesAsObjects(branches),
targetBranchChoices: getTargetBranchChoices(branches),
...combinedConfig,
};
}

// in the config `branches` can either a string or an object.
// in the config `branches` can either be a string or an object.
// We need to transform it so that it is always treated as an object troughout the application
function getBranchesAsObjects(branches?: Config['branches']) {
function getTargetBranchChoices(branches?: Config['branches']) {
if (!branches) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/options/config/projectConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('getProjectConfig', () => {
it('should return empty config', async () => {
jest.spyOn(fs, 'readFile').mockResolvedValueOnce('{}');
const projectConfig = await getProjectConfig();
expect(projectConfig).toEqual({ branchChoices: undefined });
expect(projectConfig).toEqual({ targetBranchChoices: undefined });
});
});

Expand Down
23 changes: 12 additions & 11 deletions src/options/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,18 @@ describe('getOptions', () => {
githubApiBaseUrlV4: 'https://api.github.com/graphql',
author: 'sqren',
backportCreatedLabels: [],
branchChoices: [
targetBranchChoices: [
{ checked: false, name: '6.0' },
{ checked: false, name: '5.9' },
],
branches: ['6.0', '6.1'],
targetBranches: ['6.0', '6.1'],
fork: true,
gitHostname: 'github.com',
labels: [],
multiple: false,
multipleBranches: true,
multipleCommits: false,
prTitle: '[{baseBranch}] {commitMessages}',
prTitle: '[{targetBranch}] {commitMessages}',
repoName: 'kibana',
repoOwner: 'elastic',
resetAuthor: false,
Expand All @@ -104,12 +104,13 @@ describe('validateRequiredOptions', () => {
const validOptions: OptionsFromCliArgs = {
accessToken: 'myAccessToken',
all: false,
branchLabelMapping: undefined,
githubApiBaseUrlV3: 'https://api.github.com',
githubApiBaseUrlV4: 'https://api.github.com/graphql',
author: undefined,
backportCreatedLabels: [],
branchChoices: [],
branches: ['branchA'],
targetBranchChoices: [],
targetBranches: ['branchA'],
commitsCount: 10,
editor: 'code',
fork: true,
Expand All @@ -136,12 +137,12 @@ describe('validateRequiredOptions', () => {
expect(() => validateRequiredOptions(validOptions)).not.toThrow();
});

it('when all options are valid and `branchChoices` is given', () => {
it('when all options are valid and `targetBranchChoices` is given', () => {
expect(() =>
validateRequiredOptions({
...validOptions,
branchChoices: [{ name: 'branchA' }],
branches: [],
targetBranchChoices: [{ name: 'branchA' }],
targetBranches: [],
})
).not.toThrow();
});
Expand All @@ -166,12 +167,12 @@ describe('validateRequiredOptions', () => {
).toThrowErrorMatchingSnapshot();
});

it('when both branches and branchChoices are missing', () => {
it('when both branches and targetBranchChoices are missing', () => {
expect(() =>
validateRequiredOptions({
...validOptions,
branchChoices: [],
branches: [],
targetBranchChoices: [],
targetBranches: [],
})
).toThrowErrorMatchingSnapshot();
});
Expand Down
Loading

0 comments on commit 7cdd72a

Please sign in to comment.