Skip to content

Commit

Permalink
Exclude sourceBranch from target branch choices (#185)
Browse files Browse the repository at this point in the history
* Exclude sourceBranch from targetBranchOptions

* Breaking: Rename “branches” to “targetBranchChoices”

* Update .travis.yml

* Update docs

* Improve docs
  • Loading branch information
sorenlouv authored Apr 25, 2020
1 parent ee143df commit 3256014
Show file tree
Hide file tree
Showing 27 changed files with 634 additions and 416 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ before_script:
- git config --global user.name "Travis Backport"
script:
- yarn tsc
- yarn lint # lint ts and eslint
- yarn eslint './**/*.{ts,js}'
- yarn jest --coverage # run tests and publish coverage
after_success:
- npm install -g coveralls
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,23 @@ Add a [project config](https://github.com/sqren/backport/blob/master/docs/config
// .backportrc.json
{
"upstream": "elastic/kibana",
"branches": [{ "name": "6.x", "checked": true }, "6.3", "6.2", "6.1", "6.0"],
"targetBranchChoices": [{ "name": "6.x", "checked": true }, "6.3", "6.2", "6.1", "6.0"],
}
```

Install backport locally:
Install locally:

```
npm install backport
```

Run backport:
Run:

```
npx backport
```

_This will start an interactive prompt. You can use the `arrow keys` to choose options, `<space>` to select checkboxes and `<enter>` to proceed._
_This will start an interactive prompt. You can use your keyboards arrow keys to choose options, `<space>` to select checkboxes and `<enter>` to proceed._

### Config options

Expand Down
22 changes: 17 additions & 5 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ Example:
```json
{
"upstream": "elastic/kibana",
"branches": [{ "name": "6.x", "checked": true }, "6.3", "6.2", "6.1", "6.0"],
"targetBranchChoices": [
{ "name": "6.x", "checked": true },
"6.3",
"6.2",
"6.1",
"6.0"
],
"targetPRLabels": ["backport"]
}
```
Expand All @@ -75,17 +81,23 @@ Config:
}
```

#### `branches` **required**
#### `targetBranchChoices` **required**

List of branches that will be available to backport to. The array can contain branch names as strings or objects that also contains the field `checked` which indicates whether the branch should be pre-selected. It is useful to pre-select branches you often backport to.
List of target branches the user can interactively select. The array can contain branch names as strings or objects that also contains the field `checked` which indicates whether the branch should be pre-selected. It is useful to pre-select branches you often backport to.

CLI: `--branch 6.1 --branch 6.0`

Config:

```json
{
"branches": [{ "name": "6.x", "checked": true }, "6.3", "6.2", "6.1", "6.0"]
"targetBranchChoices": [
{ "name": "6.x", "checked": true },
"6.3",
"6.2",
"6.1",
"6.0"
]
}
```

Expand All @@ -101,7 +113,7 @@ CLI: `--all`

#### `branchLabelMapping`

Pre-select branch options based on labels on the source PR.
Pre-select target branch options based on the source PR labels.

Example:

Expand Down
26 changes: 13 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
},
"license": "MIT",
"scripts": {
"lint": "tsc --project ./src/test/tsconfig.json && eslint './**/*.{ts,js}'",
"lint": "tsc --project ./tsconfig.test.json && eslint './**/*.{ts,js}'",
"postinstall": "test -f ./dist/scripts/runPostinstall.js && node ./dist/scripts/runPostinstall.js || echo 'Dist folder missing'",
"prepublishOnly": "tsc",
"prepublishOnly": "tsc --project ./tsconfig.prod.json",
"test": "jest",
"start": "ts-node --transpile-only ./src/index.ts"
},
Expand Down Expand Up @@ -69,8 +69,8 @@
"lodash.isempty": "^4.4.0",
"lodash.isstring": "^4.0.1",
"lodash.uniq": "^4.5.0",
"make-dir": "^3.0.2",
"ora": "^4.0.3",
"make-dir": "^3.1.0",
"ora": "^4.0.4",
"safe-json-stringify": "^1.2.0",
"strip-json-comments": "^3.1.0",
"winston": "^3.2.1",
Expand All @@ -81,7 +81,7 @@
"@types/dedent": "^0.7.0",
"@types/inquirer": "^6.5.0",
"@types/jest": "^25.2.1",
"@types/lodash": "^4.14.144",
"@types/lodash": "^4.14.150",
"@types/lodash.flatmap": "^4.5.6",
"@types/lodash.isempty": "^4.4.6",
"@types/lodash.isstring": "^4.0.6",
Expand All @@ -90,22 +90,22 @@
"@types/safe-json-stringify": "^1.1.0",
"@types/yargs": "^15.0.4",
"@types/yargs-parser": "^15.0.0",
"@typescript-eslint/eslint-plugin": "^2.28.0",
"@typescript-eslint/parser": "^2.28.0",
"@typescript-eslint/eslint-plugin": "^2.29.0",
"@typescript-eslint/parser": "^2.29.0",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.1",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-jest": "^23.8.2",
"eslint-plugin-prettier": "^3.1.3",
"husky": "^4.2.3",
"jest": "^25.3.0",
"jest": "^25.4.0",
"jest-snapshot-serializer-ansi": "^1.0.0",
"lint-staged": "^10.1.2",
"lint-staged": "^10.1.7",
"lodash": "^4.17.15",
"prettier": "^2.0.4",
"prettier": "^2.0.5",
"strip-ansi": "^6.0.0",
"ts-jest": "^25.3.1",
"ts-node": "^8.8.2",
"ts-jest": "^25.4.0",
"ts-node": "^8.9.0",
"typescript": "^3.8.3"
}
}
10 changes: 5 additions & 5 deletions src/__snapshots__/runWithOptions.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ Array [
"existingTargetPullRequests": Array [],
"formattedMessage": "Add 👻 (2e63475c)",
"pullNumber": undefined,
"selectedTargetBranches": Array [],
"sha": "2e63475c483f7844b0f2833bc57fdee32095bacb",
"sourceBranch": "mySourceBranch",
"targetBranches": Array [],
},
},
Object {
Expand All @@ -103,9 +103,9 @@ Array [
"existingTargetPullRequests": Array [],
"formattedMessage": "Add witch (#85)",
"pullNumber": 85,
"selectedTargetBranches": Array [],
"sha": "f3b618b9421fdecdb36862f907afbdd6344b361d",
"sourceBranch": "mySourceBranch",
"targetBranches": Array [],
},
},
Object {
Expand All @@ -120,9 +120,9 @@ Array [
],
"formattedMessage": "Add SF mention (#80)",
"pullNumber": 80,
"selectedTargetBranches": Array [],
"sha": "79cf18453ec32a4677009dcbab1c9c8c73fc14fe",
"sourceBranch": "mySourceBranch",
"targetBranches": Array [],
},
},
Object {
Expand All @@ -132,9 +132,9 @@ Array [
"existingTargetPullRequests": Array [],
"formattedMessage": "Add backport config (3827bbba)",
"pullNumber": undefined,
"selectedTargetBranches": Array [],
"sha": "3827bbbaf39914eda4f02f6940189844375fd097",
"sourceBranch": "mySourceBranch",
"targetBranches": Array [],
},
},
Object {
Expand All @@ -144,9 +144,9 @@ Array [
"existingTargetPullRequests": Array [],
"formattedMessage": "Initial commit (5ea0da55)",
"pullNumber": undefined,
"selectedTargetBranches": Array [],
"sha": "5ea0da550ac191029459289d67f99ad7d310812b",
"sourceBranch": "mySourceBranch",
"targetBranches": Array [],
},
},
Separator {
Expand Down
5 changes: 3 additions & 2 deletions src/options/cliArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export function getOptionsFromCliArgs(
default: configOptions.sourcePRLabels,
description: 'Add labels to the source (original) PR',
type: 'array',
alias: 'sourcePRLabel',
})
.option('sourceBranch', {
default: configOptions.sourceBranch,
Expand All @@ -146,13 +147,13 @@ export function getOptionsFromCliArgs(
default: [] as string[],
description: 'Branch(es) to backport to',
type: 'array',
alias: ['branch', 'b'],
alias: ['targetBranch', 'branch', 'b'],
string: true, // ensure `6.0` is not coerced to `6`
})
.option('targetPRLabels', {
default: configOptions.targetPRLabels,
description: 'Add labels to the target (backport) PR',
alias: 'labels',
alias: ['labels', 'label', 'l'],
type: 'array',
})
.option('upstream', {
Expand Down
12 changes: 7 additions & 5 deletions src/options/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export async function getOptionsFromConfigFiles() {
getGlobalConfig(),
]);

const { branches, ...combinedConfig } = {
const { targetBranchChoices, ...combinedConfig } = {
...globalConfig,
...projectConfig,
};
Expand All @@ -31,19 +31,21 @@ export async function getOptionsFromConfigFiles() {
gitHostname: 'github.com',
githubApiBaseUrlV3: 'https://api.github.com',
githubApiBaseUrlV4: 'https://api.github.com/graphql',
targetBranchChoices: getTargetBranchChoices(branches),
targetBranchChoices: getTargetBranchChoicesAsObject(targetBranchChoices),
...combinedConfig,
};
}

// 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 getTargetBranchChoices(branches?: Config['branches']) {
if (!branches) {
function getTargetBranchChoicesAsObject(
targetBranchChoices?: Config['targetBranchChoices']
) {
if (!targetBranchChoices) {
return;
}

return branches.map((choice) => {
return targetBranchChoices.map((choice) => {
if (isString(choice)) {
return {
name: choice,
Expand Down
3 changes: 2 additions & 1 deletion src/runWithOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { HandledError } from './services/HandledError';
import { logger, consoleLog } from './services/logger';
import { sequentially } from './services/sequentially';
import { cherrypickAndCreateTargetPullRequest } from './ui/cherrypickAndCreateTargetPullRequest';
import { getTargetBranches } from './ui/getBranches';
import { getCommits } from './ui/getCommits';
import { getTargetBranches } from './ui/getTargetBranches';
import { maybeSetupRepo } from './ui/maybeSetupRepo';

export async function runWithOptions(options: BackportOptions) {
logger.verbose('Backport options', options);
if (options.dryRun) {
consoleLog(chalk.red('Dry run: Nothing will be pushed to Github\n'));
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ describe('cherrypick', () => {
sourceBranch: '7.x',
formattedMessage: '',
sha: 'abcd',
targetBranches: [],
selectedTargetBranches: [],
};

it('should return `needsResolving: false` when no errors are encountered', async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/services/github/v3/fetchCommitBySha.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('fetchCommitBySha', () => {
formattedMessage: 'myMessage (sha12345)',
pullNumber: undefined,
sha: 'sha123456789',
targetBranches: [],
selectedTargetBranches: [],
});

expect(axiosSpy.mock.calls).toMatchSnapshot();
Expand Down
2 changes: 1 addition & 1 deletion src/services/github/v3/fetchCommitBySha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export async function fetchCommitBySha(

return {
sourceBranch: 'master',
targetBranches: [],
selectedTargetBranches: [],
formattedMessage,
sha: fullSha,
};
Expand Down
4 changes: 2 additions & 2 deletions src/services/github/v4/fetchCommitByPullNumber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ export async function fetchCommitByPullNumber(
const labels = res.repository.pullRequest.labels.nodes.map(
(label) => label.name
);
const targetBranches = getTargetBranchesFromLabels({
const selectedTargetBranches = getTargetBranchesFromLabels({
labels,
branchLabelMapping,
});

return {
sourceBranch,
targetBranches,
selectedTargetBranches,
sha,
formattedMessage,
pullNumber,
Expand Down
14 changes: 7 additions & 7 deletions src/services/github/v4/fetchCommitsByAuthor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,37 +36,37 @@ describe('fetchCommitsByAuthor', () => {
sha: '2e63475c483f7844b0f2833bc57fdee32095bacb',
formattedMessage: 'Add 👻 (2e63475c)',
existingTargetPullRequests: [],
targetBranches: [],
selectedTargetBranches: [],
sourceBranch: 'master',
},
{
sha: 'f3b618b9421fdecdb36862f907afbdd6344b361d',
formattedMessage: 'Add witch (#85)',
pullNumber: 85,
existingTargetPullRequests: [],
targetBranches: [],
selectedTargetBranches: [],
sourceBranch: 'master',
},
{
sha: '79cf18453ec32a4677009dcbab1c9c8c73fc14fe',
formattedMessage: 'Add SF mention (#80)',
pullNumber: 80,
existingTargetPullRequests: [{ branch: '6.3', state: 'MERGED' }],
targetBranches: [],
selectedTargetBranches: [],
sourceBranch: 'master',
},
{
sha: '3827bbbaf39914eda4f02f6940189844375fd097',
formattedMessage: 'Add backport config (3827bbba)',
existingTargetPullRequests: [],
targetBranches: [],
selectedTargetBranches: [],
sourceBranch: 'master',
},
{
sha: '5ea0da550ac191029459289d67f99ad7d310812b',
formattedMessage: 'Initial commit (5ea0da55)',
existingTargetPullRequests: [],
targetBranches: [],
selectedTargetBranches: [],
sourceBranch: 'master',
},
];
Expand All @@ -92,7 +92,7 @@ describe('fetchCommitsByAuthor', () => {
pullNumber: 80,
sha: '79cf18453ec32a4677009dcbab1c9c8c73fc14fe',
sourceBranch: 'master',
targetBranches: [],
selectedTargetBranches: [],
},
];
expect(res).toEqual(expectedCommits);
Expand All @@ -107,7 +107,7 @@ describe('fetchCommitsByAuthor', () => {
pullNumber: 80,
sha: '79cf18453ec32a4677009dcbab1c9c8c73fc14fe',
sourceBranch: 'master',
targetBranches: [],
selectedTargetBranches: [],
},
];
expect(res).toEqual(expectedCommits);
Expand Down
Loading

0 comments on commit 3256014

Please sign in to comment.