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,28 +25,32 @@ Object.defineProperty(exports, "__esModule", { value: true });
25
25
  const child_process_1 = require("child_process");
26
26
  const path_1 = __importDefault(require("path"));
27
27
  const strip_ansi_1 = __importDefault(require("strip-ansi"));
28
- const child_process_promisified_1 = require("./services/child-process-promisified");
29
- const getDevAccessToken_1 = require("./test/private/getDevAccessToken");
30
- const sandbox_1 = require("./test/sandbox");
31
- const packageVersion = __importStar(require("./utils/packageVersion"));
32
- const TIMEOUT_IN_SECONDS = 10;
33
- jest.setTimeout(15000);
28
+ const child_process_promisified_1 = require("../../services/child-process-promisified");
29
+ const packageVersion = __importStar(require("../../utils/packageVersion"));
30
+ const getDevAccessToken_1 = require("../private/getDevAccessToken");
31
+ const sandbox_1 = require("../sandbox");
32
+ const TIMEOUT_IN_SECONDS = 15;
33
+ jest.setTimeout(TIMEOUT_IN_SECONDS * 1000);
34
34
  const devAccessToken = (0, getDevAccessToken_1.getDevAccessToken)();
35
35
  describe('inquirer cli', () => {
36
36
  it('--version', async () => {
37
- const res = await runBackportAsync([`--version`]);
38
- expect(res).toContain(process.env.npm_package_version);
37
+ const res = await runBackportViaCli([`--version`], {
38
+ showLoadingSpinner: true,
39
+ });
40
+ expect(res).toEqual(process.env.npm_package_version);
39
41
  });
40
42
  it('-v', async () => {
41
- const res = await runBackportAsync([`-v`]);
42
- expect(res).toContain(process.env.npm_package_version);
43
+ const res = await runBackportViaCli([`-v`], {
44
+ showLoadingSpinner: true,
45
+ });
46
+ expect(res).toEqual(process.env.npm_package_version);
43
47
  });
44
48
  it('PACKAGE_VERSION should match', async () => {
45
49
  // @ts-expect-error
46
50
  expect(packageVersion.UNMOCKED_PACKAGE_VERSION).toBe(process.env.npm_package_version);
47
51
  });
48
52
  it('--help', async () => {
49
- const res = await runBackportAsync([`--help`]);
53
+ const res = await runBackportViaCli([`--help`]);
50
54
  expect(res).toMatchInlineSnapshot(`
51
55
  "entrypoint.cli.ts [args]
52
56
  Options:
@@ -67,6 +71,7 @@ describe('inquirer cli', () => {
67
71
  --dir Location where the temporary repository will be stored
68
72
  [string]
69
73
  --details Show details about each commit [boolean]
74
+ --dryRun Run backport locally without pushing to Github [boolean]
70
75
  --editor Editor to be opened during conflict resolution [string]
71
76
  --skipRemoteConfig Use local .backportrc.json config instead of loading from
72
77
  Github [boolean]
@@ -109,7 +114,7 @@ describe('inquirer cli', () => {
109
114
  `);
110
115
  });
111
116
  it('should return error when branch is missing', async () => {
112
- const res = await runBackportAsync([
117
+ const res = await runBackportViaCli([
113
118
  '--skip-remote-config',
114
119
  '--repo-owner',
115
120
  'backport-org',
@@ -128,7 +133,7 @@ describe('inquirer cli', () => {
128
133
  await (0, sandbox_1.resetSandbox)(sandboxPath);
129
134
  await (0, child_process_promisified_1.exec)(`git init`, { cwd: sandboxPath });
130
135
  await (0, child_process_promisified_1.exec)(`git remote add origin git@github.com:backport-org/backport-e2e.git`, { cwd: sandboxPath });
131
- const res = await runBackportAsync(['--accessToken', devAccessToken], {
136
+ const res = await runBackportViaCli(['--accessToken', devAccessToken], {
132
137
  cwd: sandboxPath,
133
138
  waitForString: 'Select commit',
134
139
  });
@@ -147,7 +152,7 @@ describe('inquirer cli', () => {
147
152
  `);
148
153
  });
149
154
  it('should return error when access token is invalid', async () => {
150
- const res = await runBackportAsync([
155
+ const res = await runBackportViaCli([
151
156
  '--branch',
152
157
  'foo',
153
158
  '--repo-owner',
@@ -160,7 +165,7 @@ describe('inquirer cli', () => {
160
165
  expect(res).toContain('Please check your access token and make sure it is valid');
161
166
  });
162
167
  it(`should return error when repo doesn't exist`, async () => {
163
- const res = await runBackportAsync([
168
+ const res = await runBackportViaCli([
164
169
  '--branch',
165
170
  'foo',
166
171
  '--repo-owner',
@@ -175,8 +180,7 @@ describe('inquirer cli', () => {
175
180
  expect(res).toMatchInlineSnapshot(`"The repository \\"foo/bar\\" doesn't exist"`);
176
181
  });
177
182
  it(`should list commits from master`, async () => {
178
- jest.setTimeout(TIMEOUT_IN_SECONDS * 1000 * 1.1);
179
- const output = await runBackportAsync([
183
+ const output = await runBackportViaCli([
180
184
  '--branch',
181
185
  'foo',
182
186
  '--repo-owner',
@@ -201,8 +205,7 @@ describe('inquirer cli', () => {
201
205
  `);
202
206
  });
203
207
  it(`should filter commits by "since" and "until"`, async () => {
204
- jest.setTimeout(TIMEOUT_IN_SECONDS * 1000 * 1.1);
205
- const output = await runBackportAsync([
208
+ const output = await runBackportViaCli([
206
209
  '--branch',
207
210
  'foo',
208
211
  '--repo-owner',
@@ -225,7 +228,7 @@ describe('inquirer cli', () => {
225
228
  `);
226
229
  });
227
230
  it(`should list commits from 7.x`, async () => {
228
- const output = await runBackportAsync([
231
+ const output = await runBackportViaCli([
229
232
  '--branch',
230
233
  'foo',
231
234
  '--repo-owner',
@@ -251,10 +254,64 @@ describe('inquirer cli', () => {
251
254
  6. Bump to 8.0.0"
252
255
  `);
253
256
  });
257
+ describe('repo: repo-with-backportrc-removed (missing .backportrc.json config file)', () => {
258
+ it('should list commits', async () => {
259
+ const output = await runBackportViaCli([
260
+ '--branch',
261
+ 'foo',
262
+ '--repo',
263
+ 'backport-org/repo-with-backportrc-removed',
264
+ '--accessToken',
265
+ devAccessToken,
266
+ ], { waitForString: 'Select commit' });
267
+ expect(output).toMatchInlineSnapshot(`
268
+ "? Select commit (Use arrow keys)
269
+ ❯ 1. Rename README.me to README.md
270
+ 2. Merge pull request #1 from backport-org/add-readme
271
+ 3. Create README.me
272
+ 4. Delete .backportrc.json
273
+ 5. Create .backportrc.json
274
+ 6. Delete .backportrc.json
275
+ 7. Create .backportrc.json"
276
+ `);
277
+ });
278
+ it('should attempt to backport by PR', async () => {
279
+ const output = await runBackportViaCli([
280
+ '--branch',
281
+ 'foo',
282
+ '--repo',
283
+ 'backport-org/repo-with-backportrc-removed',
284
+ '--pr',
285
+ '1',
286
+ '--accessToken',
287
+ devAccessToken,
288
+ ], { waitForString: "is invalid or doesn't exist" });
289
+ expect(output).toMatchInlineSnapshot(`
290
+ "
291
+ Backporting to foo:
292
+ The branch \\"foo\\" is invalid or doesn't exist"
293
+ `);
294
+ });
295
+ it('should attempt to backport by commit sha', async () => {
296
+ const output = await runBackportViaCli([
297
+ '--branch',
298
+ 'foo',
299
+ '--repo',
300
+ 'backport-org/repo-with-backportrc-removed',
301
+ '--sha',
302
+ 'be59df6912a550c8cb49ba3e18be3e512f3d608c',
303
+ '--accessToken',
304
+ devAccessToken,
305
+ ], { waitForString: `Backporting to foo:` });
306
+ expect(output).toMatchInlineSnapshot(`
307
+ "
308
+ Backporting to foo:"
309
+ `);
310
+ });
311
+ });
254
312
  describe('repo: different-merge-strategies', () => {
255
313
  it('list all commits regardless how they were merged', async () => {
256
- jest.setTimeout(TIMEOUT_IN_SECONDS * 1000 * 1.1);
257
- const output = await runBackportAsync([
314
+ const output = await runBackportViaCli([
258
315
  '--branch',
259
316
  'foo',
260
317
  '--repo-owner',
@@ -263,21 +320,76 @@ describe('inquirer cli', () => {
263
320
  'different-merge-strategies',
264
321
  '--accessToken',
265
322
  devAccessToken,
323
+ '-n',
324
+ '20',
266
325
  ], { waitForString: 'Select commit' });
267
326
  expect(output).toMatchInlineSnapshot(`
268
327
  "? Select commit (Use arrow keys)
269
- ❯ 1. Using squash to merge commits (#3) 7.x
270
- 2. Rebase strategy: Second commit 7.x
271
- 3. Rebase strategy: First commit
272
- 4. Merge pull request #1 from backport-org/merge-strategy
273
- 5. Merge strategy: Second commit
274
- 6. Merge strategy: First commit
275
- 7. Initial commit"
328
+ ❯ 1. Merge pull request #9 from backport-org/many-merge-commits
329
+ 2. Merge strategy: Eighth of many merges
330
+ 3. Merge strategy: Seventh of many merges
331
+ 4. Merge strategy: Sixth of many merges
332
+ 5. Merge strategy: Fifth of many merges
333
+ 6. Merge strategy: Fourth of many merges
334
+ 7. Merge strategy: Third of many merges
335
+ 8. Merge strategy: Second of many merges
336
+ 9. Merge strategy: First of many merges
337
+ 10.Using squash to merge commits (#3) 7.x
338
+ 11.Rebase strategy: Second commit 7.x
339
+ 12.Rebase strategy: First commit
340
+ 13.Merge pull request #1 from backport-org/merge-strategy
341
+ 14.Merge strategy: Second commit
342
+ 15.Merge strategy: First commit
343
+ 16.Initial commit"
344
+ `);
345
+ });
346
+ });
347
+ describe('repo: test-that-repo-can-be-cloned', () => {
348
+ let sandboxPath;
349
+ beforeAll(async () => {
350
+ sandboxPath = (0, sandbox_1.getSandboxPath)({
351
+ filename: __filename,
352
+ specname: 'test-cloning',
353
+ });
354
+ await (0, sandbox_1.resetSandbox)(sandboxPath);
355
+ });
356
+ function run() {
357
+ return runBackportViaCli([
358
+ '--repo',
359
+ 'backport-org/test-that-repo-can-be-cloned',
360
+ '--branch',
361
+ 'foo',
362
+ '--pr',
363
+ '1',
364
+ '--dir',
365
+ sandboxPath,
366
+ '--dry-run',
367
+ '--accessToken',
368
+ devAccessToken,
369
+ ], { showLoadingSpinner: true, waitForString: 'Backporting to foo:' });
370
+ }
371
+ it('clones the repo on the very first run', async () => {
372
+ const output = await run();
373
+ expect(output).toContain('Cloning repository from github.com');
374
+ expect(output).toMatchInlineSnapshot(`
375
+ "- Initializing...
376
+ ? Select pull request Beginning of a beautiful repo (#1)
377
+ ✔ 100% Cloning repository from github.com (one-time operation)
378
+ Backporting to foo:"
379
+ `);
380
+ });
381
+ it('does not clone the repo on subsequent runs', async () => {
382
+ const output = await run();
383
+ expect(output).not.toContain('Cloning repository from github.com');
384
+ expect(output).toMatchInlineSnapshot(`
385
+ "- Initializing...
386
+ ? Select pull request Beginning of a beautiful repo (#1)
387
+ Backporting to foo:"
276
388
  `);
277
389
  });
278
390
  });
279
391
  });
280
- function runBackportAsync(cliArgs, { waitForString, cwd, } = {}) {
392
+ function runBackportViaCli(cliArgs, { showLoadingSpinner, waitForString, cwd, } = {}) {
281
393
  const tsNodeBinary = path_1.default.resolve('./node_modules/.bin/ts-node');
282
394
  const entrypointFile = path_1.default.resolve('./src/entrypoint.cli.ts');
283
395
  const proc = (0, child_process_1.spawn)(tsNodeBinary, [
@@ -296,20 +408,22 @@ function runBackportAsync(cliArgs, { waitForString, cwd, } = {}) {
296
408
  }, TIMEOUT_IN_SECONDS * 1000);
297
409
  proc.stdout.on('data', (chunk) => {
298
410
  data += chunk;
299
- const output = data.toString();
411
+ const rawOutput = data.toString();
412
+ // remove ansi codes and whitespace
413
+ const output = (0, strip_ansi_1.default)(rawOutput).replace(/\s+$/gm, '');
300
414
  if (!waitForString || output.includes(waitForString)) {
301
415
  clearTimeout(timeout);
302
- // remove ansi codes and whitespace
303
- const strippedOutput = (0, strip_ansi_1.default)(output).replace(/\s+$/gm, '');
304
- resolve(strippedOutput);
416
+ resolve(output);
305
417
  }
306
418
  });
307
- // for debugging only
308
- // proc.stderr.on('data', (chunk) => {
309
- // console.log('stderr', chunk.toString());
310
- // });
419
+ // ora (loading spinner) is redirected to stderr
420
+ if (showLoadingSpinner) {
421
+ proc.stderr.on('data', (chunk) => {
422
+ data += chunk;
423
+ });
424
+ }
311
425
  proc.on('error', (err) => {
312
- reject(`runBackportAsync failed with: ${err}`);
426
+ reject(`runBackportViaCli failed with: ${err}`);
313
427
  });
314
428
  });
315
429
  // kill child process
@@ -0,0 +1,363 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const rest_1 = require("@octokit/rest");
4
+ const entrypoint_module_1 = require("../../entrypoint.module");
5
+ const commitFormatters_1 = require("../../services/github/commitFormatters");
6
+ const getDevAccessToken_1 = require("../private/getDevAccessToken");
7
+ const sandbox_1 = require("../sandbox");
8
+ jest.unmock('find-up');
9
+ jest.setTimeout(15000);
10
+ const accessToken = (0, getDevAccessToken_1.getDevAccessToken)();
11
+ const octokit = new rest_1.Octokit({ auth: accessToken });
12
+ const sandboxPath = (0, sandbox_1.getSandboxPath)({ filename: __filename });
13
+ // repo
14
+ const REPO_OWNER = 'backport-org';
15
+ const REPO_NAME = 'integration-test';
16
+ const AUTHOR = 'sqren';
17
+ // commit 1
18
+ const COMMIT_SHA_1 = '5bf29b7d847ea3dbde9280448f0f62ad0f22d3ad';
19
+ const BRANCH_WITH_ONE_COMMIT = `backport/7.x/commit-${(0, commitFormatters_1.getShortSha)(COMMIT_SHA_1)}`;
20
+ // commit 2
21
+ const COMMIT_SHA_2 = '59d6ff1ca90a4ce210c0a4f0e159214875c19d60';
22
+ const BRANCH_WITH_TWO_COMMITS = `backport/7.x/commit-${(0, commitFormatters_1.getShortSha)(COMMIT_SHA_1)}_commit-${(0, commitFormatters_1.getShortSha)(COMMIT_SHA_2)}`;
23
+ describe('backport e2e', () => {
24
+ describe('when a single commit is backported', () => {
25
+ let res;
26
+ let pullRequestResponse;
27
+ beforeAll(async () => {
28
+ await resetState(accessToken);
29
+ res = await (0, entrypoint_module_1.backportRun)({
30
+ dir: sandboxPath,
31
+ accessToken,
32
+ repoOwner: 'backport-org',
33
+ repoName: 'integration-test',
34
+ sha: COMMIT_SHA_1,
35
+ targetBranches: ['7.x'],
36
+ });
37
+ // @ts-expect-error
38
+ const pullRequestNumber = res.results[0].pullRequestNumber;
39
+ pullRequestResponse = await octokit.pulls.get({
40
+ owner: REPO_OWNER,
41
+ repo: REPO_NAME,
42
+ pull_number: pullRequestNumber,
43
+ });
44
+ });
45
+ it('returns the backport result', () => {
46
+ expect(res).toEqual({
47
+ commits: [
48
+ {
49
+ expectedTargetPullRequests: [],
50
+ sourceBranch: 'master',
51
+ sourceCommit: {
52
+ committedDate: '2020-08-15T10:37:41Z',
53
+ message: 'Add ❤️ emoji',
54
+ sha: COMMIT_SHA_1,
55
+ },
56
+ sourcePullRequest: undefined,
57
+ },
58
+ ],
59
+ results: [
60
+ {
61
+ didUpdate: false,
62
+ pullRequestNumber: expect.any(Number),
63
+ pullRequestUrl: expect.stringContaining('https://github.com/backport-org/integration-test/pull/'),
64
+ status: 'success',
65
+ targetBranch: '7.x',
66
+ },
67
+ ],
68
+ status: 'success',
69
+ });
70
+ });
71
+ it('pull request: status code', async () => {
72
+ expect(pullRequestResponse.status).toEqual(200);
73
+ });
74
+ it('pull request: title', async () => {
75
+ expect(pullRequestResponse.data.title).toEqual('[7.x] Add ❤️ emoji');
76
+ });
77
+ it('pull request: body', async () => {
78
+ expect(pullRequestResponse.data.body).toMatchInlineSnapshot(`
79
+ "# Backport
80
+
81
+ This will backport the following commits from \`master\` to \`7.x\`:
82
+ - Add ❤️ emoji (5bf29b7d)
83
+
84
+ <!--- Backport version: 1.2.3-mocked -->
85
+
86
+ ### Questions ?
87
+ Please refer to the [Backport tool documentation](https://github.com/sqren/backport)"
88
+ `);
89
+ });
90
+ it('pull request: head branch is in fork repo', async () => {
91
+ expect(pullRequestResponse.data.head.label).toEqual(`sqren:${BRANCH_WITH_ONE_COMMIT}`);
92
+ });
93
+ it('pull request: base branch', async () => {
94
+ expect(pullRequestResponse.data.base.label).toEqual('backport-org:7.x');
95
+ });
96
+ it('does not create any new branches in origin (backport-org/integration-test)', async () => {
97
+ const branches = await getBranchesOnGithub({
98
+ accessToken,
99
+ repoOwner: REPO_OWNER,
100
+ repoName: REPO_NAME,
101
+ });
102
+ expect(branches.map((b) => b.name)).toEqual(['7.x', 'master']);
103
+ });
104
+ it('creates a branch in the fork (sqren/integration-test)', async () => {
105
+ const branches = await getBranchesOnGithub({
106
+ accessToken,
107
+ repoOwner: AUTHOR,
108
+ repoName: REPO_NAME,
109
+ });
110
+ expect(branches.map((b) => b.name)).toEqual([
111
+ '7.x',
112
+ BRANCH_WITH_ONE_COMMIT,
113
+ 'master',
114
+ ]);
115
+ });
116
+ });
117
+ describe('when two commits are backported', () => {
118
+ let res;
119
+ let pullRequestResponse;
120
+ beforeAll(async () => {
121
+ await resetState(accessToken);
122
+ res = await (0, entrypoint_module_1.backportRun)({
123
+ dir: sandboxPath,
124
+ accessToken,
125
+ repoOwner: 'backport-org',
126
+ repoName: 'integration-test',
127
+ sha: [COMMIT_SHA_1, COMMIT_SHA_2],
128
+ targetBranches: ['7.x'],
129
+ });
130
+ // @ts-expect-error
131
+ const pullRequestNumber = res.results[0].pullRequestNumber;
132
+ pullRequestResponse = await octokit.pulls.get({
133
+ owner: REPO_OWNER,
134
+ repo: REPO_NAME,
135
+ pull_number: pullRequestNumber,
136
+ });
137
+ });
138
+ it('returns the backport result containing both commits', () => {
139
+ expect(res).toEqual({
140
+ commits: [
141
+ {
142
+ expectedTargetPullRequests: [],
143
+ sourceBranch: 'master',
144
+ sourceCommit: {
145
+ committedDate: '2020-08-15T10:37:41Z',
146
+ message: 'Add ❤️ emoji',
147
+ sha: COMMIT_SHA_1,
148
+ },
149
+ sourcePullRequest: undefined,
150
+ },
151
+ {
152
+ expectedTargetPullRequests: [],
153
+ sourceBranch: 'master',
154
+ sourceCommit: {
155
+ committedDate: '2020-08-15T10:44:04Z',
156
+ message: 'Add family emoji (#2)',
157
+ sha: COMMIT_SHA_2,
158
+ },
159
+ sourcePullRequest: undefined,
160
+ },
161
+ ],
162
+ results: [
163
+ {
164
+ didUpdate: false,
165
+ pullRequestNumber: expect.any(Number),
166
+ pullRequestUrl: expect.stringContaining('https://github.com/backport-org/integration-test/pull/'),
167
+ status: 'success',
168
+ targetBranch: '7.x',
169
+ },
170
+ ],
171
+ status: 'success',
172
+ });
173
+ });
174
+ it('pull request: status code', async () => {
175
+ expect(pullRequestResponse.status).toEqual(200);
176
+ });
177
+ it('pull request: title', async () => {
178
+ expect(pullRequestResponse.data.title).toEqual('[7.x] Add ❤️ emoji | Add family emoji (#2)');
179
+ });
180
+ it('pull request: body', async () => {
181
+ expect(pullRequestResponse.data.body).toMatchInlineSnapshot(`
182
+ "# Backport
183
+
184
+ This will backport the following commits from \`master\` to \`7.x\`:
185
+ - Add ❤️ emoji (5bf29b7d)
186
+ - Add family emoji (#2) (59d6ff1c)
187
+
188
+ <!--- Backport version: 1.2.3-mocked -->
189
+
190
+ ### Questions ?
191
+ Please refer to the [Backport tool documentation](https://github.com/sqren/backport)"
192
+ `);
193
+ });
194
+ it('pull request: head branch contains both commits in name', async () => {
195
+ expect(pullRequestResponse.data.head.label).toEqual(`sqren:${BRANCH_WITH_TWO_COMMITS}`);
196
+ });
197
+ it('pull request: base branch', async () => {
198
+ expect(pullRequestResponse.data.base.label).toEqual('backport-org:7.x');
199
+ });
200
+ });
201
+ describe('when disabling fork mode', () => {
202
+ let res;
203
+ let pullRequestResponse;
204
+ beforeAll(async () => {
205
+ await resetState(accessToken);
206
+ res = await (0, entrypoint_module_1.backportRun)({
207
+ fork: false,
208
+ dir: sandboxPath,
209
+ accessToken,
210
+ repoOwner: 'backport-org',
211
+ repoName: 'integration-test',
212
+ sha: COMMIT_SHA_1,
213
+ targetBranches: ['7.x'],
214
+ });
215
+ // @ts-expect-error
216
+ const pullRequestNumber = res.results[0].pullRequestNumber;
217
+ pullRequestResponse = await octokit.pulls.get({
218
+ owner: REPO_OWNER,
219
+ repo: REPO_NAME,
220
+ pull_number: pullRequestNumber,
221
+ });
222
+ });
223
+ it('pull request: title', async () => {
224
+ expect(pullRequestResponse.data.title).toEqual('[7.x] Add ❤️ emoji');
225
+ });
226
+ it('pull request: body', async () => {
227
+ expect(pullRequestResponse.data.body).toMatchInlineSnapshot(`
228
+ "# Backport
229
+
230
+ This will backport the following commits from \`master\` to \`7.x\`:
231
+ - Add ❤️ emoji (5bf29b7d)
232
+
233
+ <!--- Backport version: 1.2.3-mocked -->
234
+
235
+ ### Questions ?
236
+ Please refer to the [Backport tool documentation](https://github.com/sqren/backport)"
237
+ `);
238
+ });
239
+ it('pull request: head branch is in origin (non-fork) repo', async () => {
240
+ expect(pullRequestResponse.data.head.label).toEqual(`backport-org:${BRANCH_WITH_ONE_COMMIT}`);
241
+ });
242
+ it('pull request: base branch', async () => {
243
+ expect(pullRequestResponse.data.base.label).toEqual('backport-org:7.x');
244
+ });
245
+ it('returns pull request', () => {
246
+ expect(res).toEqual({
247
+ commits: [
248
+ {
249
+ expectedTargetPullRequests: [],
250
+ sourceBranch: 'master',
251
+ sourceCommit: {
252
+ committedDate: '2020-08-15T10:37:41Z',
253
+ message: 'Add ❤️ emoji',
254
+ sha: COMMIT_SHA_1,
255
+ },
256
+ sourcePullRequest: undefined,
257
+ },
258
+ ],
259
+ results: [
260
+ {
261
+ didUpdate: false,
262
+ pullRequestNumber: expect.any(Number),
263
+ pullRequestUrl: expect.stringContaining('https://github.com/backport-org/integration-test/pull/'),
264
+ status: 'success',
265
+ targetBranch: '7.x',
266
+ },
267
+ ],
268
+ status: 'success',
269
+ });
270
+ });
271
+ it('creates a new branch in origin (backport-org/integration-test)', async () => {
272
+ const branches = await getBranchesOnGithub({
273
+ accessToken,
274
+ repoOwner: REPO_OWNER,
275
+ repoName: REPO_NAME,
276
+ });
277
+ expect(branches.map((b) => b.name)).toEqual([
278
+ '7.x',
279
+ BRANCH_WITH_ONE_COMMIT,
280
+ 'master',
281
+ ]);
282
+ });
283
+ it('does not create branches in the fork (sqren/integration-test)', async () => {
284
+ const branches = await getBranchesOnGithub({
285
+ accessToken,
286
+ repoOwner: AUTHOR,
287
+ repoName: REPO_NAME,
288
+ });
289
+ expect(branches.map((b) => b.name)).toEqual(['7.x', 'master']);
290
+ });
291
+ });
292
+ });
293
+ async function getBranchesOnGithub({ accessToken, repoOwner, repoName, }) {
294
+ // console.log(`fetch branches for ${repoOwner}`);
295
+ const octokit = new rest_1.Octokit({
296
+ auth: accessToken,
297
+ });
298
+ const res = await octokit.repos.listBranches({
299
+ owner: repoOwner,
300
+ repo: repoName,
301
+ });
302
+ return res.data;
303
+ }
304
+ async function deleteBranchOnGithub({ accessToken, repoOwner, repoName, branchName, }) {
305
+ try {
306
+ const octokit = new rest_1.Octokit({
307
+ auth: accessToken,
308
+ });
309
+ // console.log({ accessToken });
310
+ const opts = {
311
+ owner: repoOwner,
312
+ repo: repoName,
313
+ ref: `heads/${branchName}`,
314
+ };
315
+ const res = await octokit.git.deleteRef(opts);
316
+ // console.log(`Deleted ${repoOwner}:heads/${branchName}`);
317
+ return res.data;
318
+ }
319
+ catch (e) {
320
+ // console.log(
321
+ // `Could not delete ${repoOwner}:heads/${branchName} (${e.message})`
322
+ // );
323
+ if (e.message === 'Reference does not exist') {
324
+ return;
325
+ }
326
+ throw e;
327
+ }
328
+ }
329
+ async function resetState(accessToken) {
330
+ const ownerBranches = await getBranchesOnGithub({
331
+ accessToken,
332
+ repoOwner: REPO_OWNER,
333
+ repoName: REPO_NAME,
334
+ });
335
+ // delete all branches except master and 7.x
336
+ await Promise.all(ownerBranches
337
+ .filter((b) => b.name !== 'master' && b.name !== '7.x')
338
+ .map((b) => {
339
+ return deleteBranchOnGithub({
340
+ accessToken,
341
+ repoOwner: REPO_OWNER,
342
+ repoName: REPO_NAME,
343
+ branchName: b.name,
344
+ });
345
+ }));
346
+ const forkBranches = await getBranchesOnGithub({
347
+ accessToken,
348
+ repoOwner: AUTHOR,
349
+ repoName: REPO_NAME,
350
+ });
351
+ // delete all branches except master and 7.x
352
+ await Promise.all(forkBranches
353
+ .filter((b) => b.name !== 'master' && b.name !== '7.x')
354
+ .map((b) => {
355
+ return deleteBranchOnGithub({
356
+ accessToken,
357
+ repoOwner: AUTHOR,
358
+ repoName: REPO_NAME,
359
+ branchName: b.name,
360
+ });
361
+ }));
362
+ await (0, sandbox_1.resetSandbox)(sandboxPath);
363
+ }
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const entrypoint_module_1 = require("../entrypoint.module");
4
- const child_process_promisified_1 = require("../services/child-process-promisified");
5
4
  const getDevAccessToken_1 = require("./private/getDevAccessToken");
6
5
  const sandbox_1 = require("./sandbox");
7
6
  jest.unmock('find-up');
@@ -38,7 +37,6 @@ describe('Handle unbackported pull requests', () => {
38
37
  const accessToken = (0, getDevAccessToken_1.getDevAccessToken)();
39
38
  const sandboxPath = (0, sandbox_1.getSandboxPath)({ filename: __filename });
40
39
  await (0, sandbox_1.resetSandbox)(sandboxPath);
41
- await (0, child_process_promisified_1.exec)('git init', { cwd: sandboxPath });
42
40
  const result = await (0, entrypoint_module_1.backportRun)({
43
41
  accessToken: accessToken,
44
42
  repoOwner: 'backport-org',