Skip to content

Commit

Permalink
Merge 47f7284 into e119d71
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenlouv committed Jun 21, 2020
2 parents e119d71 + 47f7284 commit 4ab06f1
Show file tree
Hide file tree
Showing 29 changed files with 1,343 additions and 1,053 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ module.exports = {
],
'no-console': 'error',
'@typescript-eslint/ban-ts-ignore': 'off',
'@typescript-eslint/ban-ts-comment': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/camelcase': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/no-explicit-any': 'off',
Expand Down
35 changes: 18 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"branching"
],
"version": "5.4.3",
"main": "./dist/index.js",
"main": "./dist/runAsModule.js",
"types": "dist/runAsModule.d.ts",
"bin": {
"backport": "./dist/index.js"
},
Expand Down Expand Up @@ -82,32 +83,32 @@
"@types/core-js": "^2.5.2",
"@types/dedent": "^0.7.0",
"@types/inquirer": "^6.5.0",
"@types/jest": "^25.2.1",
"@types/lodash": "^4.14.150",
"@types/jest": "^26.0.0",
"@types/lodash": "^4.14.155",
"@types/lodash.flatmap": "^4.5.6",
"@types/lodash.isempty": "^4.4.6",
"@types/lodash.isstring": "^4.0.6",
"@types/lodash.uniq": "^4.5.6",
"@types/node": "^12.12.31",
"@types/safe-json-stringify": "^1.1.0",
"@types/yargs": "^15.0.4",
"@types/node": "^14.0.13",
"@types/safe-json-stringify": "^1.1.1",
"@types/yargs": "^15.0.5",
"@types/yargs-parser": "^15.0.0",
"@typescript-eslint/eslint-plugin": "^2.30.0",
"@typescript-eslint/parser": "^2.30.0",
"eslint": "^6.8.0",
"@typescript-eslint/eslint-plugin": "^3.3.0",
"@typescript-eslint/parser": "^3.3.0",
"eslint": "^7.3.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-jest": "^23.8.2",
"eslint-plugin-prettier": "^3.1.3",
"eslint-plugin-import": "^2.21.2",
"eslint-plugin-jest": "^23.16.0",
"eslint-plugin-prettier": "^3.1.4",
"husky": "^4.2.3",
"jest": "^25.5.4",
"jest": "^26.0.1",
"jest-snapshot-serializer-ansi": "^1.0.0",
"lint-staged": "^10.2.2",
"lint-staged": "^10.2.11",
"lodash": "^4.17.15",
"prettier": "^2.0.5",
"strip-ansi": "^6.0.0",
"ts-jest": "^25.4.0",
"ts-node": "^8.10.1",
"typescript": "^3.8.3"
"ts-jest": "^26.1.0",
"ts-node": "^8.10.2",
"typescript": "^3.9.5"
}
}
55 changes: 55 additions & 0 deletions src/options/ConfigOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Logger } from '../services/logger';

export interface BranchChoice {
name: string;
checked?: boolean;
}
export type BranchChoiceRaw = string | BranchChoice;

type AutoFixConflictsHandler = ({
files,
directory,
logger,
}: {
files: string[];
directory: string;
logger: Logger;
}) => boolean;

export interface ConfigOptions {
accessToken?: string;
all?: boolean;
assignees?: string[];
author?: string;
autoAssign?: boolean;
autoFixConflicts?: AutoFixConflictsHandler;
branchLabelMapping?: Record<string, string>;
editor?: string;
fork?: boolean;
gitHostname?: string;
githubApiBaseUrlV3?: string;
githubApiBaseUrlV4?: string;
maxNumber?: number;
multiple?: boolean;
multipleBranches?: boolean;
multipleCommits?: boolean;
noVerify?: boolean;
path?: string;
prDescription?: string;
prFilter?: string;
prTitle?: string;
pullNumber?: number;
sourceBranch?: string;
sourcePRLabels?: string[];
targetBranchChoices?: BranchChoiceRaw[];
targetBranches?: string[];
targetPRLabels?: string[];
upstream?: string;
username?: string;

// deprecated: renamed to `targetBranchChoices`
branches?: BranchChoiceRaw[];

// deprecated: renamed to `targetPRLabels`
labels?: string[];
}
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`validateRequiredOptions should throw when accessToken is empty 1`] = `
exports[`getValidatedOptions2 should throw when accessToken is empty 1`] = `
"Please update your config file: /myHomeDir/.backport/config.json.
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 accessToken is missing 1`] = `
exports[`getValidatedOptions2 should throw when accessToken is missing 1`] = `
"Please update your config file: /myHomeDir/.backport/config.json.
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 targetBranchChoices are missing 1`] = `
exports[`getValidatedOptions2 should throw when both \`targetBranches\` and \`targetBranchChoices\` are missing 1`] = `
"You must specify a target branch
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`] = `
exports[`getValidatedOptions2 should throw when upstream is empty 1`] = `
"You must specify a valid Github repository
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`] = `
exports[`getValidatedOptions2 should throw when upstream is missing 1`] = `
"You must specify a valid Github repository
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 username is empty 1`] = `
exports[`getValidatedOptions2 should throw when username is empty 1`] = `
"Please update your config file: /myHomeDir/.backport/config.json.
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 username is missing 1`] = `
exports[`getValidatedOptions2 should throw when username is missing 1`] = `
"Please update your config file: /myHomeDir/.backport/config.json.
It must contain a valid \\"username\\" and \\"accessToken\\".
Expand Down
124 changes: 114 additions & 10 deletions src/options/cliArgs.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { getOptionsFromCliArgs, OptionsFromCliArgs } from './cliArgs';
import { ConfigOptions } from './ConfigOptions';
import { getOptionsFromCliArgs } from './cliArgs';

describe('getOptionsFromCliArgs', () => {
it('should return correct options', () => {
const configOptions: Partial<OptionsFromCliArgs> = {
it('should merge config and cli options', () => {
const configOptions: ConfigOptions = {
accessToken: 'myAccessToken',
all: false,
fork: true,
Expand Down Expand Up @@ -62,7 +63,7 @@ describe('getOptionsFromCliArgs', () => {
});

it('should accept both camel-case and dashed-case and convert them to camel cased', () => {
const configOptions: Partial<OptionsFromCliArgs> = {};
const configOptions: ConfigOptions = {};
const argv = [
'--access-token',
'my access token',
Expand All @@ -78,13 +79,116 @@ describe('getOptionsFromCliArgs', () => {
expect('api-hostname' in res).toEqual(false);
});

it('should accept aliases (--pr) but only return the full name (--pullNumber) in the result', () => {
const configOptions: Partial<OptionsFromCliArgs> = {};
const argv = ['--pr', '1337'];
describe('pullNumber', () => {
it('should accept `--pr` alias but only return the full representation (`pullNumber`)', () => {
const configOptions: ConfigOptions = {};
const argv = ['--pr', '1337'];

const res = getOptionsFromCliArgs(configOptions, argv);
const res = getOptionsFromCliArgs(configOptions, argv);

expect(res.pullNumber).toEqual(1337);

//@ts-expect-error
expect(res.pr).toBe(undefined);
});
});

describe('autoAssign', () => {
it('should set assignees to current user if `autoAssign` is true', () => {
const configOptions: ConfigOptions = { username: 'sqren' };
const argv = ['--auto-assign'];

const res = getOptionsFromCliArgs(configOptions, argv);

expect(res.assignees).toEqual(['sqren']);
});
});

describe('assignees', () => {
it('should set assignees', () => {
const configOptions: ConfigOptions = { username: 'sqren' };
const argv = ['--assignees', 'john'];
const res = getOptionsFromCliArgs(configOptions, argv);
expect(res.assignees).toEqual(['john']);
});
});

describe('multipleBranches', () => {
it('should be settable', () => {
const configOptions: ConfigOptions = { multipleBranches: false };
const argv = [] as const;
const res = getOptionsFromCliArgs(configOptions, argv);
expect(res.multipleBranches).toBe(false);
});

it('should respect `multiple` option', () => {
const configOptions: ConfigOptions = { multipleBranches: false };
const argv = ['--multiple'];
const res = getOptionsFromCliArgs(configOptions, argv);
expect(res.multipleBranches).toBe(true);
});
});

describe('noVerify', () => {
it('should be settable', () => {
const configOptions: ConfigOptions = { noVerify: false };
const argv = [] as const;
const res = getOptionsFromCliArgs(configOptions, argv);
expect(res.noVerify).toBe(false);
});

expect(res.pullNumber).toEqual(1337);
expect('pr' in res).toEqual(false);
it('should be flipped by `verify`', () => {
const configOptions: ConfigOptions = { noVerify: false };
const argv = ['--verify'];
const res = getOptionsFromCliArgs(configOptions, argv);
expect(res.noVerify).toBe(true);
});
});

describe('mainline', () => {
it('should default to 1', () => {
const configOptions: ConfigOptions = {};
const argv = ['--mainline'];
const res = getOptionsFromCliArgs(configOptions, argv);
expect(res.mainline).toEqual(1);
});

it('should accept numbers', () => {
const configOptions: ConfigOptions = {};
const argv = ['--mainline', '2'];
const res = getOptionsFromCliArgs(configOptions, argv);
expect(res.mainline).toEqual(2);
});
});

describe('targetBranches', () => {
it('should not coerce 6.0 to 6', () => {
const configOptions: ConfigOptions = {};
const argv = ['-b', '6.0'];
const res = getOptionsFromCliArgs(configOptions, argv);
expect(res.targetBranches).toEqual(['6.0']);
});
});

describe('targetBranchChoices', () => {
it('should support objects', () => {
const configOptions: ConfigOptions = {
targetBranchChoices: [{ name: '7.x', checked: false }],
};
const argv = [] as const;
const res = getOptionsFromCliArgs(configOptions, argv);
expect(res.targetBranchChoices).toEqual([
{ name: '7.x', checked: false },
]);
});

it('should convert primitive values to objects', () => {
const configOptions: ConfigOptions = {};
const argv = ['--target-branch-choices', '8.x'];
const res = getOptionsFromCliArgs(configOptions, argv);
expect(res.targetBranchChoices).toEqual([
{ name: '8.x', checked: false },
]);
});
});
});
Loading

0 comments on commit 4ab06f1

Please sign in to comment.