backport 7.2.0 → 7.3.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 (68) hide show
  1. package/README.md +12 -5
  2. package/dist/backportRun.js +12 -1
  3. package/dist/entrypoint.cli.d.ts +0 -1
  4. package/dist/entrypoint.cli.js +0 -1
  5. package/dist/entrypoint.cli.private.test.js +1 -0
  6. package/dist/options/ConfigOptions.d.ts +3 -1
  7. package/dist/options/cliArgs.d.ts +3 -1
  8. package/dist/options/cliArgs.js +16 -7
  9. package/dist/options/config/config.d.ts +3 -1
  10. package/dist/options/config/readConfigFile.d.ts +3 -1
  11. package/dist/options/options.d.ts +3 -1
  12. package/dist/options/options.js +3 -1
  13. package/dist/options/options.test.js +2 -1
  14. package/dist/runSequentially.d.ts +3 -1
  15. package/dist/runSequentially.js +2 -3
  16. package/dist/services/git.d.ts +15 -7
  17. package/dist/services/git.integration.test.js +38 -58
  18. package/dist/services/git.js +21 -28
  19. package/dist/services/git.test.js +56 -17
  20. package/dist/services/github/v3/createPullRequest.test.js +52 -0
  21. package/dist/services/github/v3/createStatusComment.test.js +12 -2
  22. package/dist/services/github/v4/fetchCommits/allFetchers.private.test.js +1 -0
  23. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.private.test.js +1 -0
  24. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.private.test.js +1 -0
  25. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.test.js +5 -0
  26. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.private.test.js +2 -0
  27. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.d.ts +4 -3
  28. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.js +1 -1
  29. package/dist/services/github/v4/getRepoOwnerAndNameFromGitRemotes.js +1 -0
  30. package/dist/services/github/v4/mocks/commitsByAuthorMock.js +20 -0
  31. package/dist/services/prompt.test.js +5 -0
  32. package/dist/services/sourceCommit/getMockSourceCommit.js +1 -0
  33. package/dist/services/sourceCommit/parseSourceCommit.d.ts +5 -0
  34. package/dist/services/sourceCommit/parseSourceCommit.js +6 -0
  35. package/dist/services/sourceCommit/parseSourceCommit.test.js +1 -0
  36. package/dist/test/e2e/cli/cli.private.test.d.ts +1 -0
  37. package/dist/test/e2e/cli/cli.private.test.js +219 -0
  38. package/dist/test/e2e/cli/commit-author.private.test.d.ts +1 -0
  39. package/dist/test/e2e/cli/commit-author.private.test.js +96 -0
  40. package/dist/test/e2e/cli/different-merge-strategies.private.test.d.ts +1 -0
  41. package/dist/test/e2e/cli/different-merge-strategies.private.test.js +108 -0
  42. package/dist/test/e2e/cli/repo-with-backportrc-removed.private.test.d.ts +1 -0
  43. package/dist/test/e2e/cli/repo-with-backportrc-removed.private.test.js +49 -0
  44. package/dist/test/e2e/cli/runBackportViaCli.d.ts +5 -0
  45. package/dist/test/e2e/cli/runBackportViaCli.js +53 -0
  46. package/dist/test/e2e/cli/test-that-repo-can-be-cloned.private.test.d.ts +1 -0
  47. package/dist/test/e2e/cli/test-that-repo-can-be-cloned.private.test.js +73 -0
  48. package/dist/test/e2e/module/module.mutation.test.d.ts +1 -0
  49. package/dist/test/e2e/module/module.mutation.test.js +369 -0
  50. package/dist/test/e2e/module.e2e.mutation.test.d.ts +1 -0
  51. package/dist/test/e2e/module.e2e.mutation.test.js +369 -0
  52. package/dist/test/e2e/module.e2e.private.test.js +6 -0
  53. package/dist/test/handleUnbackportedPullRequests.private.test.js +1 -0
  54. package/dist/ui/cherrypickAndCreateTargetPullRequest.d.ts +3 -1
  55. package/dist/ui/cherrypickAndCreateTargetPullRequest.js +29 -10
  56. package/dist/ui/cherrypickAndCreateTargetPullRequest.test.js +38 -25
  57. package/dist/ui/getCommitsWithoutBackports.test.js +20 -0
  58. package/dist/ui/getGitConfigAuthor.d.ts +6 -0
  59. package/dist/ui/getGitConfigAuthor.js +15 -0
  60. package/dist/ui/getTargetBranches.test.js +2 -0
  61. package/dist/ui/setupRepo.d.ts +2 -0
  62. package/dist/ui/setupRepo.js +56 -0
  63. package/dist/ui/setupRepo.test.d.ts +1 -0
  64. package/dist/ui/setupRepo.test.js +236 -0
  65. package/dist/utils/packageVersion.d.ts +1 -1
  66. package/dist/utils/packageVersion.js +1 -1
  67. package/package.json +11 -11
  68. package/yarn.lock +61 -81
@@ -62,18 +62,23 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
62
62
  const options = {
63
63
  assignees: [],
64
64
  authenticatedUsername: 'sqren_authenticated',
65
+ author: 'sqren',
65
66
  fork: true,
66
67
  prTitle: '[{targetBranch}] {commitMessages}',
68
+ repoForkOwner: 'sqren',
67
69
  repoName: 'kibana',
68
70
  repoOwner: 'elastic',
69
71
  reviewers: [],
70
72
  sourceBranch: 'myDefaultSourceBranch',
71
73
  sourcePRLabels: [],
72
74
  targetPRLabels: ['backport'],
73
- author: 'sqren',
74
75
  };
75
76
  const commits = [
76
77
  {
78
+ author: {
79
+ email: 'soren.louv@elastic.co',
80
+ name: 'Søren Louv-Jansen',
81
+ },
77
82
  sourceBranch: '7.x',
78
83
  sourceCommit: {
79
84
  committedDate: 'fff',
@@ -91,6 +96,10 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
91
96
  expectedTargetPullRequests: [],
92
97
  },
93
98
  {
99
+ author: {
100
+ email: 'soren.louv@elastic.co',
101
+ name: 'Søren Louv-Jansen',
102
+ },
94
103
  sourceBranch: '7.x',
95
104
  sourceCommit: {
96
105
  committedDate: 'ggg',
@@ -135,7 +144,7 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
135
144
 
136
145
  ### Questions ?
137
146
  Please refer to the [Backport tool documentation](https://github.com/sqren/backport)",
138
- "head": "sqren_authenticated:backport/6.x/pr-1000_pr-2000",
147
+ "head": "sqren:backport/6.x/pr-1000_pr-2000",
139
148
  "title": "[6.x] My original commit message (#1000) | My other commit message (#2000)",
140
149
  },
141
150
  ]
@@ -162,7 +171,7 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
162
171
  "Pulling latest changes",
163
172
  "Cherry-picking: My original commit message (#1000)",
164
173
  "Cherry-picking: My other commit message (#2000)",
165
- "Pushing branch \\"sqren_authenticated:backport/6.x/pr-1000_pr-2000\\"",
174
+ "Pushing branch \\"sqren:backport/6.x/pr-1000_pr-2000\\"",
166
175
  undefined,
167
176
  "Creating pull request",
168
177
  "Adding labels: backport",
@@ -177,17 +186,22 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
177
186
  const options = {
178
187
  assignees: [],
179
188
  authenticatedUsername: 'sqren_authenticated',
189
+ author: 'sqren',
180
190
  fork: true,
181
191
  prTitle: '[{targetBranch}] {commitMessages}',
192
+ repoForkOwner: 'the_fork_owner',
182
193
  repoName: 'kibana',
183
194
  repoOwner: 'elastic',
184
195
  reviewers: [],
185
196
  sourcePRLabels: [],
186
197
  targetPRLabels: ['backport'],
187
- author: 'sqren',
188
198
  };
189
199
  const commits = [
190
200
  {
201
+ author: {
202
+ email: 'soren.louv@elastic.co',
203
+ name: 'Søren Louv-Jansen',
204
+ },
191
205
  sourceCommit: {
192
206
  committedDate: 'hhh',
193
207
  sha: 'mySha',
@@ -223,7 +237,7 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
223
237
 
224
238
  ### Questions ?
225
239
  Please refer to the [Backport tool documentation](https://github.com/sqren/backport)",
226
- "head": "sqren_authenticated:backport/6.x/commit-mySha",
240
+ "head": "the_fork_owner:backport/6.x/commit-mySha",
227
241
  "title": "[6.x] My original commit message",
228
242
  },
229
243
  ]
@@ -246,16 +260,17 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
246
260
  const options = {
247
261
  assignees: [],
248
262
  authenticatedUsername: 'sqren_authenticated',
263
+ author: 'sqren',
249
264
  fork: true,
250
265
  githubApiBaseUrlV4: 'http://localhost/graphql',
251
266
  prTitle: '[{targetBranch}] {commitMessages}',
267
+ repoForkOwner: 'sqren',
252
268
  repoName: 'kibana',
253
269
  repoOwner: 'elastic',
254
270
  reviewers: [],
255
271
  sourceBranch: 'myDefaultSourceBranch',
256
272
  sourcePRLabels: [],
257
273
  targetPRLabels: ['backport'],
258
- author: 'sqren',
259
274
  };
260
275
  const scope = (0, nock_1.default)('https://api.github.com')
261
276
  .post('/repos/elastic/kibana/pulls')
@@ -271,6 +286,10 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
271
286
  options,
272
287
  commits: [
273
288
  {
289
+ author: {
290
+ email: 'soren.louv@elastic.co',
291
+ name: 'Søren Louv-Jansen',
292
+ },
274
293
  sourceCommit: {
275
294
  committedDate: '2021-08-18T16:11:38Z',
276
295
  sha: 'mySha',
@@ -299,7 +318,7 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
299
318
 
300
319
  ### Questions ?
301
320
  Please refer to the [Backport tool documentation](https://github.com/sqren/backport)",
302
- "head": "sqren_authenticated:backport/6.x/commit-mySha",
321
+ "head": "sqren:backport/6.x/commit-mySha",
303
322
  "title": "[6.x] My original commit message",
304
323
  },
305
324
  ]
@@ -362,7 +381,7 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
362
381
  "Pulling latest changes",
363
382
  "Cherry-picking: My original commit message",
364
383
  "Finalizing cherrypick",
365
- "Pushing branch \\"sqren_authenticated:backport/6.x/commit-mySha\\"",
384
+ "Pushing branch \\"sqren:backport/6.x/commit-mySha\\"",
366
385
  undefined,
367
386
  "Creating pull request",
368
387
  "Adding labels: backport",
@@ -370,24 +389,18 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
370
389
  `);
371
390
  });
372
391
  it('logs correctly', async () => {
373
- expect(consoleLogSpy.mock.calls[0][0]).toMatchInlineSnapshot(`
374
- "
375
- Backporting to 6.x:"
376
- `);
377
- expect(consoleLogSpy.mock.calls[1][0]).toMatchInlineSnapshot(`
392
+ expect(consoleLogSpy.mock.calls.map((call) => call[0]).join(''))
393
+ .toMatchInlineSnapshot(`
378
394
  "
395
+ Backporting to 6.x:
379
396
  The commit could not be backported due to conflicts
380
- "
381
- `);
382
- expect(consoleLogSpy.mock.calls[2][0]).toMatchInlineSnapshot(`
383
- "
397
+ Please fix the conflicts in /myHomeDir/.backport/repositories/elastic/kibana
384
398
  ----------------------------------------
385
- "
386
- `);
387
- expect(consoleLogSpy.mock.calls[3][0]).toMatchInlineSnapshot(`
388
- "
399
+
389
400
  ----------------------------------------
390
- "
401
+
402
+ ----------------------------------------
403
+ View pull request: myHtmlUrl"
391
404
  `);
392
405
  });
393
406
  });
@@ -406,9 +419,9 @@ function setupExecSpyForCherryPick() {
406
419
  if (cmd.startsWith('git fetch')) {
407
420
  return { stderr: '', stdout: '' };
408
421
  }
409
- // cherrypick
410
- if (cmd === 'git cherry-pick -x mySha') {
411
- throw new ExecError_1.ExecError({ cmd });
422
+ // emulate cherrypick exception
423
+ if (cmd.includes('cherry-pick -x mySha')) {
424
+ throw new ExecError_1.ExecError({ cmd, code: 128 });
412
425
  }
413
426
  // getIsCommitInBranch
414
427
  if (cmd.startsWith('git merge-base --is-ancestor')) {
@@ -37,6 +37,10 @@ describe('getCommitsWithoutBackports', () => {
37
37
  .spyOn(fetchCommitsByAuthorModule, 'fetchCommitsByAuthor')
38
38
  .mockResolvedValueOnce([
39
39
  {
40
+ author: {
41
+ email: 'soren.louv@elastic.co',
42
+ name: 'Søren Louv-Jansen',
43
+ },
40
44
  sourceCommit: {
41
45
  committedDate: '10',
42
46
  message: 'First commit (#1)',
@@ -59,6 +63,7 @@ describe('getCommitsWithoutBackports', () => {
59
63
  return (0, getCommitsWithoutBackports_1.getCommitsWithoutBackports)({
60
64
  options: {},
61
65
  commit: {
66
+ author: { email: 'soren.louv@elastic.co', name: 'Søren Louv-Jansen' },
62
67
  sourceCommit: {
63
68
  committedDate: '100',
64
69
  message: 'Second commit (#2)',
@@ -130,6 +135,10 @@ describe('getCommitsWithoutBackports', () => {
130
135
  .spyOn(fetchCommitsByAuthorModule, 'fetchCommitsByAuthor')
131
136
  .mockResolvedValueOnce([
132
137
  {
138
+ author: {
139
+ email: 'soren.louv@elastic.co',
140
+ name: 'Søren Louv-Jansen',
141
+ },
133
142
  sourceCommit: {
134
143
  committedDate: offendingCommitDate,
135
144
  message: 'First commit (#1)',
@@ -159,6 +168,7 @@ describe('getCommitsWithoutBackports', () => {
159
168
  return (0, getCommitsWithoutBackports_1.getCommitsWithoutBackports)({
160
169
  options: {},
161
170
  commit: {
171
+ author: { email: 'soren.louv@elastic.co', name: 'Søren Louv-Jansen' },
162
172
  sourceCommit: {
163
173
  committedDate: currentCommitDate,
164
174
  message: 'Second commit (#2)',
@@ -201,6 +211,10 @@ describe('getCommitsWithoutBackports', () => {
201
211
  .spyOn(fetchCommitsByAuthorModule, 'fetchCommitsByAuthor')
202
212
  .mockResolvedValueOnce([
203
213
  {
214
+ author: {
215
+ email: 'soren.louv@elastic.co',
216
+ name: 'Søren Louv-Jansen',
217
+ },
204
218
  sourceCommit: {
205
219
  committedDate: '10',
206
220
  message: 'First commit (#1)',
@@ -231,6 +245,7 @@ describe('getCommitsWithoutBackports', () => {
231
245
  options: {},
232
246
  // commit that is being backported
233
247
  commit: {
248
+ author: { email: 'soren.louv@elastic.co', name: 'Søren Louv-Jansen' },
234
249
  sourceCommit: {
235
250
  committedDate: '100',
236
251
  message: 'Second commit (#2)',
@@ -272,6 +287,10 @@ describe('getCommitsWithoutBackports', () => {
272
287
  .spyOn(fetchCommitsByAuthorModule, 'fetchCommitsByAuthor')
273
288
  .mockResolvedValueOnce([
274
289
  {
290
+ author: {
291
+ email: 'soren.louv@elastic.co',
292
+ name: 'Søren Louv-Jansen',
293
+ },
275
294
  sourceCommit: {
276
295
  committedDate: '10',
277
296
  sha: 'xyz',
@@ -286,6 +305,7 @@ describe('getCommitsWithoutBackports', () => {
286
305
  const commitsWithoutBackports = await (0, getCommitsWithoutBackports_1.getCommitsWithoutBackports)({
287
306
  options: {},
288
307
  commit: {
308
+ author: { email: 'soren.louv@elastic.co', name: 'Søren Louv-Jansen' },
289
309
  sourceCommit: {
290
310
  committedDate: '100',
291
311
  message: 'Second commit (#2)',
@@ -0,0 +1,6 @@
1
+ import { ValidConfigOptions } from '../options/options';
2
+ export declare type GitConfigAuthor = {
3
+ name?: string;
4
+ email?: string;
5
+ };
6
+ export declare function getGitConfigAuthor(options: ValidConfigOptions): Promise<GitConfigAuthor | undefined>;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getGitConfigAuthor = void 0;
4
+ const git_1 = require("../services/git");
5
+ async function getGitConfigAuthor(options) {
6
+ const localRepoPath = await (0, git_1.getLocalRepoPath)(options);
7
+ if (!localRepoPath) {
8
+ return;
9
+ }
10
+ return {
11
+ name: await (0, git_1.getGitConfig)({ dir: localRepoPath, key: 'user.name' }),
12
+ email: await (0, git_1.getGitConfig)({ dir: localRepoPath, key: 'user.email' }),
13
+ };
14
+ }
15
+ exports.getGitConfigAuthor = getGitConfigAuthor;
@@ -39,6 +39,7 @@ describe('getTargetBranches', () => {
39
39
  };
40
40
  const commits = [
41
41
  {
42
+ author: { email: 'soren.louv@elastic.co', name: 'Søren Louv-Jansen' },
42
43
  sourceCommit: {
43
44
  committedDate: 'aaa',
44
45
  message: 'hey',
@@ -126,6 +127,7 @@ describe('getTargetBranches', () => {
126
127
  };
127
128
  const commits = [
128
129
  {
130
+ author: { email: 'soren.louv@elastic.co', name: 'Søren Louv-Jansen' },
129
131
  sourceCommit: {
130
132
  committedDate: 'bbb',
131
133
  message: '[backport] Bump to 5.1.3 (#62286)',
@@ -0,0 +1,2 @@
1
+ import { ValidConfigOptions } from '../options/options';
2
+ export declare function setupRepo(options: ValidConfigOptions): Promise<void>;
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.setupRepo = void 0;
4
+ const del = require("del");
5
+ const HandledError_1 = require("../services/HandledError");
6
+ const env_1 = require("../services/env");
7
+ const git_1 = require("../services/git");
8
+ const ora_1 = require("./ora");
9
+ async function setupRepo(options) {
10
+ const repoPath = (0, env_1.getRepoPath)(options);
11
+ const isAlreadyCloned = await getIsRepoCloned(options);
12
+ if (options.cwd.includes(repoPath)) {
13
+ throw new HandledError_1.HandledError(`Refusing to clone repo into "${repoPath}" when current working directory is "${options.cwd}". Please change backport directory via \`--dir\` option or run backport from another location`);
14
+ }
15
+ // clone repo if folder does not already exists
16
+ if (!isAlreadyCloned) {
17
+ const spinner = (0, ora_1.ora)(options.ci).start();
18
+ try {
19
+ const localRepoPath = await (0, git_1.getLocalRepoPath)(options);
20
+ const remoteRepoPath = (0, git_1.getRemoteUrl)(options, options.repoOwner);
21
+ const sourcePath = localRepoPath ? localRepoPath : remoteRepoPath;
22
+ // show the full path for local repos, but only the host name for remote repos (to avoid showing the access token)
23
+ const sourcePathHumanReadable = !localRepoPath
24
+ ? options.gitHostname
25
+ : sourcePath;
26
+ const spinnerCloneText = `Cloning repository from ${sourcePathHumanReadable} (one-time operation)`;
27
+ spinner.text = `0% ${spinnerCloneText}`;
28
+ await del(repoPath, { force: true });
29
+ await (0, git_1.cloneRepo)({ sourcePath, targetPath: repoPath }, (progress) => {
30
+ spinner.text = `${progress}% ${spinnerCloneText}`;
31
+ });
32
+ spinner.succeed(`100% ${spinnerCloneText}`);
33
+ }
34
+ catch (e) {
35
+ spinner.fail();
36
+ await del(repoPath, { force: true });
37
+ throw e;
38
+ }
39
+ }
40
+ // delete default "origin" remote to avoid confusion
41
+ await (0, git_1.deleteRemote)(options, 'origin');
42
+ // ensure remote are setup with latest accessToken
43
+ await (0, git_1.deleteRemote)(options, options.repoForkOwner);
44
+ await (0, git_1.addRemote)(options, options.repoForkOwner);
45
+ // add remote for non-fork repo (if the above is a fork)
46
+ if (options.repoForkOwner !== options.repoOwner) {
47
+ await (0, git_1.deleteRemote)(options, options.repoOwner);
48
+ await (0, git_1.addRemote)(options, options.repoOwner);
49
+ }
50
+ }
51
+ exports.setupRepo = setupRepo;
52
+ async function getIsRepoCloned(options) {
53
+ const repoPath = (0, env_1.getRepoPath)(options);
54
+ const projectRoot = await (0, git_1.getGitProjectRootPath)(repoPath);
55
+ return repoPath === projectRoot;
56
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,236 @@
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 del_1 = __importDefault(require("del"));
27
+ const childProcess = __importStar(require("../services/child-process-promisified"));
28
+ const gitModule = __importStar(require("../services/git"));
29
+ const mocks_1 = require("../test/mocks");
30
+ const setupRepo_1 = require("./setupRepo");
31
+ describe('setupRepo', () => {
32
+ let execSpy;
33
+ beforeEach(() => {
34
+ jest.spyOn(os_1.default, 'homedir').mockReturnValue('/myHomeDir');
35
+ execSpy = jest
36
+ .spyOn(childProcess, 'exec')
37
+ .mockResolvedValue({ stderr: '', stdout: '' });
38
+ });
39
+ afterEach(() => {
40
+ jest.restoreAllMocks();
41
+ });
42
+ describe('if an error occurs while cloning', () => {
43
+ it('should delete repo', async () => {
44
+ expect.assertions(2);
45
+ execSpy = jest
46
+ .spyOn(childProcess, 'execAsCallback')
47
+ .mockImplementation((cmd) => {
48
+ if (cmd.startsWith('git clone')) {
49
+ throw new Error('Simulated git clone failure');
50
+ }
51
+ throw new Error('unknown error');
52
+ });
53
+ await expect((0, setupRepo_1.setupRepo)({
54
+ repoName: 'kibana',
55
+ repoOwner: 'elastic',
56
+ cwd: '/path/to/source/repo',
57
+ })).rejects.toThrowError('Simulated git clone failure');
58
+ expect(del_1.default).toHaveBeenCalledWith('/myHomeDir/.backport/repositories/elastic/kibana', { force: true });
59
+ });
60
+ });
61
+ describe('while cloning the repo', () => {
62
+ it('updates the progress', async () => {
63
+ let onCloneComplete;
64
+ let dataHandler;
65
+ const oraMock = (0, mocks_1.getOraMock)();
66
+ const spinnerTextSpy = jest.spyOn(oraMock, 'text', 'set');
67
+ const spinnerSuccessSpy = jest.spyOn(oraMock, 'succeed');
68
+ jest.spyOn(gitModule, 'getLocalRepoPath').mockResolvedValue(undefined);
69
+ jest
70
+ .spyOn(childProcess, 'execAsCallback')
71
+ //@ts-expect-error
72
+ .mockImplementation((cmdString, cmdOptions, onComplete) => {
73
+ // callback should be called to finalize the operation
74
+ if (onComplete) {
75
+ //@ts-expect-error
76
+ onCloneComplete = onComplete;
77
+ }
78
+ return {
79
+ stderr: {
80
+ on: (name, handler) => {
81
+ dataHandler = handler;
82
+ },
83
+ },
84
+ };
85
+ });
86
+ setTimeout(() => {
87
+ dataHandler('Receiving objects: 1%');
88
+ dataHandler('Receiving objects: 10%');
89
+ dataHandler('Receiving objects: 20%');
90
+ dataHandler('Receiving objects: 100%');
91
+ dataHandler('Updating files: 1%');
92
+ dataHandler('Updating files: 10%');
93
+ dataHandler('Updating files: 20%');
94
+ dataHandler('Updating files: 100%');
95
+ onCloneComplete();
96
+ }, 50);
97
+ await (0, setupRepo_1.setupRepo)({
98
+ repoName: 'kibana',
99
+ repoOwner: 'elastic',
100
+ gitHostname: 'github.com',
101
+ cwd: '/path/to/source/repo',
102
+ });
103
+ expect(spinnerTextSpy.mock.calls.map((call) => call[0]))
104
+ .toMatchInlineSnapshot(`
105
+ Array [
106
+ "0% Cloning repository from github.com (one-time operation)",
107
+ "1% Cloning repository from github.com (one-time operation)",
108
+ "9% Cloning repository from github.com (one-time operation)",
109
+ "18% Cloning repository from github.com (one-time operation)",
110
+ "90% Cloning repository from github.com (one-time operation)",
111
+ "90% Cloning repository from github.com (one-time operation)",
112
+ "91% Cloning repository from github.com (one-time operation)",
113
+ "92% Cloning repository from github.com (one-time operation)",
114
+ "100% Cloning repository from github.com (one-time operation)",
115
+ ]
116
+ `);
117
+ expect(spinnerSuccessSpy).toHaveBeenCalledWith('100% Cloning repository from github.com (one-time operation)');
118
+ });
119
+ });
120
+ describe('if repo already exists', () => {
121
+ beforeEach(() => {
122
+ jest
123
+ .spyOn(childProcess, 'execAsCallback')
124
+ //@ts-expect-error
125
+ .mockImplementation((cmdString, cmdOptions, callback) => {
126
+ //@ts-expect-error
127
+ callback();
128
+ return { stderr: { on: () => null } };
129
+ });
130
+ });
131
+ it('should re-create remotes for both source repo and fork', async () => {
132
+ await (0, setupRepo_1.setupRepo)({
133
+ accessToken: 'myAccessToken',
134
+ authenticatedUsername: 'sqren_authenticated',
135
+ cwd: '/path/to/source/repo',
136
+ fork: true,
137
+ repoForkOwner: 'sqren',
138
+ repoName: 'kibana',
139
+ repoOwner: 'elastic',
140
+ });
141
+ expect(execSpy.mock.calls.map(([cmd, { cwd }]) => ({ cmd, cwd }))).toEqual([
142
+ {
143
+ cmd: 'git rev-parse --show-toplevel',
144
+ cwd: '/myHomeDir/.backport/repositories/elastic/kibana',
145
+ },
146
+ { cmd: 'git remote --verbose', cwd: '/path/to/source/repo' },
147
+ {
148
+ cmd: 'git remote rm origin',
149
+ cwd: '/myHomeDir/.backport/repositories/elastic/kibana',
150
+ },
151
+ {
152
+ cmd: 'git remote rm sqren',
153
+ cwd: '/myHomeDir/.backport/repositories/elastic/kibana',
154
+ },
155
+ {
156
+ cmd: 'git remote add sqren https://x-access-token:myAccessToken@github.com/sqren/kibana.git',
157
+ cwd: '/myHomeDir/.backport/repositories/elastic/kibana',
158
+ },
159
+ {
160
+ cmd: 'git remote rm elastic',
161
+ cwd: '/myHomeDir/.backport/repositories/elastic/kibana',
162
+ },
163
+ {
164
+ cmd: 'git remote add elastic https://x-access-token:myAccessToken@github.com/elastic/kibana.git',
165
+ cwd: '/myHomeDir/.backport/repositories/elastic/kibana',
166
+ },
167
+ ]);
168
+ });
169
+ });
170
+ describe('if repo does not exists locally', () => {
171
+ let spinnerSuccessSpy;
172
+ beforeEach(async () => {
173
+ const oraMock = (0, mocks_1.getOraMock)();
174
+ spinnerSuccessSpy = jest.spyOn(oraMock, 'succeed');
175
+ jest
176
+ .spyOn(childProcess, 'execAsCallback')
177
+ //@ts-expect-error
178
+ .mockImplementation((cmdString, cmdOptions, callback) => {
179
+ //@ts-expect-error
180
+ callback();
181
+ return { stderr: { on: () => null } };
182
+ });
183
+ await (0, setupRepo_1.setupRepo)({
184
+ accessToken: 'myAccessToken',
185
+ gitHostname: 'github.com',
186
+ repoName: 'kibana',
187
+ repoOwner: 'elastic',
188
+ cwd: '/path/to/source/repo',
189
+ });
190
+ });
191
+ it('should clone it from github.com', async () => {
192
+ expect(spinnerSuccessSpy).toHaveBeenCalledWith('100% Cloning repository from github.com (one-time operation)');
193
+ expect(childProcess.execAsCallback).toHaveBeenCalledWith('git clone https://x-access-token:myAccessToken@github.com/elastic/kibana.git /myHomeDir/.backport/repositories/elastic/kibana --progress', expect.any(Object), expect.any(Function));
194
+ });
195
+ });
196
+ describe('if repo exists locally', () => {
197
+ let spinnerSuccessSpy;
198
+ beforeEach(async () => {
199
+ const oraMock = (0, mocks_1.getOraMock)();
200
+ spinnerSuccessSpy = jest.spyOn(oraMock, 'succeed');
201
+ jest
202
+ .spyOn(gitModule, 'getLocalRepoPath')
203
+ .mockResolvedValue('/path/to/source/repo');
204
+ jest
205
+ .spyOn(gitModule, 'getGitConfig')
206
+ .mockResolvedValue('email-or-username');
207
+ jest
208
+ .spyOn(childProcess, 'execAsCallback')
209
+ //@ts-expect-error
210
+ .mockImplementation((cmdString, cmdOptions, callback) => {
211
+ //@ts-expect-error
212
+ callback();
213
+ return { stderr: { on: () => null } };
214
+ });
215
+ await (0, setupRepo_1.setupRepo)({
216
+ repoName: 'kibana',
217
+ repoOwner: 'elastic',
218
+ cwd: '/path/to/source/repo',
219
+ });
220
+ });
221
+ it('should clone it from local folder', async () => {
222
+ expect(spinnerSuccessSpy).toHaveBeenCalledWith('100% Cloning repository from /path/to/source/repo (one-time operation)');
223
+ expect(childProcess.execAsCallback).toHaveBeenCalledWith('git clone /path/to/source/repo /myHomeDir/.backport/repositories/elastic/kibana --progress', expect.any(Object), expect.any(Function));
224
+ });
225
+ });
226
+ describe('if `repoPath` is a parent of current working directory (cwd)', () => {
227
+ it('should clone it from local folder', async () => {
228
+ await expect(() => (0, setupRepo_1.setupRepo)({
229
+ repoName: 'kibana',
230
+ repoOwner: 'elastic',
231
+ cwd: '/myHomeDir/.backport/repositories/owner/repo/foo',
232
+ dir: '/myHomeDir/.backport/repositories/owner/repo',
233
+ })).rejects.toThrowError('Refusing to clone repo into "/myHomeDir/.backport/repositories/owner/repo" when current working directory is "/myHomeDir/.backport/repositories/owner/repo/foo". Please change backport directory via `--dir` option or run backport from another location');
234
+ });
235
+ });
236
+ });
@@ -1 +1 @@
1
- export declare const PACKAGE_VERSION = "7.2.0";
1
+ export declare const PACKAGE_VERSION = "7.3.1";
@@ -1,4 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PACKAGE_VERSION = void 0;
4
- exports.PACKAGE_VERSION = '7.2.0';
4
+ exports.PACKAGE_VERSION = '7.3.1';
package/package.json CHANGED
@@ -14,7 +14,7 @@
14
14
  "branches",
15
15
  "branching"
16
16
  ],
17
- "version": "7.2.0",
17
+ "version": "7.3.1",
18
18
  "main": "./dist/entrypoint.module.js",
19
19
  "types": "dist/entrypoint.module.d.ts",
20
20
  "bin": {
@@ -23,14 +23,14 @@
23
23
  "license": "MIT",
24
24
  "scripts": {
25
25
  "postinstall": "test -f ./dist/scripts/runPostinstall.js && node ./dist/scripts/runPostinstall.js || echo 'Dist folder missing'",
26
- "version": "echo \"Bumping version\" && yarn extractPackageVersion && git add src/utils/packageVersion.ts && yarn build",
26
+ "version": "echo \"Bumping version\" && yarn extractPackageVersion && git add src/utils/packageVersion.ts && yarn tsc",
27
27
  "prepare": "husky install",
28
- "prepublishOnly": "yarn lint",
29
- "build": "tsc --project ./tsconfig.prod.json",
30
- "extractPackageVersion": "node -p \"'export const PACKAGE_VERSION = ' + JSON.stringify(require('./package.json').version) + ';'\" | prettier > src/utils/packageVersion.ts",
31
- "lint": "echo \"Running lint\" && tsc --project ./tsconfig.test.json && eslint './**/*.{ts,js}'",
28
+ "prepublishOnly": "yarn lint && yarn tsc",
29
+ "extractPackageVersion": "node -p \"'export const PACKAGE_VERSION = ' + JSON.stringify(require('./package.json').version) + ';'\" | prettier --parser typescript > src/utils/packageVersion.ts",
30
+ "lint-and-test": "yarn tsc && yarn lint && yarn test-all",
31
+ "lint": "echo \"Running lint\" && eslint './**/*.{ts,js}'",
32
32
  "start": "ts-node --transpile-only ./src/entrypoint.cli.ts",
33
- "test-all": "yarn lint && jest --config ./jest.config.all.js",
33
+ "test-all": "jest --config ./jest.config.all.js",
34
34
  "test-mutation": "jest --config ./jest.config.mutation.js",
35
35
  "test-private": "jest --config ./jest.config.private.js",
36
36
  "test": "jest"
@@ -86,14 +86,14 @@
86
86
  "@types/core-js": "^2.5.5",
87
87
  "@types/dedent": "^0.7.0",
88
88
  "@types/inquirer": "^8.2.0",
89
- "@types/jest": "^27.4.0",
89
+ "@types/jest": "^27.4.1",
90
90
  "@types/lodash": "^4.14.178",
91
- "@types/node": "^17.0.18",
91
+ "@types/node": "^17.0.21",
92
92
  "@types/safe-json-stringify": "^1.1.2",
93
93
  "@types/yargs": "^17.0.8",
94
94
  "@types/yargs-parser": "^20.2.1",
95
- "@typescript-eslint/eslint-plugin": "^5.12.0",
96
- "@typescript-eslint/parser": "^5.12.0",
95
+ "@typescript-eslint/eslint-plugin": "^5.12.1",
96
+ "@typescript-eslint/parser": "^5.12.1",
97
97
  "eslint": "^8.9.0",
98
98
  "eslint-config-prettier": "^8.4.0",
99
99
  "eslint-plugin-import": "^2.25.4",