backport 6.0.0 → 6.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. package/README.md +179 -52
  2. package/dist/entrypoint.cli.js +2 -0
  3. package/dist/entrypoint.module.d.ts +16 -4
  4. package/dist/entrypoint.module.js +41 -13
  5. package/dist/entrypoint.module.private.test.d.ts +1 -0
  6. package/dist/entrypoint.module.private.test.js +34 -0
  7. package/dist/main.js +1 -2
  8. package/dist/options/ConfigOptions.d.ts +16 -6
  9. package/dist/options/cliArgs.d.ts +36 -37
  10. package/dist/options/cliArgs.js +25 -24
  11. package/dist/options/cliArgs.test.js +6 -10
  12. package/dist/options/config/config.d.ts +28 -52
  13. package/dist/options/config/config.js +2 -36
  14. package/dist/options/config/config.test.js +10 -27
  15. package/dist/options/config/getOptionsFromGit.d.ts +3 -2
  16. package/dist/options/config/getOptionsFromGit.js +1 -3
  17. package/dist/options/config/globalConfig.js +2 -5
  18. package/dist/options/config/globalConfig.test.js +0 -2
  19. package/dist/options/config/projectConfig.test.js +51 -14
  20. package/dist/options/config/readConfigFile.d.ts +52 -0
  21. package/dist/options/config/readConfigFile.js +28 -2
  22. package/dist/options/options.d.ts +55 -32
  23. package/dist/options/options.js +101 -7
  24. package/dist/options/options.test.js +248 -97
  25. package/dist/options/parseRequiredOptions.d.ts +3 -3
  26. package/dist/options/parseRequiredOptions.js +2 -4
  27. package/dist/options/parseRequiredOptions.test.js +6 -11
  28. package/dist/runSequentially.test.js +40 -78
  29. package/dist/services/git.d.ts +5 -2
  30. package/dist/services/git.js +11 -10
  31. package/dist/services/git.test.js +12 -9
  32. package/dist/services/github/v3/addAssigneesToPullRequest.d.ts +1 -1
  33. package/dist/services/github/v3/addAssigneesToPullRequest.js +2 -3
  34. package/dist/services/github/v3/addAssigneesToPullRequest.test.js +4 -5
  35. package/dist/services/github/v3/createPullRequest.js +10 -13
  36. package/dist/services/github/v3/createPullRequest.test.d.ts +1 -0
  37. package/dist/services/github/v3/createPullRequest.test.js +110 -0
  38. package/dist/services/github/v3/createStatusComment.js +12 -14
  39. package/dist/services/github/v3/createStatusComment.test.js +58 -29
  40. package/dist/services/github/v4/apiRequestV4.d.ts +1 -1
  41. package/dist/services/github/v4/apiRequestV4.js +1 -1
  42. package/dist/services/github/v4/enablePullRequestAutoMerge.js +1 -1
  43. package/dist/services/github/v4/enablePullRequestAutoMerge.private.test.js +0 -1
  44. package/dist/services/github/v4/fetchAuthorId.d.ts +5 -2
  45. package/dist/services/github/v4/fetchAuthorId.js +5 -6
  46. package/dist/services/github/v4/fetchAuthorId.private.test.js +3 -6
  47. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.d.ts +8 -1
  48. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.js +1 -1
  49. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.private.test.js +6 -3
  50. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.d.ts +8 -1
  51. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.js +1 -1
  52. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.private.test.js +3 -3
  53. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.d.ts +12 -1
  54. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.js +6 -8
  55. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.private.test.js +10 -12
  56. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.test.js +5 -7
  57. package/dist/services/github/v4/fetchCommits/fetchProjectConfig.d.ts +3 -0
  58. package/dist/services/github/v4/fetchCommits/fetchProjectConfig.js +55 -0
  59. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.js +5 -5
  60. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.private.test.js +2 -6
  61. package/dist/services/github/v4/fetchExistingPullRequest.private.test.js +0 -2
  62. package/dist/services/github/v4/fetchProjectConfig.d.ts +8 -0
  63. package/dist/services/github/v4/fetchProjectConfig.js +56 -0
  64. package/dist/services/github/v4/fetchProjectConfig.private.test.d.ts +1 -0
  65. package/dist/services/github/v4/fetchProjectConfig.private.test.js +52 -0
  66. package/dist/services/github/v4/fetchProjectConfig.test.d.ts +1 -0
  67. package/dist/services/github/v4/fetchProjectConfig.test.js +37 -0
  68. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.d.ts +67 -0
  69. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.js +130 -0
  70. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.private.test.d.ts +1 -0
  71. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.private.test.js +130 -0
  72. package/dist/services/github/v4/getOptionsFromGithub/index.d.ts +3 -1
  73. package/dist/services/github/v4/getOptionsFromGithub/index.private.test.js +22 -1
  74. package/dist/services/github/v4/getOptionsFromGithub/query.d.ts +4 -1
  75. package/dist/services/github/v4/getOptionsFromGithub/query.js +3 -0
  76. package/dist/services/sourceCommit/getExpectedTargetPullRequests.test.js +32 -15
  77. package/dist/services/sourceCommit/parseSourceCommit.d.ts +5 -1
  78. package/dist/services/sourceCommit/parseSourceCommit.js +2 -2
  79. package/dist/services/sourceCommit/parseSourceCommit.test.js +120 -5
  80. package/dist/test/inquirer.private.test.js +33 -36
  81. package/dist/test/integration/integration.private.test.js +114 -95
  82. package/dist/test/mockConfigFiles.d.ts +5 -0
  83. package/dist/test/mockConfigFiles.js +37 -0
  84. package/dist/test/nockHelpers.d.ts +1 -1
  85. package/dist/test/nockHelpers.js +4 -4
  86. package/dist/test/private/getDevAccessToken.js +1 -1
  87. package/dist/test/setupFiles/automatic-mocks.js +1 -29
  88. package/dist/ui/cherrypickAndCreatePullRequest.test.js +80 -28
  89. package/dist/ui/cherrypickAndCreateTargetPullRequest.d.ts +0 -9
  90. package/dist/ui/cherrypickAndCreateTargetPullRequest.js +13 -48
  91. package/dist/ui/cherrypickAndCreateTargetPullRequest.test.js +222 -260
  92. package/dist/ui/getCommits.d.ts +1 -1
  93. package/dist/ui/getCommitsWithoutBackports.d.ts +11 -0
  94. package/dist/ui/getCommitsWithoutBackports.js +53 -0
  95. package/dist/ui/getCommitsWithoutBackports.test.d.ts +1 -0
  96. package/dist/ui/getCommitsWithoutBackports.test.js +259 -0
  97. package/dist/ui/getTargetBranches.d.ts +2 -1
  98. package/dist/ui/getTargetBranches.js +17 -1
  99. package/dist/ui/maybeSetupRepo.js +4 -3
  100. package/dist/ui/maybeSetupRepo.test.js +49 -3
  101. package/dist/utils/excludeUndefined.d.ts +6 -0
  102. package/dist/utils/excludeUndefined.js +7 -0
  103. package/dist/utils/excludeUndefined.test.d.ts +1 -0
  104. package/dist/utils/excludeUndefined.test.js +24 -0
  105. package/package.json +9 -8
  106. package/yarn.lock +98 -77
@@ -1,45 +1,69 @@
1
- import { ConfigFileOptions } from './ConfigOptions';
1
+ import { ConfigFileOptions, TargetBranchChoiceOrString } from './ConfigOptions';
2
2
  export declare type ValidConfigOptions = Readonly<Awaited<ReturnType<typeof getOptions>>>;
3
- export declare function getOptions(argv: string[], optionsFromModule?: ConfigFileOptions): Promise<{
4
- targetBranchChoices: import("./ConfigOptions").TargetBranchChoice[];
5
- accessToken: string;
6
- username: string;
7
- repoName: string;
8
- repoOwner: string;
3
+ export declare const defaultConfigOptions: {
9
4
  assignees: string[];
10
- author: string;
11
- all: boolean;
5
+ autoAssign: boolean;
6
+ autoMerge: boolean;
7
+ autoMergeMethod: string;
8
+ backportBinary: string;
9
+ cherrypickRef: boolean;
10
+ ci: boolean;
11
+ commitPaths: string[];
12
+ details: boolean;
13
+ fork: boolean;
14
+ maxNumber: number;
12
15
  multipleBranches: boolean;
13
16
  multipleCommits: boolean;
14
- commitPaths: string[];
17
+ noVerify: boolean;
18
+ resetAuthor: boolean;
15
19
  reviewers: string[];
16
- sourcePRLabels: (string[] | undefined) & (string[] | never[]);
17
- targetBranches: (string[] | undefined) & (string[] | never[]);
20
+ sourcePRLabels: string[];
21
+ targetBranchChoices: TargetBranchChoiceOrString[];
22
+ targetBranches: string[];
18
23
  targetPRLabels: string[];
19
- cherrypickRef: boolean;
24
+ verbose: boolean;
25
+ };
26
+ export declare function getOptions(argv: string[], optionsFromModule?: ConfigFileOptions): Promise<{
27
+ accessToken: string;
28
+ repoName: string;
29
+ repoOwner: string;
20
30
  fork: boolean;
21
- noVerify: boolean;
31
+ verbose: boolean;
32
+ assignees: string[];
33
+ author: string | null;
22
34
  autoAssign: boolean;
23
35
  autoMerge: boolean;
24
36
  autoMergeMethod: string;
37
+ cherrypickRef: boolean;
25
38
  ci: boolean;
39
+ commitPaths: string[];
26
40
  details: boolean;
27
- editor: string | undefined;
28
- forceLocalConfig: boolean | undefined;
29
- gitHostname: string;
30
- githubApiBaseUrlV3: string;
31
- githubApiBaseUrlV4: string;
32
- mainline: number | undefined;
41
+ editor?: string | undefined;
42
+ gitHostname?: string | undefined;
43
+ githubApiBaseUrlV3?: string | undefined;
44
+ githubApiBaseUrlV4?: string | undefined;
33
45
  maxNumber: number;
34
- prDescription: string | undefined;
35
- prTitle: string;
36
- prFilter: string | undefined;
37
- pullNumber: number | undefined;
46
+ multipleBranches: boolean;
47
+ multipleCommits: boolean;
48
+ noVerify: boolean;
49
+ prDescription?: string | undefined;
50
+ prFilter?: string | undefined;
51
+ prTitle?: string | undefined;
52
+ pullNumber?: number | undefined;
38
53
  resetAuthor: boolean;
39
- sha: string | undefined;
54
+ reviewers: string[];
55
+ sha?: string | undefined;
56
+ skipRemoteConfig?: boolean | undefined;
40
57
  sourceBranch: string;
41
- upstream: string | undefined;
42
- verbose: boolean;
58
+ sourcePRLabels: string[];
59
+ targetBranchChoices: string[] | TargetBranchChoiceOrString[];
60
+ targetBranches: string[];
61
+ targetPRLabels: string[];
62
+ mainline?: number | undefined;
63
+ historicalBranchLabelMappings: {
64
+ branchLabelMapping: Record<string, string>;
65
+ committedDate: string;
66
+ }[];
43
67
  autoFixConflicts?: (({ files, directory, logger, targetBranch, }: {
44
68
  files: string[];
45
69
  directory: string;
@@ -52,12 +76,11 @@ export declare function getOptions(argv: string[], optionsFromModule?: ConfigFil
52
76
  };
53
77
  targetBranch: string;
54
78
  }) => boolean | Promise<boolean>) | undefined;
79
+ backportBinary: string;
55
80
  multiple?: boolean | undefined;
56
81
  branchLabelMapping?: Record<string, string> | undefined;
57
- branches?: import("./ConfigOptions").TargetBranchChoiceOrString[] | undefined;
82
+ upstream?: string | undefined;
83
+ branches?: TargetBranchChoiceOrString[] | undefined;
58
84
  labels?: string[] | undefined;
59
- historicalBranchLabelMappings: {
60
- branchLabelMapping: Record<string, string>;
61
- committedDate: string;
62
- }[];
85
+ authenticatedUsername: string;
63
86
  }>;
@@ -1,18 +1,112 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getOptions = void 0;
4
- const getOptionsFromGithub_1 = require("../services/github/v4/getOptionsFromGithub");
3
+ exports.getOptions = exports.defaultConfigOptions = void 0;
4
+ const lodash_1 = require("lodash");
5
+ const HandledError_1 = require("../services/HandledError");
6
+ const env_1 = require("../services/env");
7
+ const getOptionsFromGithub_1 = require("../services/github/v4/getOptionsFromGithub/getOptionsFromGithub");
5
8
  const logger_1 = require("./../services/logger");
6
9
  const cliArgs_1 = require("./cliArgs");
7
10
  const config_1 = require("./config/config");
8
- const parseRequiredOptions_1 = require("./parseRequiredOptions");
11
+ const PROJECT_CONFIG_DOCS_LINK = 'https://github.com/sqren/backport/blob/main/docs/configuration.md#project-config-backportrcjson';
12
+ const GLOBAL_CONFIG_DOCS_LINK = 'https://github.com/sqren/backport/blob/main/docs/configuration.md#global-config-backportconfigjson';
13
+ exports.defaultConfigOptions = {
14
+ assignees: [],
15
+ autoAssign: false,
16
+ autoMerge: false,
17
+ autoMergeMethod: 'merge',
18
+ backportBinary: 'backport',
19
+ cherrypickRef: true,
20
+ ci: false,
21
+ commitPaths: [],
22
+ details: false,
23
+ fork: true,
24
+ maxNumber: 10,
25
+ multipleBranches: true,
26
+ multipleCommits: false,
27
+ noVerify: true,
28
+ resetAuthor: false,
29
+ reviewers: [],
30
+ sourcePRLabels: [],
31
+ targetBranchChoices: [],
32
+ targetBranches: [],
33
+ targetPRLabels: [],
34
+ verbose: false,
35
+ };
9
36
  async function getOptions(argv, optionsFromModule) {
10
37
  const optionsFromConfigFiles = await (0, config_1.getOptionsFromConfigFiles)(optionsFromModule);
11
38
  const optionsFromCliArgs = (0, cliArgs_1.getOptionsFromCliArgs)(argv);
39
+ // combined options from cli and config files
40
+ const combined = getCombinedOptions({
41
+ optionsFromConfigFiles,
42
+ optionsFromCliArgs,
43
+ });
12
44
  // update logger
13
- (0, logger_1.updateLogger)({ ...optionsFromConfigFiles, ...optionsFromCliArgs });
14
- const optionsFromGithub = await (0, getOptionsFromGithub_1.getOptionsFromGithub)(optionsFromConfigFiles, optionsFromCliArgs);
15
- const parsedOptions = (0, parseRequiredOptions_1.parseRequiredOptions)(optionsFromConfigFiles, optionsFromCliArgs, optionsFromGithub);
16
- return parsedOptions;
45
+ (0, logger_1.updateLogger)(combined);
46
+ // required
47
+ const accessToken = requireAccessToken(combined);
48
+ const repoName = requireRepoName(combined);
49
+ const repoOwner = requireRepoOwner(combined);
50
+ const optionsFromGithub = await (0, getOptionsFromGithub_1.getOptionsFromGithub)({
51
+ ...combined,
52
+ // required props
53
+ accessToken,
54
+ repoName,
55
+ repoOwner,
56
+ });
57
+ const res = {
58
+ // default author
59
+ author: optionsFromGithub.authenticatedUsername,
60
+ // default values have lowest precedence
61
+ ...exports.defaultConfigOptions,
62
+ // local config options override default options
63
+ ...optionsFromConfigFiles,
64
+ // remote config options override local config options
65
+ ...optionsFromGithub,
66
+ // cli args override the above
67
+ ...optionsFromCliArgs,
68
+ // required properties
69
+ accessToken,
70
+ repoName,
71
+ repoOwner,
72
+ };
73
+ requireTargetBranch(res);
74
+ return res;
17
75
  }
18
76
  exports.getOptions = getOptions;
77
+ function getCombinedOptions({ optionsFromConfigFiles, optionsFromCliArgs, }) {
78
+ return {
79
+ ...exports.defaultConfigOptions,
80
+ ...optionsFromConfigFiles,
81
+ ...optionsFromCliArgs,
82
+ };
83
+ }
84
+ function requireAccessToken(combinedOptions) {
85
+ // accessToken must be supplied in config or via cli args
86
+ if (!combinedOptions.accessToken) {
87
+ const globalConfigPath = (0, env_1.getGlobalConfigPath)();
88
+ throw new HandledError_1.HandledError(`Please update your config file: ${globalConfigPath}.\nIt must contain a valid "accessToken".\n\nRead more: ${GLOBAL_CONFIG_DOCS_LINK}`);
89
+ }
90
+ return combinedOptions.accessToken;
91
+ }
92
+ function requireRepoName(combinedOptions) {
93
+ if (!combinedOptions.repoName) {
94
+ throw new HandledError_1.HandledError(`Please specify a repo name: "--repo-name kibana".\n\nRead more: ${PROJECT_CONFIG_DOCS_LINK}`);
95
+ }
96
+ return combinedOptions.repoName;
97
+ }
98
+ function requireRepoOwner(combinedOptions) {
99
+ if (!combinedOptions.repoOwner) {
100
+ throw new HandledError_1.HandledError(`Please specify a repo owner: "--repo-owner elastic".\n\nRead more: ${PROJECT_CONFIG_DOCS_LINK}`);
101
+ }
102
+ return combinedOptions.repoOwner;
103
+ }
104
+ function requireTargetBranch(config) {
105
+ // ensure `targetBranches` or `targetBranchChoices` are given
106
+ if ((0, lodash_1.isEmpty)(config.targetBranches) &&
107
+ (0, lodash_1.isEmpty)(config.targetBranchChoices) &&
108
+ // this is primarily necessary on CI where `targetBranches` and `targetBranchChoices` and not given
109
+ (0, lodash_1.isEmpty)(config.branchLabelMapping)) {
110
+ throw new HandledError_1.HandledError(`Please specify a target branch: "--branch 6.1".\n\nRead more: ${PROJECT_CONFIG_DOCS_LINK}`);
111
+ }
112
+ }
@@ -24,159 +24,310 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
24
24
  Object.defineProperty(exports, "__esModule", { value: true });
25
25
  const nock_1 = __importDefault(require("nock"));
26
26
  const logger = __importStar(require("../services/logger"));
27
+ const mockConfigFiles_1 = require("../test/mockConfigFiles");
27
28
  const nockHelpers_1 = require("../test/nockHelpers");
28
29
  const options_1 = require("./options");
29
- function mockGetGithubConfigOptions({ defaultBranch, refName, }) {
30
- return (0, nockHelpers_1.mockGqlRequest)({
31
- name: 'GithubConfigOptions',
32
- statusCode: 200,
33
- body: {
34
- data: {
35
- repository: {
36
- isFork: false,
37
- parent: null,
38
- ref: refName ? { name: refName } : null,
39
- defaultBranchRef: {
40
- name: defaultBranch,
41
- target: { history: { edges: [] } },
42
- },
43
- },
44
- },
45
- },
46
- });
47
- }
48
- describe('getOptions', () => {
49
- const defaultArgs = [
50
- '--branch',
51
- '6.0',
52
- '--branch',
53
- '6.1',
54
- '--upstream',
55
- 'elastic/kibana',
30
+ jest.unmock('../services/fs-promisified');
31
+ const defaultConfigs = {
32
+ projectConfig: {
56
33
  // use localhost to avoid CORS issues with nock
57
- '--github-api-base-url-v4',
58
- 'http://localhost/graphql',
59
- ];
34
+ githubApiBaseUrlV4: 'http://localhost/graphql',
35
+ repoOwner: 'elastic',
36
+ repoName: 'kibana',
37
+ targetBranchChoices: ['7.9', '8.0'],
38
+ },
39
+ globalConfig: { accessToken: 'abc', editor: 'code' },
40
+ };
41
+ describe('getOptions', () => {
60
42
  afterEach(() => {
61
43
  jest.clearAllMocks();
62
44
  nock_1.default.cleanAll();
63
45
  });
64
- it('should use the default repository branch as sourceBranch', async () => {
65
- const mockCalls = mockGetGithubConfigOptions({
66
- defaultBranch: 'my-default-branch',
67
- });
68
- const options = await (0, options_1.getOptions)(defaultArgs);
69
- expect(mockCalls.length).toBe(1);
70
- expect(options.sourceBranch).toBe('my-default-branch');
46
+ beforeEach(() => {
47
+ (0, mockConfigFiles_1.mockConfigFiles)(defaultConfigs);
71
48
  });
72
- it('should use explicit sourceBranch instead of defaultBranch', async () => {
73
- const argv = [
74
- '--branch',
75
- '6.0',
76
- '--branch',
77
- '6.1',
78
- '--upstream',
79
- 'elastic/kibana',
80
- '--source-branch',
81
- 'my-source-branch',
82
- '--github-api-base-url-v4',
83
- 'http://localhost/graphql',
84
- ];
85
- mockGetGithubConfigOptions({ defaultBranch: 'my-default-branch' });
86
- const options = await (0, options_1.getOptions)(argv);
87
- expect(options.sourceBranch).toBe('my-source-branch');
49
+ describe('should throw', () => {
50
+ beforeEach(() => {
51
+ mockGithubConfigOptions({});
52
+ });
53
+ it('when accessToken is missing', async () => {
54
+ (0, mockConfigFiles_1.mockConfigFiles)({
55
+ projectConfig: defaultConfigs.projectConfig,
56
+ globalConfig: { accessToken: undefined },
57
+ });
58
+ await expect(() => (0, options_1.getOptions)([])).rejects
59
+ .toThrowErrorMatchingInlineSnapshot(`
60
+ "Please update your config file: /Users/sqren/.backport/config.json.
61
+ It must contain a valid \\"accessToken\\".
62
+
63
+ Read more: https://github.com/sqren/backport/blob/main/docs/configuration.md#global-config-backportconfigjson"
64
+ `);
65
+ });
66
+ it('when `targetBranches`, `targetBranchChoices` and `branchLabelMapping` are all empty', async () => {
67
+ mockProjectConfig({
68
+ targetBranches: undefined,
69
+ targetBranchChoices: undefined,
70
+ branchLabelMapping: undefined,
71
+ });
72
+ await expect(() => (0, options_1.getOptions)([])).rejects
73
+ .toThrowErrorMatchingInlineSnapshot(`
74
+ "Please specify a target branch: \\"--branch 6.1\\".
75
+
76
+ Read more: https://github.com/sqren/backport/blob/main/docs/configuration.md#project-config-backportrcjson"
77
+ `);
78
+ });
79
+ it('when repoName is missing', async () => {
80
+ mockProjectConfig({ repoName: '' });
81
+ await expect(() => (0, options_1.getOptions)([])).rejects
82
+ .toThrowErrorMatchingInlineSnapshot(`
83
+ "Please specify a repo name: \\"--repo-name kibana\\".
84
+
85
+ Read more: https://github.com/sqren/backport/blob/main/docs/configuration.md#project-config-backportrcjson"
86
+ `);
87
+ });
88
+ it('when repoOwner is missing', async () => {
89
+ mockProjectConfig({ repoOwner: '' });
90
+ await expect(() => (0, options_1.getOptions)([])).rejects
91
+ .toThrowErrorMatchingInlineSnapshot(`
92
+ "Please specify a repo owner: \\"--repo-owner elastic\\".
93
+
94
+ Read more: https://github.com/sqren/backport/blob/main/docs/configuration.md#project-config-backportrcjson"
95
+ `);
96
+ });
88
97
  });
89
98
  it('should ensure that "backport" branch does not exist', async () => {
90
- mockGetGithubConfigOptions({
91
- defaultBranch: 'my-default-branch',
92
- refName: 'backport',
93
- });
94
- await expect((0, options_1.getOptions)(defaultArgs)).rejects.toThrowError('You must delete the branch "backport" to continue. See https://github.com/sqren/backport/issues/155 for details');
99
+ mockGithubConfigOptions({ refName: 'backport' });
100
+ await expect((0, options_1.getOptions)([])).rejects.toThrowError('You must delete the branch "backport" to continue. See https://github.com/sqren/backport/issues/155 for details');
95
101
  });
96
102
  it('should merge config options and module options', async () => {
97
- mockGetGithubConfigOptions({ defaultBranch: 'my-default-branch' });
103
+ mockGithubConfigOptions({});
98
104
  const myFn = async () => true;
99
- const options = await (0, options_1.getOptions)(defaultArgs, { autoFixConflicts: myFn });
105
+ const options = await (0, options_1.getOptions)([], { autoFixConflicts: myFn });
100
106
  expect(options.autoFixConflicts).toBe(myFn);
101
107
  });
102
108
  it('should call updateLogger', async () => {
103
- mockGetGithubConfigOptions({ defaultBranch: 'my-default-branch' });
104
- await (0, options_1.getOptions)(defaultArgs);
109
+ mockGithubConfigOptions({});
110
+ await (0, options_1.getOptions)([]);
105
111
  expect(logger.updateLogger).toHaveBeenCalledTimes(1);
106
112
  });
107
113
  it('should return options', async () => {
108
- mockGetGithubConfigOptions({ defaultBranch: 'default-branch-from-github' });
109
- const options = await (0, options_1.getOptions)(defaultArgs);
114
+ mockGithubConfigOptions({
115
+ viewerLogin: 'john.diller',
116
+ defaultBranchRef: 'default-branch-from-github',
117
+ historicalMappings: [
118
+ {
119
+ committedDate: '2022-01-02T20:52:45.173Z',
120
+ branchLabelMapping: { foo: 'bar' },
121
+ },
122
+ ],
123
+ });
124
+ const options = await (0, options_1.getOptions)([]);
110
125
  expect(options).toEqual({
111
- accessToken: 'myAccessToken',
112
- all: false,
126
+ accessToken: 'abc',
113
127
  assignees: [],
114
- author: 'sqren',
115
128
  autoAssign: false,
129
+ authenticatedUsername: 'john.diller',
116
130
  autoMerge: false,
117
131
  autoMergeMethod: 'merge',
118
- ci: false,
132
+ backportBinary: 'backport',
133
+ branchLabelMapping: {
134
+ foo: 'bar',
135
+ },
119
136
  cherrypickRef: true,
137
+ ci: false,
138
+ commitPaths: [],
120
139
  details: false,
140
+ editor: 'code',
121
141
  fork: true,
122
- gitHostname: 'github.com',
123
- githubApiBaseUrlV3: 'https://api.github.com',
124
142
  githubApiBaseUrlV4: 'http://localhost/graphql',
125
- commitPaths: [],
126
- mainline: 1,
143
+ historicalBranchLabelMappings: [
144
+ {
145
+ branchLabelMapping: { foo: 'bar' },
146
+ committedDate: '2022-01-02T20:52:45.173Z',
147
+ },
148
+ ],
127
149
  maxNumber: 10,
128
150
  multipleBranches: true,
129
151
  multipleCommits: false,
130
152
  noVerify: true,
131
- prTitle: '[{targetBranch}] {commitMessages}',
132
153
  repoName: 'kibana',
133
154
  repoOwner: 'elastic',
134
155
  resetAuthor: false,
135
156
  reviewers: [],
136
157
  sourceBranch: 'default-branch-from-github',
137
158
  sourcePRLabels: [],
138
- targetBranchChoices: [
139
- { checked: false, name: '6.0' },
140
- { checked: false, name: '5.9' },
141
- ],
142
- targetBranches: ['6.0', '6.1'],
159
+ targetBranchChoices: ['7.9', '8.0'],
160
+ targetBranches: [],
143
161
  targetPRLabels: [],
144
- upstream: 'elastic/kibana',
145
- username: 'sqren',
162
+ author: 'john.diller',
146
163
  verbose: false,
147
164
  });
148
165
  });
166
+ describe('sourceBranch', () => {
167
+ beforeEach(() => {
168
+ mockGithubConfigOptions({ defaultBranchRef: 'some-default-branch' });
169
+ });
170
+ it('uses the `defaultBranchRef` as default', async () => {
171
+ const options = await (0, options_1.getOptions)([]);
172
+ expect(options.sourceBranch).toBe('some-default-branch');
173
+ });
174
+ it('uses the sourceBranch given via cli instead of `defaultBranchRef`', async () => {
175
+ const options = await (0, options_1.getOptions)([
176
+ '--source-branch',
177
+ 'cli-source-branch',
178
+ ]);
179
+ expect(options.sourceBranch).toBe('cli-source-branch');
180
+ });
181
+ });
182
+ describe('fork', () => {
183
+ beforeEach(() => {
184
+ mockGithubConfigOptions({});
185
+ });
186
+ it('is enabled by default', async () => {
187
+ const { fork } = await (0, options_1.getOptions)([]);
188
+ expect(fork).toBe(true);
189
+ });
190
+ it('can be disabled via `--no-fork` flag', async () => {
191
+ const { fork } = await (0, options_1.getOptions)(['--no-fork']);
192
+ expect(fork).toBe(false);
193
+ });
194
+ it('can be disabled via config file', async () => {
195
+ mockProjectConfig({ fork: false });
196
+ const { fork } = await (0, options_1.getOptions)([]);
197
+ expect(fork).toBe(false);
198
+ });
199
+ });
200
+ describe('reviewers', () => {
201
+ beforeEach(() => {
202
+ mockGithubConfigOptions({});
203
+ });
204
+ it('can be set via `--reviewer` flag', async () => {
205
+ const { reviewers } = await (0, options_1.getOptions)(['--reviewer', 'peter']);
206
+ expect(reviewers).toEqual(['peter']);
207
+ });
208
+ it('can be set via config file', async () => {
209
+ mockProjectConfig({ reviewers: ['john'] });
210
+ const { reviewers } = await (0, options_1.getOptions)([]);
211
+ expect(reviewers).toEqual(['john']);
212
+ });
213
+ });
214
+ describe('mainline', () => {
215
+ beforeEach(() => {
216
+ mockGithubConfigOptions({});
217
+ });
218
+ it('is not enabled by default', async () => {
219
+ const { mainline } = await (0, options_1.getOptions)([]);
220
+ expect(mainline).toBe(undefined);
221
+ });
222
+ it('can be set via `--mainline` flag', async () => {
223
+ const { mainline } = await (0, options_1.getOptions)(['--mainline']);
224
+ expect(mainline).toBe(1);
225
+ });
226
+ it('accepts numeric values', async () => {
227
+ const { mainline } = await (0, options_1.getOptions)(['--mainline', '2']);
228
+ expect(mainline).toBe(2);
229
+ });
230
+ });
231
+ describe('author', () => {
232
+ beforeEach(() => {
233
+ mockGithubConfigOptions({ viewerLogin: 'billy.bob' });
234
+ });
235
+ it('defaults to authenticated user', async () => {
236
+ const { author } = await (0, options_1.getOptions)([]);
237
+ expect(author).toBe('billy.bob');
238
+ });
239
+ it('can be overridden via `--author` flag', async () => {
240
+ const { author } = await (0, options_1.getOptions)(['--author', 'john.doe']);
241
+ expect(author).toBe('john.doe');
242
+ });
243
+ it('can be reset via `--all` flag', async () => {
244
+ const { author } = await (0, options_1.getOptions)(['--all']);
245
+ expect(author).toBe(null);
246
+ });
247
+ it('can be reset via config file (similar to `--all` flag)', async () => {
248
+ mockProjectConfig({ author: null });
249
+ const { author } = await (0, options_1.getOptions)([]);
250
+ expect(author).toBe(null);
251
+ });
252
+ it('can be overridden via config file', async () => {
253
+ mockProjectConfig({ author: 'jane.doe' });
254
+ const { author } = await (0, options_1.getOptions)([]);
255
+ expect(author).toBe('jane.doe');
256
+ });
257
+ });
149
258
  describe('cherrypickRef', () => {
150
259
  beforeEach(() => {
151
- mockGetGithubConfigOptions({ defaultBranch: 'my-default-branch' });
260
+ mockGithubConfigOptions({});
152
261
  });
153
- const defaultArgs = [
154
- // use localhost to avoid CORS issues with nock
155
- '--github-api-base-url-v4',
156
- 'http://localhost/graphql',
157
- ];
158
262
  it('should default to true', async () => {
159
- const { cherrypickRef } = await (0, options_1.getOptions)(defaultArgs);
263
+ const { cherrypickRef } = await (0, options_1.getOptions)([]);
160
264
  expect(cherrypickRef).toBe(true);
161
265
  });
162
266
  it('should negate with `noCherrypickRef` cli arg', async () => {
163
- const { cherrypickRef } = await (0, options_1.getOptions)([
164
- ...defaultArgs,
165
- '--no-cherrypick-ref',
166
- ]);
267
+ const { cherrypickRef } = await (0, options_1.getOptions)(['--no-cherrypick-ref']);
167
268
  expect(cherrypickRef).toBe(false);
168
269
  });
169
270
  it('should be settable via config file', async () => {
170
- const { cherrypickRef } = await (0, options_1.getOptions)([...defaultArgs], {
171
- cherrypickRef: false,
172
- });
271
+ mockProjectConfig({ cherrypickRef: false });
272
+ const { cherrypickRef } = await (0, options_1.getOptions)([]);
173
273
  expect(cherrypickRef).toBe(false);
174
274
  });
175
275
  it('cli args overwrites config', async () => {
176
- const { cherrypickRef } = await (0, options_1.getOptions)([...defaultArgs, '--cherrypick-ref'], {
177
- cherrypickRef: false,
178
- });
276
+ mockProjectConfig({ cherrypickRef: false });
277
+ const { cherrypickRef } = await (0, options_1.getOptions)(['--cherrypick-ref']);
179
278
  expect(cherrypickRef).toBe(true);
180
279
  });
181
280
  });
182
281
  });
282
+ function mockProjectConfig(projectConfig) {
283
+ return (0, mockConfigFiles_1.mockConfigFiles)({
284
+ globalConfig: { accessToken: 'abc' },
285
+ projectConfig: {
286
+ // use localhost to avoid CORS issues with nock
287
+ githubApiBaseUrlV4: 'http://localhost/graphql',
288
+ repoOwner: 'elastic',
289
+ repoName: 'kibana',
290
+ targetBranchChoices: ['7.9', '8.0'],
291
+ ...projectConfig,
292
+ },
293
+ });
294
+ }
295
+ function mockGithubConfigOptions({ viewerLogin = 'DO_NOT_USE-sqren', defaultBranchRef = 'DO_NOT_USE-default-branch-name', refName, historicalMappings = [], }) {
296
+ return (0, nockHelpers_1.mockGqlRequest)({
297
+ name: 'GithubConfigOptions',
298
+ statusCode: 200,
299
+ body: {
300
+ data: {
301
+ viewer: {
302
+ login: viewerLogin,
303
+ },
304
+ repository: {
305
+ isFork: false,
306
+ parent: null,
307
+ ref: refName ? { name: refName } : null,
308
+ defaultBranchRef: {
309
+ name: defaultBranchRef,
310
+ target: {
311
+ history: {
312
+ edges: historicalMappings.map(({ committedDate, branchLabelMapping }) => {
313
+ return {
314
+ remoteConfig: {
315
+ committedDate,
316
+ file: {
317
+ object: {
318
+ text: JSON.stringify({
319
+ branchLabelMapping,
320
+ }),
321
+ },
322
+ },
323
+ },
324
+ };
325
+ }),
326
+ },
327
+ },
328
+ },
329
+ },
330
+ },
331
+ },
332
+ });
333
+ }
@@ -17,7 +17,7 @@ export declare function parseRequiredOptions(optionsFromConfigFiles: OptionsFrom
17
17
  reviewers: string[];
18
18
  sourcePRLabels: (string[] | undefined) & (string[] | never[]);
19
19
  targetBranches: (string[] | undefined) & (string[] | never[]);
20
- targetPRLabels: string[];
20
+ targetPRLabels: (string[] | undefined) & (string[] | never[]);
21
21
  cherrypickRef: boolean;
22
22
  fork: boolean;
23
23
  noVerify: boolean;
@@ -40,7 +40,6 @@ export declare function parseRequiredOptions(optionsFromConfigFiles: OptionsFrom
40
40
  resetAuthor: boolean;
41
41
  sha: string | undefined;
42
42
  sourceBranch: string;
43
- upstream: string | undefined;
44
43
  verbose: boolean;
45
44
  autoFixConflicts?: (({ files, directory, logger, targetBranch, }: {
46
45
  files: string[];
@@ -56,6 +55,7 @@ export declare function parseRequiredOptions(optionsFromConfigFiles: OptionsFrom
56
55
  }) => boolean | Promise<boolean>) | undefined;
57
56
  multiple?: boolean | undefined;
58
57
  branchLabelMapping?: Record<string, string> | undefined;
58
+ upstream?: string | undefined;
59
59
  branches?: TargetBranchChoiceOrString[] | undefined;
60
60
  labels?: string[] | undefined;
61
61
  historicalBranchLabelMappings: {
@@ -63,7 +63,7 @@ export declare function parseRequiredOptions(optionsFromConfigFiles: OptionsFrom
63
63
  committedDate: string;
64
64
  }[];
65
65
  };
66
- export declare function getRequiredOptions({ accessToken, username, upstream, }: ConfigFileOptions): {
66
+ export declare function getRequiredOptions({ accessToken, username, repoName, repoOwner, }: ConfigFileOptions): {
67
67
  accessToken: string;
68
68
  username: string;
69
69
  repoOwner: string;