backport 5.6.3 → 6.0.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 (230) hide show
  1. package/README.md +48 -39
  2. package/dist/entrypoint.cli.js +1 -1
  3. package/dist/entrypoint.module.d.ts +6 -5
  4. package/dist/entrypoint.module.js +8 -6
  5. package/dist/ink.d.ts +1 -0
  6. package/dist/ink.js +49 -0
  7. package/dist/main.d.ts +11 -6
  8. package/dist/main.js +40 -24
  9. package/dist/options/ConfigOptions.d.ts +47 -39
  10. package/dist/options/cliArgs.d.ts +28 -34
  11. package/dist/options/cliArgs.js +113 -94
  12. package/dist/options/cliArgs.test.js +103 -119
  13. package/dist/options/config/config.d.ts +81 -2
  14. package/dist/options/config/config.js +46 -6
  15. package/dist/options/config/config.test.js +24 -2
  16. package/dist/options/config/getOptionsFromGit.d.ts +3 -0
  17. package/dist/options/config/getOptionsFromGit.js +10 -0
  18. package/dist/options/config/globalConfig.d.ts +2 -1
  19. package/dist/options/config/globalConfig.js +8 -8
  20. package/dist/options/config/globalConfig.test.js +7 -3
  21. package/dist/options/config/projectConfig.d.ts +2 -1
  22. package/dist/options/config/projectConfig.js +3 -3
  23. package/dist/options/config/projectConfig.test.js +4 -4
  24. package/dist/options/config/readConfigFile.d.ts +2 -2
  25. package/dist/options/config/readConfigFile.js +3 -3
  26. package/dist/options/options.d.ts +40 -27
  27. package/dist/options/options.js +8 -16
  28. package/dist/options/options.test.js +66 -26
  29. package/dist/options/parseRequiredOptions.d.ts +71 -0
  30. package/dist/options/parseRequiredOptions.js +70 -0
  31. package/dist/options/parseRequiredOptions.test.d.ts +1 -0
  32. package/dist/options/parseRequiredOptions.test.js +127 -0
  33. package/dist/runSequentially.d.ts +20 -0
  34. package/dist/runSequentially.js +41 -0
  35. package/dist/runSequentially.test.d.ts +1 -0
  36. package/dist/runSequentially.test.js +235 -0
  37. package/dist/runWithOptions.js +7 -14
  38. package/dist/runWithOptions.test.js +142 -16
  39. package/dist/scripts/generate-gql.d.ts +1 -0
  40. package/dist/scripts/generate-gql.js +15 -0
  41. package/dist/scripts/postinstall.js +4 -4
  42. package/dist/scripts/postinstall.test.js +9 -2
  43. package/dist/scripts/runPostinstall.js +1 -1
  44. package/dist/services/HandledError.d.ts +11 -1
  45. package/dist/services/HandledError.js +2 -1
  46. package/dist/services/child-process-promisified.d.ts +1 -1
  47. package/dist/services/child-process-promisified.js +2 -2
  48. package/dist/services/env.js +5 -5
  49. package/dist/services/env.test.js +11 -4
  50. package/dist/services/fs-promisified.js +4 -4
  51. package/dist/services/getExpectedTarget.d.ts +9 -0
  52. package/dist/services/getExpectedTarget.js +77 -0
  53. package/dist/services/getExpectedTargetPullRequests.d.ts +9 -0
  54. package/dist/services/getExpectedTargetPullRequests.js +77 -0
  55. package/dist/services/git.d.ts +19 -4
  56. package/dist/services/git.integration.test.d.ts +1 -0
  57. package/dist/services/git.integration.test.js +168 -0
  58. package/dist/services/git.js +129 -57
  59. package/dist/services/git.test.js +155 -51
  60. package/dist/services/github/commitFormatters.d.ts +3 -7
  61. package/dist/services/github/commitFormatters.js +10 -19
  62. package/dist/services/github/commitFormatters.test.js +2 -24
  63. package/dist/services/github/v3/addAssigneesToPullRequest.d.ts +1 -1
  64. package/dist/services/github/v3/addAssigneesToPullRequest.js +2 -6
  65. package/dist/services/github/v3/addAssigneesToPullRequest.test.js +2 -3
  66. package/dist/services/github/v3/addLabelsToPullRequest.d.ts +1 -1
  67. package/dist/services/github/v3/addLabelsToPullRequest.js +2 -6
  68. package/dist/services/github/v3/addReviewersToPullRequest.d.ts +2 -0
  69. package/dist/services/github/v3/addReviewersToPullRequest.js +34 -0
  70. package/dist/services/github/v3/createPullRequest.d.ts +3 -2
  71. package/dist/services/github/v3/createPullRequest.js +33 -24
  72. package/dist/services/github/v3/createStatusComment.d.ts +11 -0
  73. package/dist/services/github/v3/createStatusComment.js +97 -0
  74. package/dist/services/github/v3/createStatusComment.test.d.ts +1 -0
  75. package/dist/services/github/v3/createStatusComment.test.js +209 -0
  76. package/dist/services/github/v4/FetchPullRequestId.d.ts +9 -0
  77. package/dist/services/github/v4/FetchPullRequestId.js +32 -0
  78. package/dist/services/github/v4/apiRequestV4.d.ts +1 -1
  79. package/dist/services/github/v4/apiRequestV4.js +8 -10
  80. package/dist/services/github/v4/apiRequestV4.test.js +7 -7
  81. package/dist/services/github/v4/disablePullRequestAutoMerge.d.ts +9 -0
  82. package/dist/services/github/v4/disablePullRequestAutoMerge.js +28 -0
  83. package/dist/services/github/v4/enablePullRequestAutoMerge.d.ts +9 -0
  84. package/dist/services/github/v4/enablePullRequestAutoMerge.js +51 -0
  85. package/dist/services/github/v4/enablePullRequestAutoMerge.private.test.d.ts +1 -0
  86. package/dist/services/github/v4/enablePullRequestAutoMerge.private.test.js +113 -0
  87. package/dist/services/github/v4/fetchAuthorId.d.ts +5 -0
  88. package/dist/services/github/v4/fetchAuthorId.js +1 -1
  89. package/dist/services/github/v4/fetchAuthorId.private.test.js +3 -3
  90. package/dist/services/github/v4/fetchCommitByPullNumber.js +11 -9
  91. package/dist/services/github/v4/fetchCommitByPullNumber.private.test.js +6 -6
  92. package/dist/services/github/v4/fetchCommitBySha.js +14 -12
  93. package/dist/services/github/v4/fetchCommitBySha.private.test.js +7 -5
  94. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.d.ts +5 -0
  95. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.js +46 -0
  96. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.private.test.d.ts +1 -0
  97. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.private.test.js +67 -0
  98. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.d.ts +5 -0
  99. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.js +36 -0
  100. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.private.test.d.ts +1 -0
  101. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.private.test.js +55 -0
  102. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.d.ts +16 -0
  103. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.js +90 -0
  104. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.private.test.d.ts +1 -0
  105. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.private.test.js +130 -0
  106. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.test.d.ts +1 -0
  107. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.test.js +127 -0
  108. package/dist/services/github/v4/fetchCommits/fetchCommitsInDateRange.d.ts +28 -0
  109. package/dist/services/github/v4/fetchCommits/fetchCommitsInDateRange.js +52 -0
  110. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.d.ts +10 -0
  111. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.js +49 -0
  112. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.private.test.d.ts +1 -0
  113. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.private.test.js +81 -0
  114. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.test.d.ts +1 -0
  115. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.test.js +252 -0
  116. package/dist/services/github/v4/fetchCommitsByAuthor.d.ts +3 -2
  117. package/dist/services/github/v4/fetchCommitsByAuthor.js +81 -70
  118. package/dist/services/github/v4/fetchCommitsByAuthor.private.test.js +81 -8
  119. package/dist/services/github/v4/fetchCommitsByAuthor.test.js +29 -21
  120. package/dist/services/github/v4/fetchDefaultBranch.d.ts +1 -1
  121. package/dist/services/github/v4/fetchDefaultBranch.js +1 -1
  122. package/dist/services/github/v4/fetchDefaultRepoBranchAndPerformStartupChecks/fetchDefaultRepoBranchAndPerformStartupChecks.d.ts +4 -0
  123. package/dist/services/github/v4/fetchDefaultRepoBranchAndPerformStartupChecks/fetchDefaultRepoBranchAndPerformStartupChecks.js +52 -0
  124. package/dist/services/github/v4/fetchDefaultRepoBranchAndPerformStartupChecks/fetchDefaultRepoBranchAndPerformStartupChecks.private.test.d.ts +1 -0
  125. package/dist/services/github/v4/fetchDefaultRepoBranchAndPerformStartupChecks/fetchDefaultRepoBranchAndPerformStartupChecks.private.test.js +32 -0
  126. package/dist/services/github/v4/fetchDefaultRepoBranchAndPerformStartupChecks/index.d.ts +4 -0
  127. package/dist/services/github/v4/fetchDefaultRepoBranchAndPerformStartupChecks/index.js +72 -0
  128. package/dist/services/github/v4/fetchDefaultRepoBranchAndPerformStartupChecks/index.private.test.d.ts +1 -0
  129. package/dist/services/github/v4/fetchDefaultRepoBranchAndPerformStartupChecks/index.private.test.js +34 -0
  130. package/dist/services/github/v4/fetchDefaultRepoBranchAndPerformStartupChecks/query.d.ts +40 -0
  131. package/dist/services/github/v4/fetchDefaultRepoBranchAndPerformStartupChecks/query.js +72 -0
  132. package/dist/services/github/v4/fetchExistingPullRequest.js +2 -3
  133. package/dist/services/github/v4/fetchExistingPullRequest.private.test.js +3 -3
  134. package/dist/services/github/v4/fetchMergedPullRequests.d.ts +19 -20
  135. package/dist/services/github/v4/fetchMergedPullRequests.js +22 -68
  136. package/dist/services/github/v4/fetchMergedPullRequests.private.test.js +8 -9
  137. package/dist/services/github/v4/fetchPullRequestAutoMergeMethod.d.ts +11 -0
  138. package/dist/services/github/v4/fetchPullRequestAutoMergeMethod.js +35 -0
  139. package/dist/services/github/v4/fetchPullRequestBySearchQuery.d.ts +1 -1
  140. package/dist/services/github/v4/fetchPullRequestBySearchQuery.js +12 -10
  141. package/dist/services/github/v4/fetchPullRequestBySearchQuery.private.test.js +8 -6
  142. package/dist/services/github/v4/fetchPullRequestBySearchQuery.test.js +2 -2
  143. package/dist/services/github/v4/getExistingTargetPullRequests.d.ts +4 -58
  144. package/dist/services/github/v4/getExistingTargetPullRequests.js +3 -58
  145. package/dist/services/github/v4/getOptionsFromGithub/index.d.ts +61 -0
  146. package/dist/services/github/v4/getOptionsFromGithub/index.js +139 -0
  147. package/dist/services/github/v4/getOptionsFromGithub/index.private.test.d.ts +1 -0
  148. package/dist/services/github/v4/getOptionsFromGithub/index.private.test.js +126 -0
  149. package/dist/services/github/v4/getOptionsFromGithub/query.d.ts +34 -0
  150. package/dist/services/github/v4/getOptionsFromGithub/query.js +44 -0
  151. package/dist/services/github/v4/getTargetBranchesFromLabels.d.ts +0 -7
  152. package/dist/services/github/v4/getTargetBranchesFromLabels.js +1 -26
  153. package/dist/services/github/v4/getTargetBranchesFromLabels.test.js +69 -32
  154. package/dist/services/github/v4/mocks/commitsByAuthorMock.d.ts +2 -2
  155. package/dist/services/github/v4/mocks/commitsByAuthorMock.js +39 -25
  156. package/dist/services/github/v4/mocks/fetchPullRequestBySearchQueryMock.d.ts +2 -2
  157. package/dist/services/github/v4/mocks/fetchPullRequestBySearchQueryMock.js +10 -0
  158. package/dist/services/github/v4/mocks/getCommitsByAuthorMock.d.ts +2 -2
  159. package/dist/services/github/v4/mocks/getCommitsByAuthorMock.js +4 -1
  160. package/dist/services/github/v4/mocks/getPullRequestNodeMock.d.ts +1 -1
  161. package/dist/services/github/v4/mocks/getPullRequestNodeMock.js +1 -0
  162. package/dist/services/github/v4/throwOnInvalidAccessToken.d.ts +1 -1
  163. package/dist/services/github/v4/throwOnInvalidAccessToken.js +10 -15
  164. package/dist/services/github/v4/throwOnInvalidAccessToken.test.js +7 -7
  165. package/dist/services/logger.d.ts +3 -3
  166. package/dist/services/logger.js +24 -24
  167. package/dist/services/prompt.test.d.ts +1 -0
  168. package/dist/services/prompt.test.js +87 -0
  169. package/dist/services/prompts.d.ts +10 -4
  170. package/dist/services/prompts.js +85 -22
  171. package/dist/services/sourceCommit/getExpectedTargetPullRequests.d.ts +9 -0
  172. package/dist/services/sourceCommit/getExpectedTargetPullRequests.js +92 -0
  173. package/dist/services/sourceCommit/getExpectedTargetPullRequests.test.d.ts +1 -0
  174. package/dist/services/sourceCommit/getExpectedTargetPullRequests.test.js +396 -0
  175. package/dist/services/sourceCommit/getMockSourceCommit.d.ts +21 -0
  176. package/dist/services/sourceCommit/getMockSourceCommit.js +67 -0
  177. package/dist/services/sourceCommit/getTargetBranchesFromLabels.d.ts +5 -0
  178. package/dist/services/sourceCommit/getTargetBranchesFromLabels.js +20 -0
  179. package/dist/services/sourceCommit/getTargetBranchesFromLabels.test.d.ts +1 -0
  180. package/dist/services/sourceCommit/getTargetBranchesFromLabels.test.js +201 -0
  181. package/dist/services/sourceCommit/parseSourceCommit.d.ts +78 -0
  182. package/dist/services/sourceCommit/parseSourceCommit.js +96 -0
  183. package/dist/services/sourceCommit/parseSourceCommit.test.d.ts +1 -0
  184. package/dist/services/sourceCommit/parseSourceCommit.test.js +114 -0
  185. package/dist/services/sourceCommit/sourceCommit.d.ts +78 -0
  186. package/dist/services/sourceCommit/sourceCommit.js +86 -0
  187. package/dist/services/sourceCommit/sourceCommit.test.d.ts +1 -0
  188. package/dist/services/sourceCommit/sourceCommit.test.js +287 -0
  189. package/dist/services/sourceCommit.d.ts +78 -0
  190. package/dist/services/sourceCommit.js +86 -0
  191. package/dist/services/sourceCommit.test.d.ts +1 -0
  192. package/dist/services/sourceCommit.test.js +280 -0
  193. package/dist/test/inquirer.private.test.js +81 -45
  194. package/dist/test/integration/createSpies.d.ts +1 -1
  195. package/dist/test/integration/createSpies.js +21 -9
  196. package/dist/test/integration/envConstants.js +5 -6
  197. package/dist/test/integration/helpers.js +1 -1
  198. package/dist/test/integration/integration.private.test.d.ts +1 -0
  199. package/dist/test/integration/integration.private.test.js +308 -0
  200. package/dist/test/integration/integration.test.js +30 -31
  201. package/dist/test/nockHelpers.d.ts +2 -2
  202. package/dist/test/nockHelpers.js +3 -7
  203. package/dist/test/setupFiles/automatic-mocks.d.ts +0 -1
  204. package/dist/test/setupFiles/automatic-mocks.js +0 -22
  205. package/dist/types/Commit.d.ts +85 -0
  206. package/dist/types/Commit.js +139 -0
  207. package/dist/types/commitNode.d.ts +89 -0
  208. package/dist/types/commitNode.js +139 -0
  209. package/dist/types/commitNode.test.d.ts +1 -0
  210. package/dist/types/commitNode.test.js +255 -0
  211. package/dist/types/commitWithAssociatedPullRequests.d.ts +86 -0
  212. package/dist/types/commitWithAssociatedPullRequests.js +145 -0
  213. package/dist/ui/cherrypickAndCreatePullRequest.test.js +277 -59
  214. package/dist/ui/cherrypickAndCreateTargetPullRequest.d.ts +11 -1
  215. package/dist/ui/cherrypickAndCreateTargetPullRequest.js +135 -67
  216. package/dist/ui/cherrypickAndCreateTargetPullRequest.test.d.ts +1 -0
  217. package/dist/ui/cherrypickAndCreateTargetPullRequest.test.js +468 -0
  218. package/dist/ui/getCommits.d.ts +1 -1
  219. package/dist/ui/getCommits.js +55 -25
  220. package/dist/ui/getTargetBranches.d.ts +2 -2
  221. package/dist/ui/getTargetBranches.js +16 -17
  222. package/dist/ui/getTargetBranches.test.js +75 -99
  223. package/dist/ui/getTargetBranchesFromLabels.d.ts +6 -0
  224. package/dist/ui/getTargetBranchesFromLabels.js +40 -0
  225. package/dist/ui/getTargetBranchesFromLabels.test.d.ts +1 -0
  226. package/dist/ui/getTargetBranchesFromLabels.test.js +288 -0
  227. package/dist/ui/maybeSetupRepo.js +9 -9
  228. package/dist/ui/maybeSetupRepo.test.js +5 -1
  229. package/package.json +36 -51
  230. package/yarn.lock +2387 -2979
@@ -22,25 +22,32 @@ 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"));
28
+ const strip_ansi_1 = __importDefault(require("strip-ansi"));
27
29
  const childProcess = __importStar(require("../services/child-process-promisified"));
30
+ const git = __importStar(require("../services/git"));
31
+ const fetchCommitsByAuthorModule = __importStar(require("../services/github/v4/fetchCommits/fetchCommitsByAuthor"));
32
+ const commitsByAuthorMock_1 = require("../services/github/v4/mocks/commitsByAuthorMock");
28
33
  const logger = __importStar(require("../services/logger"));
29
34
  const prompts = __importStar(require("../services/prompts"));
30
35
  const ExecError_1 = require("../test/ExecError");
36
+ const nockHelpers_1 = require("../test/nockHelpers");
31
37
  const cherrypickAndCreateTargetPullRequest_1 = require("./cherrypickAndCreateTargetPullRequest");
32
38
  describe('cherrypickAndCreateTargetPullRequest', () => {
33
39
  let execSpy;
34
40
  let addLabelsScope;
35
41
  let consoleLogSpy;
36
42
  beforeEach(() => {
43
+ jest.spyOn(os_1.default, 'homedir').mockReturnValue('/myHomeDir');
37
44
  execSpy = jest
38
45
  .spyOn(childProcess, 'exec')
39
46
  // mock all exec commands to respond without errors
40
47
  .mockResolvedValue({ stdout: '', stderr: '' });
41
48
  consoleLogSpy = jest.spyOn(logger, 'consoleLog');
42
49
  // ensure labels are added
43
- addLabelsScope = nock_1.default('https://api.github.com')
50
+ addLabelsScope = (0, nock_1.default)('https://api.github.com')
44
51
  .post('/repos/elastic/kibana/issues/1337/labels', {
45
52
  labels: ['backport'],
46
53
  })
@@ -56,46 +63,45 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
56
63
  beforeEach(async () => {
57
64
  const options = {
58
65
  assignees: [],
59
- githubApiBaseUrlV3: 'https://api.github.com',
60
66
  fork: true,
61
- targetPRLabels: ['backport'],
67
+ githubApiBaseUrlV3: 'https://api.github.com',
62
68
  prDescription: 'myPrSuffix',
63
69
  prTitle: '[{targetBranch}] {commitMessages}',
64
70
  repoName: 'kibana',
65
71
  repoOwner: 'elastic',
66
- username: 'sqren',
72
+ reviewers: [],
67
73
  sourceBranch: 'myDefaultSourceBranch',
68
74
  sourcePRLabels: [],
75
+ targetPRLabels: ['backport'],
76
+ username: 'sqren',
69
77
  };
70
78
  const commits = [
71
79
  {
80
+ committedDate: 'fff',
72
81
  sourceBranch: '7.x',
73
82
  sha: 'mySha',
74
- formattedMessage: 'myCommitMessage (#1000)',
75
- originalMessage: 'My original commit message',
83
+ originalMessage: 'My original commit message (#1000)',
76
84
  pullNumber: 1000,
77
- targetBranchesFromLabels: [],
78
- existingTargetPullRequests: [],
85
+ expectedTargetPullRequests: [],
79
86
  },
80
87
  {
88
+ committedDate: 'ggg',
81
89
  sourceBranch: '7.x',
82
90
  sha: 'mySha2',
83
- formattedMessage: 'myOtherCommitMessage (#2000)',
84
- originalMessage: 'My original commit message',
91
+ originalMessage: 'My other commit message (#2000)',
85
92
  pullNumber: 2000,
86
- targetBranchesFromLabels: [],
87
- existingTargetPullRequests: [],
93
+ expectedTargetPullRequests: [],
88
94
  },
89
95
  ];
90
- const scope = nock_1.default('https://api.github.com')
96
+ const scope = (0, nock_1.default)('https://api.github.com')
91
97
  .post('/repos/elastic/kibana/pulls', {
92
- title: '[6.x] myCommitMessage (#1000) | myOtherCommitMessage (#2000)',
98
+ title: '[6.x] My original commit message (#1000) | My other commit message (#2000)',
93
99
  head: 'sqren:backport/6.x/pr-1000_pr-2000',
94
100
  base: '6.x',
95
- body: 'Backports the following commits to 6.x:\n - myCommitMessage (#1000)\n - myOtherCommitMessage (#2000)\n\nmyPrSuffix',
101
+ body: 'Backports the following commits to 6.x:\n - #1000\n - #2000\n\nmyPrSuffix',
96
102
  })
97
103
  .reply(200, { number: 1337, html_url: 'myHtmlUrl' });
98
- res = await cherrypickAndCreateTargetPullRequest_1.cherrypickAndCreateTargetPullRequest({
104
+ res = await (0, cherrypickAndCreateTargetPullRequest_1.cherrypickAndCreateTargetPullRequest)({
99
105
  options,
100
106
  commits,
101
107
  targetBranch: '6.x',
@@ -104,7 +110,7 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
104
110
  nock_1.default.cleanAll();
105
111
  });
106
112
  it('returns the expected response', () => {
107
- expect(res).toEqual({ url: 'myHtmlUrl', number: 1337 });
113
+ expect(res).toEqual({ didUpdate: false, url: 'myHtmlUrl', number: 1337 });
108
114
  });
109
115
  it('should make correct git commands', () => {
110
116
  expect(execSpy.mock.calls).toMatchSnapshot();
@@ -122,8 +128,8 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
122
128
  .toMatchInlineSnapshot(`
123
129
  Array [
124
130
  "Pulling latest changes",
125
- "Cherry-picking: myCommitMessage (#1000)",
126
- "Cherry-picking: myOtherCommitMessage (#2000)",
131
+ "Cherry-picking: My original commit message (#1000)",
132
+ "Cherry-picking: My other commit message (#2000)",
127
133
  "Pushing branch \\"sqren:backport/6.x/pr-1000_pr-2000\\"",
128
134
  undefined,
129
135
  "Creating pull request",
@@ -137,81 +143,90 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
137
143
  beforeEach(async () => {
138
144
  const options = {
139
145
  assignees: [],
140
- githubApiBaseUrlV3: 'https://api.github.com',
141
146
  fork: true,
142
- targetPRLabels: ['backport'],
147
+ githubApiBaseUrlV3: 'https://api.github.com',
143
148
  prTitle: '[{targetBranch}] {commitMessages}',
144
149
  repoName: 'kibana',
145
150
  repoOwner: 'elastic',
146
- username: 'sqren',
151
+ reviewers: [],
147
152
  sourcePRLabels: [],
153
+ targetPRLabels: ['backport'],
154
+ username: 'sqren',
148
155
  };
149
- const scope = nock_1.default('https://api.github.com')
156
+ const commits = [
157
+ {
158
+ committedDate: 'hhh',
159
+ sourceBranch: '7.x',
160
+ sha: 'mySha',
161
+ originalMessage: 'My original commit message',
162
+ expectedTargetPullRequests: [],
163
+ },
164
+ ];
165
+ const scope = (0, nock_1.default)('https://api.github.com')
150
166
  .post('/repos/elastic/kibana/pulls', {
151
- title: '[6.x] myCommitMessage (mySha)',
167
+ title: '[6.x] My original commit message',
152
168
  head: 'sqren:backport/6.x/commit-mySha',
153
169
  base: '6.x',
154
- body: 'Backports the following commits to 6.x:\n - myCommitMessage (mySha)',
170
+ body: 'Backports the following commits to 6.x:\n - My original commit message (mySha)',
155
171
  })
156
172
  .reply(200, { number: 1337, html_url: 'myHtmlUrl' });
157
- res = await cherrypickAndCreateTargetPullRequest_1.cherrypickAndCreateTargetPullRequest({
173
+ res = await (0, cherrypickAndCreateTargetPullRequest_1.cherrypickAndCreateTargetPullRequest)({
158
174
  options,
159
- commits: [
160
- {
161
- sourceBranch: '7.x',
162
- sha: 'mySha',
163
- formattedMessage: 'myCommitMessage (mySha)',
164
- originalMessage: 'My original commit message',
165
- targetBranchesFromLabels: [],
166
- existingTargetPullRequests: [],
167
- },
168
- ],
175
+ commits,
169
176
  targetBranch: '6.x',
170
177
  });
171
178
  scope.done();
172
179
  nock_1.default.cleanAll();
173
180
  });
174
181
  it('returns the expected response', () => {
175
- expect(res).toEqual({ url: 'myHtmlUrl', number: 1337 });
182
+ expect(res).toEqual({ didUpdate: false, url: 'myHtmlUrl', number: 1337 });
176
183
  });
177
184
  });
178
185
  describe('when cherry-picking fails', () => {
179
186
  let res;
180
187
  let promptSpy;
181
188
  let execSpy;
189
+ let commitsByAuthorCalls;
182
190
  beforeEach(async () => {
183
191
  // spies
184
192
  promptSpy = jest.spyOn(prompts, 'confirmPrompt').mockResolvedValue(true);
185
- execSpy = setupExecSpy();
193
+ execSpy = setupExecSpyForCherryPick();
186
194
  const options = {
187
195
  assignees: [],
188
196
  fork: true,
189
- targetPRLabels: ['backport'],
197
+ githubApiBaseUrlV4: 'http://localhost/graphql',
190
198
  prTitle: '[{targetBranch}] {commitMessages}',
191
199
  repoName: 'kibana',
192
200
  repoOwner: 'elastic',
193
- username: 'sqren',
201
+ reviewers: [],
194
202
  sourceBranch: 'myDefaultSourceBranch',
195
203
  sourcePRLabels: [],
204
+ targetPRLabels: ['backport'],
205
+ username: 'sqren',
196
206
  };
197
- const scope = nock_1.default('https://api.github.com')
207
+ const scope = (0, nock_1.default)('https://api.github.com')
198
208
  .post('/repos/elastic/kibana/pulls', {
199
- title: '[6.x] myCommitMessage',
209
+ title: '[6.x] My original commit message',
200
210
  head: 'sqren:backport/6.x/commit-mySha',
201
211
  base: '6.x',
202
- body: 'Backports the following commits to 6.x:\n - myCommitMessage',
212
+ body: 'Backports the following commits to 6.x:\n - My original commit message (mySha)',
203
213
  })
204
214
  .reply(200, { html_url: 'myHtmlUrl', number: 1337 });
205
- res = await cherrypickAndCreateTargetPullRequest_1.cherrypickAndCreateTargetPullRequest({
215
+ commitsByAuthorCalls =
216
+ (0, nockHelpers_1.mockGqlRequest)({
217
+ name: 'CommitsByAuthor',
218
+ statusCode: 200,
219
+ body: { data: commitsByAuthorMock_1.commitsByAuthorMock },
220
+ });
221
+ res = await (0, cherrypickAndCreateTargetPullRequest_1.cherrypickAndCreateTargetPullRequest)({
206
222
  options,
207
223
  commits: [
208
224
  {
225
+ committedDate: 'eee',
209
226
  sourceBranch: '7.x',
210
227
  sha: 'mySha',
211
- formattedMessage: 'myCommitMessage',
212
228
  originalMessage: 'My original commit message',
213
- targetBranchesFromLabels: [],
214
- existingTargetPullRequests: [],
229
+ expectedTargetPullRequests: [],
215
230
  },
216
231
  ],
217
232
  targetBranch: '6.x',
@@ -220,12 +235,12 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
220
235
  nock_1.default.cleanAll();
221
236
  });
222
237
  it('creates pull request', () => {
223
- expect(res).toEqual({ url: 'myHtmlUrl', number: 1337 });
238
+ expect(res).toEqual({ didUpdate: false, url: 'myHtmlUrl', number: 1337 });
224
239
  });
225
240
  it('shows the right prompts', () => {
226
- expect(promptSpy.mock.calls.length).toBe(3);
241
+ expect(promptSpy.mock.calls.length).toBe(4);
227
242
  expect(promptSpy.mock.calls[0][0]).toMatchInlineSnapshot(`
228
- "Please fix the issues in: /myHomeDir/.backport/repositories/elastic/kibana
243
+ "Fix the following conflicts manually:
229
244
 
230
245
  Conflicting files:
231
246
  - /myHomeDir/.backport/repositories/elastic/kibana/conflicting-file.txt
@@ -234,7 +249,7 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
234
249
  Press ENTER when the conflicts are resolved and files are staged"
235
250
  `);
236
251
  expect(promptSpy.mock.calls[1][0]).toMatchInlineSnapshot(`
237
- "Please fix the issues in: /myHomeDir/.backport/repositories/elastic/kibana
252
+ "Fix the following conflicts manually:
238
253
 
239
254
  Conflicting files:
240
255
  - /myHomeDir/.backport/repositories/elastic/kibana/conflicting-file.txt
@@ -243,13 +258,24 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
243
258
  Press ENTER when the conflicts are resolved and files are staged"
244
259
  `);
245
260
  expect(promptSpy.mock.calls[2][0]).toMatchInlineSnapshot(`
246
- "Please fix the issues in: /myHomeDir/.backport/repositories/elastic/kibana
247
-
261
+ "Fix the following conflicts manually:
248
262
 
249
- Unstaged files:
263
+ Conflicting files:
250
264
  - /myHomeDir/.backport/repositories/elastic/kibana/conflicting-file.txt
251
265
 
266
+
252
267
  Press ENTER when the conflicts are resolved and files are staged"
268
+ `);
269
+ });
270
+ it('commitsByAuthor is called with correct arguments', () => {
271
+ expect(commitsByAuthorCalls[0].variables).toMatchInlineSnapshot(`
272
+ Object {
273
+ "authorId": null,
274
+ "commitPath": "conflicting-file.txt",
275
+ "repoName": "kibana",
276
+ "repoOwner": "elastic",
277
+ "sourceBranch": "myDefaultSourceBranch",
278
+ }
253
279
  `);
254
280
  });
255
281
  it('calls exec correctly', () => {
@@ -260,7 +286,7 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
260
286
  .toMatchInlineSnapshot(`
261
287
  Array [
262
288
  "Pulling latest changes",
263
- "Cherry-picking: myCommitMessage",
289
+ "Cherry-picking: My original commit message",
264
290
  "Finalizing cherrypick",
265
291
  "Pushing branch \\"sqren:backport/6.x/commit-mySha\\"",
266
292
  undefined,
@@ -276,7 +302,7 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
276
302
  `);
277
303
  expect(consoleLogSpy.mock.calls[1][0]).toMatchInlineSnapshot(`
278
304
  "
279
- ----------------------------------------
305
+ The commit could not be backported due to conflicts
280
306
  "
281
307
  `);
282
308
  expect(consoleLogSpy.mock.calls[2][0]).toMatchInlineSnapshot(`
@@ -284,11 +310,199 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
284
310
  ----------------------------------------
285
311
  "
286
312
  `);
287
- expect(consoleLogSpy.mock.calls[3][0]).toMatchInlineSnapshot(`"View pull request: myHtmlUrl"`);
313
+ expect(consoleLogSpy.mock.calls[3][0]).toMatchInlineSnapshot(`
314
+ "
315
+ ----------------------------------------
316
+ "
317
+ `);
288
318
  });
289
319
  });
290
320
  });
291
- function setupExecSpy() {
321
+ describe('getCommitsWithoutBackports', () => {
322
+ afterEach(() => {
323
+ jest.restoreAllMocks();
324
+ });
325
+ describe('expectedTargetPullRequests', () => {
326
+ function setup({ expectedTargetPullRequests, }) {
327
+ // simulate 1 unbackported commit
328
+ jest
329
+ .spyOn(fetchCommitsByAuthorModule, 'fetchCommitsByAuthor')
330
+ .mockResolvedValueOnce([
331
+ {
332
+ committedDate: '10',
333
+ expectedTargetPullRequests,
334
+ originalMessage: 'First commit (#1)',
335
+ sha: 'xyz',
336
+ sourceBranch: 'main',
337
+ pullUrl: 'https://www.github.com/foo',
338
+ },
339
+ ]);
340
+ // simulate commit is definitely not backported
341
+ jest.spyOn(git, 'getIsCommitInBranch').mockResolvedValueOnce(false);
342
+ return (0, cherrypickAndCreateTargetPullRequest_1.getCommitsWithoutBackports)({
343
+ options: {},
344
+ commit: {
345
+ committedDate: '100',
346
+ expectedTargetPullRequests: [],
347
+ originalMessage: 'Second commit (#2)',
348
+ sha: 'abc',
349
+ sourceBranch: 'main',
350
+ },
351
+ targetBranch: '7.x',
352
+ conflictingFiles: ['/foo/bar/baz.ts'],
353
+ });
354
+ }
355
+ it('should display unbackported commits if no backports were created', async () => {
356
+ const commitsWithoutBackports = await setup({
357
+ expectedTargetPullRequests: [],
358
+ });
359
+ expect(commitsWithoutBackports[0].formatted).toEqual(' - First commit (#1)\n https://www.github.com/foo');
360
+ });
361
+ it('should display as pending backport if a backport is OPEN', async () => {
362
+ const commitsWithoutBackports = await setup({
363
+ expectedTargetPullRequests: [{ state: 'OPEN', branch: '7.x' }],
364
+ });
365
+ expect((0, strip_ansi_1.default)(commitsWithoutBackports[0].formatted)).toEqual(' - First commit (#1) (backport pending)\n https://www.github.com/foo');
366
+ });
367
+ it('should not display commit as unbackported if a backport was MERGED', async () => {
368
+ const commitsWithoutBackports = await setup({
369
+ expectedTargetPullRequests: [{ state: 'MERGED', branch: '7.x' }],
370
+ });
371
+ expect(commitsWithoutBackports).toEqual([]);
372
+ });
373
+ });
374
+ describe('commitDate', () => {
375
+ function setup({ offendingCommitDate, currentCommitDate, }) {
376
+ // simulate 1 commit with a pending backport
377
+ jest
378
+ .spyOn(fetchCommitsByAuthorModule, 'fetchCommitsByAuthor')
379
+ .mockResolvedValueOnce([
380
+ {
381
+ committedDate: offendingCommitDate,
382
+ expectedTargetPullRequests: [],
383
+ originalMessage: 'First commit (#1)',
384
+ sha: 'xyz',
385
+ sourceBranch: 'main',
386
+ },
387
+ ]);
388
+ // simulate commit is definitely not backported
389
+ jest.spyOn(git, 'getIsCommitInBranch').mockResolvedValueOnce(false);
390
+ return (0, cherrypickAndCreateTargetPullRequest_1.getCommitsWithoutBackports)({
391
+ options: {},
392
+ commit: {
393
+ committedDate: currentCommitDate,
394
+ expectedTargetPullRequests: [],
395
+ originalMessage: 'Second commit (#2)',
396
+ sha: 'abc',
397
+ sourceBranch: 'main',
398
+ },
399
+ targetBranch: '7.x',
400
+ conflictingFiles: ['/foo/bar/baz.ts'],
401
+ });
402
+ }
403
+ it('should not display commit as unbackport if is newer than the commit currently being backported', async () => {
404
+ const commitsWithoutBackports = await setup({
405
+ offendingCommitDate: '100',
406
+ currentCommitDate: '10',
407
+ });
408
+ expect(commitsWithoutBackports).toEqual([]);
409
+ });
410
+ it('should display commit as unbackport if is older than the commit currently being backported', async () => {
411
+ const commitsWithoutBackports = await setup({
412
+ offendingCommitDate: '10',
413
+ currentCommitDate: '100',
414
+ });
415
+ expect(commitsWithoutBackports[0].formatted).toEqual(' - First commit (#1)');
416
+ });
417
+ });
418
+ describe('isCommitInBranch', () => {
419
+ function setup({ isCommitInBranch }) {
420
+ // simulate 1 unbackported commit
421
+ jest
422
+ .spyOn(fetchCommitsByAuthorModule, 'fetchCommitsByAuthor')
423
+ .mockResolvedValueOnce([
424
+ {
425
+ committedDate: '10',
426
+ expectedTargetPullRequests: [],
427
+ originalMessage: 'First commit (#1)',
428
+ sha: 'xyz',
429
+ sourceBranch: 'main',
430
+ },
431
+ ]);
432
+ // simulate commit is definitely not backported
433
+ jest
434
+ .spyOn(git, 'getIsCommitInBranch')
435
+ .mockResolvedValueOnce(isCommitInBranch);
436
+ return (0, cherrypickAndCreateTargetPullRequest_1.getCommitsWithoutBackports)({
437
+ options: {},
438
+ commit: {
439
+ committedDate: '100',
440
+ expectedTargetPullRequests: [],
441
+ originalMessage: 'Second commit (#2)',
442
+ sha: 'abc',
443
+ sourceBranch: 'main',
444
+ },
445
+ targetBranch: '7.x',
446
+ conflictingFiles: ['/foo/bar/baz.ts'],
447
+ });
448
+ }
449
+ it('should not display commit as unbackport if `isCommitInBranch=true`', async () => {
450
+ const commitsWithoutBackports = await setup({ isCommitInBranch: true });
451
+ expect(commitsWithoutBackports).toEqual([]);
452
+ });
453
+ it('should display commit as unbackport if `isCommitInBranch=false`', async () => {
454
+ const commitsWithoutBackports = await setup({ isCommitInBranch: false });
455
+ expect(commitsWithoutBackports[0].formatted).toEqual(' - First commit (#1)');
456
+ });
457
+ });
458
+ describe('targetBranch', () => {
459
+ function setup({ offendingCommitTargetBranch, currentCommitTargetBranch, }) {
460
+ // simulate 1 commit with a pending backport
461
+ jest
462
+ .spyOn(fetchCommitsByAuthorModule, 'fetchCommitsByAuthor')
463
+ .mockResolvedValueOnce([
464
+ {
465
+ committedDate: '10',
466
+ expectedTargetPullRequests: [
467
+ { branch: offendingCommitTargetBranch, state: 'OPEN' },
468
+ ],
469
+ originalMessage: 'First commit (#1)',
470
+ sha: 'xyz',
471
+ sourceBranch: 'main',
472
+ },
473
+ ]);
474
+ // simulate commit is definitely not backported
475
+ jest.spyOn(git, 'getIsCommitInBranch').mockResolvedValueOnce(false);
476
+ return (0, cherrypickAndCreateTargetPullRequest_1.getCommitsWithoutBackports)({
477
+ options: {},
478
+ commit: {
479
+ committedDate: '100',
480
+ expectedTargetPullRequests: [],
481
+ originalMessage: 'Second commit (#2)',
482
+ sha: 'abc',
483
+ sourceBranch: 'main',
484
+ },
485
+ targetBranch: currentCommitTargetBranch,
486
+ conflictingFiles: ['/foo/bar/baz.ts'],
487
+ });
488
+ }
489
+ it('should display backport as pending if target branches match', async () => {
490
+ const commitsWithoutBackports = await setup({
491
+ offendingCommitTargetBranch: '7.x',
492
+ currentCommitTargetBranch: '7.x',
493
+ });
494
+ expect((0, strip_ansi_1.default)(commitsWithoutBackports[0].formatted)).toEqual(' - First commit (#1) (backport pending)');
495
+ });
496
+ it("should not display backport as pending if target branches don't match", async () => {
497
+ const commitsWithoutBackports = await setup({
498
+ offendingCommitTargetBranch: '7.x',
499
+ currentCommitTargetBranch: '8.x',
500
+ });
501
+ expect((0, strip_ansi_1.default)(commitsWithoutBackports[0].formatted)).toEqual(' - First commit (#1)');
502
+ });
503
+ });
504
+ });
505
+ function setupExecSpyForCherryPick() {
292
506
  let conflictingFilesCheck = 0;
293
507
  let unstagedFilesCheck = 0;
294
508
  return jest
@@ -306,6 +520,10 @@ function setupExecSpy() {
306
520
  if (cmd === 'git cherry-pick mySha') {
307
521
  throw new ExecError_1.ExecError({ cmd });
308
522
  }
523
+ // getIsCommitInBranch
524
+ if (cmd.startsWith('git merge-base --is-ancestor')) {
525
+ return { stderr: '', stdout: '' };
526
+ }
309
527
  // getConflictingFiles
310
528
  if (cmd === 'git --no-pager diff --check') {
311
529
  conflictingFilesCheck++;
@@ -342,6 +560,6 @@ function setupExecSpy() {
342
560
  if (cmd.includes('git branch -D ')) {
343
561
  return { stdout: ``, stderr: '' };
344
562
  }
345
- throw new Error(`Missing mock for "${cmd}"`);
563
+ throw new Error(`Missing exec mock for "${cmd}"`);
346
564
  });
347
565
  }
@@ -1,5 +1,5 @@
1
1
  import { ValidConfigOptions } from '../options/options';
2
- import { Commit } from '../types/Commit';
2
+ import { Commit } from '../services/sourceCommit/parseSourceCommit';
3
3
  export declare function cherrypickAndCreateTargetPullRequest({ options, commits, targetBranch, }: {
4
4
  options: ValidConfigOptions;
5
5
  commits: Commit[];
@@ -7,5 +7,15 @@ export declare function cherrypickAndCreateTargetPullRequest({ options, commits,
7
7
  }): Promise<{
8
8
  url: string;
9
9
  number: number;
10
+ didUpdate: boolean;
10
11
  }>;
12
+ export declare function getCommitsWithoutBackports({ options, commit, targetBranch, conflictingFiles, }: {
13
+ options: ValidConfigOptions;
14
+ commit: Commit;
15
+ targetBranch: string;
16
+ conflictingFiles: string[];
17
+ }): Promise<{
18
+ formatted: string;
19
+ commit: Commit;
20
+ }[]>;
11
21
  export declare function getBackportBranchName(targetBranch: string, commits: Commit[]): string;