Skip to content

Commit

Permalink
Merge 9e04557 into b2765cf
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenlouv committed Apr 25, 2020
2 parents b2765cf + 9e04557 commit fa82525
Show file tree
Hide file tree
Showing 26 changed files with 614 additions and 408 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
2 changes: 1 addition & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,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
4 changes: 2 additions & 2 deletions src/services/github/v4/fetchCommitsByAuthor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,14 @@ export async function fetchCommitsByAuthor(
const labels = sourcePullRequest?.node.labels.nodes.map(
(node) => node.name
);
const targetBranches = getTargetBranchesFromLabels({
const selectedTargetBranches = getTargetBranchesFromLabels({
labels,
branchLabelMapping,
});

return {
sourceBranch,
targetBranches,
selectedTargetBranches,
sha,
formattedMessage,
pullNumber,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getTargetBranchesFromLabels } from './getTargetBranchesFromLabels';
describe('getTargetBranchesFromLabels', () => {
it(`should support Kibana's label format`, () => {
const branchLabelMapping = {
'v8.0.0': '', // current major (master)
'v8.0.0': '', // current major (master) should be ignored
'^v7.8.0$': '7.x', // current minor (7.x)
'^v(\\d+).(\\d+).\\d+$': '$1.$2', // all other branches
};
Expand Down
43 changes: 42 additions & 1 deletion src/test/integration/__snapshots__/integration.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,47 @@ Array [
},
},
],
Array [
"[VERBOSE] Backport options",
Object {
"accessToken": "myAccessToken",
"all": false,
"author": "sqren",
"branchLabelMapping": undefined,
"dryRun": false,
"editor": undefined,
"fork": true,
"gitHostname": "github.com",
"githubApiBaseUrlV3": "https://api.github.com",
"githubApiBaseUrlV4": "https://api.github.com/graphql",
"maxNumber": undefined,
"multiple": false,
"multipleBranches": true,
"multipleCommits": false,
"path": undefined,
"prDescription": undefined,
"prTitle": "[{targetBranch}] {commitMessages}",
"repoName": "backport-demo",
"repoOwner": "elastic",
"resetAuthor": false,
"sourceBranch": "master",
"sourcePRLabels": Array [],
"targetBranchChoices": Array [
Object {
"checked": false,
"name": "6.0",
},
Object {
"checked": false,
"name": "5.9",
},
],
"targetBranches": Array [],
"targetPRLabels": Array [],
"username": "sqren",
"verbose": false,
},
],
Array [
"[INFO] POST https://api.github.com/graphql (status: 200)",
undefined,
Expand Down Expand Up @@ -478,7 +519,7 @@ fatal: No such remote: 'elastic'
"",
],
Array [
"[INFO] Backporting [{\\"sourceBranch\\":\\"master\\",\\"targetBranches\\":[],\\"sha\\":\\"f3b618b9421fdecdb36862f907afbdd6344b361d\\",\\"formattedMessage\\":\\"Add witch (#85)\\",\\"pullNumber\\":85,\\"existingTargetPullRequests\\":[]}] to 6.0",
"[INFO] Backporting [{\\"sourceBranch\\":\\"master\\",\\"selectedTargetBranches\\":[],\\"sha\\":\\"f3b618b9421fdecdb36862f907afbdd6344b361d\\",\\"formattedMessage\\":\\"Add witch (#85)\\",\\"pullNumber\\":85,\\"existingTargetPullRequests\\":[]}] to 6.0",
undefined,
],
Array [
Expand Down
Loading

0 comments on commit fa82525

Please sign in to comment.