backport 7.2.0-beta.2 → 7.3.0-beta.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 (135) hide show
  1. package/README.md +10 -5
  2. package/dist/entrypoint.cli.d.ts +0 -1
  3. package/dist/entrypoint.cli.js +0 -1
  4. package/dist/entrypoint.cli.private.test.d.ts +1 -0
  5. package/dist/entrypoint.cli.private.test.js +47 -0
  6. package/dist/options/ConfigOptions.d.ts +2 -0
  7. package/dist/options/cliArgs.test.d.ts +1 -0
  8. package/dist/options/cliArgs.test.js +182 -0
  9. package/dist/options/config/config.d.ts +2 -0
  10. package/dist/options/config/config.test.d.ts +1 -0
  11. package/dist/options/config/config.test.js +32 -0
  12. package/dist/options/config/globalConfig.test.d.ts +1 -0
  13. package/dist/options/config/globalConfig.test.js +61 -0
  14. package/dist/options/config/projectConfig.test.d.ts +1 -0
  15. package/dist/options/config/projectConfig.test.js +109 -0
  16. package/dist/options/config/readConfigFile.d.ts +2 -0
  17. package/dist/options/options.d.ts +2 -0
  18. package/dist/options/options.test.d.ts +1 -0
  19. package/dist/options/options.test.js +384 -0
  20. package/dist/runSequentially.js +2 -2
  21. package/dist/runSequentially.test.d.ts +1 -0
  22. package/dist/runSequentially.test.js +243 -0
  23. package/dist/scripts/postinstall.test.d.ts +1 -0
  24. package/dist/scripts/postinstall.test.js +53 -0
  25. package/dist/services/env.test.d.ts +1 -0
  26. package/dist/services/env.test.js +21 -0
  27. package/dist/services/git.d.ts +11 -4
  28. package/dist/services/git.integration.test.d.ts +1 -0
  29. package/dist/services/git.integration.test.js +286 -0
  30. package/dist/services/git.js +34 -16
  31. package/dist/services/git.test.d.ts +1 -0
  32. package/dist/services/git.test.js +543 -0
  33. package/dist/services/github/commitFormatters.test.d.ts +1 -0
  34. package/dist/services/github/commitFormatters.test.js +11 -0
  35. package/dist/services/github/v3/addAssigneesToPullRequest.test.d.ts +1 -0
  36. package/dist/services/github/v3/addAssigneesToPullRequest.test.js +27 -0
  37. package/dist/services/github/v3/createPullRequest.test.d.ts +1 -0
  38. package/dist/services/github/v3/createPullRequest.test.js +377 -0
  39. package/dist/services/github/v3/createStatusComment.test.d.ts +1 -0
  40. package/dist/services/github/v3/createStatusComment.test.js +396 -0
  41. package/dist/services/github/v4/apiRequestV4.test.d.ts +1 -0
  42. package/dist/services/github/v4/apiRequestV4.test.js +109 -0
  43. package/dist/services/github/v4/enablePullRequestAutoMerge.private.test.d.ts +1 -0
  44. package/dist/services/github/v4/enablePullRequestAutoMerge.private.test.js +112 -0
  45. package/dist/services/github/v4/fetchAuthorId.private.test.d.ts +1 -0
  46. package/dist/services/github/v4/fetchAuthorId.private.test.js +28 -0
  47. package/dist/services/github/v4/fetchCommits/allFetchers.private.test.d.ts +1 -0
  48. package/dist/services/github/v4/fetchCommits/allFetchers.private.test.js +94 -0
  49. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.private.test.d.ts +1 -0
  50. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.private.test.js +135 -0
  51. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.private.test.d.ts +1 -0
  52. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.private.test.js +117 -0
  53. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.private.test.d.ts +1 -0
  54. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.private.test.js +195 -0
  55. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.test.d.ts +1 -0
  56. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.test.js +157 -0
  57. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.private.test.d.ts +1 -0
  58. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.private.test.js +104 -0
  59. package/dist/services/github/v4/fetchExistingPullRequest.private.test.d.ts +1 -0
  60. package/dist/services/github/v4/fetchExistingPullRequest.private.test.js +47 -0
  61. package/dist/services/github/v4/fetchRemoteProjectConfig.private.test.d.ts +1 -0
  62. package/dist/services/github/v4/fetchRemoteProjectConfig.private.test.js +52 -0
  63. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.d.ts +2 -0
  64. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.private.test.d.ts +1 -0
  65. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.private.test.js +117 -0
  66. package/dist/services/github/v4/getRepoOwnerAndNameFromGitRemotes.js +1 -0
  67. package/dist/services/github/v4/getRepoOwnerAndNameFromGitRemotes.private.test.d.ts +1 -0
  68. package/dist/services/github/v4/getRepoOwnerAndNameFromGitRemotes.private.test.js +71 -0
  69. package/dist/services/github/v4/mocks/commitsByAuthorMock.d.ts +2 -0
  70. package/dist/services/github/v4/mocks/commitsByAuthorMock.js +176 -0
  71. package/dist/services/github/v4/throwOnInvalidAccessToken.test.d.ts +1 -0
  72. package/dist/services/github/v4/throwOnInvalidAccessToken.test.js +87 -0
  73. package/dist/services/prompt.test.d.ts +1 -0
  74. package/dist/services/prompt.test.js +139 -0
  75. package/dist/services/sourceCommit/getExpectedTargetPullRequests.test.d.ts +1 -0
  76. package/dist/services/sourceCommit/getExpectedTargetPullRequests.test.js +467 -0
  77. package/dist/services/sourceCommit/getMockSourceCommit.js +1 -0
  78. package/dist/services/sourceCommit/parseSourceCommit.d.ts +5 -0
  79. package/dist/services/sourceCommit/parseSourceCommit.js +6 -0
  80. package/dist/services/sourceCommit/parseSourceCommit.test.d.ts +1 -0
  81. package/dist/services/sourceCommit/parseSourceCommit.test.js +226 -0
  82. package/dist/test/ExecError.d.ts +16 -0
  83. package/dist/test/ExecError.js +15 -0
  84. package/dist/test/e2e/cli/cli.private.test.d.ts +1 -0
  85. package/dist/test/e2e/cli/cli.private.test.js +215 -0
  86. package/dist/test/e2e/cli/different-merge-strategies.private.test.d.ts +1 -0
  87. package/dist/test/e2e/cli/different-merge-strategies.private.test.js +108 -0
  88. package/dist/test/e2e/cli/repo-with-backportrc-removed.private.test.d.ts +1 -0
  89. package/dist/test/e2e/cli/repo-with-backportrc-removed.private.test.js +49 -0
  90. package/dist/test/e2e/cli/runBackportViaCli.d.ts +5 -0
  91. package/dist/test/e2e/cli/runBackportViaCli.js +53 -0
  92. package/dist/test/e2e/cli/test-that-repo-can-be-cloned.private.test.d.ts +1 -0
  93. package/dist/test/e2e/cli/test-that-repo-can-be-cloned.private.test.js +73 -0
  94. package/dist/test/e2e/cli.e2e.private.test.d.ts +1 -0
  95. package/dist/test/e2e/cli.e2e.private.test.js +433 -0
  96. package/dist/test/e2e/module.e2e.mutation.test.d.ts +1 -0
  97. package/dist/test/e2e/module.e2e.mutation.test.js +369 -0
  98. package/dist/test/e2e/module.e2e.private.test.d.ts +1 -0
  99. package/dist/test/e2e/module.e2e.private.test.js +369 -0
  100. package/dist/test/handleUnbackportedPullRequests.private.test.d.ts +1 -0
  101. package/dist/test/handleUnbackportedPullRequests.private.test.js +56 -0
  102. package/dist/test/mockConfigFiles.d.ts +5 -0
  103. package/dist/test/mockConfigFiles.js +21 -0
  104. package/dist/test/mocks.d.ts +10 -0
  105. package/dist/test/mocks.js +23 -0
  106. package/dist/test/nockHelpers.d.ts +16 -0
  107. package/dist/test/nockHelpers.js +36 -0
  108. package/dist/test/private/getDevAccessToken.d.ts +2 -0
  109. package/dist/test/private/getDevAccessToken.js +12 -0
  110. package/dist/test/sandbox.d.ts +5 -0
  111. package/dist/test/sandbox.js +43 -0
  112. package/dist/test/setupFiles/automatic-mocks.d.ts +1 -0
  113. package/dist/test/setupFiles/automatic-mocks.js +63 -0
  114. package/dist/ui/cherrypickAndCreateTargetPullRequest.js +3 -3
  115. package/dist/ui/cherrypickAndCreateTargetPullRequest.test.d.ts +1 -0
  116. package/dist/ui/cherrypickAndCreateTargetPullRequest.test.js +469 -0
  117. package/dist/ui/getCommitsWithoutBackports.test.d.ts +1 -0
  118. package/dist/ui/getCommitsWithoutBackports.test.js +331 -0
  119. package/dist/ui/getTargetBranches.test.d.ts +1 -0
  120. package/dist/ui/getTargetBranches.test.js +200 -0
  121. package/dist/ui/maybeSetupRepo.js +7 -3
  122. package/dist/ui/maybeSetupRepo.test.d.ts +1 -0
  123. package/dist/ui/maybeSetupRepo.test.js +238 -0
  124. package/dist/ui/ora.d.ts +1 -1
  125. package/dist/ui/ora.js +2 -2
  126. package/dist/ui/setupRepo.d.ts +3 -0
  127. package/dist/ui/setupRepo.js +88 -0
  128. package/dist/ui/setupRepo.test.d.ts +1 -0
  129. package/dist/ui/setupRepo.test.js +288 -0
  130. package/dist/utils/excludeUndefined.test.d.ts +1 -0
  131. package/dist/utils/excludeUndefined.test.js +24 -0
  132. package/dist/utils/packageVersion.d.ts +1 -1
  133. package/dist/utils/packageVersion.js +1 -1
  134. package/package.json +6 -6
  135. package/yarn.lock +3 -3
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ /*
3
+ * This file is included in `setupFiles` in jest.config.js
4
+ * It will be run once per test file
5
+ */
6
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
7
+ if (k2 === undefined) k2 = k;
8
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ const packageVersionModule = __importStar(require("../../utils/packageVersion"));
27
+ const mocks_1 = require("../mocks");
28
+ /* eslint-disable @typescript-eslint/no-empty-function */
29
+ jest.mock('find-up', () => {
30
+ return jest.fn(async () => '/path/to/project/config');
31
+ });
32
+ // @ts-expect-error
33
+ // eslint-disable-next-line no-import-assign
34
+ packageVersionModule.UNMOCKED_PACKAGE_VERSION =
35
+ packageVersionModule.PACKAGE_VERSION;
36
+ // @ts-expect-error
37
+ // eslint-disable-next-line no-import-assign
38
+ packageVersionModule.PACKAGE_VERSION = '1.2.3-mocked';
39
+ jest.mock('make-dir', () => {
40
+ return jest.fn(() => Promise.resolve('/some/path'));
41
+ });
42
+ jest.mock('del', () => {
43
+ return jest.fn(async (path) => `Attempted to delete ${path}`);
44
+ });
45
+ (0, mocks_1.mockOra)();
46
+ // silence logger
47
+ jest.mock('../../services/logger', () => {
48
+ const spy = jest.fn();
49
+ return {
50
+ initLogger: jest.fn(),
51
+ redactAccessToken: jest.fn((str) => str),
52
+ consoleLog: jest.fn(),
53
+ updateLogger: jest.fn(),
54
+ logger: {
55
+ spy: spy,
56
+ info: (msg, meta) => spy(`[INFO] ${msg}`, meta),
57
+ verbose: (msg, meta) => spy(`[VERBOSE] ${msg}`, meta),
58
+ warn: (msg, meta) => spy(`[WARN] ${msg}`, meta),
59
+ error: (msg, meta) => spy(`[ERROR] ${msg}`, meta),
60
+ debug: (msg, meta) => spy(`[DEBUG] ${msg}`, meta),
61
+ },
62
+ };
63
+ });
@@ -31,7 +31,7 @@ async function cherrypickAndCreateTargetPullRequest({ options, commits, targetBr
31
31
  await (0, git_1.setCommitAuthor)(options, options.authenticatedUsername);
32
32
  }
33
33
  if (options.dryRun) {
34
- (0, ora_1.ora)(options.ci).succeed('Dry run');
34
+ (0, ora_1.ora)(options.ci).succeed('Dry run complete');
35
35
  return { url: 'https://localhost/dry-run', didUpdate: false, number: 1337 };
36
36
  }
37
37
  await (0, git_1.pushBackportBranch)({ options, backportBranch });
@@ -116,10 +116,10 @@ async function waitForCherrypick(options, commit, targetBranch) {
116
116
  cherrypickSpinner.fail();
117
117
  throw e;
118
118
  }
119
+ const repoPath = (0, env_1.getRepoPath)(options);
119
120
  // resolve conflicts automatically
120
121
  if (options.autoFixConflicts) {
121
122
  const autoResolveSpinner = (0, ora_1.ora)(options.ci, 'Attempting to resolve conflicts automatically').start();
122
- const repoPath = (0, env_1.getRepoPath)(options);
123
123
  const didAutoFix = await options.autoFixConflicts({
124
124
  files: conflictingFiles.map((f) => f.absolute),
125
125
  directory: repoPath,
@@ -146,6 +146,7 @@ async function waitForCherrypick(options, commit, targetBranch) {
146
146
  });
147
147
  }
148
148
  (0, logger_1.consoleLog)(chalk_1.default.bold('\nThe commit could not be backported due to conflicts\n'));
149
+ (0, logger_1.consoleLog)(`Please fix the conflicts in ${repoPath}`);
149
150
  if (commitsWithoutBackports.length > 0) {
150
151
  (0, logger_1.consoleLog)(chalk_1.default.italic(`Hint: Before fixing the conflicts manually you should consider backporting the following pull requests to "${targetBranch}":`));
151
152
  (0, logger_1.consoleLog)(`${commitsWithoutBackports.map((c) => c.formatted).join('\n')}\n\n`);
@@ -154,7 +155,6 @@ async function waitForCherrypick(options, commit, targetBranch) {
154
155
  * Commit could not be cleanly cherrypicked: Initiating conflict resolution
155
156
  */
156
157
  if (options.editor) {
157
- const repoPath = (0, env_1.getRepoPath)(options);
158
158
  await (0, child_process_promisified_1.exec)(`${options.editor} ${repoPath}`, { cwd: options.cwd });
159
159
  }
160
160
  // list files with conflict markers + unstaged files and require user to resolve them
@@ -0,0 +1,469 @@
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 nock_1 = __importDefault(require("nock"));
27
+ const ora_1 = __importDefault(require("ora"));
28
+ const childProcess = __importStar(require("../services/child-process-promisified"));
29
+ const commitsByAuthorMock_1 = require("../services/github/v4/mocks/commitsByAuthorMock");
30
+ const logger = __importStar(require("../services/logger"));
31
+ const prompts = __importStar(require("../services/prompts"));
32
+ const ExecError_1 = require("../test/ExecError");
33
+ const nockHelpers_1 = require("../test/nockHelpers");
34
+ const cherrypickAndCreateTargetPullRequest_1 = require("./cherrypickAndCreateTargetPullRequest");
35
+ describe('cherrypickAndCreateTargetPullRequest', () => {
36
+ let execSpy;
37
+ let addLabelsScope;
38
+ let consoleLogSpy;
39
+ beforeEach(() => {
40
+ jest.spyOn(os_1.default, 'homedir').mockReturnValue('/myHomeDir');
41
+ execSpy = jest
42
+ .spyOn(childProcess, 'exec')
43
+ // mock all exec commands to respond without errors
44
+ .mockResolvedValue({ stdout: '', stderr: '' });
45
+ consoleLogSpy = jest.spyOn(logger, 'consoleLog');
46
+ // ensure labels are added
47
+ addLabelsScope = (0, nock_1.default)('https://api.github.com')
48
+ .post('/repos/elastic/kibana/issues/1337/labels', {
49
+ labels: ['backport'],
50
+ })
51
+ .reply(200);
52
+ });
53
+ afterEach(() => {
54
+ jest.clearAllMocks();
55
+ addLabelsScope.done();
56
+ nock_1.default.cleanAll();
57
+ });
58
+ describe('when commit has a pull request reference', () => {
59
+ let res;
60
+ let createPullRequestCalls;
61
+ beforeEach(async () => {
62
+ const options = {
63
+ assignees: [],
64
+ authenticatedUsername: 'sqren_authenticated',
65
+ fork: true,
66
+ prTitle: '[{targetBranch}] {commitMessages}',
67
+ repoName: 'kibana',
68
+ repoOwner: 'elastic',
69
+ reviewers: [],
70
+ sourceBranch: 'myDefaultSourceBranch',
71
+ sourcePRLabels: [],
72
+ targetPRLabels: ['backport'],
73
+ author: 'sqren',
74
+ };
75
+ const commits = [
76
+ {
77
+ author: {
78
+ email: 'soren.louv@elastic.co',
79
+ name: 'Søren Louv-Jansen',
80
+ },
81
+ sourceBranch: '7.x',
82
+ sourceCommit: {
83
+ committedDate: 'fff',
84
+ sha: 'mySha',
85
+ message: 'My original commit message (#1000)',
86
+ },
87
+ sourcePullRequest: {
88
+ url: 'foo',
89
+ number: 1000,
90
+ mergeCommit: {
91
+ sha: 'mySha',
92
+ message: 'My original commit message (#1000)',
93
+ },
94
+ },
95
+ expectedTargetPullRequests: [],
96
+ },
97
+ {
98
+ author: {
99
+ email: 'soren.louv@elastic.co',
100
+ name: 'Søren Louv-Jansen',
101
+ },
102
+ sourceBranch: '7.x',
103
+ sourceCommit: {
104
+ committedDate: 'ggg',
105
+ sha: 'mySha2',
106
+ message: 'My other commit message (#2000)',
107
+ },
108
+ sourcePullRequest: {
109
+ url: 'foo',
110
+ number: 2000,
111
+ mergeCommit: {
112
+ sha: 'mySha2',
113
+ message: 'My other commit message (#2000)',
114
+ },
115
+ },
116
+ expectedTargetPullRequests: [],
117
+ },
118
+ ];
119
+ const scope = (0, nock_1.default)('https://api.github.com')
120
+ .post('/repos/elastic/kibana/pulls')
121
+ .reply(200, { number: 1337, html_url: 'myHtmlUrl' });
122
+ createPullRequestCalls = (0, nockHelpers_1.listenForCallsToNockScope)(scope);
123
+ res = await (0, cherrypickAndCreateTargetPullRequest_1.cherrypickAndCreateTargetPullRequest)({
124
+ options,
125
+ commits,
126
+ targetBranch: '6.x',
127
+ });
128
+ scope.done();
129
+ nock_1.default.cleanAll();
130
+ });
131
+ it('creates the pull request with multiple PR references', () => {
132
+ expect(createPullRequestCalls).toMatchInlineSnapshot(`
133
+ Array [
134
+ Object {
135
+ "base": "6.x",
136
+ "body": "# Backport
137
+
138
+ This will backport the following commits from \`7.x\` to \`6.x\`:
139
+ - [My original commit message (#1000)](foo)
140
+ - [My other commit message (#2000)](foo)
141
+
142
+ <!--- Backport version: 1.2.3-mocked -->
143
+
144
+ ### Questions ?
145
+ Please refer to the [Backport tool documentation](https://github.com/sqren/backport)",
146
+ "head": "sqren_authenticated:backport/6.x/pr-1000_pr-2000",
147
+ "title": "[6.x] My original commit message (#1000) | My other commit message (#2000)",
148
+ },
149
+ ]
150
+ `);
151
+ });
152
+ it('returns the expected response', () => {
153
+ expect(res).toEqual({ didUpdate: false, url: 'myHtmlUrl', number: 1337 });
154
+ });
155
+ it('should make correct git commands', () => {
156
+ expect(execSpy.mock.calls).toMatchSnapshot();
157
+ });
158
+ it('logs correctly', () => {
159
+ expect(consoleLogSpy.mock.calls.length).toBe(2);
160
+ expect(consoleLogSpy.mock.calls[0][0]).toMatchInlineSnapshot(`
161
+ "
162
+ Backporting to 6.x:"
163
+ `);
164
+ expect(consoleLogSpy.mock.calls[1][0]).toMatchInlineSnapshot(`"View pull request: myHtmlUrl"`);
165
+ });
166
+ it('should start the spinner with the correct text', () => {
167
+ expect(ora_1.default.mock.calls.map((call) => call[0].text))
168
+ .toMatchInlineSnapshot(`
169
+ Array [
170
+ "Pulling latest changes",
171
+ "Cherry-picking: My original commit message (#1000)",
172
+ "Cherry-picking: My other commit message (#2000)",
173
+ "Pushing branch \\"sqren_authenticated:backport/6.x/pr-1000_pr-2000\\"",
174
+ undefined,
175
+ "Creating pull request",
176
+ "Adding labels: backport",
177
+ ]
178
+ `);
179
+ });
180
+ });
181
+ describe('when commit does not have a pull request reference', () => {
182
+ let res;
183
+ let createPullRequestCalls;
184
+ beforeEach(async () => {
185
+ const options = {
186
+ assignees: [],
187
+ authenticatedUsername: 'sqren_authenticated',
188
+ fork: true,
189
+ prTitle: '[{targetBranch}] {commitMessages}',
190
+ repoName: 'kibana',
191
+ repoOwner: 'elastic',
192
+ reviewers: [],
193
+ sourcePRLabels: [],
194
+ targetPRLabels: ['backport'],
195
+ author: 'sqren',
196
+ };
197
+ const commits = [
198
+ {
199
+ author: {
200
+ email: 'soren.louv@elastic.co',
201
+ name: 'Søren Louv-Jansen',
202
+ },
203
+ sourceCommit: {
204
+ committedDate: 'hhh',
205
+ sha: 'mySha',
206
+ message: 'My original commit message',
207
+ },
208
+ sourceBranch: '7.x',
209
+ expectedTargetPullRequests: [],
210
+ },
211
+ ];
212
+ const scope = (0, nock_1.default)('https://api.github.com')
213
+ .post('/repos/elastic/kibana/pulls')
214
+ .reply(200, { number: 1337, html_url: 'myHtmlUrl' });
215
+ createPullRequestCalls = (0, nockHelpers_1.listenForCallsToNockScope)(scope);
216
+ res = await (0, cherrypickAndCreateTargetPullRequest_1.cherrypickAndCreateTargetPullRequest)({
217
+ options,
218
+ commits,
219
+ targetBranch: '6.x',
220
+ });
221
+ scope.done();
222
+ nock_1.default.cleanAll();
223
+ });
224
+ it('creates the pull request with commit reference', () => {
225
+ expect(createPullRequestCalls).toMatchInlineSnapshot(`
226
+ Array [
227
+ Object {
228
+ "base": "6.x",
229
+ "body": "# Backport
230
+
231
+ This will backport the following commits from \`7.x\` to \`6.x\`:
232
+ - My original commit message (mySha)
233
+
234
+ <!--- Backport version: 1.2.3-mocked -->
235
+
236
+ ### Questions ?
237
+ Please refer to the [Backport tool documentation](https://github.com/sqren/backport)",
238
+ "head": "sqren_authenticated:backport/6.x/commit-mySha",
239
+ "title": "[6.x] My original commit message",
240
+ },
241
+ ]
242
+ `);
243
+ });
244
+ it('returns the expected response', () => {
245
+ expect(res).toEqual({ didUpdate: false, url: 'myHtmlUrl', number: 1337 });
246
+ });
247
+ });
248
+ describe('when cherry-picking fails', () => {
249
+ let res;
250
+ let promptSpy;
251
+ let execSpy;
252
+ let commitsByAuthorCalls;
253
+ let createPullRequestCalls;
254
+ beforeEach(async () => {
255
+ // spies
256
+ promptSpy = jest.spyOn(prompts, 'confirmPrompt').mockResolvedValue(true);
257
+ execSpy = setupExecSpyForCherryPick();
258
+ const options = {
259
+ assignees: [],
260
+ authenticatedUsername: 'sqren_authenticated',
261
+ fork: true,
262
+ githubApiBaseUrlV4: 'http://localhost/graphql',
263
+ prTitle: '[{targetBranch}] {commitMessages}',
264
+ repoName: 'kibana',
265
+ repoOwner: 'elastic',
266
+ reviewers: [],
267
+ sourceBranch: 'myDefaultSourceBranch',
268
+ sourcePRLabels: [],
269
+ targetPRLabels: ['backport'],
270
+ author: 'sqren',
271
+ };
272
+ const scope = (0, nock_1.default)('https://api.github.com')
273
+ .post('/repos/elastic/kibana/pulls')
274
+ .reply(200, { number: 1337, html_url: 'myHtmlUrl' });
275
+ createPullRequestCalls = (0, nockHelpers_1.listenForCallsToNockScope)(scope);
276
+ commitsByAuthorCalls =
277
+ (0, nockHelpers_1.mockGqlRequest)({
278
+ name: 'CommitsByAuthor',
279
+ statusCode: 200,
280
+ body: { data: commitsByAuthorMock_1.commitsByAuthorMock },
281
+ });
282
+ res = await (0, cherrypickAndCreateTargetPullRequest_1.cherrypickAndCreateTargetPullRequest)({
283
+ options,
284
+ commits: [
285
+ {
286
+ author: {
287
+ email: 'soren.louv@elastic.co',
288
+ name: 'Søren Louv-Jansen',
289
+ },
290
+ sourceCommit: {
291
+ committedDate: '2021-08-18T16:11:38Z',
292
+ sha: 'mySha',
293
+ message: 'My original commit message',
294
+ },
295
+ sourceBranch: '7.x',
296
+ expectedTargetPullRequests: [],
297
+ },
298
+ ],
299
+ targetBranch: '6.x',
300
+ });
301
+ scope.done();
302
+ nock_1.default.cleanAll();
303
+ });
304
+ it('creates the pull request with commit reference', () => {
305
+ expect(createPullRequestCalls).toMatchInlineSnapshot(`
306
+ Array [
307
+ Object {
308
+ "base": "6.x",
309
+ "body": "# Backport
310
+
311
+ This will backport the following commits from \`7.x\` to \`6.x\`:
312
+ - My original commit message (mySha)
313
+
314
+ <!--- Backport version: 1.2.3-mocked -->
315
+
316
+ ### Questions ?
317
+ Please refer to the [Backport tool documentation](https://github.com/sqren/backport)",
318
+ "head": "sqren_authenticated:backport/6.x/commit-mySha",
319
+ "title": "[6.x] My original commit message",
320
+ },
321
+ ]
322
+ `);
323
+ });
324
+ it('returns the expected response', () => {
325
+ expect(res).toEqual({ didUpdate: false, url: 'myHtmlUrl', number: 1337 });
326
+ });
327
+ it('shows the right prompts', () => {
328
+ expect(promptSpy.mock.calls.length).toBe(4);
329
+ expect(promptSpy.mock.calls[0][0]).toMatchInlineSnapshot(`
330
+ "Fix the following conflicts manually:
331
+
332
+ Conflicting files:
333
+ - /myHomeDir/.backport/repositories/elastic/kibana/conflicting-file.txt
334
+
335
+
336
+ Press ENTER when the conflicts are resolved and files are staged"
337
+ `);
338
+ expect(promptSpy.mock.calls[1][0]).toMatchInlineSnapshot(`
339
+ "Fix the following conflicts manually:
340
+
341
+ Conflicting files:
342
+ - /myHomeDir/.backport/repositories/elastic/kibana/conflicting-file.txt
343
+
344
+
345
+ Press ENTER when the conflicts are resolved and files are staged"
346
+ `);
347
+ expect(promptSpy.mock.calls[2][0]).toMatchInlineSnapshot(`
348
+ "Fix the following conflicts manually:
349
+
350
+ Conflicting files:
351
+ - /myHomeDir/.backport/repositories/elastic/kibana/conflicting-file.txt
352
+
353
+
354
+ Press ENTER when the conflicts are resolved and files are staged"
355
+ `);
356
+ });
357
+ it('commitsByAuthor is called with correct arguments', () => {
358
+ expect(commitsByAuthorCalls[0].variables).toMatchInlineSnapshot(`
359
+ Object {
360
+ "authorId": null,
361
+ "commitPath": "conflicting-file.txt",
362
+ "dateSince": null,
363
+ "dateUntil": "2021-08-18T16:11:38Z",
364
+ "maxNumber": 10,
365
+ "repoName": "kibana",
366
+ "repoOwner": "elastic",
367
+ "sourceBranch": "myDefaultSourceBranch",
368
+ }
369
+ `);
370
+ });
371
+ it('calls exec correctly', () => {
372
+ expect(execSpy.mock.calls).toMatchSnapshot();
373
+ });
374
+ it('calls ora correctly', () => {
375
+ expect(ora_1.default.mock.calls.map((call) => call[0].text))
376
+ .toMatchInlineSnapshot(`
377
+ Array [
378
+ "Pulling latest changes",
379
+ "Cherry-picking: My original commit message",
380
+ "Finalizing cherrypick",
381
+ "Pushing branch \\"sqren_authenticated:backport/6.x/commit-mySha\\"",
382
+ undefined,
383
+ "Creating pull request",
384
+ "Adding labels: backport",
385
+ ]
386
+ `);
387
+ });
388
+ it('logs correctly', async () => {
389
+ expect(consoleLogSpy.mock.calls.map((call) => call[0]).join(''))
390
+ .toMatchInlineSnapshot(`
391
+ "
392
+ Backporting to 6.x:
393
+ The commit could not be backported due to conflicts
394
+ Please fix the conflicts in /myHomeDir/.backport/repositories/elastic/kibana
395
+ ----------------------------------------
396
+
397
+ ----------------------------------------
398
+
399
+ ----------------------------------------
400
+ View pull request: myHtmlUrl"
401
+ `);
402
+ });
403
+ });
404
+ });
405
+ function setupExecSpyForCherryPick() {
406
+ let conflictingFilesCheck = 0;
407
+ let unstagedFilesCheck = 0;
408
+ return jest
409
+ .spyOn(childProcess, 'exec')
410
+ .mockImplementation(async (cmd) => {
411
+ // createFeatureBranch
412
+ if (cmd.includes('git checkout -B')) {
413
+ return { stdout: 'create feature branch succeeded', stderr: '' };
414
+ }
415
+ // git fetch
416
+ if (cmd.startsWith('git fetch')) {
417
+ return { stderr: '', stdout: '' };
418
+ }
419
+ // cherrypick
420
+ if (cmd === 'git cherry-pick -x mySha') {
421
+ throw new ExecError_1.ExecError({ cmd });
422
+ }
423
+ // getIsCommitInBranch
424
+ if (cmd.startsWith('git merge-base --is-ancestor')) {
425
+ return { stderr: '', stdout: '' };
426
+ }
427
+ // getConflictingFiles
428
+ if (cmd === 'git --no-pager diff --check') {
429
+ conflictingFilesCheck++;
430
+ if (conflictingFilesCheck >= 4) {
431
+ return { stderr: '', stdout: '' };
432
+ }
433
+ throw new ExecError_1.ExecError({
434
+ code: 2,
435
+ cmd,
436
+ stdout: `conflicting-file.txt:1: leftover conflict marker\nconflicting-file.txt:3: leftover conflict marker\nconflicting-file.txt:5: leftover conflict marker\n`,
437
+ });
438
+ }
439
+ // getUnstagedFiles
440
+ if (cmd === 'git --no-pager diff --name-only') {
441
+ unstagedFilesCheck++;
442
+ if (unstagedFilesCheck >= 5) {
443
+ return { stderr: '', stdout: '' };
444
+ }
445
+ return { stdout: `conflicting-file.txt\n`, stderr: '' };
446
+ }
447
+ // addUnstagedFiles
448
+ if (cmd === 'git add --update') {
449
+ return { stdout: ``, stderr: '' };
450
+ }
451
+ // finalizeCherrypick
452
+ if (cmd.includes('git commit --no-edit')) {
453
+ return { stdout: ``, stderr: '' };
454
+ }
455
+ // pushFeatureBranch
456
+ if (cmd.startsWith('git push ')) {
457
+ return { stdout: ``, stderr: '' };
458
+ }
459
+ // deleteFeatureBranch
460
+ if (cmd.includes('git branch -D ')) {
461
+ return { stdout: ``, stderr: '' };
462
+ }
463
+ // getIsMergeCommit
464
+ if (cmd.startsWith('git rev-list -1 --merges')) {
465
+ return { stdout: ``, stderr: '' };
466
+ }
467
+ throw new Error(`Missing exec mock for "${cmd}"`);
468
+ });
469
+ }
@@ -0,0 +1 @@
1
+ export {};