backport 5.6.6 → 6.1.2

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 (234) hide show
  1. package/README.md +186 -50
  2. package/dist/entrypoint.cli.js +3 -1
  3. package/dist/entrypoint.module.d.ts +18 -5
  4. package/dist/entrypoint.module.js +42 -12
  5. package/dist/entrypoint.module.private.test.d.ts +1 -0
  6. package/dist/entrypoint.module.private.test.js +34 -0
  7. package/dist/ink.d.ts +1 -0
  8. package/dist/ink.js +49 -0
  9. package/dist/main.d.ts +11 -6
  10. package/dist/main.js +40 -25
  11. package/dist/options/ConfigOptions.d.ts +28 -13
  12. package/dist/options/cliArgs.d.ts +36 -35
  13. package/dist/options/cliArgs.js +91 -52
  14. package/dist/options/cliArgs.test.js +35 -43
  15. package/dist/options/config/config.d.ts +35 -57
  16. package/dist/options/config/config.js +5 -39
  17. package/dist/options/config/config.test.js +11 -27
  18. package/dist/options/config/getOptionsFromGit.d.ts +3 -2
  19. package/dist/options/config/getOptionsFromGit.js +1 -3
  20. package/dist/options/config/globalConfig.d.ts +2 -51
  21. package/dist/options/config/globalConfig.js +9 -12
  22. package/dist/options/config/globalConfig.test.js +7 -5
  23. package/dist/options/config/projectConfig.d.ts +2 -51
  24. package/dist/options/config/projectConfig.js +2 -2
  25. package/dist/options/config/projectConfig.test.js +53 -16
  26. package/dist/options/config/readConfigFile.d.ts +44 -41
  27. package/dist/options/config/readConfigFile.js +30 -4
  28. package/dist/options/options.d.ts +59 -31
  29. package/dist/options/options.js +103 -10
  30. package/dist/options/options.test.js +266 -79
  31. package/dist/options/parseRequiredOptions.d.ts +19 -14
  32. package/dist/options/parseRequiredOptions.js +9 -15
  33. package/dist/options/parseRequiredOptions.test.js +30 -26
  34. package/dist/runSequentially.d.ts +20 -0
  35. package/dist/runSequentially.js +41 -0
  36. package/dist/runSequentially.test.d.ts +1 -0
  37. package/dist/runSequentially.test.js +196 -0
  38. package/dist/runWithOptions.js +7 -14
  39. package/dist/runWithOptions.test.js +138 -17
  40. package/dist/scripts/generate-gql.d.ts +1 -0
  41. package/dist/scripts/generate-gql.js +15 -0
  42. package/dist/scripts/postinstall.js +4 -4
  43. package/dist/scripts/postinstall.test.js +9 -2
  44. package/dist/scripts/runPostinstall.js +1 -1
  45. package/dist/services/HandledError.d.ts +11 -1
  46. package/dist/services/HandledError.js +2 -1
  47. package/dist/services/child-process-promisified.d.ts +1 -1
  48. package/dist/services/child-process-promisified.js +1 -1
  49. package/dist/services/env.js +5 -5
  50. package/dist/services/env.test.js +11 -4
  51. package/dist/services/fs-promisified.js +4 -4
  52. package/dist/services/getExpectedTarget.d.ts +9 -0
  53. package/dist/services/getExpectedTarget.js +77 -0
  54. package/dist/services/getExpectedTargetPullRequests.d.ts +9 -0
  55. package/dist/services/getExpectedTargetPullRequests.js +77 -0
  56. package/dist/services/git.d.ts +20 -6
  57. package/dist/services/git.integration.test.d.ts +1 -0
  58. package/dist/services/git.integration.test.js +168 -0
  59. package/dist/services/git.js +93 -67
  60. package/dist/services/git.test.js +68 -68
  61. package/dist/services/github/commitFormatters.d.ts +3 -7
  62. package/dist/services/github/commitFormatters.js +10 -19
  63. package/dist/services/github/commitFormatters.test.js +2 -24
  64. package/dist/services/github/v3/addAssigneesToPullRequest.d.ts +1 -1
  65. package/dist/services/github/v3/addAssigneesToPullRequest.js +3 -8
  66. package/dist/services/github/v3/addAssigneesToPullRequest.test.js +6 -8
  67. package/dist/services/github/v3/addLabelsToPullRequest.d.ts +1 -1
  68. package/dist/services/github/v3/addLabelsToPullRequest.js +2 -6
  69. package/dist/services/github/v3/addReviewersToPullRequest.d.ts +2 -0
  70. package/dist/services/github/v3/addReviewersToPullRequest.js +34 -0
  71. package/dist/services/github/v3/createPullRequest.d.ts +3 -2
  72. package/dist/services/github/v3/createPullRequest.js +31 -25
  73. package/dist/services/github/v3/createPullRequest.test.d.ts +1 -0
  74. package/dist/services/github/v3/createPullRequest.test.js +110 -0
  75. package/dist/services/github/v3/createStatusComment.d.ts +11 -0
  76. package/dist/services/github/v3/createStatusComment.js +95 -0
  77. package/dist/services/github/v3/createStatusComment.test.d.ts +1 -0
  78. package/dist/services/github/v3/createStatusComment.test.js +209 -0
  79. package/dist/services/github/v4/FetchPullRequestId.js +1 -1
  80. package/dist/services/github/v4/apiRequestV4.d.ts +2 -2
  81. package/dist/services/github/v4/apiRequestV4.js +6 -8
  82. package/dist/services/github/v4/apiRequestV4.test.js +7 -7
  83. package/dist/services/github/v4/disablePullRequestAutoMerge.js +3 -4
  84. package/dist/services/github/v4/enablePullRequestAutoMerge.js +9 -8
  85. package/dist/services/github/v4/enablePullRequestAutoMerge.private.test.js +93 -33
  86. package/dist/services/github/v4/fetchAuthorId.d.ts +5 -2
  87. package/dist/services/github/v4/fetchAuthorId.js +6 -7
  88. package/dist/services/github/v4/fetchAuthorId.private.test.js +6 -9
  89. package/dist/services/github/v4/fetchCommitByPullNumber.js +11 -9
  90. package/dist/services/github/v4/fetchCommitByPullNumber.private.test.js +4 -4
  91. package/dist/services/github/v4/fetchCommitBySha.js +14 -12
  92. package/dist/services/github/v4/fetchCommitBySha.private.test.js +4 -4
  93. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.d.ts +12 -0
  94. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.js +46 -0
  95. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.private.test.d.ts +1 -0
  96. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.private.test.js +70 -0
  97. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.d.ts +12 -0
  98. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.js +36 -0
  99. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.private.test.d.ts +1 -0
  100. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.private.test.js +55 -0
  101. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.d.ts +27 -0
  102. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.js +88 -0
  103. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.private.test.d.ts +1 -0
  104. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.private.test.js +128 -0
  105. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.test.d.ts +1 -0
  106. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.test.js +125 -0
  107. package/dist/services/github/v4/fetchCommits/fetchCommitsInDateRange.d.ts +28 -0
  108. package/dist/services/github/v4/fetchCommits/fetchCommitsInDateRange.js +52 -0
  109. package/dist/services/github/v4/fetchCommits/fetchProjectConfig.d.ts +3 -0
  110. package/dist/services/github/v4/fetchCommits/fetchProjectConfig.js +55 -0
  111. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.d.ts +10 -0
  112. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.js +49 -0
  113. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.private.test.d.ts +1 -0
  114. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.private.test.js +77 -0
  115. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.test.d.ts +1 -0
  116. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.test.js +252 -0
  117. package/dist/services/github/v4/fetchCommitsByAuthor.d.ts +2 -1
  118. package/dist/services/github/v4/fetchCommitsByAuthor.js +81 -70
  119. package/dist/services/github/v4/fetchCommitsByAuthor.private.test.js +78 -5
  120. package/dist/services/github/v4/fetchCommitsByAuthor.test.js +29 -21
  121. package/dist/services/github/v4/fetchDefaultBranch.js +1 -1
  122. package/dist/services/github/v4/fetchExistingPullRequest.js +2 -3
  123. package/dist/services/github/v4/fetchExistingPullRequest.private.test.js +3 -5
  124. package/dist/services/github/v4/fetchMergedPullRequests.d.ts +18 -19
  125. package/dist/services/github/v4/fetchMergedPullRequests.js +22 -69
  126. package/dist/services/github/v4/fetchMergedPullRequests.private.test.js +8 -9
  127. package/dist/services/github/v4/fetchProjectConfig.d.ts +8 -0
  128. package/dist/services/github/v4/fetchProjectConfig.js +56 -0
  129. package/dist/services/github/v4/fetchProjectConfig.private.test.d.ts +1 -0
  130. package/dist/services/github/v4/fetchProjectConfig.private.test.js +52 -0
  131. package/dist/services/github/v4/fetchProjectConfig.test.d.ts +1 -0
  132. package/dist/services/github/v4/fetchProjectConfig.test.js +37 -0
  133. package/dist/services/github/v4/fetchPullRequestAutoMergeMethod.js +2 -3
  134. package/dist/services/github/v4/fetchPullRequestBySearchQuery.js +12 -10
  135. package/dist/services/github/v4/fetchPullRequestBySearchQuery.private.test.js +3 -3
  136. package/dist/services/github/v4/fetchPullRequestBySearchQuery.test.js +2 -2
  137. package/dist/services/github/v4/getExistingTargetPullRequests.d.ts +4 -58
  138. package/dist/services/github/v4/getExistingTargetPullRequests.js +3 -58
  139. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.d.ts +67 -0
  140. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.js +130 -0
  141. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.private.test.d.ts +1 -0
  142. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.private.test.js +130 -0
  143. package/dist/services/github/v4/getOptionsFromGithub/index.d.ts +19 -11
  144. package/dist/services/github/v4/getOptionsFromGithub/index.js +52 -22
  145. package/dist/services/github/v4/getOptionsFromGithub/index.private.test.js +46 -7
  146. package/dist/services/github/v4/getOptionsFromGithub/query.d.ts +16 -16
  147. package/dist/services/github/v4/getOptionsFromGithub/query.js +16 -15
  148. package/dist/services/github/v4/getTargetBranchesFromLabels.d.ts +0 -7
  149. package/dist/services/github/v4/getTargetBranchesFromLabels.js +1 -26
  150. package/dist/services/github/v4/getTargetBranchesFromLabels.test.js +69 -32
  151. package/dist/services/github/v4/mocks/commitsByAuthorMock.d.ts +2 -2
  152. package/dist/services/github/v4/mocks/commitsByAuthorMock.js +39 -25
  153. package/dist/services/github/v4/mocks/fetchPullRequestBySearchQueryMock.d.ts +1 -1
  154. package/dist/services/github/v4/mocks/fetchPullRequestBySearchQueryMock.js +10 -0
  155. package/dist/services/github/v4/mocks/getCommitsByAuthorMock.d.ts +1 -1
  156. package/dist/services/github/v4/mocks/getCommitsByAuthorMock.js +2 -0
  157. package/dist/services/github/v4/mocks/getPullRequestNodeMock.d.ts +1 -1
  158. package/dist/services/github/v4/mocks/getPullRequestNodeMock.js +1 -0
  159. package/dist/services/github/v4/throwOnInvalidAccessToken.js +10 -15
  160. package/dist/services/github/v4/throwOnInvalidAccessToken.test.js +7 -7
  161. package/dist/services/logger.d.ts +3 -3
  162. package/dist/services/logger.js +16 -15
  163. package/dist/services/prompt.test.d.ts +1 -0
  164. package/dist/services/prompt.test.js +87 -0
  165. package/dist/services/prompts.d.ts +10 -4
  166. package/dist/services/prompts.js +85 -22
  167. package/dist/services/sourceCommit/getExpectedTargetPullRequests.d.ts +9 -0
  168. package/dist/services/sourceCommit/getExpectedTargetPullRequests.js +92 -0
  169. package/dist/services/sourceCommit/getExpectedTargetPullRequests.test.d.ts +1 -0
  170. package/dist/services/sourceCommit/getExpectedTargetPullRequests.test.js +413 -0
  171. package/dist/services/sourceCommit/getMockSourceCommit.d.ts +21 -0
  172. package/dist/services/sourceCommit/getMockSourceCommit.js +67 -0
  173. package/dist/services/sourceCommit/getTargetBranchesFromLabels.d.ts +5 -0
  174. package/dist/services/sourceCommit/getTargetBranchesFromLabels.js +20 -0
  175. package/dist/services/sourceCommit/getTargetBranchesFromLabels.test.d.ts +1 -0
  176. package/dist/services/sourceCommit/getTargetBranchesFromLabels.test.js +201 -0
  177. package/dist/services/sourceCommit/parseSourceCommit.d.ts +82 -0
  178. package/dist/services/sourceCommit/parseSourceCommit.js +96 -0
  179. package/dist/services/sourceCommit/parseSourceCommit.test.d.ts +1 -0
  180. package/dist/services/sourceCommit/parseSourceCommit.test.js +229 -0
  181. package/dist/services/sourceCommit/sourceCommit.d.ts +78 -0
  182. package/dist/services/sourceCommit/sourceCommit.js +86 -0
  183. package/dist/services/sourceCommit/sourceCommit.test.d.ts +1 -0
  184. package/dist/services/sourceCommit/sourceCommit.test.js +287 -0
  185. package/dist/services/sourceCommit.d.ts +78 -0
  186. package/dist/services/sourceCommit.js +86 -0
  187. package/dist/services/sourceCommit.test.d.ts +1 -0
  188. package/dist/services/sourceCommit.test.js +280 -0
  189. package/dist/test/inquirer.private.test.js +55 -51
  190. package/dist/test/integration/createSpies.js +6 -6
  191. package/dist/test/integration/envConstants.js +5 -6
  192. package/dist/test/integration/helpers.js +1 -1
  193. package/dist/test/integration/integration.private.test.d.ts +1 -0
  194. package/dist/test/integration/integration.private.test.js +327 -0
  195. package/dist/test/integration/integration.test.js +28 -29
  196. package/dist/test/mockConfigFiles.d.ts +5 -0
  197. package/dist/test/mockConfigFiles.js +37 -0
  198. package/dist/test/nockHelpers.d.ts +1 -1
  199. package/dist/test/nockHelpers.js +7 -11
  200. package/dist/test/private/getDevAccessToken.js +1 -1
  201. package/dist/test/setupFiles/automatic-mocks.d.ts +0 -1
  202. package/dist/test/setupFiles/automatic-mocks.js +1 -51
  203. package/dist/types/Commit.d.ts +85 -0
  204. package/dist/types/Commit.js +139 -0
  205. package/dist/types/commitNode.d.ts +89 -0
  206. package/dist/types/commitNode.js +139 -0
  207. package/dist/types/commitNode.test.d.ts +1 -0
  208. package/dist/types/commitNode.test.js +255 -0
  209. package/dist/types/commitWithAssociatedPullRequests.d.ts +86 -0
  210. package/dist/types/commitWithAssociatedPullRequests.js +145 -0
  211. package/dist/ui/cherrypickAndCreatePullRequest.test.js +346 -76
  212. package/dist/ui/cherrypickAndCreateTargetPullRequest.d.ts +2 -1
  213. package/dist/ui/cherrypickAndCreateTargetPullRequest.js +95 -67
  214. package/dist/ui/cherrypickAndCreateTargetPullRequest.test.d.ts +1 -0
  215. package/dist/ui/cherrypickAndCreateTargetPullRequest.test.js +468 -0
  216. package/dist/ui/getCommits.d.ts +1 -1
  217. package/dist/ui/getCommits.js +55 -25
  218. package/dist/ui/getCommitsWithoutBackports.d.ts +11 -0
  219. package/dist/ui/getCommitsWithoutBackports.js +53 -0
  220. package/dist/ui/getTargetBranches.d.ts +3 -2
  221. package/dist/ui/getTargetBranches.js +33 -18
  222. package/dist/ui/getTargetBranches.test.js +75 -99
  223. package/dist/ui/getTargetBranchesFromLabels.d.ts +6 -0
  224. package/dist/ui/getTargetBranchesFromLabels.js +40 -0
  225. package/dist/ui/getTargetBranchesFromLabels.test.d.ts +1 -0
  226. package/dist/ui/getTargetBranchesFromLabels.test.js +288 -0
  227. package/dist/ui/maybeSetupRepo.js +11 -10
  228. package/dist/ui/maybeSetupRepo.test.js +54 -4
  229. package/dist/utils/excludeUndefined.d.ts +6 -0
  230. package/dist/utils/excludeUndefined.js +7 -0
  231. package/dist/utils/excludeUndefined.test.d.ts +1 -0
  232. package/dist/utils/excludeUndefined.test.js +24 -0
  233. package/package.json +37 -51
  234. package/yarn.lock +2414 -2993
@@ -1,52 +1,67 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.getTargetBranchChoices = exports.getTargetBranches = void 0;
7
- const lodash_intersection_1 = __importDefault(require("lodash.intersection"));
8
- const lodash_isempty_1 = __importDefault(require("lodash.isempty"));
4
+ const lodash_1 = require("lodash");
9
5
  const HandledError_1 = require("../services/HandledError");
10
6
  const prompts_1 = require("../services/prompts");
11
- const filterEmpty_1 = require("../utils/filterEmpty");
12
7
  function getTargetBranches(options, commits) {
13
8
  // target branches already specified (in contrast to letting the user choose from a list)
14
- if (!lodash_isempty_1.default(options.targetBranches)) {
9
+ if (!(0, lodash_1.isEmpty)(options.targetBranches)) {
15
10
  return options.targetBranches;
16
11
  }
17
- // intersection of target branches from the selected commits
18
- const targetBranchesFromLabels = lodash_intersection_1.default(...commits.map((commit) => commit.targetBranchesFromLabels)).filter(filterEmpty_1.filterNil);
12
+ // target branches from the first commit
13
+ const missingTargetBranches = commits.length === 1
14
+ ? commits[0].expectedTargetPullRequests
15
+ .filter((pr) => pr.state === 'MISSING')
16
+ .map((pr) => pr.branch)
17
+ : [];
19
18
  // automatically backport to specified target branches
20
19
  if (options.ci) {
21
- if (lodash_isempty_1.default(targetBranchesFromLabels)) {
20
+ if ((0, lodash_1.isEmpty)(missingTargetBranches)) {
22
21
  throw new HandledError_1.HandledError(`There are no branches to backport to. Aborting.`);
23
22
  }
24
- return targetBranchesFromLabels;
23
+ return missingTargetBranches;
25
24
  }
26
25
  // sourceBranch should be the same for all commits, so picking `sourceBranch` from the first commit should be fine 🤞
27
26
  // this is specifically needed when backporting a PR like `backport --pr 123` and the source PR was merged to a non-default (aka non-master) branch.
28
27
  const { sourceBranch } = commits[0];
29
- const targetBranchChoices = getTargetBranchChoices(options, targetBranchesFromLabels, sourceBranch);
28
+ const targetBranchChoices = getTargetBranchChoices(options, missingTargetBranches, sourceBranch);
30
29
  // render interactive list of branches
31
- return prompts_1.promptForTargetBranches({
30
+ return (0, prompts_1.promptForTargetBranches)({
32
31
  targetBranchChoices,
33
32
  isMultipleChoice: options.multipleBranches,
34
33
  });
35
34
  }
36
35
  exports.getTargetBranches = getTargetBranches;
37
- function getTargetBranchChoices(options, targetBranchesFromLabels, sourceBranch) {
36
+ function getTargetBranchChoices(options, missingTargetBranches, sourceBranch) {
38
37
  // exclude sourceBranch from targetBranchChoices
39
- const targetBranchesChoices = options.targetBranchChoices.filter((choice) => choice.name !== sourceBranch);
40
- if (lodash_isempty_1.default(targetBranchesChoices)) {
38
+ const targetBranchesChoices = getTargetBranchChoicesAsObject(options.targetBranchChoices).filter((choice) => choice.name !== sourceBranch);
39
+ if ((0, lodash_1.isEmpty)(targetBranchesChoices)) {
41
40
  throw new HandledError_1.HandledError('Missing target branch choices');
42
41
  }
43
42
  if (!options.branchLabelMapping) {
44
43
  return targetBranchesChoices;
45
44
  }
46
- // select target branches based on pull request labels
45
+ // select missing target branches (based on pull request labels)
47
46
  return targetBranchesChoices.map((choice) => {
48
- const isChecked = targetBranchesFromLabels.includes(choice.name);
47
+ const isChecked = missingTargetBranches.includes(choice.name);
49
48
  return { ...choice, checked: isChecked };
50
49
  });
51
50
  }
52
51
  exports.getTargetBranchChoices = getTargetBranchChoices;
52
+ // `targetBranchChoices` can either be a string or an object.
53
+ // It must be transformed so it is always treated as an object troughout the application
54
+ function getTargetBranchChoicesAsObject(targetBranchChoices) {
55
+ if (!targetBranchChoices) {
56
+ return [];
57
+ }
58
+ return targetBranchChoices.map((choice) => {
59
+ if ((0, lodash_1.isString)(choice)) {
60
+ return {
61
+ name: choice,
62
+ checked: false,
63
+ };
64
+ }
65
+ return choice;
66
+ });
67
+ }
@@ -29,138 +29,123 @@ describe('getTargetBranches', () => {
29
29
  .spyOn(prompts, 'promptForTargetBranches')
30
30
  .mockResolvedValueOnce(['branchA']);
31
31
  });
32
- describe('when `targetBranchesFromLabels=["7.x"]`', () => {
33
- let targetBranchChoices;
32
+ describe('when `options.targetBranches` is empty', () => {
33
+ let branches;
34
34
  beforeEach(async () => {
35
35
  const options = {
36
36
  targetBranches: [],
37
- multipleBranches: true,
38
- targetBranchChoices: [
39
- { name: 'master' },
40
- { name: '7.x' },
41
- { name: '7.7' },
42
- { name: '7.6' },
43
- { name: '7.5' },
44
- ],
45
- branchLabelMapping: {},
46
- sourceBranch: 'master',
37
+ targetBranchChoices: [{ name: 'branchA' }, { name: 'branchB' }],
38
+ multipleBranches: false,
47
39
  };
48
40
  const commits = [
49
41
  {
50
- sourceBranch: 'master',
51
- targetBranchesFromLabels: ['7.x'],
52
- sha: 'my-sha',
53
- formattedMessage: '[backport] Bump to 5.1.3 (#62286)',
54
- originalMessage: '[backport] Bump to 5.1.3 (#62286)',
55
- pullNumber: 62286,
56
- existingTargetPullRequests: [],
42
+ committedDate: 'aaa',
43
+ originalMessage: 'hey',
44
+ sha: 'abcd',
45
+ sourceBranch: '7.x',
46
+ pullNumber: 1337,
47
+ expectedTargetPullRequests: [],
57
48
  },
58
49
  ];
59
- await getTargetBranches_1.getTargetBranches(options, commits);
60
- targetBranchChoices = promptSpy.mock.calls[0][0].targetBranchChoices;
61
- });
62
- it('should list the correct branches', async () => {
63
- expect(targetBranchChoices).toEqual([
64
- { checked: true, name: '7.x' },
65
- { checked: false, name: '7.7' },
66
- { checked: false, name: '7.6' },
67
- { checked: false, name: '7.5' },
68
- ]);
50
+ branches = await (0, getTargetBranches_1.getTargetBranches)(options, commits);
69
51
  });
70
- it('should not list the sourceBranch (master)', async () => {
71
- expect(targetBranchChoices).not.toContainEqual(expect.objectContaining({ name: 'master' }));
52
+ it('should return branches from prompt', () => {
53
+ expect(branches).toEqual(['branchA']);
72
54
  });
73
- it('should select 7.x', async () => {
74
- expect(targetBranchChoices).toContainEqual({
75
- name: '7.x',
76
- checked: true,
55
+ it('should call prompt with correct args', () => {
56
+ expect(promptSpy).toHaveBeenLastCalledWith({
57
+ targetBranchChoices: [{ name: 'branchA' }, { name: 'branchB' }],
58
+ isMultipleChoice: false,
77
59
  });
78
60
  });
79
61
  });
80
- describe('when `targetBranchesFromLabels=["8.0.0"]`', () => {
62
+ describe('when `options.targetBranches` is not empty', () => {
63
+ let branches;
64
+ beforeEach(() => {
65
+ branches = (0, getTargetBranches_1.getTargetBranches)({
66
+ targetBranches: ['branchA', 'branchB'],
67
+ targetBranchChoices: [],
68
+ multipleBranches: false,
69
+ }, []);
70
+ });
71
+ it('should return branches from `options.branches`', () => {
72
+ expect(branches).toEqual(['branchA', 'branchB']);
73
+ });
74
+ it('should not call prompt', () => {
75
+ expect(promptSpy).not.toHaveBeenCalled();
76
+ });
77
+ });
78
+ describe('when ci=true', () => {
79
+ it('should throw when there are no missing backports', () => {
80
+ const commits = [];
81
+ expect(() => {
82
+ return (0, getTargetBranches_1.getTargetBranches)({
83
+ expectedTargetPullRequests: [],
84
+ ci: true,
85
+ }, commits);
86
+ }).toThrow('There are no branches to backport to. Aborting.');
87
+ });
88
+ it('should return missing backports', () => {
89
+ const commits = [
90
+ {
91
+ expectedTargetPullRequests: [
92
+ { branch: '7.2', state: 'MERGED' },
93
+ { branch: '7.1', state: 'OPEN' },
94
+ { branch: '7.x', state: 'MISSING' },
95
+ ],
96
+ },
97
+ ];
98
+ const targetBranches = (0, getTargetBranches_1.getTargetBranches)({ ci: true }, commits);
99
+ expect(targetBranches).toEqual(['7.x']);
100
+ });
101
+ });
102
+ describe('when `expectedTargetPullRequests` is missing a backport to 7.x', () => {
81
103
  let targetBranchChoices;
82
104
  beforeEach(async () => {
83
105
  const options = {
84
106
  targetBranches: [],
85
107
  multipleBranches: true,
86
108
  targetBranchChoices: [
109
+ { name: 'master' },
87
110
  { name: '7.x' },
88
111
  { name: '7.7' },
89
112
  { name: '7.6' },
90
113
  { name: '7.5' },
91
114
  ],
115
+ branchLabelMapping: {},
92
116
  sourceBranch: 'master',
93
117
  };
94
118
  const commits = [
95
119
  {
120
+ committedDate: 'bbb',
96
121
  sourceBranch: 'master',
97
- targetBranchesFromLabels: ['8.0.0'],
98
122
  sha: 'my-sha',
99
- formattedMessage: '[backport] Bump to 5.1.3 (#62286)',
100
123
  originalMessage: '[backport] Bump to 5.1.3 (#62286)',
101
124
  pullNumber: 62286,
102
- existingTargetPullRequests: [],
125
+ expectedTargetPullRequests: [{ branch: '7.x', state: 'MISSING' }],
103
126
  },
104
127
  ];
105
- await getTargetBranches_1.getTargetBranches(options, commits);
128
+ await (0, getTargetBranches_1.getTargetBranches)(options, commits);
106
129
  targetBranchChoices = promptSpy.mock.calls[0][0].targetBranchChoices;
107
130
  });
108
131
  it('should list the correct branches', async () => {
109
132
  expect(targetBranchChoices).toEqual([
110
- { name: '7.x' },
111
- { name: '7.7' },
112
- { name: '7.6' },
113
- { name: '7.5' },
133
+ { checked: true, name: '7.x' },
134
+ { checked: false, name: '7.7' },
135
+ { checked: false, name: '7.6' },
136
+ { checked: false, name: '7.5' },
114
137
  ]);
115
138
  });
116
- });
117
- describe('when `options.targetBranches` is empty', () => {
118
- let branches;
119
- beforeEach(async () => {
120
- const options = {
121
- targetBranches: [],
122
- targetBranchChoices: [{ name: 'branchA' }, { name: 'branchB' }],
123
- multipleBranches: false,
124
- };
125
- const commits = [
126
- {
127
- formattedMessage: 'hey',
128
- originalMessage: 'hey',
129
- targetBranchesFromLabels: [],
130
- sha: 'abcd',
131
- sourceBranch: '7.x',
132
- pullNumber: 1337,
133
- existingTargetPullRequests: [],
134
- },
135
- ];
136
- branches = await getTargetBranches_1.getTargetBranches(options, commits);
137
- });
138
- it('should return branches from prompt', () => {
139
- expect(branches).toEqual(['branchA']);
139
+ it('should not list the sourceBranch (master)', async () => {
140
+ expect(targetBranchChoices).not.toContainEqual(expect.objectContaining({ name: 'master' }));
140
141
  });
141
- it('should call prompt with correct args', () => {
142
- expect(promptSpy).toHaveBeenLastCalledWith({
143
- targetBranchChoices: [{ name: 'branchA' }, { name: 'branchB' }],
144
- isMultipleChoice: false,
142
+ it('should select 7.x', async () => {
143
+ expect(targetBranchChoices).toContainEqual({
144
+ name: '7.x',
145
+ checked: true,
145
146
  });
146
147
  });
147
148
  });
148
- describe('when `options.targetBranches` is not empty', () => {
149
- let branches;
150
- beforeEach(() => {
151
- branches = getTargetBranches_1.getTargetBranches({
152
- targetBranches: ['branchA', 'branchB'],
153
- targetBranchChoices: [],
154
- multipleBranches: false,
155
- }, []);
156
- });
157
- it('should return branches from `options.branches`', () => {
158
- expect(branches).toEqual(['branchA', 'branchB']);
159
- });
160
- it('should not call prompt', () => {
161
- expect(promptSpy).not.toHaveBeenCalled();
162
- });
163
- });
164
149
  });
165
150
  describe('getTargetBranchChoices', () => {
166
151
  const options = {
@@ -176,16 +161,7 @@ describe('getTargetBranchChoices', () => {
176
161
  const sourceBranch = 'master';
177
162
  it('should not check any branches if no labels match', () => {
178
163
  const targetBranchesFromLabels = [];
179
- const branches = getTargetBranches_1.getTargetBranchChoices(options, targetBranchesFromLabels, sourceBranch);
180
- expect(branches).toEqual([
181
- { checked: false, name: '7.x' },
182
- { checked: false, name: '7.8' },
183
- { checked: false, name: '7.7' },
184
- ]);
185
- });
186
- it('should not return default branches when running in "--ci" mode', () => {
187
- const targetBranchesFromLabels = [];
188
- const branches = getTargetBranches_1.getTargetBranchChoices({ ...options, ci: true }, targetBranchesFromLabels, sourceBranch);
164
+ const branches = (0, getTargetBranches_1.getTargetBranchChoices)(options, targetBranchesFromLabels, sourceBranch);
189
165
  expect(branches).toEqual([
190
166
  { checked: false, name: '7.x' },
191
167
  { checked: false, name: '7.8' },
@@ -194,7 +170,7 @@ describe('getTargetBranchChoices', () => {
194
170
  });
195
171
  it('should pre-select branches based on labels', () => {
196
172
  const targetBranchesFromLabels = ['7.7'];
197
- const branches = getTargetBranches_1.getTargetBranchChoices(options, targetBranchesFromLabels, sourceBranch);
173
+ const branches = (0, getTargetBranches_1.getTargetBranchChoices)(options, targetBranchesFromLabels, sourceBranch);
198
174
  expect(branches).toEqual([
199
175
  { checked: false, name: '7.x' },
200
176
  { checked: false, name: '7.8' },
@@ -0,0 +1,6 @@
1
+ import { ValidConfigOptions } from '../options/options';
2
+ import { Commit } from '../types/Commit';
3
+ export declare function getTargetBranchesFromLabels({ options, commits, }: {
4
+ options: ValidConfigOptions;
5
+ commits: Commit[];
6
+ }): string[];
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getTargetBranchesFromLabels = void 0;
7
+ const lodash_isempty_1 = __importDefault(require("lodash.isempty"));
8
+ const matcher_1 = __importDefault(require("matcher"));
9
+ function getTargetBranchesFromLabels({ options, commits, }) {
10
+ // don't infer target branches if multiple commits are selected
11
+ if (commits.length != 1) {
12
+ return [];
13
+ }
14
+ // use first (and only) commit
15
+ const commit = commits[0];
16
+ // commit must be related to a PR that contain labels
17
+ if (!commit.sourcePRLabels) {
18
+ return [];
19
+ }
20
+ const existingPRs = commit.existingTargetPullRequests.map((pr) => pr.branch);
21
+ return (options.targetBranchChoices
22
+ .filter((targetBranchChoice) => {
23
+ var _a, _b;
24
+ // if there's no sourcePRLabels, it should match by the branch name
25
+ if (targetBranchChoice.checked !== false &&
26
+ lodash_isempty_1.default(targetBranchChoice.sourcePRLabels)) {
27
+ return (_a = commit.sourcePRLabels) === null || _a === void 0 ? void 0 : _a.includes(targetBranchChoice.name);
28
+ }
29
+ // match by sourcePRLabels
30
+ return (_b = targetBranchChoice.sourcePRLabels) === null || _b === void 0 ? void 0 : _b.every((label) =>
31
+ //@ts-expect-error
32
+ matcher_1.default.isMatch(commit.sourcePRLabels, label));
33
+ })
34
+ // remove target branch if a pr already exists for the given branch
35
+ .filter((targetBranch) => !existingPRs.includes(targetBranch.name))
36
+ // remove target branch if source branch is identical
37
+ .filter((targetBranch) => commit.sourceBranch !== targetBranch.name)
38
+ .map((targetBranch) => targetBranch.name));
39
+ }
40
+ exports.getTargetBranchesFromLabels = getTargetBranchesFromLabels;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,288 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const getTargetBranchesFromLabels_1 = require("./getTargetBranchesFromLabels");
4
+ describe('getTargetBranchesFromLabels', () => {
5
+ it(`should support Kibana's label format`, () => {
6
+ const targetBranchChoices = [
7
+ { name: 'master', sourcePRLabels: ['v8.0.0'] },
8
+ { name: '7.x', sourcePRLabels: ['v7.8.0'] },
9
+ { name: '7.7', sourcePRLabels: ['v7.7.0'] },
10
+ { name: '7.6', sourcePRLabels: ['v7.6.0'] },
11
+ { name: '7.5', sourcePRLabels: ['v7.5.0'] },
12
+ { name: '7.4', sourcePRLabels: ['v7.4.1'] },
13
+ { name: '7.3', sourcePRLabels: ['v7.3.3'] },
14
+ { name: '7.2', sourcePRLabels: ['v7.2.2'] },
15
+ { name: '7.1', sourcePRLabels: ['v7.1.2'] },
16
+ { name: '7.0', sourcePRLabels: ['v7.0.2'] },
17
+ { name: '6.8', sourcePRLabels: ['v6.8.4'] },
18
+ { name: '6.7', sourcePRLabels: ['v6.7.2'] },
19
+ { name: '6.6', sourcePRLabels: ['v6.6.3'] },
20
+ { name: '6.5', sourcePRLabels: ['v6.5.5'] },
21
+ { name: '6.4', sourcePRLabels: ['v6.4.4'] },
22
+ { name: '6.3', sourcePRLabels: ['v6.3.3'] },
23
+ { name: '6.2', sourcePRLabels: ['v6.2.5'] },
24
+ { name: '6.1', sourcePRLabels: ['v6.1.4'] },
25
+ { name: '6.0', sourcePRLabels: ['v6.0.1'] },
26
+ { name: '5.6', sourcePRLabels: ['v5.6.16'] },
27
+ ];
28
+ const sourceBranch = 'master';
29
+ const existingTargetPullRequests = [];
30
+ const sourcePRLabels = [
31
+ 'release_note:fix',
32
+ 'v5.4.3',
33
+ 'v5.5.3',
34
+ 'v5.6.16',
35
+ 'v6.0.1',
36
+ 'v6.1.4',
37
+ 'v6.2.5',
38
+ 'v6.3.3',
39
+ 'v6.4.4',
40
+ 'v6.5.5',
41
+ 'v6.6.3',
42
+ 'v6.7.2',
43
+ 'v6.8.4',
44
+ 'v7.0.2',
45
+ 'v7.1.2',
46
+ 'v7.2.2',
47
+ 'v7.3.3',
48
+ 'v7.4.1',
49
+ 'v7.5.0',
50
+ 'v7.6.0',
51
+ 'v7.7.0',
52
+ 'v7.8.0',
53
+ 'v8.0.0', // master
54
+ ];
55
+ const targetBranches = getTargetBranchesFromLabels_1.getTargetBranchesFromLabels({
56
+ commits: [
57
+ { sourceBranch, existingTargetPullRequests, sourcePRLabels },
58
+ ],
59
+ options: {
60
+ targetBranchChoices,
61
+ },
62
+ });
63
+ expect(targetBranches).toEqual([
64
+ '7.x',
65
+ '7.7',
66
+ '7.6',
67
+ '7.5',
68
+ '7.4',
69
+ '7.3',
70
+ '7.2',
71
+ '7.1',
72
+ '7.0',
73
+ '6.8',
74
+ '6.7',
75
+ '6.6',
76
+ '6.5',
77
+ '6.4',
78
+ '6.3',
79
+ '6.2',
80
+ '6.1',
81
+ '6.0',
82
+ '5.6',
83
+ ]);
84
+ });
85
+ it('returns target branch if all labels in sourcePRLabels matches', () => {
86
+ const sourceBranch = 'master';
87
+ const existingTargetPullRequests = [];
88
+ const sourcePRLabels = ['label-foo', 'label-bar'];
89
+ const targetBranchChoices = [
90
+ { name: 'branch-1', sourcePRLabels: ['label-foo', 'label-bar'] },
91
+ { name: 'branch-2', sourcePRLabels: [] },
92
+ { name: 'branch-3', sourcePRLabels: [] },
93
+ ];
94
+ const targetBranches = getTargetBranchesFromLabels_1.getTargetBranchesFromLabels({
95
+ commits: [
96
+ { sourceBranch, existingTargetPullRequests, sourcePRLabels },
97
+ ],
98
+ options: {
99
+ targetBranchChoices,
100
+ },
101
+ });
102
+ expect(targetBranches).toEqual(['branch-1']);
103
+ });
104
+ it('does not return target branch if only some items in sourcePRLabels matches', () => {
105
+ const sourceBranch = 'master';
106
+ const existingTargetPullRequests = [];
107
+ const sourcePRLabels = ['label-foo'];
108
+ const targetBranchChoices = [
109
+ { name: 'branch-1', sourcePRLabels: ['label-foo', 'label-bar'] },
110
+ { name: 'branch-2', sourcePRLabels: [] },
111
+ { name: 'branch-3', sourcePRLabels: [] },
112
+ ];
113
+ const targetBranches = getTargetBranchesFromLabels_1.getTargetBranchesFromLabels({
114
+ commits: [
115
+ { sourceBranch, existingTargetPullRequests, sourcePRLabels },
116
+ ],
117
+ options: {
118
+ targetBranchChoices,
119
+ },
120
+ });
121
+ expect(targetBranches).toEqual([]);
122
+ });
123
+ it('does not return branch if a related PR is already open', () => {
124
+ const sourceBranch = 'master';
125
+ const existingTargetPullRequests = [
126
+ { branch: 'branch-1', state: 'OPEN' },
127
+ ];
128
+ const sourcePRLabels = ['label-foo', 'label-bar', 'label-baz'];
129
+ const targetBranchChoices = [
130
+ { name: 'branch-1', sourcePRLabels: ['label-foo', 'label-bar'] },
131
+ { name: 'branch-2', sourcePRLabels: ['label-baz'] },
132
+ { name: 'branch-3', sourcePRLabels: [] },
133
+ ];
134
+ const targetBranches = getTargetBranchesFromLabels_1.getTargetBranchesFromLabels({
135
+ commits: [
136
+ { sourceBranch, existingTargetPullRequests, sourcePRLabels },
137
+ ],
138
+ options: {
139
+ targetBranchChoices,
140
+ },
141
+ });
142
+ expect(targetBranches).toEqual(['branch-2']);
143
+ });
144
+ it('does not return branch if a related PR is already merged', () => {
145
+ const sourceBranch = 'master';
146
+ const existingTargetPullRequests = [
147
+ { branch: 'branch-2', state: 'MERGED' },
148
+ ];
149
+ const sourcePRLabels = ['label-foo', 'label-bar', 'label-baz'];
150
+ const targetBranchChoices = [
151
+ { name: 'branch-1', sourcePRLabels: ['label-foo', 'label-bar'] },
152
+ { name: 'branch-2', sourcePRLabels: ['label-baz'] },
153
+ { name: 'branch-3', sourcePRLabels: [] },
154
+ ];
155
+ const targetBranches = getTargetBranchesFromLabels_1.getTargetBranchesFromLabels({
156
+ commits: [
157
+ { sourceBranch, existingTargetPullRequests, sourcePRLabels },
158
+ ],
159
+ options: {
160
+ targetBranchChoices,
161
+ },
162
+ });
163
+ expect(targetBranches).toEqual(['branch-1']);
164
+ });
165
+ it('does not return branch if sourceBranch is identical', () => {
166
+ const sourceBranch = 'branch-2';
167
+ const existingTargetPullRequests = [];
168
+ const sourcePRLabels = ['label-foo', 'label-bar', 'label-baz'];
169
+ const targetBranchChoices = [
170
+ { name: 'branch-1', sourcePRLabels: ['label-foo', 'label-bar'] },
171
+ { name: 'branch-2', sourcePRLabels: ['label-baz'] },
172
+ { name: 'branch-3', sourcePRLabels: [] },
173
+ ];
174
+ const targetBranches = getTargetBranchesFromLabels_1.getTargetBranchesFromLabels({
175
+ commits: [
176
+ { sourceBranch, existingTargetPullRequests, sourcePRLabels },
177
+ ],
178
+ options: {
179
+ targetBranchChoices,
180
+ },
181
+ });
182
+ expect(targetBranches).toEqual(['branch-1']);
183
+ });
184
+ it('ignores non-matching labels', () => {
185
+ const sourceBranch = 'master';
186
+ const existingTargetPullRequests = [];
187
+ const sourcePRLabels = [
188
+ 'label-foo',
189
+ 'label-bar',
190
+ 'label-baz',
191
+ 'label-unknown',
192
+ ];
193
+ const targetBranchChoices = [
194
+ { name: 'branch-1', sourcePRLabels: ['label-foo', 'label-bar'] },
195
+ { name: 'branch-2', sourcePRLabels: ['label-baz'] },
196
+ { name: 'branch-3', sourcePRLabels: [] },
197
+ ];
198
+ const targetBranches = getTargetBranchesFromLabels_1.getTargetBranchesFromLabels({
199
+ commits: [
200
+ { sourceBranch, existingTargetPullRequests, sourcePRLabels },
201
+ ],
202
+ options: {
203
+ targetBranchChoices,
204
+ },
205
+ });
206
+ expect(targetBranches).toEqual(['branch-1', 'branch-2']);
207
+ });
208
+ it('supports wildcard matching', () => {
209
+ const sourceBranch = 'master';
210
+ const existingTargetPullRequests = [];
211
+ const sourcePRLabels = ['label-c-foo'];
212
+ const targetBranchChoices = [
213
+ { name: 'branch-1', sourcePRLabels: ['label-a'] },
214
+ { name: 'branch-2', sourcePRLabels: ['label-b'] },
215
+ { name: 'branch-3', sourcePRLabels: ['label-c-*'] },
216
+ { name: 'branch-4', sourcePRLabels: ['label-d'] },
217
+ ];
218
+ const targetBranches = getTargetBranchesFromLabels_1.getTargetBranchesFromLabels({
219
+ commits: [
220
+ { sourceBranch, existingTargetPullRequests, sourcePRLabels },
221
+ ],
222
+ options: {
223
+ targetBranchChoices,
224
+ },
225
+ });
226
+ expect(targetBranches).toEqual(['branch-3']);
227
+ });
228
+ it('supports literal wildcards', () => {
229
+ const sourceBranch = 'master';
230
+ const existingTargetPullRequests = [];
231
+ const sourcePRLabels = ['label-wildcard-*'];
232
+ const targetBranchChoices = [
233
+ { name: 'branch-1', sourcePRLabels: ['label-a'] },
234
+ { name: 'branch-2', sourcePRLabels: ['label-b'] },
235
+ { name: 'branch-3', sourcePRLabels: ['label-wildcard-*'] },
236
+ { name: 'branch-4', sourcePRLabels: ['label-d'] },
237
+ ];
238
+ const targetBranches = getTargetBranchesFromLabels_1.getTargetBranchesFromLabels({
239
+ commits: [
240
+ { sourceBranch, existingTargetPullRequests, sourcePRLabels },
241
+ ],
242
+ options: {
243
+ targetBranchChoices,
244
+ },
245
+ });
246
+ expect(targetBranches).toEqual(['branch-3']);
247
+ });
248
+ it('returns empty if no commits are selected', () => {
249
+ const targetBranchChoices = [
250
+ { name: 'branch-1', sourcePRLabels: ['label-a'] },
251
+ { name: 'branch-2', sourcePRLabels: ['label-b'] },
252
+ ];
253
+ const targetBranches = getTargetBranchesFromLabels_1.getTargetBranchesFromLabels({
254
+ commits: [],
255
+ options: {
256
+ targetBranchChoices,
257
+ },
258
+ });
259
+ expect(targetBranches).toEqual([]);
260
+ });
261
+ it('returns empty if multiple commits are selected', () => {
262
+ const sourceBranch = 'master';
263
+ const existingTargetPullRequests = [];
264
+ const targetBranchChoices = [
265
+ { name: 'branch-1', sourcePRLabels: ['label-foo'] },
266
+ { name: 'branch-2', sourcePRLabels: ['label-baz'] },
267
+ { name: 'branch-3', sourcePRLabels: [] },
268
+ ];
269
+ const targetBranches = getTargetBranchesFromLabels_1.getTargetBranchesFromLabels({
270
+ commits: [
271
+ {
272
+ sourceBranch,
273
+ existingTargetPullRequests,
274
+ sourcePRLabels: ['label-foo'],
275
+ },
276
+ {
277
+ sourceBranch,
278
+ existingTargetPullRequests,
279
+ sourcePRLabels: ['label-bar'],
280
+ },
281
+ ],
282
+ options: {
283
+ targetBranchChoices,
284
+ },
285
+ });
286
+ expect(targetBranches).toEqual([]);
287
+ });
288
+ });