backport 6.1.2 → 6.2.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 (166) hide show
  1. package/README.md +39 -37
  2. package/dist/backportRun.d.ts +14 -0
  3. package/dist/backportRun.js +75 -0
  4. package/dist/entrypoint.api.d.ts +22 -0
  5. package/dist/entrypoint.api.js +57 -0
  6. package/dist/entrypoint.api.private.test.d.ts +1 -0
  7. package/dist/entrypoint.api.private.test.js +37 -0
  8. package/dist/entrypoint.module.d.ts +4 -1
  9. package/dist/entrypoint.module.js +10 -4
  10. package/dist/entrypoint.module.private.test.js +4 -1
  11. package/dist/main.js +5 -1
  12. package/dist/options/ConfigOptions.d.ts +2 -0
  13. package/dist/options/cliArgs.d.ts +5 -1
  14. package/dist/options/cliArgs.js +24 -2
  15. package/dist/options/config/config.d.ts +6 -1
  16. package/dist/options/config/config.js +2 -2
  17. package/dist/options/config/config.test.js +1 -1
  18. package/dist/options/config/getProjectConfig.d.ts +0 -0
  19. package/dist/options/config/getProjectConfig.js +1 -0
  20. package/dist/options/config/globalConfig.d.ts +1 -1
  21. package/dist/options/config/globalConfig.js +4 -8
  22. package/dist/options/config/globalConfig.test.js +2 -2
  23. package/dist/options/config/readConfigFile.d.ts +3 -1
  24. package/dist/options/options.d.ts +8 -1
  25. package/dist/options/options.js +9 -2
  26. package/dist/options/options.test.js +7 -3
  27. package/dist/react-components/0-loadOptions.d.ts +6 -0
  28. package/dist/react-components/0-loadOptions.js +45 -0
  29. package/dist/react-components/1-loadCommit.d.ts +8 -0
  30. package/dist/react-components/1-loadCommit.js +88 -0
  31. package/dist/react-components/1a-loadCommit.d.ts +5 -0
  32. package/dist/react-components/1a-loadCommit.js +76 -0
  33. package/dist/react-components/1b-commitChoices.d.ts +5 -0
  34. package/dist/react-components/1b-commitChoices.js +98 -0
  35. package/dist/react-components/2-commitChoices.d.ts +8 -0
  36. package/dist/react-components/2-commitChoices.js +96 -0
  37. package/dist/react-components/2-commitsPrompt.d.ts +8 -0
  38. package/dist/react-components/2-commitsPrompt.js +96 -0
  39. package/dist/react-components/2-targetBranchChoices.d.ts +9 -0
  40. package/dist/react-components/2-targetBranchChoices.js +109 -0
  41. package/dist/react-components/3-cherrypick.d.ts +1 -0
  42. package/dist/react-components/3-cherrypick.js +2 -0
  43. package/dist/react-components/App.d.ts +2 -0
  44. package/dist/react-components/App.js +35 -0
  45. package/dist/react-components/BackportStateContext.d.ts +33 -0
  46. package/dist/react-components/BackportStateContext.js +42 -0
  47. package/dist/react-components/CommitPrompt/2-commitsPrompt.d.ts +8 -0
  48. package/dist/react-components/CommitPrompt/2-commitsPrompt.js +96 -0
  49. package/dist/react-components/CommitPrompt/CommitPrompt.d.ts +8 -0
  50. package/dist/react-components/CommitPrompt/CommitPrompt.js +96 -0
  51. package/dist/react-components/CommitPrompt/getCommits.d.ts +2 -0
  52. package/dist/react-components/CommitPrompt/getCommits.js +27 -0
  53. package/dist/react-components/CommitPrompt.d.ts +8 -0
  54. package/dist/react-components/CommitPrompt.js +96 -0
  55. package/dist/react-components/CustomMultiSelect.d.ts +13 -0
  56. package/dist/react-components/CustomMultiSelect.js +21 -0
  57. package/dist/react-components/CustomMultiSelectItem.d.ts +5 -0
  58. package/dist/react-components/CustomMultiSelectItem.js +12 -0
  59. package/dist/react-components/CustomSelect.d.ts +14 -0
  60. package/dist/react-components/CustomSelect.js +34 -0
  61. package/dist/react-components/ErrorBoundary.d.ts +15 -0
  62. package/dist/react-components/ErrorBoundary.js +53 -0
  63. package/dist/react-components/MultiSelect.d.ts +7 -0
  64. package/dist/react-components/MultiSelect.js +16 -0
  65. package/dist/react-components/hooks/useAsync.d.ts +12 -0
  66. package/dist/react-components/hooks/useAsync.js +73 -0
  67. package/dist/runSequentially.test.js +10 -7
  68. package/dist/services/HandledError.d.ts +1 -1
  69. package/dist/services/backportRun/backportRun.ci.d.ts +6 -0
  70. package/dist/services/backportRun/backportRun.ci.js +81 -0
  71. package/dist/services/backportRun/backportRun.d.ts +14 -0
  72. package/dist/services/backportRun/backportRun.interactive.d.ts +1 -0
  73. package/dist/services/backportRun/backportRun.interactive.js +4 -0
  74. package/dist/services/backportRun/backportRun.js +14 -0
  75. package/dist/services/backportRun/backportState.d.ts +4 -0
  76. package/dist/services/backportRun/backportState.js +28 -0
  77. package/dist/services/backportRun/getCommits.d.ts +2 -0
  78. package/dist/services/backportRun/getCommits.js +20 -0
  79. package/dist/services/backportRun/runSequentially.d.ts +20 -0
  80. package/dist/services/backportRun/runSequentially.js +41 -0
  81. package/dist/services/backportRun/runSequentially.test.d.ts +1 -0
  82. package/dist/services/backportRun/runSequentially.test.js +199 -0
  83. package/dist/services/cherrypickAndCreateTargetPullRequest.d.ts +12 -0
  84. package/dist/services/cherrypickAndCreateTargetPullRequest.js +206 -0
  85. package/dist/services/cherrypickAndCreateTargetPullRequest.test.d.ts +1 -0
  86. package/dist/services/cherrypickAndCreateTargetPullRequest.test.js +403 -0
  87. package/dist/services/env.d.ts +2 -3
  88. package/dist/services/env.js +9 -10
  89. package/dist/services/env.test.js +0 -6
  90. package/dist/services/getCommits.d.ts +2 -0
  91. package/dist/services/getCommits.js +38 -0
  92. package/dist/services/getCommitsWithoutBackports.d.ts +11 -0
  93. package/dist/services/getCommitsWithoutBackports.js +68 -0
  94. package/dist/services/getCommitsWithoutBackports.test.d.ts +1 -0
  95. package/dist/services/getCommitsWithoutBackports.test.js +259 -0
  96. package/dist/services/getTargetBranches.d.ts +5 -0
  97. package/dist/services/getTargetBranches.js +60 -0
  98. package/dist/services/getTargetBranches.test.d.ts +1 -0
  99. package/dist/services/getTargetBranches.test.js +180 -0
  100. package/dist/services/git.d.ts +2 -1
  101. package/dist/services/git.js +5 -3
  102. package/dist/services/github/v3/createPullRequest.js +1 -1
  103. package/dist/services/github/v3/createPullRequest.test.js +3 -3
  104. package/dist/services/github/v3/createStatusComment.d.ts +1 -1
  105. package/dist/services/github/v3/createStatusComment.js +40 -15
  106. package/dist/services/github/v3/createStatusComment.test.js +190 -73
  107. package/dist/services/github/v4/fetchCommits/allFetchers.private.test.d.ts +1 -0
  108. package/dist/services/github/v4/fetchCommits/allFetchers.private.test.js +78 -0
  109. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.js +1 -1
  110. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.private.test.js +64 -0
  111. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.js +5 -5
  112. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.private.test.js +60 -0
  113. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.d.ts +2 -0
  114. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.js +17 -10
  115. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.private.test.js +72 -0
  116. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.test.js +6 -0
  117. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.js +8 -7
  118. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.private.test.js +8 -0
  119. package/dist/services/github/v4/fetchProjectConfig.d.ts +1 -1
  120. package/dist/services/github/v4/fetchProjectConfig.js +3 -3
  121. package/dist/services/github/v4/fetchProjectConfig.private.test.js +5 -5
  122. package/dist/services/github/v4/fetchRemoteProjectConfig.d.ts +8 -0
  123. package/dist/services/github/v4/fetchRemoteProjectConfig.js +56 -0
  124. package/dist/services/github/v4/fetchRemoteProjectConfig.private.test.d.ts +1 -0
  125. package/dist/services/github/v4/fetchRemoteProjectConfig.private.test.js +52 -0
  126. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.d.ts +2 -0
  127. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.js +0 -7
  128. package/dist/services/github/v4/mocks/commitsByAuthorMock.js +16 -5
  129. package/dist/services/logger.d.ts +0 -1
  130. package/dist/services/logger.js +3 -3
  131. package/dist/services/maybeSetupRepo.d.ts +2 -0
  132. package/dist/services/maybeSetupRepo.js +33 -0
  133. package/dist/services/maybeSetupRepo.test.d.ts +1 -0
  134. package/dist/services/maybeSetupRepo.test.js +78 -0
  135. package/dist/services/sourceCommit/getExpectedTargetPullRequests.d.ts +4 -0
  136. package/dist/services/sourceCommit/getExpectedTargetPullRequests.js +8 -2
  137. package/dist/services/sourceCommit/getExpectedTargetPullRequests.test.js +20 -0
  138. package/dist/services/sourceCommit/getMockSourceCommit.d.ts +1 -0
  139. package/dist/services/sourceCommit/getMockSourceCommit.js +72 -46
  140. package/dist/services/sourceCommit/parseSourceCommit.d.ts +22 -8
  141. package/dist/services/sourceCommit/parseSourceCommit.js +29 -10
  142. package/dist/services/sourceCommit/parseSourceCommit.test.js +10 -2
  143. package/dist/test/backport-e2e.private.test.d.ts +1 -0
  144. package/dist/test/backport-e2e.private.test.js +327 -0
  145. package/dist/test/handleUnbackportedPullRequests.private.test.d.ts +1 -0
  146. package/dist/test/handleUnbackportedPullRequests.private.test.js +43 -0
  147. package/dist/test/inquirer.private.test.js +1 -1
  148. package/dist/test/integration/integration.private.test.js +2 -2
  149. package/dist/test/nockHelpers.js +1 -3
  150. package/dist/test/setupFiles/automatic-mocks.js +1 -0
  151. package/dist/todo-delete.d.ts +1 -0
  152. package/dist/todo-delete.js +17 -0
  153. package/dist/ui/cherrypickAndCreateTargetPullRequest.js +4 -3
  154. package/dist/ui/cherrypickAndCreateTargetPullRequest.test.js +224 -260
  155. package/dist/ui/getCommits.js +1 -1
  156. package/dist/ui/getCommitsWithoutBackports.js +17 -2
  157. package/dist/ui/getCommitsWithoutBackports.test.d.ts +1 -0
  158. package/dist/ui/getCommitsWithoutBackports.test.js +259 -0
  159. package/dist/ui/maybeSetupRepo.js +0 -6
  160. package/dist/ui/maybeSetupRepo.test.js +8 -4
  161. package/dist/utils/redact.d.ts +1 -0
  162. package/dist/utils/redact.js +7 -0
  163. package/dist/utils/sequentially.d.ts +1 -0
  164. package/dist/utils/sequentially.js +10 -0
  165. package/package.json +3 -3
  166. package/yarn.lock +462 -284
@@ -0,0 +1,403 @@
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 os_1 = __importDefault(require("os"));
26
+ const nock_1 = __importDefault(require("nock"));
27
+ const childProcess = __importStar(require("../services/child-process-promisified"));
28
+ const commitsByAuthorMock_1 = require("../services/github/v4/mocks/commitsByAuthorMock");
29
+ const logger = __importStar(require("../services/logger"));
30
+ const prompts = __importStar(require("../services/prompts"));
31
+ const ExecError_1 = require("../test/ExecError");
32
+ const nockHelpers_1 = require("../test/nockHelpers");
33
+ const cherrypickAndCreateTargetPullRequest_1 = require("./cherrypickAndCreateTargetPullRequest");
34
+ describe('cherrypickAndCreateTargetPullRequest', () => {
35
+ let execSpy;
36
+ let addLabelsScope;
37
+ let consoleLogSpy;
38
+ beforeEach(() => {
39
+ jest.spyOn(os_1.default, 'homedir').mockReturnValue('/myHomeDir');
40
+ execSpy = jest
41
+ .spyOn(childProcess, 'exec')
42
+ // mock all exec commands to respond without errors
43
+ .mockResolvedValue({ stdout: '', stderr: '' });
44
+ consoleLogSpy = jest.spyOn(logger, 'consoleLog');
45
+ // ensure labels are added
46
+ addLabelsScope = (0, nock_1.default)('https://api.github.com')
47
+ .post('/repos/elastic/kibana/issues/1337/labels', {
48
+ labels: ['backport'],
49
+ })
50
+ .reply(200);
51
+ });
52
+ afterEach(() => {
53
+ jest.clearAllMocks();
54
+ addLabelsScope.done();
55
+ nock_1.default.cleanAll();
56
+ });
57
+ describe('when commit has a pull request reference', () => {
58
+ let res;
59
+ let createPullRequestCalls;
60
+ beforeEach(async () => {
61
+ const options = {
62
+ assignees: [],
63
+ authenticatedUsername: 'sqren_authenticated',
64
+ fork: true,
65
+ prDescription: 'myPrSuffix',
66
+ prTitle: '[{targetBranch}] {commitMessages}',
67
+ repoName: 'kibana',
68
+ repoOwner: 'elastic',
69
+ reviewers: [],
70
+ sourceBranch: 'myDefaultSourceBranch',
71
+ sourcePRLabels: [],
72
+ targetPRLabels: ['backport'],
73
+ author: 'sqren',
74
+ };
75
+ const commits = [
76
+ {
77
+ committedDate: 'fff',
78
+ sourceBranch: '7.x',
79
+ sha: 'mySha',
80
+ originalMessage: 'My original commit message (#1000)',
81
+ pullNumber: 1000,
82
+ expectedTargetPullRequests: [],
83
+ },
84
+ {
85
+ committedDate: 'ggg',
86
+ sourceBranch: '7.x',
87
+ sha: 'mySha2',
88
+ originalMessage: 'My other commit message (#2000)',
89
+ pullNumber: 2000,
90
+ expectedTargetPullRequests: [],
91
+ },
92
+ ];
93
+ const scope = (0, nock_1.default)('https://api.github.com')
94
+ .post('/repos/elastic/kibana/pulls')
95
+ .reply(200, { number: 1337, html_url: 'myHtmlUrl' });
96
+ createPullRequestCalls = (0, nockHelpers_1.listenForCallsToNockScope)(scope);
97
+ res = await (0, cherrypickAndCreateTargetPullRequest_1.cherrypickAndCreateTargetPullRequest)({
98
+ options,
99
+ commits,
100
+ targetBranch: '6.x',
101
+ });
102
+ scope.done();
103
+ nock_1.default.cleanAll();
104
+ });
105
+ it('creates the pull request with multiple PR references', () => {
106
+ expect(createPullRequestCalls).toMatchInlineSnapshot(`
107
+ Array [
108
+ Object {
109
+ "base": "6.x",
110
+ "body": "# Backport
111
+
112
+ This is an automatic backport to \`6.x\` of:
113
+ - #1000
114
+ - #2000
115
+
116
+ ### Questions ?
117
+ Please refer to the [Backport tool documentation](https://github.com/sqren/backport)
118
+
119
+ myPrSuffix",
120
+ "head": "sqren_authenticated:backport/6.x/pr-1000_pr-2000",
121
+ "title": "[6.x] My original commit message (#1000) | My other commit message (#2000)",
122
+ },
123
+ ]
124
+ `);
125
+ });
126
+ it('returns the expected response', () => {
127
+ expect(res).toEqual({ didUpdate: false, url: 'myHtmlUrl', number: 1337 });
128
+ });
129
+ it('should make correct git commands', () => {
130
+ expect(execSpy.mock.calls).toMatchSnapshot();
131
+ });
132
+ it('logs correctly', () => {
133
+ expect(consoleLogSpy.mock.calls.length).toBe(2);
134
+ expect(consoleLogSpy.mock.calls[0][0]).toMatchInlineSnapshot(`
135
+ "
136
+ Backporting to 6.x:"
137
+ `);
138
+ expect(consoleLogSpy.mock.calls[1][0]).toMatchInlineSnapshot(`"View pull request: myHtmlUrl"`);
139
+ });
140
+ });
141
+ describe('when commit does not have a pull request reference', () => {
142
+ let res;
143
+ let createPullRequestCalls;
144
+ beforeEach(async () => {
145
+ const options = {
146
+ assignees: [],
147
+ authenticatedUsername: 'sqren_authenticated',
148
+ fork: true,
149
+ prTitle: '[{targetBranch}] {commitMessages}',
150
+ repoName: 'kibana',
151
+ repoOwner: 'elastic',
152
+ reviewers: [],
153
+ sourcePRLabels: [],
154
+ targetPRLabels: ['backport'],
155
+ author: 'sqren',
156
+ };
157
+ const commits = [
158
+ {
159
+ committedDate: 'hhh',
160
+ sourceBranch: '7.x',
161
+ sha: 'mySha',
162
+ originalMessage: 'My original commit message',
163
+ expectedTargetPullRequests: [],
164
+ },
165
+ ];
166
+ const scope = (0, nock_1.default)('https://api.github.com')
167
+ .post('/repos/elastic/kibana/pulls')
168
+ .reply(200, { number: 1337, html_url: 'myHtmlUrl' });
169
+ createPullRequestCalls = (0, nockHelpers_1.listenForCallsToNockScope)(scope);
170
+ res = await (0, cherrypickAndCreateTargetPullRequest_1.cherrypickAndCreateTargetPullRequest)({
171
+ options,
172
+ commits,
173
+ targetBranch: '6.x',
174
+ });
175
+ scope.done();
176
+ nock_1.default.cleanAll();
177
+ });
178
+ it('creates the pull request with commit reference', () => {
179
+ expect(createPullRequestCalls).toMatchInlineSnapshot(`
180
+ Array [
181
+ Object {
182
+ "base": "6.x",
183
+ "body": "# Backport
184
+
185
+ This is an automatic backport to \`6.x\` of:
186
+ - My original commit message (mySha)
187
+
188
+ ### Questions ?
189
+ Please refer to the [Backport tool documentation](https://github.com/sqren/backport)
190
+ ",
191
+ "head": "sqren_authenticated:backport/6.x/commit-mySha",
192
+ "title": "[6.x] My original commit message",
193
+ },
194
+ ]
195
+ `);
196
+ });
197
+ it('returns the expected response', () => {
198
+ expect(res).toEqual({ didUpdate: false, url: 'myHtmlUrl', number: 1337 });
199
+ });
200
+ });
201
+ describe('when cherry-picking fails', () => {
202
+ let res;
203
+ let promptSpy;
204
+ let execSpy;
205
+ let commitsByAuthorCalls;
206
+ let createPullRequestCalls;
207
+ beforeEach(async () => {
208
+ // spies
209
+ promptSpy = jest.spyOn(prompts, 'confirmPrompt').mockResolvedValue(true);
210
+ execSpy = setupExecSpyForCherryPick();
211
+ const options = {
212
+ assignees: [],
213
+ authenticatedUsername: 'sqren_authenticated',
214
+ fork: true,
215
+ githubApiBaseUrlV4: 'http://localhost/graphql',
216
+ prTitle: '[{targetBranch}] {commitMessages}',
217
+ repoName: 'kibana',
218
+ repoOwner: 'elastic',
219
+ reviewers: [],
220
+ sourceBranch: 'myDefaultSourceBranch',
221
+ sourcePRLabels: [],
222
+ targetPRLabels: ['backport'],
223
+ author: 'sqren',
224
+ };
225
+ const scope = (0, nock_1.default)('https://api.github.com')
226
+ .post('/repos/elastic/kibana/pulls')
227
+ .reply(200, { number: 1337, html_url: 'myHtmlUrl' });
228
+ createPullRequestCalls = (0, nockHelpers_1.listenForCallsToNockScope)(scope);
229
+ commitsByAuthorCalls =
230
+ (0, nockHelpers_1.mockGqlRequest)({
231
+ name: 'CommitsByAuthor',
232
+ statusCode: 200,
233
+ body: { data: commitsByAuthorMock_1.commitsByAuthorMock },
234
+ });
235
+ res = await (0, cherrypickAndCreateTargetPullRequest_1.cherrypickAndCreateTargetPullRequest)({
236
+ options,
237
+ commits: [
238
+ {
239
+ committedDate: '2021-08-18T16:11:38Z',
240
+ sourceBranch: '7.x',
241
+ sha: 'mySha',
242
+ originalMessage: 'My original commit message',
243
+ expectedTargetPullRequests: [],
244
+ },
245
+ ],
246
+ targetBranch: '6.x',
247
+ });
248
+ scope.done();
249
+ nock_1.default.cleanAll();
250
+ });
251
+ it('creates the pull request with commit reference', () => {
252
+ expect(createPullRequestCalls).toMatchInlineSnapshot(`
253
+ Array [
254
+ Object {
255
+ "base": "6.x",
256
+ "body": "# Backport
257
+
258
+ This is an automatic backport to \`6.x\` of:
259
+ - My original commit message (mySha)
260
+
261
+ ### Questions ?
262
+ Please refer to the [Backport tool documentation](https://github.com/sqren/backport)
263
+ ",
264
+ "head": "sqren_authenticated:backport/6.x/commit-mySha",
265
+ "title": "[6.x] My original commit message",
266
+ },
267
+ ]
268
+ `);
269
+ });
270
+ it('returns the expected response', () => {
271
+ expect(res).toEqual({ didUpdate: false, url: 'myHtmlUrl', number: 1337 });
272
+ });
273
+ it('shows the right prompts', () => {
274
+ expect(promptSpy.mock.calls.length).toBe(4);
275
+ expect(promptSpy.mock.calls[0][0]).toMatchInlineSnapshot(`
276
+ "Fix the following conflicts manually:
277
+
278
+ Conflicting files:
279
+ - /myHomeDir/.backport/repositories/elastic/kibana/conflicting-file.txt
280
+
281
+
282
+ Press ENTER when the conflicts are resolved and files are staged"
283
+ `);
284
+ expect(promptSpy.mock.calls[1][0]).toMatchInlineSnapshot(`
285
+ "Fix the following conflicts manually:
286
+
287
+ Conflicting files:
288
+ - /myHomeDir/.backport/repositories/elastic/kibana/conflicting-file.txt
289
+
290
+
291
+ Press ENTER when the conflicts are resolved and files are staged"
292
+ `);
293
+ expect(promptSpy.mock.calls[2][0]).toMatchInlineSnapshot(`
294
+ "Fix the following conflicts manually:
295
+
296
+ Conflicting files:
297
+ - /myHomeDir/.backport/repositories/elastic/kibana/conflicting-file.txt
298
+
299
+
300
+ Press ENTER when the conflicts are resolved and files are staged"
301
+ `);
302
+ });
303
+ it('commitsByAuthor is called with correct arguments', () => {
304
+ expect(commitsByAuthorCalls[0].variables).toMatchInlineSnapshot(`
305
+ Object {
306
+ "authorId": null,
307
+ "commitPath": "conflicting-file.txt",
308
+ "dateSince": null,
309
+ "dateUntil": "2021-08-18T16:11:38Z",
310
+ "maxNumber": 10,
311
+ "repoName": "kibana",
312
+ "repoOwner": "elastic",
313
+ "sourceBranch": "myDefaultSourceBranch",
314
+ }
315
+ `);
316
+ });
317
+ it('calls exec correctly', () => {
318
+ expect(execSpy.mock.calls).toMatchSnapshot();
319
+ });
320
+ it('logs correctly', async () => {
321
+ expect(consoleLogSpy.mock.calls[0][0]).toMatchInlineSnapshot(`
322
+ "
323
+ Backporting to 6.x:"
324
+ `);
325
+ expect(consoleLogSpy.mock.calls[1][0]).toMatchInlineSnapshot(`
326
+ "
327
+ The commit could not be backported due to conflicts
328
+ "
329
+ `);
330
+ expect(consoleLogSpy.mock.calls[2][0]).toMatchInlineSnapshot(`
331
+ "
332
+ ----------------------------------------
333
+ "
334
+ `);
335
+ expect(consoleLogSpy.mock.calls[3][0]).toMatchInlineSnapshot(`
336
+ "
337
+ ----------------------------------------
338
+ "
339
+ `);
340
+ });
341
+ });
342
+ });
343
+ function setupExecSpyForCherryPick() {
344
+ let conflictingFilesCheck = 0;
345
+ let unstagedFilesCheck = 0;
346
+ return jest
347
+ .spyOn(childProcess, 'exec')
348
+ .mockImplementation(async (cmd) => {
349
+ // createFeatureBranch
350
+ if (cmd.includes('git checkout -B')) {
351
+ return { stdout: 'create feature branch succeeded', stderr: '' };
352
+ }
353
+ // git fetch
354
+ if (cmd.startsWith('git fetch')) {
355
+ return { stderr: '', stdout: '' };
356
+ }
357
+ // cherrypick
358
+ if (cmd === 'git cherry-pick -x mySha') {
359
+ throw new ExecError_1.ExecError({ cmd });
360
+ }
361
+ // getIsCommitInBranch
362
+ if (cmd.startsWith('git merge-base --is-ancestor')) {
363
+ return { stderr: '', stdout: '' };
364
+ }
365
+ // getConflictingFiles
366
+ if (cmd === 'git --no-pager diff --check') {
367
+ conflictingFilesCheck++;
368
+ if (conflictingFilesCheck >= 4) {
369
+ return { stderr: '', stdout: '' };
370
+ }
371
+ throw new ExecError_1.ExecError({
372
+ code: 2,
373
+ cmd,
374
+ stdout: `conflicting-file.txt:1: leftover conflict marker\nconflicting-file.txt:3: leftover conflict marker\nconflicting-file.txt:5: leftover conflict marker\n`,
375
+ });
376
+ }
377
+ // getUnstagedFiles
378
+ if (cmd === 'git --no-pager diff --name-only') {
379
+ unstagedFilesCheck++;
380
+ if (unstagedFilesCheck >= 5) {
381
+ return { stderr: '', stdout: '' };
382
+ }
383
+ return { stdout: `conflicting-file.txt\n`, stderr: '' };
384
+ }
385
+ // addUnstagedFiles
386
+ if (cmd === 'git add --update') {
387
+ return { stdout: ``, stderr: '' };
388
+ }
389
+ // finalizeCherrypick
390
+ if (cmd.includes('git commit --no-edit')) {
391
+ return { stdout: ``, stderr: '' };
392
+ }
393
+ // pushFeatureBranch
394
+ if (cmd.startsWith('git push ')) {
395
+ return { stdout: ``, stderr: '' };
396
+ }
397
+ // deleteFeatureBranch
398
+ if (cmd.includes('git branch -D ')) {
399
+ return { stdout: ``, stderr: '' };
400
+ }
401
+ throw new Error(`Missing exec mock for "${cmd}"`);
402
+ });
403
+ }
@@ -1,6 +1,5 @@
1
1
  import { ValidConfigOptions } from '../options/options';
2
+ export declare function getBackportDirPath(): string;
2
3
  export declare function getLogfilePath(): string;
3
4
  export declare function getGlobalConfigPath(): string;
4
- export declare function getReposPath(): string;
5
- export declare function getRepoOwnerPath({ repoOwner }: ValidConfigOptions): string;
6
- export declare function getRepoPath({ repoOwner, repoName }: ValidConfigOptions): string;
5
+ export declare function getRepoPath({ repoOwner, repoName, dir }: ValidConfigOptions): string;
@@ -3,9 +3,13 @@ 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
- exports.getRepoPath = exports.getRepoOwnerPath = exports.getReposPath = exports.getGlobalConfigPath = exports.getLogfilePath = void 0;
6
+ exports.getRepoPath = exports.getGlobalConfigPath = exports.getLogfilePath = exports.getBackportDirPath = void 0;
7
7
  const os_1 = require("os");
8
8
  const path_1 = __importDefault(require("path"));
9
+ function getBackportDirPath() {
10
+ return path_1.default.join((0, os_1.homedir)(), '.backport');
11
+ }
12
+ exports.getBackportDirPath = getBackportDirPath;
9
13
  function getLogfilePath() {
10
14
  return path_1.default.join((0, os_1.homedir)(), '.backport', 'backport.log');
11
15
  }
@@ -14,15 +18,10 @@ function getGlobalConfigPath() {
14
18
  return path_1.default.join((0, os_1.homedir)(), '.backport', 'config.json');
15
19
  }
16
20
  exports.getGlobalConfigPath = getGlobalConfigPath;
17
- function getReposPath() {
18
- return path_1.default.join((0, os_1.homedir)(), '.backport', 'repositories');
19
- }
20
- exports.getReposPath = getReposPath;
21
- function getRepoOwnerPath({ repoOwner }) {
22
- return path_1.default.join((0, os_1.homedir)(), '.backport', 'repositories', repoOwner);
23
- }
24
- exports.getRepoOwnerPath = getRepoOwnerPath;
25
- function getRepoPath({ repoOwner, repoName }) {
21
+ function getRepoPath({ repoOwner, repoName, dir }) {
22
+ if (dir) {
23
+ return dir;
24
+ }
26
25
  return path_1.default.join((0, os_1.homedir)(), '.backport', 'repositories', repoOwner, repoName);
27
26
  }
28
27
  exports.getRepoPath = getRepoPath;
@@ -12,12 +12,6 @@ describe('env', () => {
12
12
  test('getGlobalConfigPath', () => {
13
13
  expect((0, env_1.getGlobalConfigPath)()).toBe('/myHomeDir/.backport/config.json');
14
14
  });
15
- test('getReposPath', () => {
16
- expect((0, env_1.getReposPath)()).toBe('/myHomeDir/.backport/repositories');
17
- });
18
- test('getRepoOwnerPath', () => {
19
- expect((0, env_1.getRepoOwnerPath)({ repoOwner: 'elastic' })).toBe('/myHomeDir/.backport/repositories/elastic');
20
- });
21
15
  test('getRepoPath', () => {
22
16
  expect((0, env_1.getRepoPath)({
23
17
  repoOwner: 'elastic',
@@ -0,0 +1,2 @@
1
+ import { ValidConfigOptions } from '../options/options';
2
+ export declare function getCommits(options: ValidConfigOptions): Promise<import("./sourceCommit/parseSourceCommit").Commit | undefined>;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getCommits = void 0;
4
+ const fetchCommitByPullNumber_1 = require("../services/github/v4/fetchCommits/fetchCommitByPullNumber");
5
+ const fetchCommitBySha_1 = require("../services/github/v4/fetchCommits/fetchCommitBySha");
6
+ async function getCommits(options) {
7
+ if (options.sha) {
8
+ return (0, fetchCommitBySha_1.fetchCommitBySha)({ ...options, sha: options.sha });
9
+ }
10
+ if (options.pullNumber) {
11
+ return await (0, fetchCommitByPullNumber_1.fetchCommitByPullNumber)({
12
+ ...options,
13
+ pullNumber: options.pullNumber, // must extract pullNumber to satisfy the ts gods
14
+ });
15
+ }
16
+ // if (options.ci) {
17
+ // throw new HandledError(
18
+ // 'When "--ci" flag is enabled either `--sha` or `--pr` must be specified'
19
+ // );
20
+ // }
21
+ // if (options.prFilter) {
22
+ // // spinner.text = 'Loading pull requests...';
23
+ // const commitChoices = await fetchPullRequestBySearchQuery(options);
24
+ // return promptForCommits({
25
+ // commitChoices,
26
+ // isMultipleChoice: options.multipleCommits,
27
+ // showDetails: options.details,
28
+ // });
29
+ // }
30
+ // // spinner.text = `Loading commits from branch "${options.sourceBranch}"...`;
31
+ // const commitChoices = await fetchCommitsByAuthor(options);
32
+ // return promptForCommits({
33
+ // commitChoices,
34
+ // isMultipleChoice: options.multipleCommits,
35
+ // showDetails: options.details,
36
+ // });
37
+ }
38
+ exports.getCommits = getCommits;
@@ -0,0 +1,11 @@
1
+ import { Commit } from '../entrypoint.api';
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,68 @@
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 git_1 = require("../services/git");
9
+ const commitFormatters_1 = require("../services/github/commitFormatters");
10
+ const fetchCommitsByAuthor_1 = require("../services/github/v4/fetchCommits/fetchCommitsByAuthor");
11
+ // when the user is facing a git conflict we should help them understand
12
+ // why the conflict occurs. In many cases it's because one or more commits haven't been backported yet
13
+ async function getCommitsWithoutBackports({ options,
14
+ // commit that is being backported
15
+ commit,
16
+ // branch the commit is being backported to
17
+ targetBranch,
18
+ // the files with conflicts on the target branch
19
+ conflictingFiles, }) {
20
+ // commits on the source branch that also touched the conflicting files
21
+ const commitsInConflictingPaths = await (0, fetchCommitsByAuthor_1.fetchCommitsByAuthor)({
22
+ ...options,
23
+ author: null,
24
+ dateSince: null,
25
+ dateUntil: commit.committedDate,
26
+ commitPaths: conflictingFiles,
27
+ });
28
+ const promises = await Promise.all(commitsInConflictingPaths
29
+ .filter((c) => {
30
+ // exclude the commit we are currently trying to backport
31
+ if (c.sha === commit.sha) {
32
+ return false;
33
+ }
34
+ // exclude commits that are newer than the commit we are trying to backport
35
+ if (c.committedDate > commit.committedDate) {
36
+ return false;
37
+ }
38
+ // only consider commits that have an associated pull request
39
+ if (!c.pullUrl) {
40
+ return false;
41
+ }
42
+ // only include commit if it has an unmerged PR for the given target branch
43
+ const hasUnmergedPr = c.expectedTargetPullRequests.some((pr) => pr.branch === targetBranch && pr.state !== 'MERGED');
44
+ return hasUnmergedPr;
45
+ })
46
+ .slice(0, 10) // limit to max 10 commits
47
+ .map(async (c) => {
48
+ const results = await Promise.all(c.expectedTargetPullRequests.map(async (targetPr) => {
49
+ if (!targetPr.mergeCommit) {
50
+ return false;
51
+ }
52
+ return (0, git_1.getIsCommitInBranch)(options, targetPr.mergeCommit.sha);
53
+ }));
54
+ const isCommitInBranch = results.some((inBranch) => inBranch === true);
55
+ return { c, isCommitInBranch };
56
+ }));
57
+ return promises
58
+ .filter(({ isCommitInBranch }) => !isCommitInBranch)
59
+ .map(({ c }) => {
60
+ // get pull request for the target branch (if it exists)
61
+ const pendingBackportPr = c.expectedTargetPullRequests.find((pr) => pr.branch === targetBranch && pr.state === 'OPEN');
62
+ const formatted = pendingBackportPr
63
+ ? ` - ${(0, commitFormatters_1.getFirstLine)(c.originalMessage)} ${chalk_1.default.gray('(backport pending)')}\n ${pendingBackportPr.url}`
64
+ : ` - ${(0, commitFormatters_1.getFirstLine)(c.originalMessage)} ${chalk_1.default.red('(backport missing)')}\n ${c.pullUrl}`;
65
+ return { formatted, commit: c };
66
+ });
67
+ }
68
+ exports.getCommitsWithoutBackports = getCommitsWithoutBackports;
@@ -0,0 +1 @@
1
+ export {};