backport 6.1.2 → 6.2.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 (166) hide show
  1. package/README.md +39 -37
  2. package/dist/backportRun.d.ts +14 -0
  3. package/dist/backportRun.js +75 -0
  4. package/dist/entrypoint.api.d.ts +22 -0
  5. package/dist/entrypoint.api.js +57 -0
  6. package/dist/entrypoint.api.private.test.d.ts +1 -0
  7. package/dist/entrypoint.api.private.test.js +37 -0
  8. package/dist/entrypoint.module.d.ts +4 -1
  9. package/dist/entrypoint.module.js +10 -4
  10. package/dist/entrypoint.module.private.test.js +4 -1
  11. package/dist/main.js +5 -1
  12. package/dist/options/ConfigOptions.d.ts +2 -0
  13. package/dist/options/cliArgs.d.ts +5 -1
  14. package/dist/options/cliArgs.js +24 -2
  15. package/dist/options/config/config.d.ts +6 -1
  16. package/dist/options/config/config.js +2 -2
  17. package/dist/options/config/config.test.js +1 -1
  18. package/dist/options/config/getProjectConfig.d.ts +0 -0
  19. package/dist/options/config/getProjectConfig.js +1 -0
  20. package/dist/options/config/globalConfig.d.ts +1 -1
  21. package/dist/options/config/globalConfig.js +4 -8
  22. package/dist/options/config/globalConfig.test.js +2 -2
  23. package/dist/options/config/readConfigFile.d.ts +3 -1
  24. package/dist/options/options.d.ts +8 -1
  25. package/dist/options/options.js +9 -2
  26. package/dist/options/options.test.js +7 -3
  27. package/dist/react-components/0-loadOptions.d.ts +6 -0
  28. package/dist/react-components/0-loadOptions.js +45 -0
  29. package/dist/react-components/1-loadCommit.d.ts +8 -0
  30. package/dist/react-components/1-loadCommit.js +88 -0
  31. package/dist/react-components/1a-loadCommit.d.ts +5 -0
  32. package/dist/react-components/1a-loadCommit.js +76 -0
  33. package/dist/react-components/1b-commitChoices.d.ts +5 -0
  34. package/dist/react-components/1b-commitChoices.js +98 -0
  35. package/dist/react-components/2-commitChoices.d.ts +8 -0
  36. package/dist/react-components/2-commitChoices.js +96 -0
  37. package/dist/react-components/2-commitsPrompt.d.ts +8 -0
  38. package/dist/react-components/2-commitsPrompt.js +96 -0
  39. package/dist/react-components/2-targetBranchChoices.d.ts +9 -0
  40. package/dist/react-components/2-targetBranchChoices.js +109 -0
  41. package/dist/react-components/3-cherrypick.d.ts +1 -0
  42. package/dist/react-components/3-cherrypick.js +2 -0
  43. package/dist/react-components/App.d.ts +2 -0
  44. package/dist/react-components/App.js +35 -0
  45. package/dist/react-components/BackportStateContext.d.ts +33 -0
  46. package/dist/react-components/BackportStateContext.js +42 -0
  47. package/dist/react-components/CommitPrompt/2-commitsPrompt.d.ts +8 -0
  48. package/dist/react-components/CommitPrompt/2-commitsPrompt.js +96 -0
  49. package/dist/react-components/CommitPrompt/CommitPrompt.d.ts +8 -0
  50. package/dist/react-components/CommitPrompt/CommitPrompt.js +96 -0
  51. package/dist/react-components/CommitPrompt/getCommits.d.ts +2 -0
  52. package/dist/react-components/CommitPrompt/getCommits.js +27 -0
  53. package/dist/react-components/CommitPrompt.d.ts +8 -0
  54. package/dist/react-components/CommitPrompt.js +96 -0
  55. package/dist/react-components/CustomMultiSelect.d.ts +13 -0
  56. package/dist/react-components/CustomMultiSelect.js +21 -0
  57. package/dist/react-components/CustomMultiSelectItem.d.ts +5 -0
  58. package/dist/react-components/CustomMultiSelectItem.js +12 -0
  59. package/dist/react-components/CustomSelect.d.ts +14 -0
  60. package/dist/react-components/CustomSelect.js +34 -0
  61. package/dist/react-components/ErrorBoundary.d.ts +15 -0
  62. package/dist/react-components/ErrorBoundary.js +53 -0
  63. package/dist/react-components/MultiSelect.d.ts +7 -0
  64. package/dist/react-components/MultiSelect.js +16 -0
  65. package/dist/react-components/hooks/useAsync.d.ts +12 -0
  66. package/dist/react-components/hooks/useAsync.js +73 -0
  67. package/dist/runSequentially.test.js +10 -7
  68. package/dist/services/HandledError.d.ts +1 -1
  69. package/dist/services/backportRun/backportRun.ci.d.ts +6 -0
  70. package/dist/services/backportRun/backportRun.ci.js +81 -0
  71. package/dist/services/backportRun/backportRun.d.ts +14 -0
  72. package/dist/services/backportRun/backportRun.interactive.d.ts +1 -0
  73. package/dist/services/backportRun/backportRun.interactive.js +4 -0
  74. package/dist/services/backportRun/backportRun.js +14 -0
  75. package/dist/services/backportRun/backportState.d.ts +4 -0
  76. package/dist/services/backportRun/backportState.js +28 -0
  77. package/dist/services/backportRun/getCommits.d.ts +2 -0
  78. package/dist/services/backportRun/getCommits.js +20 -0
  79. package/dist/services/backportRun/runSequentially.d.ts +20 -0
  80. package/dist/services/backportRun/runSequentially.js +41 -0
  81. package/dist/services/backportRun/runSequentially.test.d.ts +1 -0
  82. package/dist/services/backportRun/runSequentially.test.js +199 -0
  83. package/dist/services/cherrypickAndCreateTargetPullRequest.d.ts +12 -0
  84. package/dist/services/cherrypickAndCreateTargetPullRequest.js +206 -0
  85. package/dist/services/cherrypickAndCreateTargetPullRequest.test.d.ts +1 -0
  86. package/dist/services/cherrypickAndCreateTargetPullRequest.test.js +403 -0
  87. package/dist/services/env.d.ts +2 -3
  88. package/dist/services/env.js +9 -10
  89. package/dist/services/env.test.js +0 -6
  90. package/dist/services/getCommits.d.ts +2 -0
  91. package/dist/services/getCommits.js +38 -0
  92. package/dist/services/getCommitsWithoutBackports.d.ts +11 -0
  93. package/dist/services/getCommitsWithoutBackports.js +68 -0
  94. package/dist/services/getCommitsWithoutBackports.test.d.ts +1 -0
  95. package/dist/services/getCommitsWithoutBackports.test.js +259 -0
  96. package/dist/services/getTargetBranches.d.ts +5 -0
  97. package/dist/services/getTargetBranches.js +60 -0
  98. package/dist/services/getTargetBranches.test.d.ts +1 -0
  99. package/dist/services/getTargetBranches.test.js +180 -0
  100. package/dist/services/git.d.ts +2 -1
  101. package/dist/services/git.js +5 -3
  102. package/dist/services/github/v3/createPullRequest.js +1 -1
  103. package/dist/services/github/v3/createPullRequest.test.js +3 -3
  104. package/dist/services/github/v3/createStatusComment.d.ts +1 -1
  105. package/dist/services/github/v3/createStatusComment.js +40 -15
  106. package/dist/services/github/v3/createStatusComment.test.js +190 -73
  107. package/dist/services/github/v4/fetchCommits/allFetchers.private.test.d.ts +1 -0
  108. package/dist/services/github/v4/fetchCommits/allFetchers.private.test.js +78 -0
  109. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.js +1 -1
  110. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.private.test.js +64 -0
  111. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.js +5 -5
  112. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.private.test.js +60 -0
  113. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.d.ts +2 -0
  114. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.js +17 -10
  115. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.private.test.js +72 -0
  116. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.test.js +6 -0
  117. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.js +8 -7
  118. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.private.test.js +8 -0
  119. package/dist/services/github/v4/fetchProjectConfig.d.ts +1 -1
  120. package/dist/services/github/v4/fetchProjectConfig.js +3 -3
  121. package/dist/services/github/v4/fetchProjectConfig.private.test.js +5 -5
  122. package/dist/services/github/v4/fetchRemoteProjectConfig.d.ts +8 -0
  123. package/dist/services/github/v4/fetchRemoteProjectConfig.js +56 -0
  124. package/dist/services/github/v4/fetchRemoteProjectConfig.private.test.d.ts +1 -0
  125. package/dist/services/github/v4/fetchRemoteProjectConfig.private.test.js +52 -0
  126. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.d.ts +2 -0
  127. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.js +0 -7
  128. package/dist/services/github/v4/mocks/commitsByAuthorMock.js +16 -5
  129. package/dist/services/logger.d.ts +0 -1
  130. package/dist/services/logger.js +3 -3
  131. package/dist/services/maybeSetupRepo.d.ts +2 -0
  132. package/dist/services/maybeSetupRepo.js +33 -0
  133. package/dist/services/maybeSetupRepo.test.d.ts +1 -0
  134. package/dist/services/maybeSetupRepo.test.js +78 -0
  135. package/dist/services/sourceCommit/getExpectedTargetPullRequests.d.ts +4 -0
  136. package/dist/services/sourceCommit/getExpectedTargetPullRequests.js +8 -2
  137. package/dist/services/sourceCommit/getExpectedTargetPullRequests.test.js +20 -0
  138. package/dist/services/sourceCommit/getMockSourceCommit.d.ts +1 -0
  139. package/dist/services/sourceCommit/getMockSourceCommit.js +72 -46
  140. package/dist/services/sourceCommit/parseSourceCommit.d.ts +22 -8
  141. package/dist/services/sourceCommit/parseSourceCommit.js +29 -10
  142. package/dist/services/sourceCommit/parseSourceCommit.test.js +10 -2
  143. package/dist/test/backport-e2e.private.test.d.ts +1 -0
  144. package/dist/test/backport-e2e.private.test.js +327 -0
  145. package/dist/test/handleUnbackportedPullRequests.private.test.d.ts +1 -0
  146. package/dist/test/handleUnbackportedPullRequests.private.test.js +43 -0
  147. package/dist/test/inquirer.private.test.js +1 -1
  148. package/dist/test/integration/integration.private.test.js +2 -2
  149. package/dist/test/nockHelpers.js +1 -3
  150. package/dist/test/setupFiles/automatic-mocks.js +1 -0
  151. package/dist/todo-delete.d.ts +1 -0
  152. package/dist/todo-delete.js +17 -0
  153. package/dist/ui/cherrypickAndCreateTargetPullRequest.js +4 -3
  154. package/dist/ui/cherrypickAndCreateTargetPullRequest.test.js +224 -260
  155. package/dist/ui/getCommits.js +1 -1
  156. package/dist/ui/getCommitsWithoutBackports.js +17 -2
  157. package/dist/ui/getCommitsWithoutBackports.test.d.ts +1 -0
  158. package/dist/ui/getCommitsWithoutBackports.test.js +259 -0
  159. package/dist/ui/maybeSetupRepo.js +0 -6
  160. package/dist/ui/maybeSetupRepo.test.js +8 -4
  161. package/dist/utils/redact.d.ts +1 -0
  162. package/dist/utils/redact.js +7 -0
  163. package/dist/utils/sequentially.d.ts +1 -0
  164. package/dist/utils/sequentially.js +10 -0
  165. package/package.json +3 -3
  166. package/yarn.lock +462 -284
@@ -22,99 +22,110 @@ 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 os_1 = __importDefault(require("os"));
25
26
  const nock_1 = __importDefault(require("nock"));
26
27
  const ora_1 = __importDefault(require("ora"));
27
28
  const childProcess = __importStar(require("../services/child-process-promisified"));
29
+ const commitsByAuthorMock_1 = require("../services/github/v4/mocks/commitsByAuthorMock");
28
30
  const logger = __importStar(require("../services/logger"));
29
31
  const prompts = __importStar(require("../services/prompts"));
30
32
  const ExecError_1 = require("../test/ExecError");
31
33
  const nockHelpers_1 = require("../test/nockHelpers");
32
34
  const cherrypickAndCreateTargetPullRequest_1 = require("./cherrypickAndCreateTargetPullRequest");
33
- const TARGET_PULL_NUMBER = 1338;
34
35
  describe('cherrypickAndCreateTargetPullRequest', () => {
36
+ let execSpy;
37
+ let addLabelsScope;
35
38
  let consoleLogSpy;
36
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: '' });
37
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);
38
52
  });
39
53
  afterEach(() => {
40
- consoleLogSpy.mockClear();
54
+ jest.clearAllMocks();
55
+ addLabelsScope.done();
56
+ nock_1.default.cleanAll();
41
57
  });
42
58
  describe('when commit has a pull request reference', () => {
43
59
  let res;
44
- let execSpy;
45
- let getAddLabelsToTargetPrCalls;
46
- let getAddLabelsToSourcePr1000Calls;
47
- let getAddLabelsToSourcePr2000Calls;
48
- let getCreatePullRequestCalls;
60
+ let createPullRequestCalls;
49
61
  beforeEach(async () => {
50
- execSpy = jest
51
- .spyOn(childProcess, 'exec')
52
- // mock all exec commands to respond without errors
53
- .mockResolvedValue({ stdout: '', stderr: '' });
54
- // ensure labels are added to target PR
55
- getAddLabelsToTargetPrCalls = mockAddLabelCall(TARGET_PULL_NUMBER);
56
- // ensure labels are added to source PR
57
- getAddLabelsToSourcePr1000Calls = mockAddLabelCall(1000);
58
- getAddLabelsToSourcePr2000Calls = mockAddLabelCall(2000);
59
- getCreatePullRequestCalls = mockCreatePullRequestCall();
60
- res = await cherrypickAndCreateTargetPullRequest_1.cherrypickAndCreateTargetPullRequest({
61
- targetBranch: '6.x',
62
- options: {
63
- assignees: [],
64
- githubApiBaseUrlV3: 'https://api.github.com',
65
- fork: true,
66
- targetPRLabels: ['backport'],
67
- prDescription: 'myPrSuffix',
68
- prTitle: '[{targetBranch}] {commitMessages}',
69
- repoName: 'kibana',
70
- repoOwner: 'elastic',
71
- username: 'sqren',
72
- sourceBranch: 'myDefaultSourceBranch',
73
- sourcePRLabels: [],
74
- targetBranchChoices: [
75
- { name: '7.0', sourcePRLabels: ['7.0.0'] },
76
- { name: '6.x', sourcePRLabels: ['6.5.1'] },
77
- ],
62
+ const options = {
63
+ assignees: [],
64
+ authenticatedUsername: 'sqren_authenticated',
65
+ fork: true,
66
+ prDescription: 'myPrSuffix',
67
+ prTitle: '[{targetBranch}] {commitMessages}',
68
+ repoName: 'kibana',
69
+ repoOwner: 'elastic',
70
+ reviewers: [],
71
+ sourceBranch: 'myDefaultSourceBranch',
72
+ sourcePRLabels: [],
73
+ targetPRLabels: ['backport'],
74
+ author: 'sqren',
75
+ };
76
+ const commits = [
77
+ {
78
+ committedDate: 'fff',
79
+ sourceBranch: '7.x',
80
+ sha: 'mySha',
81
+ originalMessage: 'My original commit message (#1000)',
82
+ pullNumber: 1000,
83
+ expectedTargetPullRequests: [],
78
84
  },
79
- commits: [
80
- {
81
- sourceBranch: '7.x',
82
- sha: 'mySha',
83
- formattedMessage: 'myCommitMessage (#1000)',
84
- originalMessage: 'My original commit message',
85
- pullNumber: 1000,
86
- sourcePRLabels: [],
87
- existingTargetPullRequests: [],
88
- },
89
- {
90
- sourceBranch: '7.x',
91
- sha: 'mySha2',
92
- formattedMessage: 'myOtherCommitMessage (#2000)',
93
- originalMessage: 'My original commit message',
94
- pullNumber: 2000,
95
- sourcePRLabels: [],
96
- existingTargetPullRequests: [],
97
- },
98
- ],
85
+ {
86
+ committedDate: 'ggg',
87
+ sourceBranch: '7.x',
88
+ sha: 'mySha2',
89
+ originalMessage: 'My other commit message (#2000)',
90
+ pullNumber: 2000,
91
+ expectedTargetPullRequests: [],
92
+ },
93
+ ];
94
+ const scope = (0, nock_1.default)('https://api.github.com')
95
+ .post('/repos/elastic/kibana/pulls')
96
+ .reply(200, { number: 1337, html_url: 'myHtmlUrl' });
97
+ createPullRequestCalls = (0, nockHelpers_1.listenForCallsToNockScope)(scope);
98
+ res = await (0, cherrypickAndCreateTargetPullRequest_1.cherrypickAndCreateTargetPullRequest)({
99
+ options,
100
+ commits,
101
+ targetBranch: '6.x',
99
102
  });
103
+ scope.done();
104
+ nock_1.default.cleanAll();
100
105
  });
101
- afterEach(() => {
102
- execSpy.mockClear();
103
- //@ts-expect-error
104
- ora_1.default.mockClear();
106
+ it('creates the pull request with multiple PR references', () => {
107
+ expect(createPullRequestCalls).toMatchInlineSnapshot(`
108
+ Array [
109
+ Object {
110
+ "base": "6.x",
111
+ "body": "# Backport
112
+
113
+ This is an automatic backport to \`6.x\` of:
114
+ - #1000
115
+ - #2000
116
+
117
+ ### Questions ?
118
+ Please refer to the [Backport tool documentation](https://github.com/sqren/backport)
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
+ `);
105
126
  });
106
127
  it('returns the expected response', () => {
107
- expect(res).toEqual({ url: 'myHtmlUrl', number: TARGET_PULL_NUMBER });
108
- });
109
- it('creates pull request', () => {
110
- expect(getCreatePullRequestCalls()).toEqual([
111
- {
112
- title: '[6.x] myCommitMessage (#1000) | myOtherCommitMessage (#2000)',
113
- head: 'sqren:backport/6.x/pr-1000_pr-2000',
114
- base: '6.x',
115
- body: 'Backports the following commits to 6.x:\n - myCommitMessage (#1000)\n - myOtherCommitMessage (#2000)\n\nmyPrSuffix',
116
- },
117
- ]);
128
+ expect(res).toEqual({ didUpdate: false, url: 'myHtmlUrl', number: 1337 });
118
129
  });
119
130
  it('should make correct git commands', () => {
120
131
  expect(execSpy.mock.calls).toMatchSnapshot();
@@ -132,211 +143,152 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
132
143
  .toMatchInlineSnapshot(`
133
144
  Array [
134
145
  "Pulling latest changes",
135
- "Cherry-picking: myCommitMessage (#1000)",
136
- "Cherry-picking: myOtherCommitMessage (#2000)",
137
- "Pushing branch \\"sqren:backport/6.x/pr-1000_pr-2000\\"",
146
+ "Cherry-picking: My original commit message (#1000)",
147
+ "Cherry-picking: My other commit message (#2000)",
148
+ "Pushing branch \\"sqren_authenticated:backport/6.x/pr-1000_pr-2000\\"",
138
149
  undefined,
139
150
  "Creating pull request",
140
- "Adding labels: \\"backport\\" to #1338",
141
- "Adding labels: \\"6.5.1\\" to #1000",
142
- "Adding labels: \\"6.5.1\\" to #2000",
151
+ "Adding labels: backport",
143
152
  ]
144
153
  `);
145
154
  });
146
- it('adds labels to the target pull request', () => {
147
- expect(getAddLabelsToTargetPrCalls()).toEqual([{ labels: ['backport'] }]);
148
- });
149
- it('adds labels to source pull requests', () => {
150
- expect(getAddLabelsToSourcePr1000Calls()).toEqual([
151
- { labels: ['6.5.1'] },
152
- ]);
153
- expect(getAddLabelsToSourcePr2000Calls()).toEqual([
154
- { labels: ['6.5.1'] },
155
- ]);
156
- });
157
- });
158
- describe('when sourcePRLabels includes wildcard', () => {
159
- let execSpy;
160
- let getAddLabelsToSourcePr1000Calls;
161
- let getAddLabelsToSourcePr2000Calls;
162
- beforeEach(async () => {
163
- execSpy = jest
164
- .spyOn(childProcess, 'exec')
165
- // mock all exec commands to respond without errors
166
- .mockResolvedValue({ stdout: '', stderr: '' });
167
- // ensure labels are added to source PR
168
- getAddLabelsToSourcePr1000Calls = mockAddLabelCall(1000);
169
- getAddLabelsToSourcePr2000Calls = mockAddLabelCall(2000);
170
- // mock create pull request
171
- mockCreatePullRequestCall();
172
- await cherrypickAndCreateTargetPullRequest_1.cherrypickAndCreateTargetPullRequest({
173
- targetBranch: '6.x',
174
- options: {
175
- assignees: [],
176
- githubApiBaseUrlV3: 'https://api.github.com',
177
- fork: true,
178
- targetPRLabels: ['backport'],
179
- prDescription: 'myPrSuffix',
180
- prTitle: '[{targetBranch}] {commitMessages}',
181
- repoName: 'kibana',
182
- repoOwner: 'elastic',
183
- username: 'sqren',
184
- sourceBranch: 'myDefaultSourceBranch',
185
- sourcePRLabels: [],
186
- targetBranchChoices: [
187
- { name: '7.0', sourcePRLabels: ['7.0.0'] },
188
- { name: '6.x', sourcePRLabels: ['6.5.*'] },
189
- ],
190
- },
191
- commits: [
192
- {
193
- sourceBranch: '7.x',
194
- sha: 'mySha',
195
- formattedMessage: 'myCommitMessage (#1000)',
196
- originalMessage: 'My original commit message',
197
- pullNumber: 1000,
198
- sourcePRLabels: [],
199
- existingTargetPullRequests: [],
200
- },
201
- {
202
- sourceBranch: '7.x',
203
- sha: 'mySha2',
204
- formattedMessage: 'myOtherCommitMessage (#2000)',
205
- originalMessage: 'My original commit message',
206
- pullNumber: 2000,
207
- sourcePRLabels: [],
208
- existingTargetPullRequests: [],
209
- },
210
- ],
211
- });
212
- });
213
- afterEach(() => {
214
- execSpy.mockClear();
215
- //@ts-expect-error
216
- ora_1.default.mockClear();
217
- });
218
- it('does not add labels to source pull requests', () => {
219
- expect(getAddLabelsToSourcePr1000Calls()).toEqual([]);
220
- expect(getAddLabelsToSourcePr2000Calls()).toEqual([]);
221
- });
222
155
  });
223
156
  describe('when commit does not have a pull request reference', () => {
224
157
  let res;
225
- let getCreatePullRequestCalls;
226
- let getAddLabelsToTargetPrCalls;
158
+ let createPullRequestCalls;
227
159
  beforeEach(async () => {
228
- // mock all exec commands to respond without errors
229
- jest
230
- .spyOn(childProcess, 'exec')
231
- .mockResolvedValue({ stdout: '', stderr: '' });
232
- getCreatePullRequestCalls = mockCreatePullRequestCall();
233
- // ensure labels are added to target PR
234
- getAddLabelsToTargetPrCalls = mockAddLabelCall(TARGET_PULL_NUMBER);
235
- res = await cherrypickAndCreateTargetPullRequest_1.cherrypickAndCreateTargetPullRequest({
236
- targetBranch: '6.x',
237
- options: {
238
- assignees: [],
239
- githubApiBaseUrlV3: 'https://api.github.com',
240
- fork: true,
241
- targetPRLabels: ['backport'],
242
- prTitle: '[{targetBranch}] {commitMessages}',
243
- repoName: 'kibana',
244
- repoOwner: 'elastic',
245
- username: 'sqren',
246
- sourcePRLabels: [],
247
- targetBranchChoices: [
248
- { name: '7.0', sourcePRLabels: ['7.0.0'] },
249
- { name: '6.x', sourcePRLabels: ['6.5.1'] },
250
- ],
160
+ const options = {
161
+ assignees: [],
162
+ authenticatedUsername: 'sqren_authenticated',
163
+ fork: true,
164
+ prTitle: '[{targetBranch}] {commitMessages}',
165
+ repoName: 'kibana',
166
+ repoOwner: 'elastic',
167
+ reviewers: [],
168
+ sourcePRLabels: [],
169
+ targetPRLabels: ['backport'],
170
+ author: 'sqren',
171
+ };
172
+ const commits = [
173
+ {
174
+ committedDate: 'hhh',
175
+ sourceBranch: '7.x',
176
+ sha: 'mySha',
177
+ originalMessage: 'My original commit message',
178
+ expectedTargetPullRequests: [],
251
179
  },
252
- commits: [
253
- {
254
- sourceBranch: '7.x',
255
- sha: 'mySha',
256
- formattedMessage: 'myCommitMessage (mySha)',
257
- originalMessage: 'My original commit message',
258
- sourcePRLabels: [],
259
- existingTargetPullRequests: [],
260
- },
261
- ],
180
+ ];
181
+ const scope = (0, nock_1.default)('https://api.github.com')
182
+ .post('/repos/elastic/kibana/pulls')
183
+ .reply(200, { number: 1337, html_url: 'myHtmlUrl' });
184
+ createPullRequestCalls = (0, nockHelpers_1.listenForCallsToNockScope)(scope);
185
+ res = await (0, cherrypickAndCreateTargetPullRequest_1.cherrypickAndCreateTargetPullRequest)({
186
+ options,
187
+ commits,
188
+ targetBranch: '6.x',
262
189
  });
190
+ scope.done();
191
+ nock_1.default.cleanAll();
263
192
  });
264
- it('creates pull request', () => {
265
- expect(getCreatePullRequestCalls()).toEqual([
266
- {
267
- title: '[6.x] myCommitMessage (mySha)',
268
- head: 'sqren:backport/6.x/commit-mySha',
269
- base: '6.x',
270
- body: 'Backports the following commits to 6.x:\n - myCommitMessage (mySha)',
271
- },
272
- ]);
193
+ it('creates the pull request with commit reference', () => {
194
+ expect(createPullRequestCalls).toMatchInlineSnapshot(`
195
+ Array [
196
+ Object {
197
+ "base": "6.x",
198
+ "body": "# Backport
199
+
200
+ This is an automatic backport to \`6.x\` of:
201
+ - My original commit message (mySha)
202
+
203
+ ### Questions ?
204
+ Please refer to the [Backport tool documentation](https://github.com/sqren/backport)
205
+ ",
206
+ "head": "sqren_authenticated:backport/6.x/commit-mySha",
207
+ "title": "[6.x] My original commit message",
208
+ },
209
+ ]
210
+ `);
273
211
  });
274
212
  it('returns the expected response', () => {
275
- expect(res).toEqual({ url: 'myHtmlUrl', number: TARGET_PULL_NUMBER });
276
- });
277
- it('adds labels to the target pull request', () => {
278
- expect(getAddLabelsToTargetPrCalls()).toEqual([{ labels: ['backport'] }]);
213
+ expect(res).toEqual({ didUpdate: false, url: 'myHtmlUrl', number: 1337 });
279
214
  });
280
215
  });
281
216
  describe('when cherry-picking fails', () => {
282
217
  let res;
283
218
  let promptSpy;
284
219
  let execSpy;
285
- let getCreatePullRequestCalls;
220
+ let commitsByAuthorCalls;
221
+ let createPullRequestCalls;
286
222
  beforeEach(async () => {
287
223
  // spies
288
224
  promptSpy = jest.spyOn(prompts, 'confirmPrompt').mockResolvedValue(true);
289
- execSpy = setupExecSpy();
290
- getCreatePullRequestCalls = mockCreatePullRequestCall();
291
- res = await cherrypickAndCreateTargetPullRequest_1.cherrypickAndCreateTargetPullRequest({
292
- targetBranch: '6.x',
293
- options: {
294
- assignees: [],
295
- fork: true,
296
- targetPRLabels: ['backport'],
297
- prTitle: '[{targetBranch}] {commitMessages}',
298
- repoName: 'kibana',
299
- repoOwner: 'elastic',
300
- username: 'sqren',
301
- sourceBranch: 'myDefaultSourceBranch',
302
- sourcePRLabels: [],
303
- targetBranchChoices: [],
304
- },
225
+ execSpy = setupExecSpyForCherryPick();
226
+ const options = {
227
+ assignees: [],
228
+ authenticatedUsername: 'sqren_authenticated',
229
+ fork: true,
230
+ githubApiBaseUrlV4: 'http://localhost/graphql',
231
+ prTitle: '[{targetBranch}] {commitMessages}',
232
+ repoName: 'kibana',
233
+ repoOwner: 'elastic',
234
+ reviewers: [],
235
+ sourceBranch: 'myDefaultSourceBranch',
236
+ sourcePRLabels: [],
237
+ targetPRLabels: ['backport'],
238
+ author: 'sqren',
239
+ };
240
+ const scope = (0, nock_1.default)('https://api.github.com')
241
+ .post('/repos/elastic/kibana/pulls')
242
+ .reply(200, { number: 1337, html_url: 'myHtmlUrl' });
243
+ createPullRequestCalls = (0, nockHelpers_1.listenForCallsToNockScope)(scope);
244
+ commitsByAuthorCalls =
245
+ (0, nockHelpers_1.mockGqlRequest)({
246
+ name: 'CommitsByAuthor',
247
+ statusCode: 200,
248
+ body: { data: commitsByAuthorMock_1.commitsByAuthorMock },
249
+ });
250
+ res = await (0, cherrypickAndCreateTargetPullRequest_1.cherrypickAndCreateTargetPullRequest)({
251
+ options,
305
252
  commits: [
306
253
  {
254
+ committedDate: '2021-08-18T16:11:38Z',
307
255
  sourceBranch: '7.x',
308
256
  sha: 'mySha',
309
- formattedMessage: 'myCommitMessage (mySha)',
310
257
  originalMessage: 'My original commit message',
311
- sourcePRLabels: [],
312
- existingTargetPullRequests: [],
258
+ expectedTargetPullRequests: [],
313
259
  },
314
260
  ],
261
+ targetBranch: '6.x',
315
262
  });
263
+ scope.done();
264
+ nock_1.default.cleanAll();
316
265
  });
317
- afterEach(() => {
318
- promptSpy.mockClear();
319
- execSpy.mockClear();
320
- //@ts-expect-error
321
- ora_1.default.mockClear();
322
- });
323
- it('creates pull request', () => {
324
- expect(getCreatePullRequestCalls()).toEqual([
325
- {
326
- title: '[6.x] myCommitMessage (mySha)',
327
- head: 'sqren:backport/6.x/commit-mySha',
328
- base: '6.x',
329
- body: 'Backports the following commits to 6.x:\n - myCommitMessage (mySha)',
330
- },
331
- ]);
266
+ it('creates the pull request with commit reference', () => {
267
+ expect(createPullRequestCalls).toMatchInlineSnapshot(`
268
+ Array [
269
+ Object {
270
+ "base": "6.x",
271
+ "body": "# Backport
272
+
273
+ This is an automatic backport to \`6.x\` of:
274
+ - My original commit message (mySha)
275
+
276
+ ### Questions ?
277
+ Please refer to the [Backport tool documentation](https://github.com/sqren/backport)
278
+ ",
279
+ "head": "sqren_authenticated:backport/6.x/commit-mySha",
280
+ "title": "[6.x] My original commit message",
281
+ },
282
+ ]
283
+ `);
332
284
  });
333
285
  it('returns the expected response', () => {
334
- expect(res).toEqual({ url: 'myHtmlUrl', number: TARGET_PULL_NUMBER });
286
+ expect(res).toEqual({ didUpdate: false, url: 'myHtmlUrl', number: 1337 });
335
287
  });
336
288
  it('shows the right prompts', () => {
337
- expect(promptSpy.mock.calls.length).toBe(3);
289
+ expect(promptSpy.mock.calls.length).toBe(4);
338
290
  expect(promptSpy.mock.calls[0][0]).toMatchInlineSnapshot(`
339
- "Please fix the issues in: /myHomeDir/.backport/repositories/elastic/kibana
291
+ "Fix the following conflicts manually:
340
292
 
341
293
  Conflicting files:
342
294
  - /myHomeDir/.backport/repositories/elastic/kibana/conflicting-file.txt
@@ -345,7 +297,7 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
345
297
  Press ENTER when the conflicts are resolved and files are staged"
346
298
  `);
347
299
  expect(promptSpy.mock.calls[1][0]).toMatchInlineSnapshot(`
348
- "Please fix the issues in: /myHomeDir/.backport/repositories/elastic/kibana
300
+ "Fix the following conflicts manually:
349
301
 
350
302
  Conflicting files:
351
303
  - /myHomeDir/.backport/repositories/elastic/kibana/conflicting-file.txt
@@ -354,13 +306,27 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
354
306
  Press ENTER when the conflicts are resolved and files are staged"
355
307
  `);
356
308
  expect(promptSpy.mock.calls[2][0]).toMatchInlineSnapshot(`
357
- "Please fix the issues in: /myHomeDir/.backport/repositories/elastic/kibana
358
-
309
+ "Fix the following conflicts manually:
359
310
 
360
- Unstaged files:
311
+ Conflicting files:
361
312
  - /myHomeDir/.backport/repositories/elastic/kibana/conflicting-file.txt
362
313
 
314
+
363
315
  Press ENTER when the conflicts are resolved and files are staged"
316
+ `);
317
+ });
318
+ it('commitsByAuthor is called with correct arguments', () => {
319
+ expect(commitsByAuthorCalls[0].variables).toMatchInlineSnapshot(`
320
+ Object {
321
+ "authorId": null,
322
+ "commitPath": "conflicting-file.txt",
323
+ "dateSince": null,
324
+ "dateUntil": "2021-08-18T16:11:38Z",
325
+ "maxNumber": 10,
326
+ "repoName": "kibana",
327
+ "repoOwner": "elastic",
328
+ "sourceBranch": "myDefaultSourceBranch",
329
+ }
364
330
  `);
365
331
  });
366
332
  it('calls exec correctly', () => {
@@ -371,12 +337,12 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
371
337
  .toMatchInlineSnapshot(`
372
338
  Array [
373
339
  "Pulling latest changes",
374
- "Cherry-picking: myCommitMessage (mySha)",
340
+ "Cherry-picking: My original commit message",
375
341
  "Finalizing cherrypick",
376
- "Pushing branch \\"sqren:backport/6.x/commit-mySha\\"",
342
+ "Pushing branch \\"sqren_authenticated:backport/6.x/commit-mySha\\"",
377
343
  undefined,
378
344
  "Creating pull request",
379
- "Adding labels: \\"backport\\" to #1338",
345
+ "Adding labels: backport",
380
346
  ]
381
347
  `);
382
348
  });
@@ -387,7 +353,7 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
387
353
  `);
388
354
  expect(consoleLogSpy.mock.calls[1][0]).toMatchInlineSnapshot(`
389
355
  "
390
- ----------------------------------------
356
+ The commit could not be backported due to conflicts
391
357
  "
392
358
  `);
393
359
  expect(consoleLogSpy.mock.calls[2][0]).toMatchInlineSnapshot(`
@@ -395,11 +361,15 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
395
361
  ----------------------------------------
396
362
  "
397
363
  `);
398
- expect(consoleLogSpy.mock.calls[3][0]).toMatchInlineSnapshot(`"View pull request: myHtmlUrl"`);
364
+ expect(consoleLogSpy.mock.calls[3][0]).toMatchInlineSnapshot(`
365
+ "
366
+ ----------------------------------------
367
+ "
368
+ `);
399
369
  });
400
370
  });
401
371
  });
402
- function setupExecSpy() {
372
+ function setupExecSpyForCherryPick() {
403
373
  let conflictingFilesCheck = 0;
404
374
  let unstagedFilesCheck = 0;
405
375
  return jest
@@ -414,9 +384,13 @@ function setupExecSpy() {
414
384
  return { stderr: '', stdout: '' };
415
385
  }
416
386
  // cherrypick
417
- if (cmd === 'git cherry-pick mySha') {
387
+ if (cmd === 'git cherry-pick -x mySha') {
418
388
  throw new ExecError_1.ExecError({ cmd });
419
389
  }
390
+ // getIsCommitInBranch
391
+ if (cmd.startsWith('git merge-base --is-ancestor')) {
392
+ return { stderr: '', stdout: '' };
393
+ }
420
394
  // getConflictingFiles
421
395
  if (cmd === 'git --no-pager diff --check') {
422
396
  conflictingFilesCheck++;
@@ -453,16 +427,6 @@ function setupExecSpy() {
453
427
  if (cmd.includes('git branch -D ')) {
454
428
  return { stdout: ``, stderr: '' };
455
429
  }
456
- throw new Error(`Missing mock for "${cmd}"`);
430
+ throw new Error(`Missing exec mock for "${cmd}"`);
457
431
  });
458
432
  }
459
- function mockCreatePullRequestCall() {
460
- return nockHelpers_1.createNockListener(nock_1.default('https://api.github.com')
461
- .post('/repos/elastic/kibana/pulls')
462
- .reply(200, { html_url: 'myHtmlUrl', number: TARGET_PULL_NUMBER }));
463
- }
464
- function mockAddLabelCall(pullNumber) {
465
- return nockHelpers_1.createNockListener(nock_1.default('https://api.github.com')
466
- .post(`/repos/elastic/kibana/issues/${pullNumber}/labels`)
467
- .reply(200));
468
- }
@@ -35,7 +35,7 @@ async function getCommits(options) {
35
35
  pullNumber: options.pullNumber, // must extract pullNumber to satisfy the ts gods
36
36
  });
37
37
  // add styles to make it look like a prompt question
38
- spinner.stopAndPersist(getOraPersistsOption('Select pull request', commit.originalMessage));
38
+ spinner.stopAndPersist(getOraPersistsOption('Select pull request', (0, commitFormatters_1.getFirstLine)(commit.originalMessage)));
39
39
  return [commit];
40
40
  }
41
41
  if (options.ci) {