backport 7.1.0 → 7.2.0-beta.4

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 (56) hide show
  1. package/dist/entrypoint.cli.js +0 -0
  2. package/dist/entrypoint.module.d.ts +4 -3
  3. package/dist/entrypoint.module.js +2 -7
  4. package/dist/options/ConfigOptions.d.ts +1 -1
  5. package/dist/options/cliArgs.d.ts +1 -0
  6. package/dist/options/cliArgs.js +4 -0
  7. package/dist/options/config/config.d.ts +1 -1
  8. package/dist/options/config/readConfigFile.d.ts +1 -1
  9. package/dist/options/options.d.ts +2 -1
  10. package/dist/runSequentially.js +1 -0
  11. package/dist/runSequentially.test.js +55 -29
  12. package/dist/services/git.d.ts +3 -3
  13. package/dist/services/git.integration.test.js +66 -13
  14. package/dist/services/git.js +47 -5
  15. package/dist/services/git.test.js +15 -1
  16. package/dist/services/github/v3/createStatusComment.js +1 -1
  17. package/dist/services/github/v4/apiRequestV4.d.ts +4 -3
  18. package/dist/services/github/v4/apiRequestV4.js +15 -14
  19. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.js +19 -12
  20. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.js +19 -12
  21. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.js +15 -9
  22. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.js +15 -8
  23. package/dist/services/github/v4/fetchRemoteProjectConfig.js +1 -1
  24. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.d.ts +1 -1
  25. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.js +2 -15
  26. package/dist/services/github/v4/getOptionsFromGithub/query.js +1 -1
  27. package/dist/services/github/v4/mocks/commitsByAuthorMock.js +2 -5
  28. package/dist/services/github/v4/throwOnInvalidAccessToken.js +6 -6
  29. package/dist/services/github/v4/throwOnInvalidAccessToken.test.js +5 -5
  30. package/dist/services/remoteConfig.d.ts +2 -0
  31. package/dist/services/remoteConfig.js +15 -2
  32. package/dist/services/sourceCommit/getExpectedTargetPullRequests.d.ts +4 -1
  33. package/dist/services/sourceCommit/getExpectedTargetPullRequests.js +5 -1
  34. package/dist/services/sourceCommit/getExpectedTargetPullRequests.test.js +73 -39
  35. package/dist/services/sourceCommit/getMockSourceCommit.js +19 -18
  36. package/dist/services/sourceCommit/parseSourceCommit.d.ts +2 -2
  37. package/dist/services/sourceCommit/parseSourceCommit.js +9 -15
  38. package/dist/{entrypoint.cli.e2e.private.test.d.ts → test/e2e/cli.e2e.private.test.d.ts} +0 -0
  39. package/dist/{entrypoint.cli.e2e.private.test.js → test/e2e/cli.e2e.private.test.js} +153 -39
  40. package/dist/{services/github/v3/index.d.ts → test/e2e/module.e2e.private.test.d.ts} +0 -0
  41. package/dist/test/e2e/module.e2e.private.test.js +363 -0
  42. package/dist/test/handleUnbackportedPullRequests.private.test.js +0 -2
  43. package/dist/ui/cherrypickAndCreateTargetPullRequest.js +12 -18
  44. package/dist/ui/cherrypickAndCreateTargetPullRequest.test.js +6 -2
  45. package/dist/ui/getCommits.js +6 -4
  46. package/dist/ui/maybeSetupRepo.js +13 -16
  47. package/dist/ui/maybeSetupRepo.test.js +50 -16
  48. package/dist/ui/ora.d.ts +1 -1
  49. package/dist/ui/ora.js +2 -2
  50. package/dist/utils/packageVersion.d.ts +1 -1
  51. package/dist/utils/packageVersion.js +1 -1
  52. package/package.json +10 -10
  53. package/yarn.lock +121 -114
  54. package/dist/services/github/v3/index.js +0 -61
  55. package/dist/test/backport-e2e.mutation.test.d.ts +0 -1
  56. package/dist/test/backport-e2e.mutation.test.js +0 -347
@@ -25,6 +25,17 @@ async function cherrypickAndCreateTargetPullRequest({ options, commits, targetBr
25
25
  const backportBranch = getBackportBranchName(targetBranch, commits);
26
26
  const repoForkOwner = (0, git_1.getRepoForkOwner)(options);
27
27
  (0, logger_1.consoleLog)(`\n${chalk_1.default.bold(`Backporting to ${targetBranch}:`)}`);
28
+ await (0, git_1.createBackportBranch)({ options, targetBranch, backportBranch });
29
+ await (0, sequentially_1.sequentially)(commits, (commit) => waitForCherrypick(options, commit, targetBranch));
30
+ if (options.resetAuthor) {
31
+ await (0, git_1.setCommitAuthor)(options, options.authenticatedUsername);
32
+ }
33
+ if (options.dryRun) {
34
+ (0, ora_1.ora)(options.ci).succeed('Dry run');
35
+ return { url: 'https://localhost/dry-run', didUpdate: false, number: 1337 };
36
+ }
37
+ await (0, git_1.pushBackportBranch)({ options, backportBranch });
38
+ await (0, git_1.deleteBackportBranch)({ options, backportBranch });
28
39
  const prPayload = {
29
40
  owner: options.repoOwner,
30
41
  repo: options.repoName,
@@ -33,13 +44,7 @@ async function cherrypickAndCreateTargetPullRequest({ options, commits, targetBr
33
44
  head: `${repoForkOwner}:${backportBranch}`,
34
45
  base: targetBranch, // eg. 7.x
35
46
  };
36
- const targetPullRequest = await backportViaFilesystem({
37
- options,
38
- prPayload,
39
- targetBranch,
40
- backportBranch,
41
- commits,
42
- });
47
+ const targetPullRequest = await (0, createPullRequest_1.createPullRequest)({ options, prPayload });
43
48
  // add assignees to target pull request
44
49
  const assignees = options.autoAssign
45
50
  ? [options.authenticatedUsername]
@@ -72,17 +77,6 @@ async function cherrypickAndCreateTargetPullRequest({ options, commits, targetBr
72
77
  return targetPullRequest;
73
78
  }
74
79
  exports.cherrypickAndCreateTargetPullRequest = cherrypickAndCreateTargetPullRequest;
75
- async function backportViaFilesystem({ options, prPayload, commits, targetBranch, backportBranch, }) {
76
- logger_1.logger.info('Backporting via filesystem');
77
- await (0, git_1.createBackportBranch)({ options, targetBranch, backportBranch });
78
- await (0, sequentially_1.sequentially)(commits, (commit) => waitForCherrypick(options, commit, targetBranch));
79
- if (options.resetAuthor) {
80
- await (0, git_1.setCommitAuthor)(options, options.authenticatedUsername);
81
- }
82
- await (0, git_1.pushBackportBranch)({ options, backportBranch });
83
- await (0, git_1.deleteBackportBranch)({ options, backportBranch });
84
- return (0, createPullRequest_1.createPullRequest)({ options, prPayload });
85
- }
86
80
  /*
87
81
  * Returns the name of the backport branch without remote name
88
82
  *
@@ -156,7 +156,7 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
156
156
  expect(consoleLogSpy.mock.calls[1][0]).toMatchInlineSnapshot(`"View pull request: myHtmlUrl"`);
157
157
  });
158
158
  it('should start the spinner with the correct text', () => {
159
- expect(ora_1.default.mock.calls.map((call) => call[0]))
159
+ expect(ora_1.default.mock.calls.map((call) => call[0].text))
160
160
  .toMatchInlineSnapshot(`
161
161
  Array [
162
162
  "Pulling latest changes",
@@ -356,7 +356,7 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
356
356
  expect(execSpy.mock.calls).toMatchSnapshot();
357
357
  });
358
358
  it('calls ora correctly', () => {
359
- expect(ora_1.default.mock.calls.map((call) => call[0]))
359
+ expect(ora_1.default.mock.calls.map((call) => call[0].text))
360
360
  .toMatchInlineSnapshot(`
361
361
  Array [
362
362
  "Pulling latest changes",
@@ -450,6 +450,10 @@ function setupExecSpyForCherryPick() {
450
450
  if (cmd.includes('git branch -D ')) {
451
451
  return { stdout: ``, stderr: '' };
452
452
  }
453
+ // getIsMergeCommit
454
+ if (cmd.startsWith('git rev-list -1 --merges')) {
455
+ return { stdout: ``, stderr: '' };
456
+ }
453
457
  throw new Error(`Missing exec mock for "${cmd}"`);
454
458
  });
455
459
  }
@@ -23,10 +23,12 @@ async function getCommits(options) {
23
23
  const spinner = (0, ora_1.ora)(options.ci).start();
24
24
  try {
25
25
  if (options.sha) {
26
- spinner.text = `Loading commit "${(0, commitFormatters_1.getShortSha)(options.sha)}"`;
27
- const commit = await (0, fetchCommitBySha_1.fetchCommitBySha)({ ...options, sha: options.sha });
28
- spinner.stopAndPersist(getOraPersistsOption('Select commit', commit.sourceCommit.message));
29
- return [commit];
26
+ const shas = Array.isArray(options.sha) ? options.sha : [options.sha];
27
+ // TODO: use Intl.ListFormat to format the sha's
28
+ spinner.text = `Loading commit "${shas.map(commitFormatters_1.getShortSha)}"`;
29
+ const commits = await Promise.all(shas.map((sha) => (0, fetchCommitBySha_1.fetchCommitBySha)({ ...options, sha })));
30
+ spinner.stopAndPersist(getOraPersistsOption('Select commit', commits.map((commit) => commit.sourceCommit.message).join(', ')));
31
+ return commits;
30
32
  }
31
33
  if (options.pullNumber) {
32
34
  spinner.text = `Loading pull request #${options.pullNumber}`;
@@ -1,17 +1,20 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.maybeSetupRepo = void 0;
4
- const promises_1 = require("fs/promises");
5
4
  const del = require("del");
6
- const ora = require("ora");
5
+ const HandledError_1 = require("../services/HandledError");
7
6
  const env_1 = require("../services/env");
8
7
  const git_1 = require("../services/git");
8
+ const ora_1 = require("./ora");
9
9
  async function maybeSetupRepo(options) {
10
10
  const repoPath = (0, env_1.getRepoPath)(options);
11
- const isAlreadyCloned = await getIsRepoCloned(repoPath);
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
+ }
12
15
  // clone repo if folder does not already exists
13
16
  if (!isAlreadyCloned) {
14
- const spinner = ora().start();
17
+ const spinner = (0, ora_1.ora)(options.ci).start();
15
18
  try {
16
19
  const sourcePath = await (0, git_1.getSourceRepoPath)(options);
17
20
  const sourcePathHumanReadable = sourcePath.includes(options.gitHostname)
@@ -19,6 +22,7 @@ async function maybeSetupRepo(options) {
19
22
  : sourcePath;
20
23
  const spinnerCloneText = `Cloning repository from ${sourcePathHumanReadable} (one-time operation)`;
21
24
  spinner.text = `0% ${spinnerCloneText}`;
25
+ await del(repoPath, { force: true });
22
26
  await (0, git_1.cloneRepo)({ sourcePath, targetPath: repoPath }, (progress) => {
23
27
  spinner.text = `${progress}% ${spinnerCloneText}`;
24
28
  });
@@ -26,7 +30,7 @@ async function maybeSetupRepo(options) {
26
30
  }
27
31
  catch (e) {
28
32
  spinner.fail();
29
- await del(repoPath);
33
+ await del(repoPath, { force: true });
30
34
  throw e;
31
35
  }
32
36
  }
@@ -42,15 +46,8 @@ async function maybeSetupRepo(options) {
42
46
  }
43
47
  }
44
48
  exports.maybeSetupRepo = maybeSetupRepo;
45
- async function getIsRepoCloned(path) {
46
- try {
47
- const stats = await (0, promises_1.stat)(path);
48
- return stats.isDirectory();
49
- }
50
- catch (e) {
51
- if (e.code === 'ENOENT') {
52
- return false;
53
- }
54
- throw e;
55
- }
49
+ async function getIsRepoCloned(options) {
50
+ const repoPath = (0, env_1.getRepoPath)(options);
51
+ const projectRoot = await (0, git_1.getGitProjectRoot)(repoPath);
52
+ return repoPath === projectRoot;
56
53
  }
@@ -22,14 +22,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
22
22
  return (mod && mod.__esModule) ? mod : { "default": mod };
23
23
  };
24
24
  Object.defineProperty(exports, "__esModule", { value: true });
25
- const promises_1 = require("fs/promises");
26
25
  const os_1 = __importDefault(require("os"));
27
26
  const del_1 = __importDefault(require("del"));
28
27
  const childProcess = __importStar(require("../services/child-process-promisified"));
29
28
  const git = __importStar(require("../services/git"));
30
29
  const mocks_1 = require("../test/mocks");
31
30
  const maybeSetupRepo_1 = require("./maybeSetupRepo");
32
- const fs = { stat: promises_1.stat };
33
31
  describe('maybeSetupRepo', () => {
34
32
  let execSpy;
35
33
  beforeEach(() => {
@@ -55,8 +53,9 @@ describe('maybeSetupRepo', () => {
55
53
  await expect((0, maybeSetupRepo_1.maybeSetupRepo)({
56
54
  repoName: 'kibana',
57
55
  repoOwner: 'elastic',
56
+ cwd: '/path/to/source/repo',
58
57
  })).rejects.toThrowError('Simulated git clone failure');
59
- expect(del_1.default).toHaveBeenCalledWith('/myHomeDir/.backport/repositories/elastic/kibana');
58
+ expect(del_1.default).toHaveBeenCalledWith('/myHomeDir/.backport/repositories/elastic/kibana', { force: true });
60
59
  });
61
60
  });
62
61
  describe('while cloning the repo', () => {
@@ -100,6 +99,7 @@ describe('maybeSetupRepo', () => {
100
99
  await (0, maybeSetupRepo_1.maybeSetupRepo)({
101
100
  repoName: 'kibana',
102
101
  repoOwner: 'elastic',
102
+ cwd: '/path/to/source/repo',
103
103
  });
104
104
  expect(spinnerTextSpy.mock.calls.map((call) => call[0]))
105
105
  .toMatchInlineSnapshot(`
@@ -120,8 +120,6 @@ describe('maybeSetupRepo', () => {
120
120
  });
121
121
  describe('if repo already exists', () => {
122
122
  beforeEach(() => {
123
- // @ts-expect-error
124
- jest.spyOn(fs, 'stat').mockResolvedValue({ isDirectory: () => true });
125
123
  jest
126
124
  .spyOn(childProcess, 'execAsCallback')
127
125
  //@ts-expect-error
@@ -139,21 +137,42 @@ describe('maybeSetupRepo', () => {
139
137
  authenticatedUsername: 'sqren_authenticated',
140
138
  repoName: 'kibana',
141
139
  repoOwner: 'elastic',
140
+ cwd: '/path/to/source/repo',
142
141
  });
143
- expect(execSpy.mock.calls.map(([cmd]) => cmd)).toEqual([
144
- 'git remote --verbose',
145
- 'git remote rm origin',
146
- 'git remote rm sqren_authenticated',
147
- 'git remote add sqren_authenticated https://x-access-token:myAccessToken@github.com/sqren_authenticated/kibana.git',
148
- 'git remote rm elastic',
149
- 'git remote add elastic https://x-access-token:myAccessToken@github.com/elastic/kibana.git',
142
+ expect(execSpy.mock.calls.map(([cmd, { cwd }]) => ({ cmd, cwd }))).toEqual([
143
+ {
144
+ cmd: 'git rev-parse --show-toplevel',
145
+ cwd: '/myHomeDir/.backport/repositories/elastic/kibana',
146
+ },
147
+ {
148
+ cmd: 'git remote --verbose',
149
+ cwd: '/path/to/source/repo',
150
+ },
151
+ {
152
+ cmd: 'git remote rm origin',
153
+ cwd: '/myHomeDir/.backport/repositories/elastic/kibana',
154
+ },
155
+ {
156
+ cmd: 'git remote rm sqren_authenticated',
157
+ cwd: '/myHomeDir/.backport/repositories/elastic/kibana',
158
+ },
159
+ {
160
+ cmd: 'git remote add sqren_authenticated https://x-access-token:myAccessToken@github.com/sqren_authenticated/kibana.git',
161
+ cwd: '/myHomeDir/.backport/repositories/elastic/kibana',
162
+ },
163
+ {
164
+ cmd: 'git remote rm elastic',
165
+ cwd: '/myHomeDir/.backport/repositories/elastic/kibana',
166
+ },
167
+ {
168
+ cmd: 'git remote add elastic https://x-access-token:myAccessToken@github.com/elastic/kibana.git',
169
+ cwd: '/myHomeDir/.backport/repositories/elastic/kibana',
170
+ },
150
171
  ]);
151
172
  });
152
173
  });
153
174
  describe('if repo does not exists locally', () => {
154
175
  beforeEach(() => {
155
- // @ts-expect-error
156
- jest.spyOn(fs, 'stat').mockResolvedValue({ isDirectory: () => true });
157
176
  jest
158
177
  .spyOn(childProcess, 'execAsCallback')
159
178
  //@ts-expect-error
@@ -171,14 +190,13 @@ describe('maybeSetupRepo', () => {
171
190
  gitHostname: 'github.com',
172
191
  repoName: 'kibana',
173
192
  repoOwner: 'elastic',
193
+ cwd: '/path/to/source/repo',
174
194
  });
175
195
  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));
176
196
  });
177
197
  });
178
198
  describe('if repo does exist locally', () => {
179
199
  beforeEach(() => {
180
- // @ts-expect-error
181
- jest.spyOn(fs, 'stat').mockResolvedValue({ isDirectory: () => true });
182
200
  jest
183
201
  .spyOn(git, 'getSourceRepoPath')
184
202
  .mockResolvedValue('/path/to/source/repo');
@@ -197,8 +215,24 @@ describe('maybeSetupRepo', () => {
197
215
  await (0, maybeSetupRepo_1.maybeSetupRepo)({
198
216
  repoName: 'kibana',
199
217
  repoOwner: 'elastic',
218
+ cwd: '/path/to/source/repo',
200
219
  });
201
220
  expect(childProcess.execAsCallback).toHaveBeenCalledWith('git clone /path/to/source/repo /myHomeDir/.backport/repositories/elastic/kibana --progress', expect.any(Object), expect.any(Function));
202
221
  });
203
222
  });
223
+ describe('if `repoPath` is a parent of current working directory (cwd)', () => {
224
+ beforeEach(() => {
225
+ jest
226
+ .spyOn(git, 'getSourceRepoPath')
227
+ .mockResolvedValue('/path/to/source/repo');
228
+ jest.spyOn(childProcess, 'execAsCallback');
229
+ });
230
+ it('should clone it from local folder', async () => {
231
+ await expect(() => (0, maybeSetupRepo_1.maybeSetupRepo)({
232
+ repoName: 'kibana',
233
+ repoOwner: 'elastic',
234
+ cwd: '/myHomeDir/.backport/repositories/elastic/kibana/foo',
235
+ })).rejects.toThrowError('Refusing to clone repo into "/myHomeDir/.backport/repositories/elastic/kibana" when current working directory is "/myHomeDir/.backport/repositories/elastic/kibana/foo". Please change backport directory via `--dir` option or run backport from another location');
236
+ });
237
+ });
204
238
  });
package/dist/ui/ora.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import oraOriginal from 'ora';
2
- export declare function ora(ci: boolean | undefined, options?: string | oraOriginal.Options | undefined): oraOriginal.Ora | {
2
+ export declare function ora(ci: boolean | undefined, text?: string | undefined): oraOriginal.Ora | {
3
3
  start: (text?: string | undefined) => any;
4
4
  succeed: () => void;
5
5
  stop: () => void;
package/dist/ui/ora.js CHANGED
@@ -15,7 +15,7 @@ const oraMock = {
15
15
  stopAndPersist: () => { },
16
16
  set text(value) { },
17
17
  };
18
- function ora(ci, options) {
19
- return ci ? oraMock : (0, ora_1.default)(options);
18
+ function ora(ci, text) {
19
+ return ci ? oraMock : (0, ora_1.default)({ text });
20
20
  }
21
21
  exports.ora = ora;
@@ -1 +1 @@
1
- export declare const PACKAGE_VERSION = "7.1.0";
1
+ export declare const PACKAGE_VERSION = "7.2.0-beta.4";
@@ -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.1.0';
4
+ exports.PACKAGE_VERSION = '7.2.0-beta.4';
package/package.json CHANGED
@@ -14,11 +14,11 @@
14
14
  "branches",
15
15
  "branching"
16
16
  ],
17
- "version": "7.1.0",
17
+ "version": "7.2.0-beta.4",
18
18
  "main": "./dist/entrypoint.module.js",
19
19
  "types": "dist/entrypoint.module.d.ts",
20
20
  "bin": {
21
- "backport": "./dist/entrypoint.cli.js"
21
+ "backport": "./bin/backport"
22
22
  },
23
23
  "license": "MIT",
24
24
  "scripts": {
@@ -78,7 +78,7 @@
78
78
  "strip-json-comments": "^3.1.1",
79
79
  "terminal-link": "^2.1.1",
80
80
  "utility-types": "^3.10.0",
81
- "winston": "^3.5.1",
81
+ "winston": "^3.6.0",
82
82
  "yargs": "^17.3.1",
83
83
  "yargs-parser": "^21.0.0"
84
84
  },
@@ -88,22 +88,22 @@
88
88
  "@types/inquirer": "^8.2.0",
89
89
  "@types/jest": "^27.4.0",
90
90
  "@types/lodash": "^4.14.178",
91
- "@types/node": "^17.0.8",
91
+ "@types/node": "^17.0.18",
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.11.0",
96
- "@typescript-eslint/parser": "^5.11.0",
97
- "eslint": "^8.8.0",
98
- "eslint-config-prettier": "^8.3.0",
95
+ "@typescript-eslint/eslint-plugin": "^5.12.0",
96
+ "@typescript-eslint/parser": "^5.12.0",
97
+ "eslint": "^8.9.0",
98
+ "eslint-config-prettier": "^8.4.0",
99
99
  "eslint-plugin-import": "^2.25.4",
100
- "eslint-plugin-jest": "^26.1.0",
100
+ "eslint-plugin-jest": "^26.1.1",
101
101
  "eslint-plugin-prettier": "^4.0.0",
102
102
  "graphql-config": "^4.1.0",
103
103
  "husky": "^7.0.4",
104
104
  "jest": "^27.5.1",
105
105
  "jest-snapshot-serializer-ansi": "^1.0.0",
106
- "lint-staged": "^12.3.3",
106
+ "lint-staged": "^12.3.4",
107
107
  "nock": "^13.2.4",
108
108
  "prettier": "^2.5.1",
109
109
  "strip-ansi": "^6.0.1",