backport 5.6.3 → 6.0.0

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 (230) hide show
  1. package/README.md +48 -39
  2. package/dist/entrypoint.cli.js +1 -1
  3. package/dist/entrypoint.module.d.ts +6 -5
  4. package/dist/entrypoint.module.js +8 -6
  5. package/dist/ink.d.ts +1 -0
  6. package/dist/ink.js +49 -0
  7. package/dist/main.d.ts +11 -6
  8. package/dist/main.js +40 -24
  9. package/dist/options/ConfigOptions.d.ts +47 -39
  10. package/dist/options/cliArgs.d.ts +28 -34
  11. package/dist/options/cliArgs.js +113 -94
  12. package/dist/options/cliArgs.test.js +103 -119
  13. package/dist/options/config/config.d.ts +81 -2
  14. package/dist/options/config/config.js +46 -6
  15. package/dist/options/config/config.test.js +24 -2
  16. package/dist/options/config/getOptionsFromGit.d.ts +3 -0
  17. package/dist/options/config/getOptionsFromGit.js +10 -0
  18. package/dist/options/config/globalConfig.d.ts +2 -1
  19. package/dist/options/config/globalConfig.js +8 -8
  20. package/dist/options/config/globalConfig.test.js +7 -3
  21. package/dist/options/config/projectConfig.d.ts +2 -1
  22. package/dist/options/config/projectConfig.js +3 -3
  23. package/dist/options/config/projectConfig.test.js +4 -4
  24. package/dist/options/config/readConfigFile.d.ts +2 -2
  25. package/dist/options/config/readConfigFile.js +3 -3
  26. package/dist/options/options.d.ts +40 -27
  27. package/dist/options/options.js +8 -16
  28. package/dist/options/options.test.js +66 -26
  29. package/dist/options/parseRequiredOptions.d.ts +71 -0
  30. package/dist/options/parseRequiredOptions.js +70 -0
  31. package/dist/options/parseRequiredOptions.test.d.ts +1 -0
  32. package/dist/options/parseRequiredOptions.test.js +127 -0
  33. package/dist/runSequentially.d.ts +20 -0
  34. package/dist/runSequentially.js +41 -0
  35. package/dist/runSequentially.test.d.ts +1 -0
  36. package/dist/runSequentially.test.js +235 -0
  37. package/dist/runWithOptions.js +7 -14
  38. package/dist/runWithOptions.test.js +142 -16
  39. package/dist/scripts/generate-gql.d.ts +1 -0
  40. package/dist/scripts/generate-gql.js +15 -0
  41. package/dist/scripts/postinstall.js +4 -4
  42. package/dist/scripts/postinstall.test.js +9 -2
  43. package/dist/scripts/runPostinstall.js +1 -1
  44. package/dist/services/HandledError.d.ts +11 -1
  45. package/dist/services/HandledError.js +2 -1
  46. package/dist/services/child-process-promisified.d.ts +1 -1
  47. package/dist/services/child-process-promisified.js +2 -2
  48. package/dist/services/env.js +5 -5
  49. package/dist/services/env.test.js +11 -4
  50. package/dist/services/fs-promisified.js +4 -4
  51. package/dist/services/getExpectedTarget.d.ts +9 -0
  52. package/dist/services/getExpectedTarget.js +77 -0
  53. package/dist/services/getExpectedTargetPullRequests.d.ts +9 -0
  54. package/dist/services/getExpectedTargetPullRequests.js +77 -0
  55. package/dist/services/git.d.ts +19 -4
  56. package/dist/services/git.integration.test.d.ts +1 -0
  57. package/dist/services/git.integration.test.js +168 -0
  58. package/dist/services/git.js +129 -57
  59. package/dist/services/git.test.js +155 -51
  60. package/dist/services/github/commitFormatters.d.ts +3 -7
  61. package/dist/services/github/commitFormatters.js +10 -19
  62. package/dist/services/github/commitFormatters.test.js +2 -24
  63. package/dist/services/github/v3/addAssigneesToPullRequest.d.ts +1 -1
  64. package/dist/services/github/v3/addAssigneesToPullRequest.js +2 -6
  65. package/dist/services/github/v3/addAssigneesToPullRequest.test.js +2 -3
  66. package/dist/services/github/v3/addLabelsToPullRequest.d.ts +1 -1
  67. package/dist/services/github/v3/addLabelsToPullRequest.js +2 -6
  68. package/dist/services/github/v3/addReviewersToPullRequest.d.ts +2 -0
  69. package/dist/services/github/v3/addReviewersToPullRequest.js +34 -0
  70. package/dist/services/github/v3/createPullRequest.d.ts +3 -2
  71. package/dist/services/github/v3/createPullRequest.js +33 -24
  72. package/dist/services/github/v3/createStatusComment.d.ts +11 -0
  73. package/dist/services/github/v3/createStatusComment.js +97 -0
  74. package/dist/services/github/v3/createStatusComment.test.d.ts +1 -0
  75. package/dist/services/github/v3/createStatusComment.test.js +209 -0
  76. package/dist/services/github/v4/FetchPullRequestId.d.ts +9 -0
  77. package/dist/services/github/v4/FetchPullRequestId.js +32 -0
  78. package/dist/services/github/v4/apiRequestV4.d.ts +1 -1
  79. package/dist/services/github/v4/apiRequestV4.js +8 -10
  80. package/dist/services/github/v4/apiRequestV4.test.js +7 -7
  81. package/dist/services/github/v4/disablePullRequestAutoMerge.d.ts +9 -0
  82. package/dist/services/github/v4/disablePullRequestAutoMerge.js +28 -0
  83. package/dist/services/github/v4/enablePullRequestAutoMerge.d.ts +9 -0
  84. package/dist/services/github/v4/enablePullRequestAutoMerge.js +51 -0
  85. package/dist/services/github/v4/enablePullRequestAutoMerge.private.test.d.ts +1 -0
  86. package/dist/services/github/v4/enablePullRequestAutoMerge.private.test.js +113 -0
  87. package/dist/services/github/v4/fetchAuthorId.d.ts +5 -0
  88. package/dist/services/github/v4/fetchAuthorId.js +1 -1
  89. package/dist/services/github/v4/fetchAuthorId.private.test.js +3 -3
  90. package/dist/services/github/v4/fetchCommitByPullNumber.js +11 -9
  91. package/dist/services/github/v4/fetchCommitByPullNumber.private.test.js +6 -6
  92. package/dist/services/github/v4/fetchCommitBySha.js +14 -12
  93. package/dist/services/github/v4/fetchCommitBySha.private.test.js +7 -5
  94. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.d.ts +5 -0
  95. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.js +46 -0
  96. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.private.test.d.ts +1 -0
  97. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.private.test.js +67 -0
  98. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.d.ts +5 -0
  99. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.js +36 -0
  100. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.private.test.d.ts +1 -0
  101. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.private.test.js +55 -0
  102. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.d.ts +16 -0
  103. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.js +90 -0
  104. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.private.test.d.ts +1 -0
  105. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.private.test.js +130 -0
  106. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.test.d.ts +1 -0
  107. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.test.js +127 -0
  108. package/dist/services/github/v4/fetchCommits/fetchCommitsInDateRange.d.ts +28 -0
  109. package/dist/services/github/v4/fetchCommits/fetchCommitsInDateRange.js +52 -0
  110. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.d.ts +10 -0
  111. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.js +49 -0
  112. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.private.test.d.ts +1 -0
  113. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.private.test.js +81 -0
  114. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.test.d.ts +1 -0
  115. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.test.js +252 -0
  116. package/dist/services/github/v4/fetchCommitsByAuthor.d.ts +3 -2
  117. package/dist/services/github/v4/fetchCommitsByAuthor.js +81 -70
  118. package/dist/services/github/v4/fetchCommitsByAuthor.private.test.js +81 -8
  119. package/dist/services/github/v4/fetchCommitsByAuthor.test.js +29 -21
  120. package/dist/services/github/v4/fetchDefaultBranch.d.ts +1 -1
  121. package/dist/services/github/v4/fetchDefaultBranch.js +1 -1
  122. package/dist/services/github/v4/fetchDefaultRepoBranchAndPerformStartupChecks/fetchDefaultRepoBranchAndPerformStartupChecks.d.ts +4 -0
  123. package/dist/services/github/v4/fetchDefaultRepoBranchAndPerformStartupChecks/fetchDefaultRepoBranchAndPerformStartupChecks.js +52 -0
  124. package/dist/services/github/v4/fetchDefaultRepoBranchAndPerformStartupChecks/fetchDefaultRepoBranchAndPerformStartupChecks.private.test.d.ts +1 -0
  125. package/dist/services/github/v4/fetchDefaultRepoBranchAndPerformStartupChecks/fetchDefaultRepoBranchAndPerformStartupChecks.private.test.js +32 -0
  126. package/dist/services/github/v4/fetchDefaultRepoBranchAndPerformStartupChecks/index.d.ts +4 -0
  127. package/dist/services/github/v4/fetchDefaultRepoBranchAndPerformStartupChecks/index.js +72 -0
  128. package/dist/services/github/v4/fetchDefaultRepoBranchAndPerformStartupChecks/index.private.test.d.ts +1 -0
  129. package/dist/services/github/v4/fetchDefaultRepoBranchAndPerformStartupChecks/index.private.test.js +34 -0
  130. package/dist/services/github/v4/fetchDefaultRepoBranchAndPerformStartupChecks/query.d.ts +40 -0
  131. package/dist/services/github/v4/fetchDefaultRepoBranchAndPerformStartupChecks/query.js +72 -0
  132. package/dist/services/github/v4/fetchExistingPullRequest.js +2 -3
  133. package/dist/services/github/v4/fetchExistingPullRequest.private.test.js +3 -3
  134. package/dist/services/github/v4/fetchMergedPullRequests.d.ts +19 -20
  135. package/dist/services/github/v4/fetchMergedPullRequests.js +22 -68
  136. package/dist/services/github/v4/fetchMergedPullRequests.private.test.js +8 -9
  137. package/dist/services/github/v4/fetchPullRequestAutoMergeMethod.d.ts +11 -0
  138. package/dist/services/github/v4/fetchPullRequestAutoMergeMethod.js +35 -0
  139. package/dist/services/github/v4/fetchPullRequestBySearchQuery.d.ts +1 -1
  140. package/dist/services/github/v4/fetchPullRequestBySearchQuery.js +12 -10
  141. package/dist/services/github/v4/fetchPullRequestBySearchQuery.private.test.js +8 -6
  142. package/dist/services/github/v4/fetchPullRequestBySearchQuery.test.js +2 -2
  143. package/dist/services/github/v4/getExistingTargetPullRequests.d.ts +4 -58
  144. package/dist/services/github/v4/getExistingTargetPullRequests.js +3 -58
  145. package/dist/services/github/v4/getOptionsFromGithub/index.d.ts +61 -0
  146. package/dist/services/github/v4/getOptionsFromGithub/index.js +139 -0
  147. package/dist/services/github/v4/getOptionsFromGithub/index.private.test.d.ts +1 -0
  148. package/dist/services/github/v4/getOptionsFromGithub/index.private.test.js +126 -0
  149. package/dist/services/github/v4/getOptionsFromGithub/query.d.ts +34 -0
  150. package/dist/services/github/v4/getOptionsFromGithub/query.js +44 -0
  151. package/dist/services/github/v4/getTargetBranchesFromLabels.d.ts +0 -7
  152. package/dist/services/github/v4/getTargetBranchesFromLabels.js +1 -26
  153. package/dist/services/github/v4/getTargetBranchesFromLabels.test.js +69 -32
  154. package/dist/services/github/v4/mocks/commitsByAuthorMock.d.ts +2 -2
  155. package/dist/services/github/v4/mocks/commitsByAuthorMock.js +39 -25
  156. package/dist/services/github/v4/mocks/fetchPullRequestBySearchQueryMock.d.ts +2 -2
  157. package/dist/services/github/v4/mocks/fetchPullRequestBySearchQueryMock.js +10 -0
  158. package/dist/services/github/v4/mocks/getCommitsByAuthorMock.d.ts +2 -2
  159. package/dist/services/github/v4/mocks/getCommitsByAuthorMock.js +4 -1
  160. package/dist/services/github/v4/mocks/getPullRequestNodeMock.d.ts +1 -1
  161. package/dist/services/github/v4/mocks/getPullRequestNodeMock.js +1 -0
  162. package/dist/services/github/v4/throwOnInvalidAccessToken.d.ts +1 -1
  163. package/dist/services/github/v4/throwOnInvalidAccessToken.js +10 -15
  164. package/dist/services/github/v4/throwOnInvalidAccessToken.test.js +7 -7
  165. package/dist/services/logger.d.ts +3 -3
  166. package/dist/services/logger.js +24 -24
  167. package/dist/services/prompt.test.d.ts +1 -0
  168. package/dist/services/prompt.test.js +87 -0
  169. package/dist/services/prompts.d.ts +10 -4
  170. package/dist/services/prompts.js +85 -22
  171. package/dist/services/sourceCommit/getExpectedTargetPullRequests.d.ts +9 -0
  172. package/dist/services/sourceCommit/getExpectedTargetPullRequests.js +92 -0
  173. package/dist/services/sourceCommit/getExpectedTargetPullRequests.test.d.ts +1 -0
  174. package/dist/services/sourceCommit/getExpectedTargetPullRequests.test.js +396 -0
  175. package/dist/services/sourceCommit/getMockSourceCommit.d.ts +21 -0
  176. package/dist/services/sourceCommit/getMockSourceCommit.js +67 -0
  177. package/dist/services/sourceCommit/getTargetBranchesFromLabels.d.ts +5 -0
  178. package/dist/services/sourceCommit/getTargetBranchesFromLabels.js +20 -0
  179. package/dist/services/sourceCommit/getTargetBranchesFromLabels.test.d.ts +1 -0
  180. package/dist/services/sourceCommit/getTargetBranchesFromLabels.test.js +201 -0
  181. package/dist/services/sourceCommit/parseSourceCommit.d.ts +78 -0
  182. package/dist/services/sourceCommit/parseSourceCommit.js +96 -0
  183. package/dist/services/sourceCommit/parseSourceCommit.test.d.ts +1 -0
  184. package/dist/services/sourceCommit/parseSourceCommit.test.js +114 -0
  185. package/dist/services/sourceCommit/sourceCommit.d.ts +78 -0
  186. package/dist/services/sourceCommit/sourceCommit.js +86 -0
  187. package/dist/services/sourceCommit/sourceCommit.test.d.ts +1 -0
  188. package/dist/services/sourceCommit/sourceCommit.test.js +287 -0
  189. package/dist/services/sourceCommit.d.ts +78 -0
  190. package/dist/services/sourceCommit.js +86 -0
  191. package/dist/services/sourceCommit.test.d.ts +1 -0
  192. package/dist/services/sourceCommit.test.js +280 -0
  193. package/dist/test/inquirer.private.test.js +81 -45
  194. package/dist/test/integration/createSpies.d.ts +1 -1
  195. package/dist/test/integration/createSpies.js +21 -9
  196. package/dist/test/integration/envConstants.js +5 -6
  197. package/dist/test/integration/helpers.js +1 -1
  198. package/dist/test/integration/integration.private.test.d.ts +1 -0
  199. package/dist/test/integration/integration.private.test.js +308 -0
  200. package/dist/test/integration/integration.test.js +30 -31
  201. package/dist/test/nockHelpers.d.ts +2 -2
  202. package/dist/test/nockHelpers.js +3 -7
  203. package/dist/test/setupFiles/automatic-mocks.d.ts +0 -1
  204. package/dist/test/setupFiles/automatic-mocks.js +0 -22
  205. package/dist/types/Commit.d.ts +85 -0
  206. package/dist/types/Commit.js +139 -0
  207. package/dist/types/commitNode.d.ts +89 -0
  208. package/dist/types/commitNode.js +139 -0
  209. package/dist/types/commitNode.test.d.ts +1 -0
  210. package/dist/types/commitNode.test.js +255 -0
  211. package/dist/types/commitWithAssociatedPullRequests.d.ts +86 -0
  212. package/dist/types/commitWithAssociatedPullRequests.js +145 -0
  213. package/dist/ui/cherrypickAndCreatePullRequest.test.js +277 -59
  214. package/dist/ui/cherrypickAndCreateTargetPullRequest.d.ts +11 -1
  215. package/dist/ui/cherrypickAndCreateTargetPullRequest.js +135 -67
  216. package/dist/ui/cherrypickAndCreateTargetPullRequest.test.d.ts +1 -0
  217. package/dist/ui/cherrypickAndCreateTargetPullRequest.test.js +468 -0
  218. package/dist/ui/getCommits.d.ts +1 -1
  219. package/dist/ui/getCommits.js +55 -25
  220. package/dist/ui/getTargetBranches.d.ts +2 -2
  221. package/dist/ui/getTargetBranches.js +16 -17
  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 +9 -9
  228. package/dist/ui/maybeSetupRepo.test.js +5 -1
  229. package/package.json +36 -51
  230. package/yarn.lock +2387 -2979
@@ -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
+ });
@@ -9,33 +9,33 @@ const ora = require("ora");
9
9
  const env_1 = require("../services/env");
10
10
  const git_1 = require("../services/git");
11
11
  async function maybeSetupRepo(options) {
12
- const isAlreadyCloned = await git_1.repoExists(options);
12
+ const isAlreadyCloned = await (0, git_1.repoExists)(options);
13
13
  // clone repo if folder does not already exists
14
14
  if (!isAlreadyCloned) {
15
15
  const spinner = ora().start();
16
16
  try {
17
17
  const spinnerCloneText = 'Cloning repository (one-time operation)';
18
18
  spinner.text = `0% ${spinnerCloneText}`;
19
- await make_dir_1.default(env_1.getRepoOwnerPath(options));
20
- await git_1.cloneRepo(options, (progress) => {
19
+ await (0, make_dir_1.default)((0, env_1.getRepoOwnerPath)(options));
20
+ await (0, git_1.cloneRepo)(options, (progress) => {
21
21
  spinner.text = `${progress}% ${spinnerCloneText}`;
22
22
  });
23
23
  spinner.succeed(`100% ${spinnerCloneText}`);
24
24
  }
25
25
  catch (e) {
26
26
  spinner.fail();
27
- await git_1.deleteRepo(options);
27
+ await (0, git_1.deleteRepo)(options);
28
28
  throw e;
29
29
  }
30
30
  }
31
31
  // delete default "origin" remote to avoid confusion
32
- await git_1.deleteRemote(options, 'origin');
32
+ await (0, git_1.deleteRemote)(options, 'origin');
33
33
  // ensure remote are setup with latest accessToken
34
- await git_1.deleteRemote(options, options.username);
35
- await git_1.addRemote(options, options.username);
34
+ await (0, git_1.deleteRemote)(options, options.username);
35
+ await (0, git_1.addRemote)(options, options.username);
36
36
  if (options.username !== options.repoOwner) {
37
- await git_1.deleteRemote(options, options.repoOwner);
38
- await git_1.addRemote(options, options.repoOwner);
37
+ await (0, git_1.deleteRemote)(options, options.repoOwner);
38
+ await (0, git_1.addRemote)(options, options.repoOwner);
39
39
  }
40
40
  }
41
41
  exports.maybeSetupRepo = maybeSetupRepo;
@@ -3,16 +3,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ const os_1 = __importDefault(require("os"));
6
7
  const del_1 = __importDefault(require("del"));
7
8
  const makeDir = require("make-dir");
8
9
  const maybeSetupRepo_1 = require("./maybeSetupRepo");
9
10
  describe('maybeSetupRepo', () => {
11
+ beforeEach(() => {
12
+ jest.spyOn(os_1.default, 'homedir').mockReturnValue('/myHomeDir');
13
+ });
10
14
  it('should delete repo if an error occurs', async () => {
11
15
  expect.assertions(2);
12
16
  makeDir.mockImplementationOnce(() => {
13
17
  throw new Error('makeDir failed');
14
18
  });
15
- await expect(maybeSetupRepo_1.maybeSetupRepo({
19
+ await expect((0, maybeSetupRepo_1.maybeSetupRepo)({
16
20
  repoOwner: 'elastic',
17
21
  repoName: 'kibana',
18
22
  username: 'sqren',