backport 6.0.0 → 6.1.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 (98) hide show
  1. package/README.md +158 -42
  2. package/dist/entrypoint.cli.js +2 -0
  3. package/dist/entrypoint.module.d.ts +16 -4
  4. package/dist/entrypoint.module.js +41 -13
  5. package/dist/entrypoint.module.private.test.d.ts +1 -0
  6. package/dist/entrypoint.module.private.test.js +33 -0
  7. package/dist/main.js +1 -2
  8. package/dist/options/ConfigOptions.d.ts +15 -6
  9. package/dist/options/cliArgs.d.ts +33 -34
  10. package/dist/options/cliArgs.js +19 -19
  11. package/dist/options/cliArgs.test.js +8 -5
  12. package/dist/options/config/config.d.ts +27 -52
  13. package/dist/options/config/config.js +2 -36
  14. package/dist/options/config/config.test.js +10 -27
  15. package/dist/options/config/getOptionsFromGit.d.ts +3 -2
  16. package/dist/options/config/getOptionsFromGit.js +1 -3
  17. package/dist/options/config/globalConfig.js +1 -4
  18. package/dist/options/config/globalConfig.test.js +0 -2
  19. package/dist/options/config/projectConfig.test.js +51 -14
  20. package/dist/options/config/readConfigFile.d.ts +51 -0
  21. package/dist/options/config/readConfigFile.js +28 -2
  22. package/dist/options/options.d.ts +52 -33
  23. package/dist/options/options.js +98 -7
  24. package/dist/options/options.test.js +180 -73
  25. package/dist/options/parseRequiredOptions.d.ts +3 -3
  26. package/dist/options/parseRequiredOptions.js +2 -4
  27. package/dist/options/parseRequiredOptions.test.js +6 -11
  28. package/dist/runSequentially.test.js +35 -78
  29. package/dist/services/git.d.ts +5 -2
  30. package/dist/services/git.js +11 -10
  31. package/dist/services/git.test.js +12 -9
  32. package/dist/services/github/v3/addAssigneesToPullRequest.d.ts +1 -1
  33. package/dist/services/github/v3/addAssigneesToPullRequest.js +2 -3
  34. package/dist/services/github/v3/addAssigneesToPullRequest.test.js +4 -5
  35. package/dist/services/github/v3/createPullRequest.js +10 -13
  36. package/dist/services/github/v3/createPullRequest.test.d.ts +1 -0
  37. package/dist/services/github/v3/createPullRequest.test.js +78 -0
  38. package/dist/services/github/v4/apiRequestV4.d.ts +1 -1
  39. package/dist/services/github/v4/apiRequestV4.js +1 -1
  40. package/dist/services/github/v4/enablePullRequestAutoMerge.js +1 -1
  41. package/dist/services/github/v4/enablePullRequestAutoMerge.private.test.js +0 -1
  42. package/dist/services/github/v4/fetchAuthorId.d.ts +5 -2
  43. package/dist/services/github/v4/fetchAuthorId.js +5 -6
  44. package/dist/services/github/v4/fetchAuthorId.private.test.js +3 -6
  45. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.d.ts +8 -1
  46. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.js +1 -1
  47. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.private.test.js +6 -3
  48. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.d.ts +8 -1
  49. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.js +1 -1
  50. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.private.test.js +3 -3
  51. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.d.ts +12 -1
  52. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.js +6 -8
  53. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.private.test.js +10 -12
  54. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.test.js +5 -7
  55. package/dist/services/github/v4/fetchCommits/fetchProjectConfig.d.ts +3 -0
  56. package/dist/services/github/v4/fetchCommits/fetchProjectConfig.js +55 -0
  57. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.js +5 -5
  58. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.private.test.js +2 -6
  59. package/dist/services/github/v4/fetchExistingPullRequest.private.test.js +0 -2
  60. package/dist/services/github/v4/fetchProjectConfig.d.ts +8 -0
  61. package/dist/services/github/v4/fetchProjectConfig.js +56 -0
  62. package/dist/services/github/v4/fetchProjectConfig.private.test.d.ts +1 -0
  63. package/dist/services/github/v4/fetchProjectConfig.private.test.js +52 -0
  64. package/dist/services/github/v4/fetchProjectConfig.test.d.ts +1 -0
  65. package/dist/services/github/v4/fetchProjectConfig.test.js +37 -0
  66. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.d.ts +66 -0
  67. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.js +130 -0
  68. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.private.test.d.ts +1 -0
  69. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.private.test.js +130 -0
  70. package/dist/services/github/v4/getOptionsFromGithub/index.d.ts +3 -1
  71. package/dist/services/github/v4/getOptionsFromGithub/index.private.test.js +22 -1
  72. package/dist/services/github/v4/getOptionsFromGithub/query.d.ts +4 -1
  73. package/dist/services/github/v4/getOptionsFromGithub/query.js +3 -0
  74. package/dist/services/sourceCommit/getExpectedTargetPullRequests.test.js +15 -15
  75. package/dist/services/sourceCommit/parseSourceCommit.d.ts +5 -1
  76. package/dist/services/sourceCommit/parseSourceCommit.js +2 -2
  77. package/dist/services/sourceCommit/parseSourceCommit.test.js +120 -5
  78. package/dist/test/inquirer.private.test.js +42 -34
  79. package/dist/test/integration/integration.private.test.js +104 -95
  80. package/dist/test/mockConfigFiles.d.ts +5 -0
  81. package/dist/test/mockConfigFiles.js +37 -0
  82. package/dist/test/nockHelpers.d.ts +1 -1
  83. package/dist/test/nockHelpers.js +4 -4
  84. package/dist/test/private/getDevAccessToken.js +1 -1
  85. package/dist/test/setupFiles/automatic-mocks.js +1 -29
  86. package/dist/ui/cherrypickAndCreatePullRequest.test.js +67 -28
  87. package/dist/ui/cherrypickAndCreateTargetPullRequest.js +6 -3
  88. package/dist/ui/getCommits.d.ts +1 -1
  89. package/dist/ui/getTargetBranches.d.ts +2 -1
  90. package/dist/ui/getTargetBranches.js +17 -1
  91. package/dist/ui/maybeSetupRepo.js +4 -3
  92. package/dist/ui/maybeSetupRepo.test.js +49 -3
  93. package/dist/utils/excludeUndefined.d.ts +6 -0
  94. package/dist/utils/excludeUndefined.js +7 -0
  95. package/dist/utils/excludeUndefined.test.d.ts +1 -0
  96. package/dist/utils/excludeUndefined.test.js +24 -0
  97. package/package.json +9 -8
  98. package/yarn.lock +98 -77
@@ -3,6 +3,7 @@ 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
+ /* eslint-disable jest/no-commented-out-tests */
6
7
  const os_1 = __importDefault(require("os"));
7
8
  const path_1 = require("path");
8
9
  const rest_1 = require("@octokit/rest");
@@ -11,6 +12,7 @@ const nock_1 = __importDefault(require("nock"));
11
12
  const options_1 = require("../../options/options");
12
13
  const runSequentially_1 = require("../../runSequentially");
13
14
  const getCommits_1 = require("../../ui/getCommits");
15
+ const mockConfigFiles_1 = require("../mockConfigFiles");
14
16
  const nockHelpers_1 = require("../nockHelpers");
15
17
  const getDevAccessToken_1 = require("../private/getDevAccessToken");
16
18
  jest.unmock('make-dir');
@@ -21,7 +23,7 @@ const HOMEDIR_PATH = (0, path_1.resolve)('./src/test/integration/mock-environmen
21
23
  const REPO_OWNER = 'backport-org';
22
24
  const REPO_NAME = 'integration-test';
23
25
  const BRANCH_NAME = 'backport/7.x/commit-5bf29b7d';
24
- const USERNAME = 'sqren';
26
+ const AUHTOR = 'sqren';
25
27
  describe('integration', () => {
26
28
  afterAll(() => {
27
29
  nock_1.default.cleanAll();
@@ -29,6 +31,10 @@ describe('integration', () => {
29
31
  beforeAll(() => {
30
32
  // set alternative homedir
31
33
  jest.spyOn(os_1.default, 'homedir').mockReturnValue(HOMEDIR_PATH);
34
+ (0, mockConfigFiles_1.mockConfigFiles)({
35
+ globalConfig: {},
36
+ projectConfig: {},
37
+ });
32
38
  });
33
39
  describe('when a single commit is backported', () => {
34
40
  let res;
@@ -45,9 +51,10 @@ describe('integration', () => {
45
51
  githubApiBaseUrlV3: 'https://api.foo.com',
46
52
  ci: true,
47
53
  sha: '5bf29b7d847ea3dbde9280448f0f62ad0f22d3ad',
48
- username: USERNAME,
54
+ author: AUHTOR,
49
55
  accessToken,
50
- upstream: 'backport-org/integration-test',
56
+ repoOwner: 'backport-org',
57
+ repoName: 'integration-test',
51
58
  });
52
59
  const commits = await (0, getCommits_1.getCommits)(options);
53
60
  const targetBranches = ['7.x'];
@@ -65,14 +72,18 @@ describe('integration', () => {
65
72
  ]);
66
73
  });
67
74
  it('sends the correct http body when creating pull request', () => {
68
- expect(createPullRequestsMockCalls).toEqual([
69
- {
70
- base: '7.x',
71
- body: 'Backports the following commits to 7.x:\n - Add ❤️ emoji (5bf29b7d)',
72
- head: 'sqren:backport/7.x/commit-5bf29b7d',
73
- title: '[7.x] Add ❤️ emoji',
74
- },
75
- ]);
75
+ expect(createPullRequestsMockCalls).toMatchInlineSnapshot(`
76
+ Array [
77
+ Object {
78
+ "base": "7.x",
79
+ "body": "This is an automatic backport of commit 5bf29b7d847ea3dbde9280448f0f62ad0f22d3ad to 7.x.
80
+
81
+ Please refer to the [Backport tool documentation](https://github.com/sqren/backport) for additional information",
82
+ "head": "sqren:backport/7.x/commit-5bf29b7d",
83
+ "title": "[7.x] Add ❤️ emoji",
84
+ },
85
+ ]
86
+ `);
76
87
  });
77
88
  it('should not create new branches in origin (backport-org/integration-test)', async () => {
78
89
  const branches = await getBranches({
@@ -85,7 +96,7 @@ describe('integration', () => {
85
96
  it('should create branch in the fork (sqren/integration-test)', async () => {
86
97
  const branches = await getBranches({
87
98
  accessToken,
88
- repoOwner: USERNAME,
99
+ repoOwner: AUHTOR,
89
100
  repoName: REPO_NAME,
90
101
  });
91
102
  expect(branches.map((b) => b.name)).toEqual([
@@ -97,82 +108,75 @@ describe('integration', () => {
97
108
  it('should have cherry picked the correct commit', async () => {
98
109
  const branches = await getBranches({
99
110
  accessToken,
100
- repoOwner: USERNAME,
111
+ repoOwner: AUHTOR,
101
112
  repoName: REPO_NAME,
102
113
  });
103
114
  const sha = branches.find((branch) => branch.name === '7.x')?.commit.sha;
104
115
  expect(sha).toEqual('b68e4fcaaf4427fe1e902c718b3bb3f07b560fb5');
105
116
  });
106
117
  });
107
- // eslint-disable-next-line jest/no-disabled-tests
108
- describe.skip('when two commits are backported', () => {
109
- let createPullRequestsMockCalls;
110
- let res;
111
- let accessToken;
112
- beforeAll(async () => {
113
- accessToken = await (0, getDevAccessToken_1.getDevAccessToken)();
114
- await resetState(accessToken);
115
- createPullRequestsMockCalls = mockCreatePullRequest({
116
- number: 1337,
117
- html_url: 'myHtmlUrl',
118
- });
119
- const options = {
120
- githubApiBaseUrlV3: 'https://api.foo.com',
121
- ci: true,
122
- sha: '5bf29b7d847ea3dbde9280448f0f62ad0f22d3ad',
123
- username: USERNAME,
124
- accessToken,
125
- upstream: 'backport-org/integration-test',
126
- };
127
- const commits = await (0, getCommits_1.getCommits)(options);
128
- const targetBranches = ['7.x'];
129
- res = await (0, runSequentially_1.runSequentially)({ options, commits, targetBranches });
130
- });
131
- it('sends the correct http body when creating pull request', () => {
132
- expect(createPullRequestsMockCalls).toEqual([
133
- {
134
- base: '7.x',
135
- body: 'Backports the following commits to 7.x:\n - Add ❤️ emoji (5bf29b7d)',
136
- head: 'sqren:backport/7.x/commit-5bf29b7d',
137
- title: '[7.x] Add ❤️ emoji',
138
- },
139
- ]);
140
- });
141
- it('returns pull request', () => {
142
- expect(res).toEqual([
143
- { pullRequestUrl: 'myHtmlUrl', success: true, targetBranch: '6.0' },
144
- ]);
145
- });
146
- it('should not create new branches in origin (backport-org/integration-test)', async () => {
147
- const branches = await getBranches({
148
- accessToken,
149
- repoOwner: REPO_OWNER,
150
- repoName: REPO_NAME,
151
- });
152
- expect(branches.map((b) => b.name)).toEqual(['7.x', '* master']);
153
- });
154
- it('should create branch in the fork (sqren/integration-test)', async () => {
155
- const branches = await getBranches({
156
- accessToken,
157
- repoOwner: USERNAME,
158
- repoName: REPO_NAME,
159
- });
160
- expect(branches.map((b) => b.name)).toEqual([
161
- '7.x',
162
- 'backport/6.0/pr-85_commit-2e63475c',
163
- 'master',
164
- ]);
165
- });
166
- it('should have cherry picked the correct commit', async () => {
167
- const branches = await getBranches({
168
- accessToken,
169
- repoOwner: USERNAME,
170
- repoName: REPO_NAME,
171
- });
172
- const sha = branches.find((branch) => branch.name === '7.x')?.commit.sha;
173
- expect(sha).toEqual('foo');
174
- });
175
- });
118
+ // describe.skip('when two commits are backported', () => {
119
+ // let createPullRequestsMockCalls: unknown[];
120
+ // let res: Awaited<ReturnType<typeof runSequentially>>;
121
+ // let accessToken: string;
122
+ // beforeAll(async () => {
123
+ // accessToken = await getDevAccessToken();
124
+ // await resetState(accessToken);
125
+ // createPullRequestsMockCalls = mockCreatePullRequest({
126
+ // number: 1337,
127
+ // html_url: 'myHtmlUrl',
128
+ // });
129
+ // const options = {
130
+ // githubApiBaseUrlV3: 'https://api.foo.com',
131
+ // ci: true,
132
+ // sha: '5bf29b7d847ea3dbde9280448f0f62ad0f22d3ad',
133
+ // author: AUHTOR,
134
+ // accessToken,
135
+ // repoOwner: 'backport-org',
136
+ // reponame: 'integration-test',
137
+ // } as ValidConfigOptions;
138
+ // const commits = await getCommits(options);
139
+ // const targetBranches: string[] = ['7.x'];
140
+ // res = await runSequentially({ options, commits, targetBranches });
141
+ // });
142
+ // it('sends the correct http body when creating pull request', () => {
143
+ // expect(createPullRequestsMockCalls).toMatchInlineSnapshot();
144
+ // });
145
+ // it('returns pull request', () => {
146
+ // expect(res).toEqual([
147
+ // { pullRequestUrl: 'myHtmlUrl', success: true, targetBranch: '6.0' },
148
+ // ]);
149
+ // });
150
+ // it('should not create new branches in origin (backport-org/integration-test)', async () => {
151
+ // const branches = await getBranches({
152
+ // accessToken,
153
+ // repoOwner: REPO_OWNER,
154
+ // repoName: REPO_NAME,
155
+ // });
156
+ // expect(branches.map((b) => b.name)).toEqual(['7.x', '* master']);
157
+ // });
158
+ // it('should create branch in the fork (sqren/integration-test)', async () => {
159
+ // const branches = await getBranches({
160
+ // accessToken,
161
+ // repoOwner: AUHTOR,
162
+ // repoName: REPO_NAME,
163
+ // });
164
+ // expect(branches.map((b) => b.name)).toEqual([
165
+ // '7.x',
166
+ // 'backport/6.0/pr-85_commit-2e63475c',
167
+ // 'master',
168
+ // ]);
169
+ // });
170
+ // it('should have cherry picked the correct commit', async () => {
171
+ // const branches = await getBranches({
172
+ // accessToken,
173
+ // repoOwner: AUHTOR,
174
+ // repoName: REPO_NAME,
175
+ // });
176
+ // const sha = branches.find((branch) => branch.name === '7.x')?.commit.sha;
177
+ // expect(sha).toEqual('foo');
178
+ // });
179
+ // });
176
180
  describe('when disabling fork mode', () => {
177
181
  let res;
178
182
  let accessToken;
@@ -185,12 +189,13 @@ describe('integration', () => {
185
189
  html_url: 'myHtmlUrl',
186
190
  });
187
191
  const options = await (0, options_1.getOptions)([], {
192
+ author: AUHTOR,
188
193
  githubApiBaseUrlV3: 'https://api.foo.com',
189
194
  ci: true,
190
195
  sha: '5bf29b7d847ea3dbde9280448f0f62ad0f22d3ad',
191
- username: USERNAME,
192
196
  accessToken,
193
- upstream: 'backport-org/integration-test',
197
+ repoOwner: 'backport-org',
198
+ repoName: 'integration-test',
194
199
  fork: false,
195
200
  });
196
201
  const commits = await (0, getCommits_1.getCommits)(options);
@@ -198,14 +203,18 @@ describe('integration', () => {
198
203
  res = await (0, runSequentially_1.runSequentially)({ options, commits, targetBranches });
199
204
  });
200
205
  it('sends the correct http body when creating pull request', () => {
201
- expect(createPullRequestsMockCalls).toEqual([
202
- {
203
- base: '7.x',
204
- body: 'Backports the following commits to 7.x:\n - Add ❤️ emoji (5bf29b7d)',
205
- head: 'backport-org:backport/7.x/commit-5bf29b7d',
206
- title: '[7.x] Add ❤️ emoji',
207
- },
208
- ]);
206
+ expect(createPullRequestsMockCalls).toMatchInlineSnapshot(`
207
+ Array [
208
+ Object {
209
+ "base": "7.x",
210
+ "body": "This is an automatic backport of commit 5bf29b7d847ea3dbde9280448f0f62ad0f22d3ad to 7.x.
211
+
212
+ Please refer to the [Backport tool documentation](https://github.com/sqren/backport) for additional information",
213
+ "head": "backport-org:backport/7.x/commit-5bf29b7d",
214
+ "title": "[7.x] Add ❤️ emoji",
215
+ },
216
+ ]
217
+ `);
209
218
  });
210
219
  it('returns pull request', () => {
211
220
  expect(res).toEqual([
@@ -233,7 +242,7 @@ describe('integration', () => {
233
242
  it('should NOT create branch in the fork (sqren/integration-test)', async () => {
234
243
  const branches = await getBranches({
235
244
  accessToken,
236
- repoOwner: USERNAME,
245
+ repoOwner: AUHTOR,
237
246
  repoName: REPO_NAME,
238
247
  });
239
248
  expect(branches.map((b) => b.name)).toEqual(['7.x', 'master']);
@@ -253,7 +262,7 @@ function mockCreatePullRequest(response) {
253
262
  const scope = (0, nock_1.default)('https://api.foo.com', { allowUnmocked: true })
254
263
  .post('/repos/backport-org/integration-test/pulls')
255
264
  .reply(200, response);
256
- return (0, nockHelpers_1.getNockCallsForScope)(scope);
265
+ return (0, nockHelpers_1.listenForCallsToNockScope)(scope);
257
266
  }
258
267
  async function getBranches({ accessToken, repoOwner, repoName, }) {
259
268
  // console.log(`fetch branches for ${repoOwner}`);
@@ -300,7 +309,7 @@ async function resetState(accessToken) {
300
309
  });
301
310
  await deleteBranch({
302
311
  accessToken,
303
- repoOwner: USERNAME,
312
+ repoOwner: AUHTOR,
304
313
  repoName: REPO_NAME,
305
314
  branchName: BRANCH_NAME,
306
315
  });
@@ -0,0 +1,5 @@
1
+ import { ConfigFileOptions } from '../options/ConfigOptions';
2
+ export declare function mockConfigFiles({ projectConfig, globalConfig, }: {
3
+ projectConfig: ConfigFileOptions;
4
+ globalConfig: ConfigFileOptions;
5
+ }): void;
@@ -0,0 +1,37 @@
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
+ exports.mockConfigFiles = void 0;
23
+ const fs = __importStar(require("../services/fs-promisified"));
24
+ function mockConfigFiles({ projectConfig, globalConfig, }) {
25
+ jest
26
+ .spyOn(fs, 'readFile')
27
+ //@ts-expect-error
28
+ .mockImplementation(async (filepath) => {
29
+ if (filepath === '/path/to/project/config') {
30
+ return JSON.stringify(projectConfig);
31
+ }
32
+ if (filepath.endsWith('.backport/config.json')) {
33
+ return JSON.stringify(globalConfig);
34
+ }
35
+ });
36
+ }
37
+ exports.mockConfigFiles = mockConfigFiles;
@@ -13,4 +13,4 @@ export declare function mockGqlRequest<T>({ name, statusCode, body, headers, api
13
13
  query: string;
14
14
  variables: string;
15
15
  }[];
16
- export declare function getNockCallsForScope(scope: nock.Scope): unknown[];
16
+ export declare function listenForCallsToNockScope(scope: nock.Scope): unknown[];
@@ -3,7 +3,7 @@ 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.getNockCallsForScope = exports.mockGqlRequest = void 0;
6
+ exports.listenForCallsToNockScope = exports.mockGqlRequest = void 0;
7
7
  const url_1 = require("url");
8
8
  const graphql_tag_1 = __importDefault(require("graphql-tag"));
9
9
  const nock_1 = __importDefault(require("nock"));
@@ -14,7 +14,7 @@ function mockGqlRequest({ name, statusCode, body, headers, apiBaseUrl, }) {
14
14
  const scope = (0, nock_1.default)(origin)
15
15
  .post(pathname, (body) => getGqlName(body.query) === name)
16
16
  .reply(statusCode, body, headers);
17
- return getNockCallsForScope(scope);
17
+ return listenForCallsToNockScope(scope);
18
18
  }
19
19
  exports.mockGqlRequest = mockGqlRequest;
20
20
  function getGqlName(query) {
@@ -24,11 +24,11 @@ function getGqlName(query) {
24
24
  // @ts-expect-error
25
25
  return obj.definitions[0].name.value;
26
26
  }
27
- function getNockCallsForScope(scope) {
27
+ function listenForCallsToNockScope(scope) {
28
28
  const calls = [];
29
29
  scope.on('request', (req, interceptor, body) => {
30
30
  calls.push(JSON.parse(body));
31
31
  });
32
32
  return calls;
33
33
  }
34
- exports.getNockCallsForScope = getNockCallsForScope;
34
+ exports.listenForCallsToNockScope = listenForCallsToNockScope;
@@ -18,7 +18,7 @@ async function getDevAccessToken() {
18
18
  return accessToken.trim();
19
19
  }
20
20
  catch (e) {
21
- throw new Error(`Missing username or accessToken in "${accessTokenFile}"`);
21
+ throw new Error(`Missing accessToken in "${accessTokenFile}"`);
22
22
  }
23
23
  }
24
24
  exports.getDevAccessToken = getDevAccessToken;
@@ -4,34 +4,6 @@
4
4
  * It will be run once per test file
5
5
  */
6
6
  /* eslint-disable @typescript-eslint/no-empty-function */
7
- jest.mock('../../services/fs-promisified', () => {
8
- return {
9
- writeFile: jest.fn(async () => 'fs.writeFile mock value'),
10
- readFile: jest.fn(async (filepath) => {
11
- // mock project config
12
- if (filepath === '/path/to/project/config') {
13
- return JSON.stringify({
14
- upstream: 'backport-org/backport-demo',
15
- targetBranchChoices: ['6.0', '5.9'],
16
- });
17
- }
18
- // mock global config
19
- if (filepath.endsWith('/.backport/config.json')) {
20
- return JSON.stringify({
21
- username: 'sqren',
22
- accessToken: 'myAccessToken',
23
- });
24
- }
25
- throw new Error(`Unknown filepath: "${filepath}"`);
26
- }),
27
- stat: jest.fn(async () => {
28
- return {
29
- isDirectory: () => { },
30
- };
31
- }),
32
- chmod: jest.fn(async () => 'fs.chmod mock value'),
33
- };
34
- });
35
7
  jest.mock('find-up', () => {
36
8
  return jest.fn(async () => '/path/to/project/config');
37
9
  });
@@ -56,9 +28,9 @@ jest.mock('ora', () => {
56
28
  jest.mock('../../services/logger', () => {
57
29
  const spy = jest.fn();
58
30
  return {
31
+ redactAccessToken: jest.fn((str) => str),
59
32
  consoleLog: jest.fn(),
60
33
  updateLogger: jest.fn(),
61
- redact: () => { },
62
34
  logger: {
63
35
  spy: spy,
64
36
  info: (msg, meta) => spy(`[INFO] ${msg}`, meta),
@@ -60,11 +60,12 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
60
60
  });
61
61
  describe('when commit has a pull request reference', () => {
62
62
  let res;
63
+ let createPullRequestCalls;
63
64
  beforeEach(async () => {
64
65
  const options = {
65
66
  assignees: [],
67
+ authenticatedUsername: 'sqren_authenticated',
66
68
  fork: true,
67
- githubApiBaseUrlV3: 'https://api.github.com',
68
69
  prDescription: 'myPrSuffix',
69
70
  prTitle: '[{targetBranch}] {commitMessages}',
70
71
  repoName: 'kibana',
@@ -73,7 +74,7 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
73
74
  sourceBranch: 'myDefaultSourceBranch',
74
75
  sourcePRLabels: [],
75
76
  targetPRLabels: ['backport'],
76
- username: 'sqren',
77
+ author: 'sqren',
77
78
  };
78
79
  const commits = [
79
80
  {
@@ -94,13 +95,9 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
94
95
  },
95
96
  ];
96
97
  const scope = (0, nock_1.default)('https://api.github.com')
97
- .post('/repos/elastic/kibana/pulls', {
98
- title: '[6.x] My original commit message (#1000) | My other commit message (#2000)',
99
- head: 'sqren:backport/6.x/pr-1000_pr-2000',
100
- base: '6.x',
101
- body: 'Backports the following commits to 6.x:\n - #1000\n - #2000\n\nmyPrSuffix',
102
- })
98
+ .post('/repos/elastic/kibana/pulls')
103
99
  .reply(200, { number: 1337, html_url: 'myHtmlUrl' });
100
+ createPullRequestCalls = (0, nockHelpers_1.listenForCallsToNockScope)(scope);
104
101
  res = await (0, cherrypickAndCreateTargetPullRequest_1.cherrypickAndCreateTargetPullRequest)({
105
102
  options,
106
103
  commits,
@@ -109,6 +106,24 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
109
106
  scope.done();
110
107
  nock_1.default.cleanAll();
111
108
  });
109
+ it('creates the pull request with multiple PR references', () => {
110
+ expect(createPullRequestCalls).toMatchInlineSnapshot(`
111
+ Array [
112
+ Object {
113
+ "base": "6.x",
114
+ "body": "This is an automatic backport of the following commits to 6.x:
115
+ - #1000
116
+ - #2000
117
+
118
+ Please refer to the [Backport tool documentation](https://github.com/sqren/backport) for additional information
119
+
120
+ myPrSuffix",
121
+ "head": "sqren_authenticated:backport/6.x/pr-1000_pr-2000",
122
+ "title": "[6.x] My original commit message (#1000) | My other commit message (#2000)",
123
+ },
124
+ ]
125
+ `);
126
+ });
112
127
  it('returns the expected response', () => {
113
128
  expect(res).toEqual({ didUpdate: false, url: 'myHtmlUrl', number: 1337 });
114
129
  });
@@ -130,7 +145,7 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
130
145
  "Pulling latest changes",
131
146
  "Cherry-picking: My original commit message (#1000)",
132
147
  "Cherry-picking: My other commit message (#2000)",
133
- "Pushing branch \\"sqren:backport/6.x/pr-1000_pr-2000\\"",
148
+ "Pushing branch \\"sqren_authenticated:backport/6.x/pr-1000_pr-2000\\"",
134
149
  undefined,
135
150
  "Creating pull request",
136
151
  "Adding labels: backport",
@@ -140,18 +155,19 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
140
155
  });
141
156
  describe('when commit does not have a pull request reference', () => {
142
157
  let res;
158
+ let createPullRequestCalls;
143
159
  beforeEach(async () => {
144
160
  const options = {
145
161
  assignees: [],
162
+ authenticatedUsername: 'sqren_authenticated',
146
163
  fork: true,
147
- githubApiBaseUrlV3: 'https://api.github.com',
148
164
  prTitle: '[{targetBranch}] {commitMessages}',
149
165
  repoName: 'kibana',
150
166
  repoOwner: 'elastic',
151
167
  reviewers: [],
152
168
  sourcePRLabels: [],
153
169
  targetPRLabels: ['backport'],
154
- username: 'sqren',
170
+ author: 'sqren',
155
171
  };
156
172
  const commits = [
157
173
  {
@@ -163,13 +179,9 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
163
179
  },
164
180
  ];
165
181
  const scope = (0, nock_1.default)('https://api.github.com')
166
- .post('/repos/elastic/kibana/pulls', {
167
- title: '[6.x] My original commit message',
168
- head: 'sqren:backport/6.x/commit-mySha',
169
- base: '6.x',
170
- body: 'Backports the following commits to 6.x:\n - My original commit message (mySha)',
171
- })
182
+ .post('/repos/elastic/kibana/pulls')
172
183
  .reply(200, { number: 1337, html_url: 'myHtmlUrl' });
184
+ createPullRequestCalls = (0, nockHelpers_1.listenForCallsToNockScope)(scope);
173
185
  res = await (0, cherrypickAndCreateTargetPullRequest_1.cherrypickAndCreateTargetPullRequest)({
174
186
  options,
175
187
  commits,
@@ -178,6 +190,20 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
178
190
  scope.done();
179
191
  nock_1.default.cleanAll();
180
192
  });
193
+ it('creates the pull request with commit reference', () => {
194
+ expect(createPullRequestCalls).toMatchInlineSnapshot(`
195
+ Array [
196
+ Object {
197
+ "base": "6.x",
198
+ "body": "This is an automatic backport of commit mySha to 6.x.
199
+
200
+ Please refer to the [Backport tool documentation](https://github.com/sqren/backport) for additional information",
201
+ "head": "sqren_authenticated:backport/6.x/commit-mySha",
202
+ "title": "[6.x] My original commit message",
203
+ },
204
+ ]
205
+ `);
206
+ });
181
207
  it('returns the expected response', () => {
182
208
  expect(res).toEqual({ didUpdate: false, url: 'myHtmlUrl', number: 1337 });
183
209
  });
@@ -187,12 +213,14 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
187
213
  let promptSpy;
188
214
  let execSpy;
189
215
  let commitsByAuthorCalls;
216
+ let createPullRequestCalls;
190
217
  beforeEach(async () => {
191
218
  // spies
192
219
  promptSpy = jest.spyOn(prompts, 'confirmPrompt').mockResolvedValue(true);
193
220
  execSpy = setupExecSpyForCherryPick();
194
221
  const options = {
195
222
  assignees: [],
223
+ authenticatedUsername: 'sqren_authenticated',
196
224
  fork: true,
197
225
  githubApiBaseUrlV4: 'http://localhost/graphql',
198
226
  prTitle: '[{targetBranch}] {commitMessages}',
@@ -202,16 +230,12 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
202
230
  sourceBranch: 'myDefaultSourceBranch',
203
231
  sourcePRLabels: [],
204
232
  targetPRLabels: ['backport'],
205
- username: 'sqren',
233
+ author: 'sqren',
206
234
  };
207
235
  const scope = (0, nock_1.default)('https://api.github.com')
208
- .post('/repos/elastic/kibana/pulls', {
209
- title: '[6.x] My original commit message',
210
- head: 'sqren:backport/6.x/commit-mySha',
211
- base: '6.x',
212
- body: 'Backports the following commits to 6.x:\n - My original commit message (mySha)',
213
- })
214
- .reply(200, { html_url: 'myHtmlUrl', number: 1337 });
236
+ .post('/repos/elastic/kibana/pulls')
237
+ .reply(200, { number: 1337, html_url: 'myHtmlUrl' });
238
+ createPullRequestCalls = (0, nockHelpers_1.listenForCallsToNockScope)(scope);
215
239
  commitsByAuthorCalls =
216
240
  (0, nockHelpers_1.mockGqlRequest)({
217
241
  name: 'CommitsByAuthor',
@@ -234,7 +258,21 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
234
258
  scope.done();
235
259
  nock_1.default.cleanAll();
236
260
  });
237
- it('creates pull request', () => {
261
+ it('creates the pull request with commit reference', () => {
262
+ expect(createPullRequestCalls).toMatchInlineSnapshot(`
263
+ Array [
264
+ Object {
265
+ "base": "6.x",
266
+ "body": "This is an automatic backport of commit mySha to 6.x.
267
+
268
+ Please refer to the [Backport tool documentation](https://github.com/sqren/backport) for additional information",
269
+ "head": "sqren_authenticated:backport/6.x/commit-mySha",
270
+ "title": "[6.x] My original commit message",
271
+ },
272
+ ]
273
+ `);
274
+ });
275
+ it('returns the expected response', () => {
238
276
  expect(res).toEqual({ didUpdate: false, url: 'myHtmlUrl', number: 1337 });
239
277
  });
240
278
  it('shows the right prompts', () => {
@@ -272,6 +310,7 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
272
310
  Object {
273
311
  "authorId": null,
274
312
  "commitPath": "conflicting-file.txt",
313
+ "maxNumber": 10,
275
314
  "repoName": "kibana",
276
315
  "repoOwner": "elastic",
277
316
  "sourceBranch": "myDefaultSourceBranch",
@@ -288,7 +327,7 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
288
327
  "Pulling latest changes",
289
328
  "Cherry-picking: My original commit message",
290
329
  "Finalizing cherrypick",
291
- "Pushing branch \\"sqren:backport/6.x/commit-mySha\\"",
330
+ "Pushing branch \\"sqren_authenticated:backport/6.x/commit-mySha\\"",
292
331
  undefined,
293
332
  "Creating pull request",
294
333
  "Adding labels: backport",
@@ -517,7 +556,7 @@ function setupExecSpyForCherryPick() {
517
556
  return { stderr: '', stdout: '' };
518
557
  }
519
558
  // cherrypick
520
- if (cmd === 'git cherry-pick mySha') {
559
+ if (cmd === 'git cherry-pick -x mySha') {
521
560
  throw new ExecError_1.ExecError({ cmd });
522
561
  }
523
562
  // getIsCommitInBranch