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
@@ -0,0 +1,468 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ var __importDefault = (this && this.__importDefault) || function (mod) {
22
+ return (mod && mod.__esModule) ? mod : { "default": mod };
23
+ };
24
+ Object.defineProperty(exports, "__esModule", { value: true });
25
+ const nock_1 = __importDefault(require("nock"));
26
+ const ora_1 = __importDefault(require("ora"));
27
+ const childProcess = __importStar(require("../services/child-process-promisified"));
28
+ const logger = __importStar(require("../services/logger"));
29
+ const prompts = __importStar(require("../services/prompts"));
30
+ const ExecError_1 = require("../test/ExecError");
31
+ const nockHelpers_1 = require("../test/nockHelpers");
32
+ const cherrypickAndCreateTargetPullRequest_1 = require("./cherrypickAndCreateTargetPullRequest");
33
+ const TARGET_PULL_NUMBER = 1338;
34
+ describe('cherrypickAndCreateTargetPullRequest', () => {
35
+ let consoleLogSpy;
36
+ beforeEach(() => {
37
+ consoleLogSpy = jest.spyOn(logger, 'consoleLog');
38
+ });
39
+ afterEach(() => {
40
+ consoleLogSpy.mockClear();
41
+ });
42
+ describe('when commit has a pull request reference', () => {
43
+ let res;
44
+ let execSpy;
45
+ let getAddLabelsToTargetPrCalls;
46
+ let getAddLabelsToSourcePr1000Calls;
47
+ let getAddLabelsToSourcePr2000Calls;
48
+ let getCreatePullRequestCalls;
49
+ beforeEach(async () => {
50
+ execSpy = jest
51
+ .spyOn(childProcess, 'exec')
52
+ // mock all exec commands to respond without errors
53
+ .mockResolvedValue({ stdout: '', stderr: '' });
54
+ // ensure labels are added to target PR
55
+ getAddLabelsToTargetPrCalls = mockAddLabelCall(TARGET_PULL_NUMBER);
56
+ // ensure labels are added to source PR
57
+ getAddLabelsToSourcePr1000Calls = mockAddLabelCall(1000);
58
+ getAddLabelsToSourcePr2000Calls = mockAddLabelCall(2000);
59
+ getCreatePullRequestCalls = mockCreatePullRequestCall();
60
+ res = await cherrypickAndCreateTargetPullRequest_1.cherrypickAndCreateTargetPullRequest({
61
+ targetBranch: '6.x',
62
+ options: {
63
+ assignees: [],
64
+ githubApiBaseUrlV3: 'https://api.github.com',
65
+ fork: true,
66
+ targetPRLabels: ['backport'],
67
+ prDescription: 'myPrSuffix',
68
+ prTitle: '[{targetBranch}] {commitMessages}',
69
+ repoName: 'kibana',
70
+ repoOwner: 'elastic',
71
+ username: 'sqren',
72
+ sourceBranch: 'myDefaultSourceBranch',
73
+ sourcePRLabels: [],
74
+ targetBranchChoices: [
75
+ { name: '7.0', sourcePRLabels: ['7.0.0'] },
76
+ { name: '6.x', sourcePRLabels: ['6.5.1'] },
77
+ ],
78
+ },
79
+ commits: [
80
+ {
81
+ sourceBranch: '7.x',
82
+ sha: 'mySha',
83
+ formattedMessage: 'myCommitMessage (#1000)',
84
+ originalMessage: 'My original commit message',
85
+ pullNumber: 1000,
86
+ sourcePRLabels: [],
87
+ existingTargetPullRequests: [],
88
+ },
89
+ {
90
+ sourceBranch: '7.x',
91
+ sha: 'mySha2',
92
+ formattedMessage: 'myOtherCommitMessage (#2000)',
93
+ originalMessage: 'My original commit message',
94
+ pullNumber: 2000,
95
+ sourcePRLabels: [],
96
+ existingTargetPullRequests: [],
97
+ },
98
+ ],
99
+ });
100
+ });
101
+ afterEach(() => {
102
+ execSpy.mockClear();
103
+ //@ts-expect-error
104
+ ora_1.default.mockClear();
105
+ });
106
+ it('returns the expected response', () => {
107
+ expect(res).toEqual({ url: 'myHtmlUrl', number: TARGET_PULL_NUMBER });
108
+ });
109
+ it('creates pull request', () => {
110
+ expect(getCreatePullRequestCalls()).toEqual([
111
+ {
112
+ title: '[6.x] myCommitMessage (#1000) | myOtherCommitMessage (#2000)',
113
+ head: 'sqren:backport/6.x/pr-1000_pr-2000',
114
+ base: '6.x',
115
+ body: 'Backports the following commits to 6.x:\n - myCommitMessage (#1000)\n - myOtherCommitMessage (#2000)\n\nmyPrSuffix',
116
+ },
117
+ ]);
118
+ });
119
+ it('should make correct git commands', () => {
120
+ expect(execSpy.mock.calls).toMatchSnapshot();
121
+ });
122
+ it('logs correctly', () => {
123
+ expect(consoleLogSpy.mock.calls.length).toBe(2);
124
+ expect(consoleLogSpy.mock.calls[0][0]).toMatchInlineSnapshot(`
125
+ "
126
+ Backporting to 6.x:"
127
+ `);
128
+ expect(consoleLogSpy.mock.calls[1][0]).toMatchInlineSnapshot(`"View pull request: myHtmlUrl"`);
129
+ });
130
+ it('should start the spinner with the correct text', () => {
131
+ expect(ora_1.default.mock.calls.map((call) => call[0]))
132
+ .toMatchInlineSnapshot(`
133
+ Array [
134
+ "Pulling latest changes",
135
+ "Cherry-picking: myCommitMessage (#1000)",
136
+ "Cherry-picking: myOtherCommitMessage (#2000)",
137
+ "Pushing branch \\"sqren:backport/6.x/pr-1000_pr-2000\\"",
138
+ undefined,
139
+ "Creating pull request",
140
+ "Adding labels: \\"backport\\" to #1338",
141
+ "Adding labels: \\"6.5.1\\" to #1000",
142
+ "Adding labels: \\"6.5.1\\" to #2000",
143
+ ]
144
+ `);
145
+ });
146
+ it('adds labels to the target pull request', () => {
147
+ expect(getAddLabelsToTargetPrCalls()).toEqual([{ labels: ['backport'] }]);
148
+ });
149
+ it('adds labels to source pull requests', () => {
150
+ expect(getAddLabelsToSourcePr1000Calls()).toEqual([
151
+ { labels: ['6.5.1'] },
152
+ ]);
153
+ expect(getAddLabelsToSourcePr2000Calls()).toEqual([
154
+ { labels: ['6.5.1'] },
155
+ ]);
156
+ });
157
+ });
158
+ describe('when sourcePRLabels includes wildcard', () => {
159
+ let execSpy;
160
+ let getAddLabelsToSourcePr1000Calls;
161
+ let getAddLabelsToSourcePr2000Calls;
162
+ beforeEach(async () => {
163
+ execSpy = jest
164
+ .spyOn(childProcess, 'exec')
165
+ // mock all exec commands to respond without errors
166
+ .mockResolvedValue({ stdout: '', stderr: '' });
167
+ // ensure labels are added to source PR
168
+ getAddLabelsToSourcePr1000Calls = mockAddLabelCall(1000);
169
+ getAddLabelsToSourcePr2000Calls = mockAddLabelCall(2000);
170
+ // mock create pull request
171
+ mockCreatePullRequestCall();
172
+ await cherrypickAndCreateTargetPullRequest_1.cherrypickAndCreateTargetPullRequest({
173
+ targetBranch: '6.x',
174
+ options: {
175
+ assignees: [],
176
+ githubApiBaseUrlV3: 'https://api.github.com',
177
+ fork: true,
178
+ targetPRLabels: ['backport'],
179
+ prDescription: 'myPrSuffix',
180
+ prTitle: '[{targetBranch}] {commitMessages}',
181
+ repoName: 'kibana',
182
+ repoOwner: 'elastic',
183
+ username: 'sqren',
184
+ sourceBranch: 'myDefaultSourceBranch',
185
+ sourcePRLabels: [],
186
+ targetBranchChoices: [
187
+ { name: '7.0', sourcePRLabels: ['7.0.0'] },
188
+ { name: '6.x', sourcePRLabels: ['6.5.*'] },
189
+ ],
190
+ },
191
+ commits: [
192
+ {
193
+ sourceBranch: '7.x',
194
+ sha: 'mySha',
195
+ formattedMessage: 'myCommitMessage (#1000)',
196
+ originalMessage: 'My original commit message',
197
+ pullNumber: 1000,
198
+ sourcePRLabels: [],
199
+ existingTargetPullRequests: [],
200
+ },
201
+ {
202
+ sourceBranch: '7.x',
203
+ sha: 'mySha2',
204
+ formattedMessage: 'myOtherCommitMessage (#2000)',
205
+ originalMessage: 'My original commit message',
206
+ pullNumber: 2000,
207
+ sourcePRLabels: [],
208
+ existingTargetPullRequests: [],
209
+ },
210
+ ],
211
+ });
212
+ });
213
+ afterEach(() => {
214
+ execSpy.mockClear();
215
+ //@ts-expect-error
216
+ ora_1.default.mockClear();
217
+ });
218
+ it('does not add labels to source pull requests', () => {
219
+ expect(getAddLabelsToSourcePr1000Calls()).toEqual([]);
220
+ expect(getAddLabelsToSourcePr2000Calls()).toEqual([]);
221
+ });
222
+ });
223
+ describe('when commit does not have a pull request reference', () => {
224
+ let res;
225
+ let getCreatePullRequestCalls;
226
+ let getAddLabelsToTargetPrCalls;
227
+ beforeEach(async () => {
228
+ // mock all exec commands to respond without errors
229
+ jest
230
+ .spyOn(childProcess, 'exec')
231
+ .mockResolvedValue({ stdout: '', stderr: '' });
232
+ getCreatePullRequestCalls = mockCreatePullRequestCall();
233
+ // ensure labels are added to target PR
234
+ getAddLabelsToTargetPrCalls = mockAddLabelCall(TARGET_PULL_NUMBER);
235
+ res = await cherrypickAndCreateTargetPullRequest_1.cherrypickAndCreateTargetPullRequest({
236
+ targetBranch: '6.x',
237
+ options: {
238
+ assignees: [],
239
+ githubApiBaseUrlV3: 'https://api.github.com',
240
+ fork: true,
241
+ targetPRLabels: ['backport'],
242
+ prTitle: '[{targetBranch}] {commitMessages}',
243
+ repoName: 'kibana',
244
+ repoOwner: 'elastic',
245
+ username: 'sqren',
246
+ sourcePRLabels: [],
247
+ targetBranchChoices: [
248
+ { name: '7.0', sourcePRLabels: ['7.0.0'] },
249
+ { name: '6.x', sourcePRLabels: ['6.5.1'] },
250
+ ],
251
+ },
252
+ commits: [
253
+ {
254
+ sourceBranch: '7.x',
255
+ sha: 'mySha',
256
+ formattedMessage: 'myCommitMessage (mySha)',
257
+ originalMessage: 'My original commit message',
258
+ sourcePRLabels: [],
259
+ existingTargetPullRequests: [],
260
+ },
261
+ ],
262
+ });
263
+ });
264
+ it('creates pull request', () => {
265
+ expect(getCreatePullRequestCalls()).toEqual([
266
+ {
267
+ title: '[6.x] myCommitMessage (mySha)',
268
+ head: 'sqren:backport/6.x/commit-mySha',
269
+ base: '6.x',
270
+ body: 'Backports the following commits to 6.x:\n - myCommitMessage (mySha)',
271
+ },
272
+ ]);
273
+ });
274
+ it('returns the expected response', () => {
275
+ expect(res).toEqual({ url: 'myHtmlUrl', number: TARGET_PULL_NUMBER });
276
+ });
277
+ it('adds labels to the target pull request', () => {
278
+ expect(getAddLabelsToTargetPrCalls()).toEqual([{ labels: ['backport'] }]);
279
+ });
280
+ });
281
+ describe('when cherry-picking fails', () => {
282
+ let res;
283
+ let promptSpy;
284
+ let execSpy;
285
+ let getCreatePullRequestCalls;
286
+ beforeEach(async () => {
287
+ // spies
288
+ promptSpy = jest.spyOn(prompts, 'confirmPrompt').mockResolvedValue(true);
289
+ execSpy = setupExecSpy();
290
+ getCreatePullRequestCalls = mockCreatePullRequestCall();
291
+ res = await cherrypickAndCreateTargetPullRequest_1.cherrypickAndCreateTargetPullRequest({
292
+ targetBranch: '6.x',
293
+ options: {
294
+ assignees: [],
295
+ fork: true,
296
+ targetPRLabels: ['backport'],
297
+ prTitle: '[{targetBranch}] {commitMessages}',
298
+ repoName: 'kibana',
299
+ repoOwner: 'elastic',
300
+ username: 'sqren',
301
+ sourceBranch: 'myDefaultSourceBranch',
302
+ sourcePRLabels: [],
303
+ targetBranchChoices: [],
304
+ },
305
+ commits: [
306
+ {
307
+ sourceBranch: '7.x',
308
+ sha: 'mySha',
309
+ formattedMessage: 'myCommitMessage (mySha)',
310
+ originalMessage: 'My original commit message',
311
+ sourcePRLabels: [],
312
+ existingTargetPullRequests: [],
313
+ },
314
+ ],
315
+ });
316
+ });
317
+ afterEach(() => {
318
+ promptSpy.mockClear();
319
+ execSpy.mockClear();
320
+ //@ts-expect-error
321
+ ora_1.default.mockClear();
322
+ });
323
+ it('creates pull request', () => {
324
+ expect(getCreatePullRequestCalls()).toEqual([
325
+ {
326
+ title: '[6.x] myCommitMessage (mySha)',
327
+ head: 'sqren:backport/6.x/commit-mySha',
328
+ base: '6.x',
329
+ body: 'Backports the following commits to 6.x:\n - myCommitMessage (mySha)',
330
+ },
331
+ ]);
332
+ });
333
+ it('returns the expected response', () => {
334
+ expect(res).toEqual({ url: 'myHtmlUrl', number: TARGET_PULL_NUMBER });
335
+ });
336
+ it('shows the right prompts', () => {
337
+ expect(promptSpy.mock.calls.length).toBe(3);
338
+ expect(promptSpy.mock.calls[0][0]).toMatchInlineSnapshot(`
339
+ "Please fix the issues in: /myHomeDir/.backport/repositories/elastic/kibana
340
+
341
+ Conflicting files:
342
+ - /myHomeDir/.backport/repositories/elastic/kibana/conflicting-file.txt
343
+
344
+
345
+ Press ENTER when the conflicts are resolved and files are staged"
346
+ `);
347
+ expect(promptSpy.mock.calls[1][0]).toMatchInlineSnapshot(`
348
+ "Please fix the issues in: /myHomeDir/.backport/repositories/elastic/kibana
349
+
350
+ Conflicting files:
351
+ - /myHomeDir/.backport/repositories/elastic/kibana/conflicting-file.txt
352
+
353
+
354
+ Press ENTER when the conflicts are resolved and files are staged"
355
+ `);
356
+ expect(promptSpy.mock.calls[2][0]).toMatchInlineSnapshot(`
357
+ "Please fix the issues in: /myHomeDir/.backport/repositories/elastic/kibana
358
+
359
+
360
+ Unstaged files:
361
+ - /myHomeDir/.backport/repositories/elastic/kibana/conflicting-file.txt
362
+
363
+ Press ENTER when the conflicts are resolved and files are staged"
364
+ `);
365
+ });
366
+ it('calls exec correctly', () => {
367
+ expect(execSpy.mock.calls).toMatchSnapshot();
368
+ });
369
+ it('calls ora correctly', () => {
370
+ expect(ora_1.default.mock.calls.map((call) => call[0]))
371
+ .toMatchInlineSnapshot(`
372
+ Array [
373
+ "Pulling latest changes",
374
+ "Cherry-picking: myCommitMessage (mySha)",
375
+ "Finalizing cherrypick",
376
+ "Pushing branch \\"sqren:backport/6.x/commit-mySha\\"",
377
+ undefined,
378
+ "Creating pull request",
379
+ "Adding labels: \\"backport\\" to #1338",
380
+ ]
381
+ `);
382
+ });
383
+ it('logs correctly', async () => {
384
+ expect(consoleLogSpy.mock.calls[0][0]).toMatchInlineSnapshot(`
385
+ "
386
+ Backporting to 6.x:"
387
+ `);
388
+ expect(consoleLogSpy.mock.calls[1][0]).toMatchInlineSnapshot(`
389
+ "
390
+ ----------------------------------------
391
+ "
392
+ `);
393
+ expect(consoleLogSpy.mock.calls[2][0]).toMatchInlineSnapshot(`
394
+ "
395
+ ----------------------------------------
396
+ "
397
+ `);
398
+ expect(consoleLogSpy.mock.calls[3][0]).toMatchInlineSnapshot(`"View pull request: myHtmlUrl"`);
399
+ });
400
+ });
401
+ });
402
+ function setupExecSpy() {
403
+ let conflictingFilesCheck = 0;
404
+ let unstagedFilesCheck = 0;
405
+ return jest
406
+ .spyOn(childProcess, 'exec')
407
+ .mockImplementation(async (cmd) => {
408
+ // createFeatureBranch
409
+ if (cmd.includes('git checkout -B')) {
410
+ return { stdout: 'create feature branch succeeded', stderr: '' };
411
+ }
412
+ // git fetch
413
+ if (cmd.startsWith('git fetch')) {
414
+ return { stderr: '', stdout: '' };
415
+ }
416
+ // cherrypick
417
+ if (cmd === 'git cherry-pick mySha') {
418
+ throw new ExecError_1.ExecError({ cmd });
419
+ }
420
+ // getConflictingFiles
421
+ if (cmd === 'git --no-pager diff --check') {
422
+ conflictingFilesCheck++;
423
+ if (conflictingFilesCheck >= 4) {
424
+ return { stderr: '', stdout: '' };
425
+ }
426
+ throw new ExecError_1.ExecError({
427
+ code: 2,
428
+ cmd,
429
+ stdout: `conflicting-file.txt:1: leftover conflict marker\nconflicting-file.txt:3: leftover conflict marker\nconflicting-file.txt:5: leftover conflict marker\n`,
430
+ });
431
+ }
432
+ // getUnstagedFiles
433
+ if (cmd === 'git --no-pager diff --name-only') {
434
+ unstagedFilesCheck++;
435
+ if (unstagedFilesCheck >= 5) {
436
+ return { stderr: '', stdout: '' };
437
+ }
438
+ return { stdout: `conflicting-file.txt\n`, stderr: '' };
439
+ }
440
+ // addUnstagedFiles
441
+ if (cmd === 'git add --update') {
442
+ return { stdout: ``, stderr: '' };
443
+ }
444
+ // finalizeCherrypick
445
+ if (cmd.includes('git commit --no-edit')) {
446
+ return { stdout: ``, stderr: '' };
447
+ }
448
+ // pushFeatureBranch
449
+ if (cmd.startsWith('git push ')) {
450
+ return { stdout: ``, stderr: '' };
451
+ }
452
+ // deleteFeatureBranch
453
+ if (cmd.includes('git branch -D ')) {
454
+ return { stdout: ``, stderr: '' };
455
+ }
456
+ throw new Error(`Missing mock for "${cmd}"`);
457
+ });
458
+ }
459
+ function mockCreatePullRequestCall() {
460
+ return nockHelpers_1.createNockListener(nock_1.default('https://api.github.com')
461
+ .post('/repos/elastic/kibana/pulls')
462
+ .reply(200, { html_url: 'myHtmlUrl', number: TARGET_PULL_NUMBER }));
463
+ }
464
+ function mockAddLabelCall(pullNumber) {
465
+ return nockHelpers_1.createNockListener(nock_1.default('https://api.github.com')
466
+ .post(`/repos/elastic/kibana/issues/${pullNumber}/labels`)
467
+ .reply(200));
468
+ }
@@ -1,2 +1,2 @@
1
1
  import { ValidConfigOptions } from '../options/options';
2
- export declare function getCommits(options: ValidConfigOptions): Promise<import("../types/Commit").Commit[]>;
2
+ export declare function getCommits(options: ValidConfigOptions): Promise<import("../entrypoint.module").Commit[]>;
@@ -1,38 +1,68 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.getCommits = void 0;
7
+ const chalk_1 = __importDefault(require("chalk"));
8
+ const ora_1 = __importDefault(require("ora"));
4
9
  const HandledError_1 = require("../services/HandledError");
5
- const fetchCommitByPullNumber_1 = require("../services/github/v4/fetchCommitByPullNumber");
6
- const fetchCommitBySha_1 = require("../services/github/v4/fetchCommitBySha");
7
- const fetchCommitsByAuthor_1 = require("../services/github/v4/fetchCommitsByAuthor");
8
- const fetchPullRequestBySearchQuery_1 = require("../services/github/v4/fetchPullRequestBySearchQuery");
10
+ const commitFormatters_1 = require("../services/github/commitFormatters");
11
+ const fetchCommitByPullNumber_1 = require("../services/github/v4/fetchCommits/fetchCommitByPullNumber");
12
+ const fetchCommitBySha_1 = require("../services/github/v4/fetchCommits/fetchCommitBySha");
13
+ const fetchCommitsByAuthor_1 = require("../services/github/v4/fetchCommits/fetchCommitsByAuthor");
14
+ const fetchPullRequestBySearchQuery_1 = require("../services/github/v4/fetchCommits/fetchPullRequestBySearchQuery");
9
15
  const prompts_1 = require("../services/prompts");
16
+ function getOraPersistsOption(question, answer) {
17
+ return {
18
+ symbol: chalk_1.default.green('?'),
19
+ text: `${chalk_1.default.bold(question)} ${chalk_1.default.cyan(answer)}`,
20
+ };
21
+ }
10
22
  async function getCommits(options) {
11
- if (options.sha) {
12
- return [await fetchCommitBySha_1.fetchCommitBySha({ ...options, sha: options.sha })]; // must extract sha to satisfy the ts gods
13
- }
14
- if (options.pullNumber) {
15
- return [
16
- await fetchCommitByPullNumber_1.fetchCommitByPullNumber({
23
+ const spinner = (0, ora_1.default)().start();
24
+ try {
25
+ if (options.sha) {
26
+ spinner.text = `Loading commit "${(0, commitFormatters_1.getShortSha)(options.sha)}"`;
27
+ const commit = await (0, fetchCommitBySha_1.fetchCommitBySha)({ ...options, sha: options.sha });
28
+ spinner.stopAndPersist(getOraPersistsOption('Select commit', commit.originalMessage));
29
+ return [commit];
30
+ }
31
+ if (options.pullNumber) {
32
+ spinner.text = `Loading pull request #${options.pullNumber}`;
33
+ const commit = await (0, fetchCommitByPullNumber_1.fetchCommitByPullNumber)({
17
34
  ...options,
18
- pullNumber: options.pullNumber,
19
- }),
20
- ];
21
- }
22
- if (options.ci) {
23
- throw new HandledError_1.HandledError('When "--ci" flag is enabled either `--sha` or `--pr` must be specified');
24
- }
25
- if (options.prFilter) {
26
- const commitChoices = await fetchPullRequestBySearchQuery_1.fetchPullRequestBySearchQuery(options);
27
- return prompts_1.promptForCommits({
35
+ pullNumber: options.pullNumber, // must extract pullNumber to satisfy the ts gods
36
+ });
37
+ // add styles to make it look like a prompt question
38
+ spinner.stopAndPersist(getOraPersistsOption('Select pull request', commit.originalMessage));
39
+ return [commit];
40
+ }
41
+ if (options.ci) {
42
+ throw new HandledError_1.HandledError('When "--ci" flag is enabled either `--sha` or `--pr` must be specified');
43
+ }
44
+ if (options.prFilter) {
45
+ spinner.text = 'Loading pull requests...';
46
+ const commitChoices = await (0, fetchPullRequestBySearchQuery_1.fetchPullRequestBySearchQuery)(options);
47
+ spinner.stop();
48
+ return (0, prompts_1.promptForCommits)({
49
+ commitChoices,
50
+ isMultipleChoice: options.multipleCommits,
51
+ showDetails: options.details,
52
+ });
53
+ }
54
+ spinner.text = `Loading commits from branch "${options.sourceBranch}"...`;
55
+ const commitChoices = await (0, fetchCommitsByAuthor_1.fetchCommitsByAuthor)(options);
56
+ spinner.stop();
57
+ return (0, prompts_1.promptForCommits)({
28
58
  commitChoices,
29
59
  isMultipleChoice: options.multipleCommits,
60
+ showDetails: options.details,
30
61
  });
31
62
  }
32
- const commitChoices = await fetchCommitsByAuthor_1.fetchCommitsByAuthor(options);
33
- return prompts_1.promptForCommits({
34
- commitChoices,
35
- isMultipleChoice: options.multipleCommits,
36
- });
63
+ catch (e) {
64
+ spinner.fail();
65
+ throw e;
66
+ }
37
67
  }
38
68
  exports.getCommits = getCommits;
@@ -0,0 +1,11 @@
1
+ import { Commit } from '../entrypoint.module';
2
+ import { ValidConfigOptions } from '../options/options';
3
+ export declare function getCommitsWithoutBackports({ options, commit, targetBranch, conflictingFiles, }: {
4
+ options: ValidConfigOptions;
5
+ commit: Commit;
6
+ targetBranch: string;
7
+ conflictingFiles: string[];
8
+ }): Promise<{
9
+ formatted: string;
10
+ commit: Commit;
11
+ }[]>;
@@ -0,0 +1,53 @@
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.getCommitsWithoutBackports = void 0;
7
+ const chalk_1 = __importDefault(require("chalk"));
8
+ const commitFormatters_1 = require("../services/github/commitFormatters");
9
+ const fetchCommitsByAuthor_1 = require("../services/github/v4/fetchCommits/fetchCommitsByAuthor");
10
+ // when the user is facing a git conflict we should help them understand
11
+ // why the conflict occurs. In many cases it's because one or more commits haven't been backported yet
12
+ async function getCommitsWithoutBackports({ options,
13
+ // commit that is being backported
14
+ commit,
15
+ // branch the commit is being backported to
16
+ targetBranch,
17
+ // the files with conflicts on the target branch
18
+ conflictingFiles, }) {
19
+ // commits on the source branch that also touched the conflicting files
20
+ const commitsInConflictingPaths = await (0, fetchCommitsByAuthor_1.fetchCommitsByAuthor)({
21
+ ...options,
22
+ author: null,
23
+ commitPaths: conflictingFiles,
24
+ });
25
+ return commitsInConflictingPaths
26
+ .filter((c) => {
27
+ // exclude the commit we are currently trying to backport
28
+ if (c.sha === commit.sha) {
29
+ return false;
30
+ }
31
+ // exclude commits that are newer than the commit we are trying to backport
32
+ if (c.committedDate > commit.committedDate) {
33
+ return false;
34
+ }
35
+ // only consider commits that have an associated pull request
36
+ if (!c.pullUrl) {
37
+ return false;
38
+ }
39
+ // only include commit if it has an unmerged PR for the given target branch
40
+ const hasUnmergedPr = c.expectedTargetPullRequests.some((pr) => pr.branch === targetBranch && pr.state !== 'MERGED');
41
+ return hasUnmergedPr;
42
+ })
43
+ .slice(0, 10) // limit to max 10 commits
44
+ .map((c) => {
45
+ // get pull request for the target branch (if it exists)
46
+ const pendingBackportPr = c.expectedTargetPullRequests.find((pr) => pr.branch === targetBranch && pr.state === 'OPEN');
47
+ const formatted = pendingBackportPr
48
+ ? ` - ${(0, commitFormatters_1.getFirstLine)(c.originalMessage)} ${chalk_1.default.gray('(backport pending)')}\n ${pendingBackportPr.url}`
49
+ : ` - ${(0, commitFormatters_1.getFirstLine)(c.originalMessage)} ${chalk_1.default.red('(backport missing)')}\n ${c.pullUrl}`;
50
+ return { formatted, commit: c };
51
+ });
52
+ }
53
+ exports.getCommitsWithoutBackports = getCommitsWithoutBackports;
@@ -1,4 +1,5 @@
1
+ import { TargetBranchChoice } from '../options/ConfigOptions';
1
2
  import { ValidConfigOptions } from '../options/options';
2
- import { Commit } from '../types/Commit';
3
+ import { Commit } from '../services/sourceCommit/parseSourceCommit';
3
4
  export declare function getTargetBranches(options: ValidConfigOptions, commits: Commit[]): string[] | Promise<string[]>;
4
- export declare function getTargetBranchChoices(options: ValidConfigOptions, targetBranchesFromLabels: string[], sourceBranch: string): import("../options/ConfigOptions").BranchChoice[];
5
+ export declare function getTargetBranchChoices(options: ValidConfigOptions, missingTargetBranches: string[], sourceBranch: string): TargetBranchChoice[];