backport 6.1.3 → 6.2.1

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 (169) hide show
  1. package/README.md +40 -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.cli.js +3 -5
  9. package/dist/entrypoint.module.d.ts +6 -3
  10. package/dist/entrypoint.module.js +13 -7
  11. package/dist/entrypoint.module.private.test.js +4 -1
  12. package/dist/main.js +5 -1
  13. package/dist/options/ConfigOptions.d.ts +4 -0
  14. package/dist/options/cliArgs.d.ts +17 -11
  15. package/dist/options/cliArgs.js +36 -5
  16. package/dist/options/config/config.d.ts +10 -1
  17. package/dist/options/config/config.js +9 -3
  18. package/dist/options/config/config.test.js +27 -1
  19. package/dist/options/config/getProjectConfig.d.ts +0 -0
  20. package/dist/options/config/getProjectConfig.js +1 -0
  21. package/dist/options/config/globalConfig.d.ts +2 -2
  22. package/dist/options/config/globalConfig.js +16 -14
  23. package/dist/options/config/globalConfig.test.js +10 -4
  24. package/dist/options/config/projectConfig.d.ts +3 -1
  25. package/dist/options/config/projectConfig.js +5 -2
  26. package/dist/options/config/projectConfig.test.js +35 -6
  27. package/dist/options/config/readConfigFile.d.ts +26 -22
  28. package/dist/options/options.d.ts +18 -9
  29. package/dist/options/options.js +11 -4
  30. package/dist/options/options.test.js +40 -35
  31. package/dist/react-components/0-loadOptions.d.ts +6 -0
  32. package/dist/react-components/0-loadOptions.js +45 -0
  33. package/dist/react-components/1-loadCommit.d.ts +8 -0
  34. package/dist/react-components/1-loadCommit.js +88 -0
  35. package/dist/react-components/1a-loadCommit.d.ts +5 -0
  36. package/dist/react-components/1a-loadCommit.js +76 -0
  37. package/dist/react-components/1b-commitChoices.d.ts +5 -0
  38. package/dist/react-components/1b-commitChoices.js +98 -0
  39. package/dist/react-components/2-commitChoices.d.ts +8 -0
  40. package/dist/react-components/2-commitChoices.js +96 -0
  41. package/dist/react-components/2-commitsPrompt.d.ts +8 -0
  42. package/dist/react-components/2-commitsPrompt.js +96 -0
  43. package/dist/react-components/2-targetBranchChoices.d.ts +9 -0
  44. package/dist/react-components/2-targetBranchChoices.js +109 -0
  45. package/dist/react-components/3-cherrypick.d.ts +1 -0
  46. package/dist/react-components/3-cherrypick.js +2 -0
  47. package/dist/react-components/App.d.ts +2 -0
  48. package/dist/react-components/App.js +35 -0
  49. package/dist/react-components/BackportStateContext.d.ts +33 -0
  50. package/dist/react-components/BackportStateContext.js +42 -0
  51. package/dist/react-components/CommitPrompt/2-commitsPrompt.d.ts +8 -0
  52. package/dist/react-components/CommitPrompt/2-commitsPrompt.js +96 -0
  53. package/dist/react-components/CommitPrompt/CommitPrompt.d.ts +8 -0
  54. package/dist/react-components/CommitPrompt/CommitPrompt.js +96 -0
  55. package/dist/react-components/CommitPrompt/getCommits.d.ts +2 -0
  56. package/dist/react-components/CommitPrompt/getCommits.js +27 -0
  57. package/dist/react-components/CommitPrompt.d.ts +8 -0
  58. package/dist/react-components/CommitPrompt.js +96 -0
  59. package/dist/react-components/CustomMultiSelect.d.ts +13 -0
  60. package/dist/react-components/CustomMultiSelect.js +21 -0
  61. package/dist/react-components/CustomMultiSelectItem.d.ts +5 -0
  62. package/dist/react-components/CustomMultiSelectItem.js +12 -0
  63. package/dist/react-components/CustomSelect.d.ts +14 -0
  64. package/dist/react-components/CustomSelect.js +34 -0
  65. package/dist/react-components/ErrorBoundary.d.ts +15 -0
  66. package/dist/react-components/ErrorBoundary.js +53 -0
  67. package/dist/react-components/MultiSelect.d.ts +7 -0
  68. package/dist/react-components/MultiSelect.js +16 -0
  69. package/dist/react-components/hooks/useAsync.d.ts +12 -0
  70. package/dist/react-components/hooks/useAsync.js +73 -0
  71. package/dist/runSequentially.test.js +11 -13
  72. package/dist/scripts/postinstall.js +3 -3
  73. package/dist/services/HandledError.d.ts +1 -1
  74. package/dist/services/backportRun/backportRun.ci.d.ts +6 -0
  75. package/dist/services/backportRun/backportRun.ci.js +81 -0
  76. package/dist/services/backportRun/backportRun.d.ts +14 -0
  77. package/dist/services/backportRun/backportRun.interactive.d.ts +1 -0
  78. package/dist/services/backportRun/backportRun.interactive.js +4 -0
  79. package/dist/services/backportRun/backportRun.js +14 -0
  80. package/dist/services/backportRun/backportState.d.ts +4 -0
  81. package/dist/services/backportRun/backportState.js +28 -0
  82. package/dist/services/backportRun/getCommits.d.ts +2 -0
  83. package/dist/services/backportRun/getCommits.js +20 -0
  84. package/dist/services/backportRun/runSequentially.d.ts +20 -0
  85. package/dist/services/backportRun/runSequentially.js +41 -0
  86. package/dist/services/backportRun/runSequentially.test.d.ts +1 -0
  87. package/dist/services/backportRun/runSequentially.test.js +199 -0
  88. package/dist/services/cherrypickAndCreateTargetPullRequest.d.ts +12 -0
  89. package/dist/services/cherrypickAndCreateTargetPullRequest.js +206 -0
  90. package/dist/services/cherrypickAndCreateTargetPullRequest.test.d.ts +1 -0
  91. package/dist/services/cherrypickAndCreateTargetPullRequest.test.js +403 -0
  92. package/dist/services/env.d.ts +5 -4
  93. package/dist/services/env.js +13 -11
  94. package/dist/services/env.test.js +0 -6
  95. package/dist/services/getCommits.d.ts +2 -0
  96. package/dist/services/getCommits.js +38 -0
  97. package/dist/services/getCommitsWithoutBackports.d.ts +11 -0
  98. package/dist/services/getCommitsWithoutBackports.js +68 -0
  99. package/dist/services/getCommitsWithoutBackports.test.d.ts +1 -0
  100. package/dist/services/getCommitsWithoutBackports.test.js +259 -0
  101. package/dist/services/getTargetBranches.d.ts +5 -0
  102. package/dist/services/getTargetBranches.js +60 -0
  103. package/dist/services/getTargetBranches.test.d.ts +1 -0
  104. package/dist/services/getTargetBranches.test.js +180 -0
  105. package/dist/services/git.d.ts +2 -1
  106. package/dist/services/git.js +5 -3
  107. package/dist/services/github/v3/createPullRequest.js +8 -5
  108. package/dist/services/github/v3/createPullRequest.test.js +60 -10
  109. package/dist/services/github/v3/createStatusComment.d.ts +2 -2
  110. package/dist/services/github/v3/createStatusComment.js +40 -15
  111. package/dist/services/github/v3/createStatusComment.test.js +174 -86
  112. package/dist/services/github/v4/fetchCommits/allFetchers.private.test.d.ts +1 -0
  113. package/dist/services/github/v4/fetchCommits/allFetchers.private.test.js +78 -0
  114. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.js +1 -1
  115. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.private.test.js +64 -0
  116. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.js +5 -5
  117. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.private.test.js +60 -0
  118. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.d.ts +2 -0
  119. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.js +17 -10
  120. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.private.test.js +72 -0
  121. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.test.js +6 -0
  122. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.js +8 -7
  123. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.private.test.js +8 -0
  124. package/dist/services/github/v4/fetchProjectConfig.d.ts +1 -1
  125. package/dist/services/github/v4/fetchProjectConfig.js +3 -3
  126. package/dist/services/github/v4/fetchProjectConfig.private.test.js +5 -5
  127. package/dist/services/github/v4/fetchRemoteProjectConfig.d.ts +8 -0
  128. package/dist/services/github/v4/fetchRemoteProjectConfig.js +56 -0
  129. package/dist/services/github/v4/fetchRemoteProjectConfig.private.test.d.ts +1 -0
  130. package/dist/services/github/v4/fetchRemoteProjectConfig.private.test.js +52 -0
  131. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.d.ts +4 -0
  132. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.js +0 -7
  133. package/dist/services/github/v4/mocks/commitsByAuthorMock.js +16 -5
  134. package/dist/services/logger.d.ts +4 -2
  135. package/dist/services/logger.js +31 -32
  136. package/dist/services/maybeSetupRepo.d.ts +2 -0
  137. package/dist/services/maybeSetupRepo.js +33 -0
  138. package/dist/services/maybeSetupRepo.test.d.ts +1 -0
  139. package/dist/services/maybeSetupRepo.test.js +78 -0
  140. package/dist/services/sourceCommit/getExpectedTargetPullRequests.d.ts +4 -0
  141. package/dist/services/sourceCommit/getExpectedTargetPullRequests.js +8 -2
  142. package/dist/services/sourceCommit/getExpectedTargetPullRequests.test.js +20 -0
  143. package/dist/services/sourceCommit/getMockSourceCommit.d.ts +1 -0
  144. package/dist/services/sourceCommit/getMockSourceCommit.js +72 -46
  145. package/dist/services/sourceCommit/parseSourceCommit.d.ts +22 -8
  146. package/dist/services/sourceCommit/parseSourceCommit.js +29 -10
  147. package/dist/services/sourceCommit/parseSourceCommit.test.js +10 -2
  148. package/dist/test/backport-e2e.private.test.d.ts +1 -0
  149. package/dist/test/backport-e2e.private.test.js +325 -0
  150. package/dist/test/handleUnbackportedPullRequests.private.test.d.ts +1 -0
  151. package/dist/test/handleUnbackportedPullRequests.private.test.js +43 -0
  152. package/dist/test/inquirer.private.test.js +3 -1
  153. package/dist/test/integration/integration.private.test.js +2 -2
  154. package/dist/test/nockHelpers.js +1 -3
  155. package/dist/test/setupFiles/automatic-mocks.js +1 -0
  156. package/dist/todo-delete.d.ts +1 -0
  157. package/dist/todo-delete.js +17 -0
  158. package/dist/ui/cherrypickAndCreateTargetPullRequest.js +4 -3
  159. package/dist/ui/cherrypickAndCreateTargetPullRequest.test.js +9 -12
  160. package/dist/ui/getCommits.js +1 -1
  161. package/dist/ui/getCommitsWithoutBackports.js +17 -2
  162. package/dist/ui/maybeSetupRepo.js +0 -6
  163. package/dist/ui/maybeSetupRepo.test.js +8 -4
  164. package/dist/utils/redact.d.ts +1 -0
  165. package/dist/utils/redact.js +7 -0
  166. package/dist/utils/sequentially.d.ts +1 -0
  167. package/dist/utils/sequentially.js +10 -0
  168. package/package.json +11 -11
  169. package/yarn.lock +570 -436
@@ -0,0 +1,259 @@
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 strip_ansi_1 = __importDefault(require("strip-ansi"));
26
+ const git = __importStar(require("../services/git"));
27
+ const fetchCommitsByAuthorModule = __importStar(require("../services/github/v4/fetchCommits/fetchCommitsByAuthor"));
28
+ const getCommitsWithoutBackports_1 = require("./getCommitsWithoutBackports");
29
+ describe('getCommitsWithoutBackports', () => {
30
+ afterEach(() => {
31
+ jest.restoreAllMocks();
32
+ });
33
+ describe('expectedTargetPullRequests', () => {
34
+ function setupExpectedPullRequests({ expectedTargetPullRequests, }) {
35
+ // simulate 1 unbackported commit
36
+ jest
37
+ .spyOn(fetchCommitsByAuthorModule, 'fetchCommitsByAuthor')
38
+ .mockResolvedValueOnce([
39
+ {
40
+ committedDate: '10',
41
+ expectedTargetPullRequests,
42
+ originalMessage: 'First commit (#1)',
43
+ sha: 'xyz',
44
+ sourceBranch: 'main',
45
+ pullUrl: 'https://www.github.com/foo/bar/pull/123',
46
+ },
47
+ ]);
48
+ // simulate commit is definitely not backported
49
+ jest.spyOn(git, 'getIsCommitInBranch').mockResolvedValueOnce(false);
50
+ return (0, getCommitsWithoutBackports_1.getCommitsWithoutBackports)({
51
+ options: {},
52
+ commit: {
53
+ committedDate: '100',
54
+ expectedTargetPullRequests: [],
55
+ originalMessage: 'Second commit (#2)',
56
+ sha: 'abc',
57
+ sourceBranch: 'main',
58
+ },
59
+ targetBranch: '7.x',
60
+ conflictingFiles: ['/foo/bar/baz.ts'],
61
+ });
62
+ }
63
+ it('should not unbackported commits if no backports were expected', async () => {
64
+ const commitsWithoutBackports = await setupExpectedPullRequests({
65
+ expectedTargetPullRequests: [],
66
+ });
67
+ expect(commitsWithoutBackports.length).toEqual(0);
68
+ });
69
+ it('should display as missing if a backport is CLOSED', async () => {
70
+ const commitsWithoutBackports = await setupExpectedPullRequests({
71
+ expectedTargetPullRequests: [
72
+ {
73
+ state: 'CLOSED',
74
+ branch: '7.x',
75
+ number: 456,
76
+ url: 'https://www.github.com/foo/bar/pull/456',
77
+ },
78
+ ],
79
+ });
80
+ expect((0, strip_ansi_1.default)(commitsWithoutBackports[0].formatted)).toEqual(' - First commit (#1) (backport missing)\n https://www.github.com/foo/bar/pull/123');
81
+ });
82
+ it('should display as missing if a backport is MISSING', async () => {
83
+ const commitsWithoutBackports = await setupExpectedPullRequests({
84
+ expectedTargetPullRequests: [{ state: 'MISSING', branch: '7.x' }],
85
+ });
86
+ expect((0, strip_ansi_1.default)(commitsWithoutBackports[0].formatted)).toEqual(' - First commit (#1) (backport missing)\n https://www.github.com/foo/bar/pull/123');
87
+ });
88
+ it('should display as pending if a backport is OPEN', async () => {
89
+ const commitsWithoutBackports = await setupExpectedPullRequests({
90
+ expectedTargetPullRequests: [
91
+ {
92
+ state: 'OPEN',
93
+ branch: '7.x',
94
+ url: 'https://www.github.com/foo/bar/pull/456',
95
+ number: 456,
96
+ },
97
+ ],
98
+ });
99
+ expect((0, strip_ansi_1.default)(commitsWithoutBackports[0].formatted)).toEqual(' - First commit (#1) (backport pending)\n https://www.github.com/foo/bar/pull/456');
100
+ });
101
+ it('should not display commit as unbackported if a backport was MERGED', async () => {
102
+ const commitsWithoutBackports = await setupExpectedPullRequests({
103
+ expectedTargetPullRequests: [
104
+ {
105
+ state: 'MERGED',
106
+ branch: '7.x',
107
+ number: 456,
108
+ url: 'https://www.github.com/foo/bar/pull/456',
109
+ },
110
+ ],
111
+ });
112
+ expect(commitsWithoutBackports).toEqual([]);
113
+ });
114
+ });
115
+ describe('commitDate', () => {
116
+ function setupCommitDateDiff({ offendingCommitDate, currentCommitDate, }) {
117
+ // simulate 1 commit with a pending backport
118
+ jest
119
+ .spyOn(fetchCommitsByAuthorModule, 'fetchCommitsByAuthor')
120
+ .mockResolvedValueOnce([
121
+ {
122
+ committedDate: offendingCommitDate,
123
+ expectedTargetPullRequests: [
124
+ {
125
+ state: 'OPEN',
126
+ branch: '7.x',
127
+ number: 456,
128
+ url: 'https://www.github.com/foo/bar/pull/456',
129
+ },
130
+ ],
131
+ originalMessage: 'First commit (#1)',
132
+ sha: 'xyz',
133
+ sourceBranch: 'main',
134
+ pullNumber: 123,
135
+ pullUrl: 'https://www.github.com/foo/bar/pull/123',
136
+ },
137
+ ]);
138
+ // simulate commit is definitely not backported
139
+ jest.spyOn(git, 'getIsCommitInBranch').mockResolvedValueOnce(false);
140
+ return (0, getCommitsWithoutBackports_1.getCommitsWithoutBackports)({
141
+ options: {},
142
+ commit: {
143
+ committedDate: currentCommitDate,
144
+ expectedTargetPullRequests: [],
145
+ originalMessage: 'Second commit (#2)',
146
+ sha: 'abc',
147
+ sourceBranch: 'main',
148
+ },
149
+ targetBranch: '7.x',
150
+ conflictingFiles: ['/foo/bar/baz.ts'],
151
+ });
152
+ }
153
+ it('should not display commit as unbackport if is newer than the commit currently being backported', async () => {
154
+ const commitsWithoutBackports = await setupCommitDateDiff({
155
+ offendingCommitDate: '100',
156
+ currentCommitDate: '10',
157
+ });
158
+ expect(commitsWithoutBackports).toEqual([]);
159
+ });
160
+ it('should display commit as unbackport if is older than the commit currently being backported', async () => {
161
+ const commitsWithoutBackports = await setupCommitDateDiff({
162
+ offendingCommitDate: '10',
163
+ currentCommitDate: '100',
164
+ });
165
+ expect((0, strip_ansi_1.default)(commitsWithoutBackports[0].formatted)).toEqual(' - First commit (#1) (backport pending)\n https://www.github.com/foo/bar/pull/456');
166
+ });
167
+ });
168
+ describe('targetBranch', () => {
169
+ function setupTargetBranchDiff({ offendingCommitTargetBranch, currentCommitTargetBranch, }) {
170
+ // return mock commits that also touched the conflicting files
171
+ jest
172
+ .spyOn(fetchCommitsByAuthorModule, 'fetchCommitsByAuthor')
173
+ .mockResolvedValueOnce([
174
+ {
175
+ pullUrl: 'https://www.github.com/foo/bar/pull/123',
176
+ pullNumber: 123,
177
+ committedDate: '10',
178
+ expectedTargetPullRequests: [
179
+ {
180
+ branch: offendingCommitTargetBranch,
181
+ state: 'OPEN',
182
+ url: 'https://www.github.com/foo/bar/pull/456',
183
+ number: 456,
184
+ },
185
+ ],
186
+ originalMessage: 'First commit (#1)',
187
+ sha: 'xyz',
188
+ sourceBranch: 'main',
189
+ },
190
+ ]);
191
+ // simulate commit is definitely not backported
192
+ jest.spyOn(git, 'getIsCommitInBranch').mockResolvedValueOnce(false);
193
+ return (0, getCommitsWithoutBackports_1.getCommitsWithoutBackports)({
194
+ options: {},
195
+ // commit that is being backported
196
+ commit: {
197
+ pullUrl: 'https://www.github.com/foo/bar/pull/123',
198
+ pullNumber: 123,
199
+ committedDate: '100',
200
+ expectedTargetPullRequests: [],
201
+ originalMessage: 'Second commit (#2)',
202
+ sha: 'abc',
203
+ sourceBranch: 'main',
204
+ },
205
+ targetBranch: currentCommitTargetBranch,
206
+ conflictingFiles: ['/foo/bar/baz.ts'],
207
+ });
208
+ }
209
+ it('should display backport as pending if target branches match', async () => {
210
+ const commitsWithoutBackports = await setupTargetBranchDiff({
211
+ offendingCommitTargetBranch: '7.x',
212
+ currentCommitTargetBranch: '7.x',
213
+ });
214
+ expect((0, strip_ansi_1.default)(commitsWithoutBackports[0].formatted)).toEqual(' - First commit (#1) (backport pending)\n https://www.github.com/foo/bar/pull/456');
215
+ });
216
+ it("should not display commit if target branches don't match", async () => {
217
+ const commitsWithoutBackports = await setupTargetBranchDiff({
218
+ offendingCommitTargetBranch: '7.x',
219
+ currentCommitTargetBranch: '8.x',
220
+ });
221
+ expect(commitsWithoutBackports.length).toBe(0);
222
+ });
223
+ });
224
+ describe("when conflicting commit doesn't have an associated pull request", () => {
225
+ it('should be ignored', async () => {
226
+ jest
227
+ .spyOn(fetchCommitsByAuthorModule, 'fetchCommitsByAuthor')
228
+ .mockResolvedValueOnce([
229
+ {
230
+ // intentionally left out:
231
+ // pullNumber: 33,
232
+ // pullUrl: 'https://www.github.com/foo/bar/pull/123',
233
+ committedDate: '10',
234
+ expectedTargetPullRequests: [],
235
+ originalMessage: 'First commit (#1)',
236
+ sha: 'xyz',
237
+ sourceBranch: 'main',
238
+ },
239
+ ]);
240
+ // simulate commit is definitely not backported
241
+ jest.spyOn(git, 'getIsCommitInBranch').mockResolvedValueOnce(false);
242
+ const commitsWithoutBackports = await (0, getCommitsWithoutBackports_1.getCommitsWithoutBackports)({
243
+ options: {},
244
+ commit: {
245
+ pullUrl: 'https://www.github.com/foo/bar/pull/123',
246
+ pullNumber: 123,
247
+ committedDate: '100',
248
+ expectedTargetPullRequests: [],
249
+ originalMessage: 'Second commit (#2)',
250
+ sha: 'abc',
251
+ sourceBranch: 'main',
252
+ },
253
+ targetBranch: '7.x',
254
+ conflictingFiles: ['/foo/bar/baz.ts'],
255
+ });
256
+ expect(commitsWithoutBackports.length).toEqual(0);
257
+ });
258
+ });
259
+ });
@@ -0,0 +1,5 @@
1
+ import { TargetBranchChoice } from '../options/ConfigOptions';
2
+ import { ValidConfigOptions } from '../options/options';
3
+ import { Commit } from '../services/sourceCommit/parseSourceCommit';
4
+ export declare function getTargetBranches(options: ValidConfigOptions, commits: Commit[]): string[] | Promise<string[]>;
5
+ export declare function getTargetBranchChoices(options: ValidConfigOptions, missingTargetBranches: string[], sourceBranch: string): TargetBranchChoice[];
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getTargetBranchChoices = exports.getTargetBranches = void 0;
4
+ const lodash_1 = require("lodash");
5
+ const HandledError_1 = require("../services/HandledError");
6
+ const prompts_1 = require("../services/prompts");
7
+ function getTargetBranches(options, commits) {
8
+ // target branches already specified (in contrast to letting the user choose from a list)
9
+ if (!(0, lodash_1.isEmpty)(options.targetBranches)) {
10
+ return options.targetBranches;
11
+ }
12
+ // target branches from the first commit
13
+ const missingTargetBranches = commits.length === 1
14
+ ? commits[0].expectedTargetPullRequests
15
+ .filter((pr) => pr.state === 'MISSING')
16
+ .map((pr) => pr.branch)
17
+ : [];
18
+ // sourceBranch should be the same for all commits, so picking `sourceBranch` from the first commit should be fine 🤞
19
+ // this is specifically needed when backporting a PR like `backport --pr 123` and the source PR was merged to a non-default (aka non-master) branch.
20
+ const { sourceBranch } = commits[0];
21
+ const targetBranchChoices = getTargetBranchChoices(options, missingTargetBranches, sourceBranch);
22
+ // render interactive list of branches
23
+ return (0, prompts_1.promptForTargetBranches)({
24
+ targetBranchChoices,
25
+ isMultipleChoice: options.multipleBranches,
26
+ });
27
+ }
28
+ exports.getTargetBranches = getTargetBranches;
29
+ function getTargetBranchChoices(options, missingTargetBranches, sourceBranch) {
30
+ // exclude sourceBranch from targetBranchChoices
31
+ const targetBranchesChoices = getTargetBranchChoicesAsObject(options.targetBranchChoices).filter((choice) => choice.name !== sourceBranch);
32
+ if ((0, lodash_1.isEmpty)(targetBranchesChoices)) {
33
+ throw new HandledError_1.HandledError('Missing target branch choices');
34
+ }
35
+ if (!options.branchLabelMapping) {
36
+ return targetBranchesChoices;
37
+ }
38
+ // select missing target branches (based on pull request labels)
39
+ return targetBranchesChoices.map((choice) => {
40
+ const isChecked = missingTargetBranches.includes(choice.name);
41
+ return { ...choice, checked: isChecked };
42
+ });
43
+ }
44
+ exports.getTargetBranchChoices = getTargetBranchChoices;
45
+ // `targetBranchChoices` can either be a string or an object.
46
+ // It must be transformed so it is always treated as an object troughout the application
47
+ function getTargetBranchChoicesAsObject(targetBranchChoices) {
48
+ if (!targetBranchChoices) {
49
+ return [];
50
+ }
51
+ return targetBranchChoices.map((choice) => {
52
+ if ((0, lodash_1.isString)(choice)) {
53
+ return {
54
+ name: choice,
55
+ checked: false,
56
+ };
57
+ }
58
+ return choice;
59
+ });
60
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,180 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ const prompts = __importStar(require("../services/prompts"));
23
+ const getTargetBranches_1 = require("./getTargetBranches");
24
+ describe('getTargetBranches', () => {
25
+ let promptSpy;
26
+ beforeEach(() => {
27
+ jest.clearAllMocks();
28
+ promptSpy = jest
29
+ .spyOn(prompts, 'promptForTargetBranches')
30
+ .mockResolvedValueOnce(['branchA']);
31
+ });
32
+ describe('when `options.targetBranches` is empty', () => {
33
+ let branches;
34
+ beforeEach(async () => {
35
+ const options = {
36
+ targetBranches: [],
37
+ targetBranchChoices: [{ name: 'branchA' }, { name: 'branchB' }],
38
+ multipleBranches: false,
39
+ };
40
+ const commits = [
41
+ {
42
+ committedDate: 'aaa',
43
+ originalMessage: 'hey',
44
+ sha: 'abcd',
45
+ sourceBranch: '7.x',
46
+ pullNumber: 1337,
47
+ expectedTargetPullRequests: [],
48
+ },
49
+ ];
50
+ branches = await (0, getTargetBranches_1.getTargetBranches)(options, commits);
51
+ });
52
+ it('should return branches from prompt', () => {
53
+ expect(branches).toEqual(['branchA']);
54
+ });
55
+ it('should call prompt with correct args', () => {
56
+ expect(promptSpy).toHaveBeenLastCalledWith({
57
+ targetBranchChoices: [{ name: 'branchA' }, { name: 'branchB' }],
58
+ isMultipleChoice: false,
59
+ });
60
+ });
61
+ });
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', () => {
103
+ let targetBranchChoices;
104
+ beforeEach(async () => {
105
+ const options = {
106
+ targetBranches: [],
107
+ multipleBranches: true,
108
+ targetBranchChoices: [
109
+ { name: 'master' },
110
+ { name: '7.x' },
111
+ { name: '7.7' },
112
+ { name: '7.6' },
113
+ { name: '7.5' },
114
+ ],
115
+ branchLabelMapping: {},
116
+ sourceBranch: 'master',
117
+ };
118
+ const commits = [
119
+ {
120
+ committedDate: 'bbb',
121
+ sourceBranch: 'master',
122
+ sha: 'my-sha',
123
+ originalMessage: '[backport] Bump to 5.1.3 (#62286)',
124
+ pullNumber: 62286,
125
+ expectedTargetPullRequests: [{ branch: '7.x', state: 'MISSING' }],
126
+ },
127
+ ];
128
+ await (0, getTargetBranches_1.getTargetBranches)(options, commits);
129
+ targetBranchChoices = promptSpy.mock.calls[0][0].targetBranchChoices;
130
+ });
131
+ it('should list the correct branches', async () => {
132
+ expect(targetBranchChoices).toEqual([
133
+ { checked: true, name: '7.x' },
134
+ { checked: false, name: '7.7' },
135
+ { checked: false, name: '7.6' },
136
+ { checked: false, name: '7.5' },
137
+ ]);
138
+ });
139
+ it('should not list the sourceBranch (master)', async () => {
140
+ expect(targetBranchChoices).not.toContainEqual(expect.objectContaining({ name: 'master' }));
141
+ });
142
+ it('should select 7.x', async () => {
143
+ expect(targetBranchChoices).toContainEqual({
144
+ name: '7.x',
145
+ checked: true,
146
+ });
147
+ });
148
+ });
149
+ });
150
+ describe('getTargetBranchChoices', () => {
151
+ const options = {
152
+ ci: false,
153
+ targetBranchChoices: [
154
+ { name: 'master', checked: true },
155
+ { name: '7.x', checked: true },
156
+ { name: '7.8', checked: false },
157
+ { name: '7.7', checked: false },
158
+ ],
159
+ branchLabelMapping: {},
160
+ };
161
+ const sourceBranch = 'master';
162
+ it('should not check any branches if no labels match', () => {
163
+ const targetBranchesFromLabels = [];
164
+ const branches = (0, getTargetBranches_1.getTargetBranchChoices)(options, targetBranchesFromLabels, sourceBranch);
165
+ expect(branches).toEqual([
166
+ { checked: false, name: '7.x' },
167
+ { checked: false, name: '7.8' },
168
+ { checked: false, name: '7.7' },
169
+ ]);
170
+ });
171
+ it('should pre-select branches based on labels', () => {
172
+ const targetBranchesFromLabels = ['7.7'];
173
+ const branches = (0, getTargetBranches_1.getTargetBranchChoices)(options, targetBranchesFromLabels, sourceBranch);
174
+ expect(branches).toEqual([
175
+ { checked: false, name: '7.x' },
176
+ { checked: false, name: '7.8' },
177
+ { checked: true, name: '7.7' },
178
+ ]);
179
+ });
180
+ });
@@ -1,4 +1,5 @@
1
1
  import { ValidConfigOptions } from '../options/options';
2
+ import { ExpectedTargetPullRequest } from './sourceCommit/getExpectedTargetPullRequests';
2
3
  import { Commit } from './sourceCommit/parseSourceCommit';
3
4
  export declare function repoExists(options: ValidConfigOptions): Promise<boolean>;
4
5
  export declare function deleteRepo(options: ValidConfigOptions): Promise<string[]>;
@@ -15,7 +16,7 @@ export declare function getIsCommitInBranch(options: ValidConfigOptions, commitS
15
16
  export declare function deleteRemote(options: ValidConfigOptions, remoteName: string): Promise<void>;
16
17
  export declare function addRemote(options: ValidConfigOptions, remoteName: string): Promise<void>;
17
18
  export declare function fetchBranch(options: ValidConfigOptions, branch: string): Promise<void>;
18
- export declare function cherrypick(options: ValidConfigOptions, sha: Commit['sha']): Promise<{
19
+ export declare function cherrypick(options: ValidConfigOptions, sha: Commit['sha'], mergedTargetPullRequest?: ExpectedTargetPullRequest): Promise<{
19
20
  conflictingFiles: {
20
21
  absolute: string;
21
22
  relative: string;
@@ -49,7 +49,7 @@ function cloneRepo(options, callback) {
49
49
  resolve();
50
50
  }
51
51
  };
52
- const execProcess = (0, child_process_promisified_1.execAsCallback)(`git clone ${getRemoteUrl(options, options.repoOwner)} --progress`, { cwd: (0, env_1.getRepoOwnerPath)(options), maxBuffer: 100 * 1024 * 1024 }, cb);
52
+ const execProcess = (0, child_process_promisified_1.execAsCallback)(`git clone ${getRemoteUrl(options, options.repoOwner)} ${(0, env_1.getRepoPath)(options)} --progress`, { maxBuffer: 100 * 1024 * 1024 }, cb);
53
53
  // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
54
54
  if (execProcess.stderr) {
55
55
  execProcess.stderr.on('data', (data) => {
@@ -158,7 +158,7 @@ async function fetchBranch(options, branch) {
158
158
  });
159
159
  }
160
160
  exports.fetchBranch = fetchBranch;
161
- async function cherrypick(options, sha) {
161
+ async function cherrypick(options, sha, mergedTargetPullRequest) {
162
162
  const mainlinArg = options.mainline != undefined ? ` --mainline ${options.mainline}` : '';
163
163
  const cherrypickRefArg = options.cherrypickRef === false ? '' : ' -x';
164
164
  const cmd = `git cherry-pick${cherrypickRefArg}${mainlinArg} ${sha}`;
@@ -174,7 +174,9 @@ async function cherrypick(options, sha) {
174
174
  // commit was already backported
175
175
  if (e.message.includes('The previous cherry-pick is now empty')) {
176
176
  const shortSha = (0, commitFormatters_1.getShortSha)(sha);
177
- throw new HandledError_1.HandledError(`Cherrypick failed because the selected commit (${shortSha}) is empty. Did you already backport this commit?`);
177
+ throw new HandledError_1.HandledError(`Cherrypick failed because the selected commit (${shortSha}) is empty. ${mergedTargetPullRequest?.url
178
+ ? `It looks like the commit was already backported in ${mergedTargetPullRequest.url}`
179
+ : 'Did you already backport this commit? '}`);
178
180
  }
179
181
  // git info missing
180
182
  if (e.message.includes('Please tell me who you are')) {
@@ -55,7 +55,6 @@ async function createPullRequest({ options, prPayload, }) {
55
55
  }
56
56
  exports.createPullRequest = createPullRequest;
57
57
  function getPullRequestBody({ options, commits, targetBranch, }) {
58
- const bodySuffix = options.prDescription ? `\n${options.prDescription}` : '';
59
58
  const commitMessages = commits
60
59
  .map((c) => {
61
60
  const message = c.pullNumber
@@ -64,21 +63,25 @@ function getPullRequestBody({ options, commits, targetBranch, }) {
64
63
  return ` - ${message}`;
65
64
  })
66
65
  .join('\n');
67
- return `# Backport
66
+ const defaultPrDescription = `# Backport
68
67
 
69
68
  This is an automatic backport to \`${targetBranch}\` of:
70
69
  ${commitMessages}
71
70
 
72
71
  ### Questions ?
73
- Please refer to the [Backport tool documentation](https://github.com/sqren/backport)
74
- ${bodySuffix}`;
72
+ Please refer to the [Backport tool documentation](https://github.com/sqren/backport)`;
73
+ return (options.prDescription ?? defaultPrDescription)
74
+ .replace('{targetBranch}', targetBranch)
75
+ .replace('{commitMessages}', commitMessages)
76
+ .replace('{defaultPrDescription}', defaultPrDescription);
75
77
  }
76
78
  exports.getPullRequestBody = getPullRequestBody;
77
79
  function getTitle({ options, commits, targetBranch, }) {
78
80
  const commitMessages = commits
79
81
  .map((commit) => (0, commitFormatters_1.getFirstLine)(commit.originalMessage))
80
82
  .join(' | ');
81
- return (options.prTitle ?? '[{targetBranch}] {commitMessages}')
83
+ const defaultPrTitle = '[{targetBranch}] {commitMessages}';
84
+ return (options.prTitle ?? defaultPrTitle)
82
85
  .replace('{targetBranch}', targetBranch)
83
86
  .replace('{commitMessages}', commitMessages)
84
87
  .slice(0, 240);