backport 8.3.1 → 8.4.2

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 (225) hide show
  1. package/README.md +11 -178
  2. package/dist/lib/cherrypickAndCreateTargetPullRequest.js +42 -21
  3. package/dist/lib/child-process-promisified.js +2 -2
  4. package/dist/lib/git.d.ts +4 -3
  5. package/dist/lib/git.js +22 -29
  6. package/dist/lib/git.private.test.js +10 -7
  7. package/dist/lib/git.test.js +47 -15
  8. package/dist/lib/github/v4/apiRequestV4.js +14 -8
  9. package/dist/lib/github/v4/fetchCommits/allFetchers.private.test.js +1 -0
  10. package/dist/lib/github/v4/getOptionsFromGithub/getOptionsFromGithub.d.ts +1 -0
  11. package/dist/lib/ora.d.ts +1 -0
  12. package/dist/options/ConfigOptions.d.ts +1 -0
  13. package/dist/options/cliArgs.d.ts +1 -0
  14. package/dist/options/cliArgs.js +5 -0
  15. package/dist/options/config/config.d.ts +1 -0
  16. package/dist/options/config/readConfigFile.d.ts +1 -0
  17. package/dist/options/options.d.ts +2 -0
  18. package/dist/options/options.js +3 -2
  19. package/dist/options/options.test.js +5 -4
  20. package/dist/test/e2e/cli/date-filters.private.test.js +1 -0
  21. package/dist/test/e2e/cli/different-merge-strategies.private.test.js +122 -6
  22. package/dist/test/e2e/cli/entrypoint.cli.private.test.js +2 -0
  23. package/dist/test/e2e/cli/error-handling-interactive-mode.private.test.js +27 -13
  24. package/dist/test/e2e/cli/error-handling-non-interactive-mode.private.test.js +3 -2
  25. package/dist/{entrypoint.cli.private.test.d.ts → test/e2e/cli/gracefully-handles-corrupt-repo.private.test.d.ts} +0 -0
  26. package/dist/test/e2e/cli/gracefully-handles-corrupt-repo.private.test.js +52 -0
  27. package/dist/test/e2e/cli/repo-with-backportrc-removed.private.test.js +2 -2
  28. package/dist/{lib/github/v4/enablePullRequestAutoMerge.private.test.d.ts → test/e2e/cli/repo-with-programatically-added-commits.mutation.test.d.ts} +0 -0
  29. package/dist/test/e2e/cli/repo-with-programatically-added-commits.mutation.test.js +114 -0
  30. package/dist/test/e2e/cli/runBackportViaCli.d.ts +8 -2
  31. package/dist/test/e2e/cli/runBackportViaCli.js +47 -17
  32. package/dist/test/e2e/cli/test-that-repo-can-be-cloned.private.test.js +7 -1
  33. package/dist/test/e2e/module/entrypoint.module.mutation.test.js +1 -1
  34. package/dist/test/handleUnbackportedPullRequests.private.test.js +1 -1
  35. package/dist/test/replaceStringAndLinebreaks.d.ts +5 -0
  36. package/dist/test/replaceStringAndLinebreaks.js +8 -0
  37. package/dist/test/sandbox.js +6 -2
  38. package/dist/utils/packageVersion.d.ts +1 -1
  39. package/dist/utils/packageVersion.js +1 -1
  40. package/package.json +23 -24
  41. package/yarn.lock +841 -950
  42. package/dist/entrypoint.cli.private.test.js +0 -47
  43. package/dist/errors/BackportError.d.ts +0 -19
  44. package/dist/errors/BackportError.js +0 -29
  45. package/dist/lib/github/v4/enablePullRequestAutoMerge.private.test.js +0 -112
  46. package/dist/runSequentially.test.d.ts +0 -1
  47. package/dist/runSequentially.test.js +0 -243
  48. package/dist/services/BackportError.d.ts +0 -19
  49. package/dist/services/BackportError.js +0 -29
  50. package/dist/services/HandledBackportError.d.ts +0 -19
  51. package/dist/services/HandledBackportError.js +0 -29
  52. package/dist/services/HandledError.d.ts +0 -19
  53. package/dist/services/HandledError.js +0 -29
  54. package/dist/services/child-process-promisified.d.ts +0 -25
  55. package/dist/services/child-process-promisified.js +0 -58
  56. package/dist/services/env.d.ts +0 -7
  57. package/dist/services/env.js +0 -30
  58. package/dist/services/env.test.d.ts +0 -1
  59. package/dist/services/env.test.js +0 -21
  60. package/dist/services/git.d.ts +0 -77
  61. package/dist/services/git.integration.test.d.ts +0 -1
  62. package/dist/services/git.integration.test.js +0 -616
  63. package/dist/services/git.js +0 -439
  64. package/dist/services/git.private.test.d.ts +0 -1
  65. package/dist/services/git.private.test.js +0 -618
  66. package/dist/services/git.test.d.ts +0 -1
  67. package/dist/services/git.test.js +0 -580
  68. package/dist/services/github/commitFormatters.d.ts +0 -3
  69. package/dist/services/github/commitFormatters.js +0 -17
  70. package/dist/services/github/commitFormatters.test.d.ts +0 -1
  71. package/dist/services/github/commitFormatters.test.js +0 -11
  72. package/dist/services/github/v3/addAssigneesToPullRequest.d.ts +0 -2
  73. package/dist/services/github/v3/addAssigneesToPullRequest.js +0 -32
  74. package/dist/services/github/v3/addAssigneesToPullRequest.test.d.ts +0 -1
  75. package/dist/services/github/v3/addAssigneesToPullRequest.test.js +0 -27
  76. package/dist/services/github/v3/addLabelsToPullRequest.d.ts +0 -2
  77. package/dist/services/github/v3/addLabelsToPullRequest.js +0 -30
  78. package/dist/services/github/v3/addReviewersToPullRequest.d.ts +0 -2
  79. package/dist/services/github/v3/addReviewersToPullRequest.js +0 -32
  80. package/dist/services/github/v3/createPullRequest.d.ts +0 -29
  81. package/dist/services/github/v3/createPullRequest.js +0 -91
  82. package/dist/services/github/v3/createPullRequest.test.d.ts +0 -1
  83. package/dist/services/github/v3/createPullRequest.test.js +0 -377
  84. package/dist/services/github/v3/createStatusComment.d.ts +0 -11
  85. package/dist/services/github/v3/createStatusComment.js +0 -131
  86. package/dist/services/github/v3/createStatusComment.test.d.ts +0 -1
  87. package/dist/services/github/v3/createStatusComment.test.js +0 -409
  88. package/dist/services/github/v3/getGithubV3ErrorMessage.d.ts +0 -14
  89. package/dist/services/github/v3/getGithubV3ErrorMessage.js +0 -16
  90. package/dist/services/github/v4/FetchPullRequestId.d.ts +0 -2
  91. package/dist/services/github/v4/FetchPullRequestId.js +0 -36
  92. package/dist/services/github/v4/apiRequestV4.d.ts +0 -31
  93. package/dist/services/github/v4/apiRequestV4.js +0 -71
  94. package/dist/services/github/v4/apiRequestV4.test.d.ts +0 -1
  95. package/dist/services/github/v4/apiRequestV4.test.js +0 -109
  96. package/dist/services/github/v4/disablePullRequestAutoMerge.d.ts +0 -2
  97. package/dist/services/github/v4/disablePullRequestAutoMerge.js +0 -32
  98. package/dist/services/github/v4/enablePullRequestAutoMerge.d.ts +0 -2
  99. package/dist/services/github/v4/enablePullRequestAutoMerge.js +0 -50
  100. package/dist/services/github/v4/enablePullRequestAutoMerge.private.test.d.ts +0 -1
  101. package/dist/services/github/v4/enablePullRequestAutoMerge.private.test.js +0 -112
  102. package/dist/services/github/v4/fetchAuthorId.d.ts +0 -10
  103. package/dist/services/github/v4/fetchAuthorId.js +0 -28
  104. package/dist/services/github/v4/fetchAuthorId.private.test.d.ts +0 -1
  105. package/dist/services/github/v4/fetchAuthorId.private.test.js +0 -25
  106. package/dist/services/github/v4/fetchCommits/allFetchers.private.test.d.ts +0 -1
  107. package/dist/services/github/v4/fetchCommits/allFetchers.private.test.js +0 -93
  108. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.d.ts +0 -11
  109. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.js +0 -58
  110. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.private.test.d.ts +0 -1
  111. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.private.test.js +0 -136
  112. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.d.ts +0 -11
  113. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.js +0 -48
  114. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.private.test.d.ts +0 -1
  115. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.private.test.js +0 -118
  116. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.d.ts +0 -29
  117. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.js +0 -110
  118. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.private.test.d.ts +0 -1
  119. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.private.test.js +0 -196
  120. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.test.d.ts +0 -1
  121. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.test.js +0 -157
  122. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.d.ts +0 -3
  123. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.js +0 -61
  124. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.private.test.d.ts +0 -1
  125. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.private.test.js +0 -107
  126. package/dist/services/github/v4/fetchCommits/fetchPullRequestsBySearchQuery.d.ts +0 -12
  127. package/dist/services/github/v4/fetchCommits/fetchPullRequestsBySearchQuery.js +0 -69
  128. package/dist/services/github/v4/fetchExistingPullRequest.d.ts +0 -9
  129. package/dist/services/github/v4/fetchExistingPullRequest.js +0 -64
  130. package/dist/services/github/v4/fetchExistingPullRequest.private.test.d.ts +0 -1
  131. package/dist/services/github/v4/fetchExistingPullRequest.private.test.js +0 -44
  132. package/dist/services/github/v4/fetchPullRequestAutoMergeMethod.d.ts +0 -2
  133. package/dist/services/github/v4/fetchPullRequestAutoMergeMethod.js +0 -39
  134. package/dist/services/github/v4/fetchRemoteProjectConfig.d.ts +0 -8
  135. package/dist/services/github/v4/fetchRemoteProjectConfig.js +0 -49
  136. package/dist/services/github/v4/fetchRemoteProjectConfig.private.test.d.ts +0 -1
  137. package/dist/services/github/v4/fetchRemoteProjectConfig.private.test.js +0 -49
  138. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.d.ts +0 -69
  139. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.js +0 -73
  140. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.private.test.d.ts +0 -1
  141. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.private.test.js +0 -116
  142. package/dist/services/github/v4/getOptionsFromGithub/query.d.ts +0 -16
  143. package/dist/services/github/v4/getOptionsFromGithub/query.js +0 -29
  144. package/dist/services/github/v4/getRepoOwnerAndNameFromGitRemotes.d.ts +0 -29
  145. package/dist/services/github/v4/getRepoOwnerAndNameFromGitRemotes.js +0 -61
  146. package/dist/services/github/v4/getRepoOwnerAndNameFromGitRemotes.private.test.d.ts +0 -1
  147. package/dist/services/github/v4/getRepoOwnerAndNameFromGitRemotes.private.test.js +0 -72
  148. package/dist/services/github/v4/mocks/commitsByAuthorMock.d.ts +0 -2
  149. package/dist/services/github/v4/mocks/commitsByAuthorMock.js +0 -176
  150. package/dist/services/github/v4/throwOnInvalidAccessToken.d.ts +0 -6
  151. package/dist/services/github/v4/throwOnInvalidAccessToken.js +0 -45
  152. package/dist/services/github/v4/throwOnInvalidAccessToken.test.d.ts +0 -1
  153. package/dist/services/github/v4/throwOnInvalidAccessToken.test.js +0 -87
  154. package/dist/services/logger.d.ts +0 -9
  155. package/dist/services/logger.js +0 -74
  156. package/dist/services/prompt.test.d.ts +0 -1
  157. package/dist/services/prompt.test.js +0 -139
  158. package/dist/services/prompts.d.ts +0 -17
  159. package/dist/services/prompts.js +0 -132
  160. package/dist/services/remoteConfig.d.ts +0 -20
  161. package/dist/services/remoteConfig.js +0 -52
  162. package/dist/services/sequentially.d.ts +0 -1
  163. package/dist/services/sequentially.js +0 -10
  164. package/dist/services/sourceCommit/getExpectedTargetPullRequests.d.ts +0 -16
  165. package/dist/services/sourceCommit/getExpectedTargetPullRequests.js +0 -102
  166. package/dist/services/sourceCommit/getExpectedTargetPullRequests.test.d.ts +0 -1
  167. package/dist/services/sourceCommit/getExpectedTargetPullRequests.test.js +0 -467
  168. package/dist/services/sourceCommit/getMockSourceCommit.d.ts +0 -26
  169. package/dist/services/sourceCommit/getMockSourceCommit.js +0 -103
  170. package/dist/services/sourceCommit/parseSourceCommit.d.ts +0 -108
  171. package/dist/services/sourceCommit/parseSourceCommit.js +0 -123
  172. package/dist/services/sourceCommit/parseSourceCommit.test.d.ts +0 -1
  173. package/dist/services/sourceCommit/parseSourceCommit.test.js +0 -226
  174. package/dist/services/test.d.ts +0 -1
  175. package/dist/services/test.js +0 -23
  176. package/dist/state.d.ts +0 -0
  177. package/dist/state.js +0 -1
  178. package/dist/test/ExecError.d.ts +0 -16
  179. package/dist/test/ExecError.js +0 -15
  180. package/dist/test/e2e/cli/cli.private.test.d.ts +0 -1
  181. package/dist/test/e2e/cli/cli.private.test.js +0 -222
  182. package/dist/test/e2e/cli/date-filters.d.ts +0 -1
  183. package/dist/test/e2e/cli/date-filters.js +0 -43
  184. package/dist/test/e2e/cli/interactive-error-handling.private.test.d.ts +0 -1
  185. package/dist/test/e2e/cli/interactive-error-handling.private.test.js +0 -80
  186. package/dist/test/e2e/cli/non-interactive-error-handling.private.test.d.ts +0 -1
  187. package/dist/test/e2e/cli/non-interactive-error-handling.private.test.js +0 -198
  188. package/dist/test/e2e/cli.e2e.private.test.d.ts +0 -1
  189. package/dist/test/e2e/cli.e2e.private.test.js +0 -433
  190. package/dist/test/e2e/module/module.mutation.test.d.ts +0 -1
  191. package/dist/test/e2e/module/module.mutation.test.js +0 -369
  192. package/dist/test/e2e/module.e2e.mutation.test.d.ts +0 -1
  193. package/dist/test/e2e/module.e2e.mutation.test.js +0 -369
  194. package/dist/test/e2e/module.e2e.private.test.d.ts +0 -1
  195. package/dist/test/e2e/module.e2e.private.test.js +0 -369
  196. package/dist/ui/cherrypickAndCreateTargetPullRequest.d.ts +0 -13
  197. package/dist/ui/cherrypickAndCreateTargetPullRequest.js +0 -243
  198. package/dist/ui/cherrypickAndCreateTargetPullRequest.test.d.ts +0 -1
  199. package/dist/ui/cherrypickAndCreateTargetPullRequest.test.js +0 -320
  200. package/dist/ui/getCommits.d.ts +0 -2
  201. package/dist/ui/getCommits.js +0 -75
  202. package/dist/ui/getCommitsWithoutBackports.d.ts +0 -11
  203. package/dist/ui/getCommitsWithoutBackports.js +0 -68
  204. package/dist/ui/getCommitsWithoutBackports.test.d.ts +0 -1
  205. package/dist/ui/getCommitsWithoutBackports.test.js +0 -335
  206. package/dist/ui/getGitConfigAuthor.d.ts +0 -6
  207. package/dist/ui/getGitConfigAuthor.js +0 -15
  208. package/dist/ui/getTargetBranches.d.ts +0 -5
  209. package/dist/ui/getTargetBranches.js +0 -67
  210. package/dist/ui/getTargetBranches.test.d.ts +0 -1
  211. package/dist/ui/getTargetBranches.test.js +0 -204
  212. package/dist/ui/maybeSetupRepo.d.ts +0 -2
  213. package/dist/ui/maybeSetupRepo.js +0 -53
  214. package/dist/ui/maybeSetupRepo.test.d.ts +0 -1
  215. package/dist/ui/maybeSetupRepo.test.js +0 -238
  216. package/dist/ui/ora.d.ts +0 -2
  217. package/dist/ui/ora.js +0 -21
  218. package/dist/ui/setupRepo.d.ts +0 -2
  219. package/dist/ui/setupRepo.js +0 -56
  220. package/dist/ui/setupRepo.test.d.ts +0 -1
  221. package/dist/ui/setupRepo.test.js +0 -273
  222. package/dist/utils/filterMissingCommits.d.ts +0 -2
  223. package/dist/utils/filterMissingCommits.js +0 -7
  224. package/dist/utils/redact.d.ts +0 -1
  225. package/dist/utils/redact.js +0 -7
@@ -1,439 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getLocalSourceRepoPath = exports.pushBackportBranch = exports.getRepoForkOwner = exports.deleteBackportBranch = exports.createBackportBranch = exports.getGitConfig = exports.getUnstagedFiles = exports.getConflictingFiles = exports.commitChanges = exports.cherrypick = exports.getCommitsInMergeCommit = exports.getIsMergeCommit = exports.fetchBranch = exports.addRemote = exports.deleteRemote = exports.getIsCommitInBranch = exports.getGitProjectRootPath = exports.getRepoInfoFromGitRemotes = exports.isLocalConfigFileModified = exports.isLocalConfigFileUntracked = exports.getLocalConfigFileCommitDate = exports.cloneRepo = exports.getRemoteUrl = void 0;
4
- const path_1 = require("path");
5
- const lodash_1 = require("lodash");
6
- const ora_1 = require("../ui/ora");
7
- const filterEmpty_1 = require("../utils/filterEmpty");
8
- const BackportError_1 = require("./BackportError");
9
- const child_process_promisified_1 = require("./child-process-promisified");
10
- const env_1 = require("./env");
11
- const commitFormatters_1 = require("./github/commitFormatters");
12
- const logger_1 = require("./logger");
13
- function getRemoteUrl({ repoName, accessToken, gitHostname = 'github.com' }, repoOwner) {
14
- return `https://x-access-token:${accessToken}@${gitHostname}/${repoOwner}/${repoName}.git`;
15
- }
16
- exports.getRemoteUrl = getRemoteUrl;
17
- async function cloneRepo({ sourcePath, targetPath }, onProgress) {
18
- return new Promise((resolve, reject) => {
19
- const subprocess = (0, child_process_promisified_1.spawnOriginal)('git', [
20
- 'clone',
21
- sourcePath,
22
- targetPath,
23
- '--progress',
24
- ]);
25
- const progress = {
26
- fileUpdate: 0,
27
- objectReceive: 0,
28
- };
29
- subprocess.on('error', (err) => reject(err));
30
- subprocess.stderr.on('data', (data) => {
31
- logger_1.logger.verbose(data);
32
- const [, objectReceiveProgress] = data.toString().match(/^Receiving objects:\s+(\d+)%/) || [];
33
- if (objectReceiveProgress) {
34
- progress.objectReceive = parseInt(objectReceiveProgress, 10);
35
- }
36
- const [, fileUpdateProgress] = data.toString().match(/^Updating files:\s+(\d+)%/) || [];
37
- if (fileUpdateProgress) {
38
- progress.objectReceive = 100;
39
- progress.fileUpdate = parseInt(fileUpdateProgress, 10);
40
- }
41
- const progressSum = Math.round(progress.fileUpdate * 0.1 + progress.objectReceive * 0.9);
42
- if (progressSum > 0) {
43
- onProgress(progressSum);
44
- }
45
- });
46
- subprocess.on('close', (code) => {
47
- if (code === 0 || code === null) {
48
- resolve();
49
- }
50
- else {
51
- reject(new Error(`Git clone failed with exit code: ${code}`));
52
- }
53
- });
54
- });
55
- }
56
- exports.cloneRepo = cloneRepo;
57
- async function getLocalConfigFileCommitDate({ cwd }) {
58
- try {
59
- const { stdout } = await (0, child_process_promisified_1.spawnPromise)('git', ['--no-pager', 'log', '-1', '--format=%cd', '.backportrc.json'], cwd);
60
- const timestamp = Date.parse(stdout);
61
- if (timestamp > 0) {
62
- return timestamp;
63
- }
64
- }
65
- catch (e) {
66
- return;
67
- }
68
- }
69
- exports.getLocalConfigFileCommitDate = getLocalConfigFileCommitDate;
70
- async function isLocalConfigFileUntracked({ cwd }) {
71
- try {
72
- // list untracked files
73
- const { stdout } = await (0, child_process_promisified_1.spawnPromise)('git', ['ls-files', '.backportrc.json', '--exclude-standard', '--others'], cwd);
74
- return !!stdout;
75
- }
76
- catch (e) {
77
- return;
78
- }
79
- }
80
- exports.isLocalConfigFileUntracked = isLocalConfigFileUntracked;
81
- async function isLocalConfigFileModified({ cwd }) {
82
- try {
83
- const { stdout } = await (0, child_process_promisified_1.spawnPromise)('git', ['--no-pager', 'diff', 'HEAD', '--name-only', '.backportrc.json'], cwd);
84
- return !!stdout;
85
- }
86
- catch (e) {
87
- return false;
88
- }
89
- }
90
- exports.isLocalConfigFileModified = isLocalConfigFileModified;
91
- async function getRepoInfoFromGitRemotes({ cwd }) {
92
- try {
93
- const { stdout } = await (0, child_process_promisified_1.spawnPromise)('git', ['remote', '--verbose'], cwd);
94
- const remotes = stdout
95
- .split('\n')
96
- .map((line) => {
97
- const match = line.match(/github.com[/|:](.+?)(.git)? \((fetch|push)\)/);
98
- return match?.[1];
99
- })
100
- .filter(filterEmpty_1.filterNil);
101
- return (0, lodash_1.uniq)(remotes).map((remote) => {
102
- const [repoOwner, repoName] = remote.split('/');
103
- return { repoOwner, repoName };
104
- });
105
- }
106
- catch (e) {
107
- return [];
108
- }
109
- }
110
- exports.getRepoInfoFromGitRemotes = getRepoInfoFromGitRemotes;
111
- async function getGitProjectRootPath(dir) {
112
- try {
113
- const cwd = dir;
114
- const { stdout } = await (0, child_process_promisified_1.spawnPromise)('git', ['rev-parse', '--show-toplevel'], cwd);
115
- return stdout.trim();
116
- }
117
- catch (e) {
118
- logger_1.logger.error('An error occurred while retrieving git project root', e);
119
- return;
120
- }
121
- }
122
- exports.getGitProjectRootPath = getGitProjectRootPath;
123
- async function getIsCommitInBranch(options, commitSha) {
124
- try {
125
- const cwd = (0, env_1.getRepoPath)(options);
126
- await (0, child_process_promisified_1.spawnPromise)('git', ['merge-base', '--is-ancestor', commitSha, 'HEAD'], cwd);
127
- return true;
128
- }
129
- catch (e) {
130
- const isSpawnError = e instanceof child_process_promisified_1.SpawnError;
131
- if (isSpawnError) {
132
- const commitNotInBranch = e.context.code === 1 && e.context.stderr === '';
133
- const commitNotExist = e.context.code === 128 &&
134
- e.context.stderr.includes('Not a valid object name');
135
- if (commitNotInBranch || commitNotExist) {
136
- return false;
137
- }
138
- }
139
- throw e;
140
- }
141
- }
142
- exports.getIsCommitInBranch = getIsCommitInBranch;
143
- async function deleteRemote(options, remoteName) {
144
- try {
145
- const cwd = (0, env_1.getRepoPath)(options);
146
- await (0, child_process_promisified_1.spawnPromise)('git', ['remote', 'rm', remoteName], cwd);
147
- }
148
- catch (e) {
149
- const isSpawnError = e instanceof child_process_promisified_1.SpawnError;
150
- if (isSpawnError &&
151
- e.context.code > 0 &&
152
- e.context.stderr.includes('No such remote')) {
153
- return;
154
- }
155
- // re-throw
156
- throw e;
157
- }
158
- }
159
- exports.deleteRemote = deleteRemote;
160
- async function addRemote(options, remoteName) {
161
- try {
162
- const cwd = (0, env_1.getRepoPath)(options);
163
- await (0, child_process_promisified_1.spawnPromise)('git', ['remote', 'add', remoteName, getRemoteUrl(options, remoteName)], cwd);
164
- }
165
- catch (e) {
166
- // note: swallowing error
167
- return;
168
- }
169
- }
170
- exports.addRemote = addRemote;
171
- async function fetchBranch(options, branch) {
172
- const cwd = (0, env_1.getRepoPath)(options);
173
- await (0, child_process_promisified_1.spawnPromise)('git', ['fetch', options.repoOwner, `${branch}:${branch}`, '--force'], cwd);
174
- }
175
- exports.fetchBranch = fetchBranch;
176
- async function getIsMergeCommit(options, sha) {
177
- const cwd = (0, env_1.getRepoPath)(options);
178
- const res = await (0, child_process_promisified_1.spawnPromise)('git', ['rev-list', '-1', '--merges', `${sha}~1..${sha}`], cwd);
179
- return res.stdout !== '';
180
- }
181
- exports.getIsMergeCommit = getIsMergeCommit;
182
- async function getCommitsInMergeCommit(options, sha) {
183
- try {
184
- const cwd = (0, env_1.getRepoPath)(options);
185
- const res = await (0, child_process_promisified_1.spawnPromise)('git', ['--no-pager', 'log', `${sha}^1..${sha}^2`, '--pretty=format:%H'], cwd);
186
- return res.stdout.split('\n');
187
- }
188
- catch (e) {
189
- const isSpawnError = e instanceof child_process_promisified_1.SpawnError;
190
- // swallow error
191
- if (isSpawnError && e.context.code === 128) {
192
- return [];
193
- }
194
- throw e;
195
- }
196
- }
197
- exports.getCommitsInMergeCommit = getCommitsInMergeCommit;
198
- async function getShaOrRange(options, sha) {
199
- if (!options.mainline) {
200
- try {
201
- const isMergeCommit = await getIsMergeCommit(options, sha);
202
- if (isMergeCommit) {
203
- const shas = await getCommitsInMergeCommit(options, sha);
204
- return `${(0, lodash_1.last)(shas)}^..${(0, lodash_1.first)(shas)}`;
205
- }
206
- }
207
- catch (e) {
208
- const isSpawnError = e instanceof child_process_promisified_1.SpawnError;
209
- // swallow error if it's a 128 exit code
210
- // exit 128 happens when the cherrypicked commit is empty which is handled below
211
- // (128 also applies to many other types of errors)
212
- if (!isSpawnError || e.context.code !== 128) {
213
- throw e;
214
- }
215
- }
216
- }
217
- return sha;
218
- }
219
- async function cherrypick({ options, sha, mergedTargetPullRequest, commitAuthor, }) {
220
- const shaOrRange = await getShaOrRange(options, sha);
221
- const cmdArgs = [
222
- '-c',
223
- `user.name="${commitAuthor.name}"`,
224
- '-c',
225
- `user.email="${commitAuthor.email}"`,
226
- `cherry-pick`,
227
- ...(options.mainline != undefined
228
- ? ['--mainline', `${options.mainline}`]
229
- : []),
230
- ...(options.cherrypickRef === false ? [] : ['-x']),
231
- shaOrRange,
232
- ];
233
- try {
234
- const cwd = (0, env_1.getRepoPath)(options);
235
- await (0, child_process_promisified_1.spawnPromise)('git', cmdArgs, cwd);
236
- return { conflictingFiles: [], unstagedFiles: [], needsResolving: false };
237
- }
238
- catch (e) {
239
- const isSpawnError = e instanceof child_process_promisified_1.SpawnError;
240
- if (isSpawnError) {
241
- // missing `mainline` option
242
- if (e.message.includes('is a merge but no -m option was given')) {
243
- throw new BackportError_1.BackportError('Cherrypick failed because the selected commit was a merge commit. Please try again by specifying the parent with the `mainline` argument:\n\n> backport --mainline\n\nor:\n\n> backport --mainline <parent-number>\n\nOr refer to the git documentation for more information: https://git-scm.com/docs/git-cherry-pick#Documentation/git-cherry-pick.txt---mainlineparent-number');
244
- }
245
- // commit was already backported
246
- if (e.message.includes('The previous cherry-pick is now empty')) {
247
- const shortSha = (0, commitFormatters_1.getShortSha)(sha);
248
- throw new BackportError_1.BackportError(`Cherrypick failed because the selected commit (${shortSha}) is empty. ${mergedTargetPullRequest?.url
249
- ? `It looks like the commit was already backported in ${mergedTargetPullRequest.url}`
250
- : 'Did you already backport this commit? '}`);
251
- }
252
- if (e.message.includes(`bad object ${sha}`)) {
253
- throw new BackportError_1.BackportError(`Cherrypick failed because commit "${sha}" was not found`);
254
- }
255
- const isCherryPickError = e.context.cmdArgs.includes('cherry-pick') && e.context.code > 0;
256
- if (isCherryPickError) {
257
- const [conflictingFiles, unstagedFiles] = await Promise.all([
258
- getConflictingFiles(options),
259
- getUnstagedFiles(options),
260
- ]);
261
- if (!(0, lodash_1.isEmpty)(conflictingFiles) || !(0, lodash_1.isEmpty)(unstagedFiles))
262
- return { conflictingFiles, unstagedFiles, needsResolving: true };
263
- }
264
- }
265
- // re-throw error if it didn't match the handled cases above
266
- throw e;
267
- }
268
- }
269
- exports.cherrypick = cherrypick;
270
- async function commitChanges(commit, options) {
271
- const noVerifyFlag = options.noVerify ? ['--no-verify'] : [];
272
- const cwd = (0, env_1.getRepoPath)(options);
273
- try {
274
- await (0, child_process_promisified_1.spawnPromise)('git', [
275
- 'commit',
276
- '--no-edit',
277
- ...noVerifyFlag, // bypass pre-commit and commit-msg hooks
278
- ], cwd);
279
- }
280
- catch (e) {
281
- const isSpawnError = e instanceof child_process_promisified_1.SpawnError;
282
- if (isSpawnError) {
283
- if (e.context.stdout.includes('nothing to commit')) {
284
- logger_1.logger.info(`Could not run "git commit". Probably because the changes were manually committed`, e);
285
- return;
286
- }
287
- // manually set the commit message if the inferred commit message is empty
288
- // this can happen if the user runs `git reset HEAD` and thereby aborts the cherrypick process
289
- if (e.context.stderr.includes('Aborting commit due to empty commit message')) {
290
- await (0, child_process_promisified_1.spawnPromise)('git', [
291
- 'commit',
292
- `--message=${commit.sourceCommit.message}`,
293
- ...noVerifyFlag, // bypass pre-commit and commit-msg hooks
294
- ], cwd);
295
- return;
296
- }
297
- }
298
- // rethrow error if it can't be handled
299
- throw e;
300
- }
301
- }
302
- exports.commitChanges = commitChanges;
303
- async function getConflictingFiles(options) {
304
- const repoPath = (0, env_1.getRepoPath)(options);
305
- try {
306
- const cwd = repoPath;
307
- await (0, child_process_promisified_1.spawnPromise)('git', ['--no-pager', 'diff', '--check'], cwd);
308
- return [];
309
- }
310
- catch (e) {
311
- const isSpawnError = e instanceof child_process_promisified_1.SpawnError;
312
- const isConflictError = isSpawnError && e.context.code === 2;
313
- if (isConflictError) {
314
- const files = e.context.stdout
315
- .split('\n')
316
- .filter((line) => !!line.trim() && !line.startsWith('+') && !line.startsWith('-'))
317
- .map((line) => {
318
- const posSeparator = line.indexOf(':');
319
- const filename = line.slice(0, posSeparator).trim();
320
- return filename;
321
- });
322
- const uniqueFiles = (0, lodash_1.uniq)(files);
323
- return uniqueFiles.map((file) => {
324
- return {
325
- absolute: (0, path_1.resolve)(repoPath, file),
326
- relative: file,
327
- };
328
- });
329
- }
330
- // rethrow error since it's unrelated
331
- throw e;
332
- }
333
- }
334
- exports.getConflictingFiles = getConflictingFiles;
335
- // retrieve the list of files that could not be cleanly merged
336
- async function getUnstagedFiles(options) {
337
- const repoPath = (0, env_1.getRepoPath)(options);
338
- const cwd = repoPath;
339
- const res = await (0, child_process_promisified_1.spawnPromise)('git', ['--no-pager', 'diff', '--name-only'], cwd);
340
- const files = res.stdout
341
- .split('\n')
342
- .filter((file) => !!file)
343
- .map((file) => (0, path_1.resolve)(repoPath, file));
344
- return (0, lodash_1.uniq)(files);
345
- }
346
- exports.getUnstagedFiles = getUnstagedFiles;
347
- async function getGitConfig({ dir, key, }) {
348
- try {
349
- const cwd = dir;
350
- const res = await (0, child_process_promisified_1.spawnPromise)('git', ['config', key], cwd);
351
- return res.stdout.trim();
352
- }
353
- catch (e) {
354
- return;
355
- }
356
- }
357
- exports.getGitConfig = getGitConfig;
358
- // How the commit flows:
359
- // ${sourceBranch} -> ${backportBranch} -> ${targetBranch}
360
- // master -> backport/7.x/pr-1234 -> 7.x
361
- async function createBackportBranch({ options, targetBranch, backportBranch, }) {
362
- const spinner = (0, ora_1.ora)(options.ci, 'Pulling latest changes').start();
363
- try {
364
- const cwd = (0, env_1.getRepoPath)(options);
365
- await (0, child_process_promisified_1.spawnPromise)('git', ['reset', '--hard'], cwd);
366
- await (0, child_process_promisified_1.spawnPromise)('git', ['clean', '-d', '--force'], cwd);
367
- await (0, child_process_promisified_1.spawnPromise)('git', ['fetch', options.repoOwner, targetBranch], cwd);
368
- await (0, child_process_promisified_1.spawnPromise)('git', [
369
- 'checkout',
370
- '-B',
371
- backportBranch,
372
- `${options.repoOwner}/${targetBranch}`,
373
- '--no-track',
374
- ], cwd);
375
- spinner.succeed();
376
- }
377
- catch (e) {
378
- spinner.fail();
379
- if (e instanceof child_process_promisified_1.SpawnError) {
380
- const isBranchInvalid = e.context.stderr.toLowerCase().includes(`couldn't find remote ref`) ||
381
- e.context.stderr.toLowerCase().includes(`invalid refspec`) ||
382
- e.context.stderr
383
- .toLowerCase()
384
- .includes(`is not a commit and a branch '${backportBranch}' cannot be created from it`);
385
- if (isBranchInvalid) {
386
- throw new BackportError_1.BackportError(`The branch "${targetBranch}" is invalid or doesn't exist`);
387
- }
388
- }
389
- throw e;
390
- }
391
- }
392
- exports.createBackportBranch = createBackportBranch;
393
- async function deleteBackportBranch({ options, backportBranch, }) {
394
- const spinner = (0, ora_1.ora)(options.ci).start();
395
- const cwd = (0, env_1.getRepoPath)(options);
396
- await (0, child_process_promisified_1.spawnPromise)('git', ['reset', '--hard'], cwd);
397
- await (0, child_process_promisified_1.spawnPromise)('git', ['checkout', options.sourceBranch], cwd);
398
- await (0, child_process_promisified_1.spawnPromise)('git', ['branch', '--delete', '--force', backportBranch], cwd);
399
- spinner.stop();
400
- }
401
- exports.deleteBackportBranch = deleteBackportBranch;
402
- /*
403
- * Returns the repo owner of the forked repo or the source repo
404
- */
405
- function getRepoForkOwner(options) {
406
- return options.fork ? options.repoForkOwner : options.repoOwner;
407
- }
408
- exports.getRepoForkOwner = getRepoForkOwner;
409
- async function pushBackportBranch({ options, backportBranch, }) {
410
- const repoForkOwner = getRepoForkOwner(options);
411
- const spinner = (0, ora_1.ora)(options.ci, `Pushing branch "${repoForkOwner}:${backportBranch}"`).start();
412
- try {
413
- const cwd = (0, env_1.getRepoPath)(options);
414
- const res = await (0, child_process_promisified_1.spawnPromise)('git', ['push', repoForkOwner, `${backportBranch}:${backportBranch}`, '--force'], cwd);
415
- spinner.succeed();
416
- return res;
417
- }
418
- catch (e) {
419
- spinner.fail();
420
- if (e instanceof child_process_promisified_1.SpawnError &&
421
- e.context.stderr.toLowerCase().includes(`repository not found`)) {
422
- throw new BackportError_1.BackportError(`Error pushing to https://github.com/${repoForkOwner}/${options.repoName}. Repository does not exist. Either fork the repository (https://github.com/${options.repoOwner}/${options.repoName}) or disable fork mode via "--no-fork".\nRead more about fork mode in the docs: https://github.com/sqren/backport/blob/main/docs/configuration.md#fork`);
423
- }
424
- if (e instanceof child_process_promisified_1.SpawnError &&
425
- e.context.stderr.includes(`could not read Username for`)) {
426
- throw new BackportError_1.BackportError(`Invalid credentials: ${e.message}`);
427
- }
428
- throw e;
429
- }
430
- }
431
- exports.pushBackportBranch = pushBackportBranch;
432
- // retrieve path to local repo (cwd) if it matches `repoName` / `repoOwner`
433
- async function getLocalSourceRepoPath(options) {
434
- const remotes = await getRepoInfoFromGitRemotes({ cwd: options.cwd });
435
- const hasMatchingGitRemote = remotes.some((remote) => remote.repoName === options.repoName &&
436
- remote.repoOwner === options.repoOwner);
437
- return hasMatchingGitRemote ? getGitProjectRootPath(options.cwd) : undefined;
438
- }
439
- exports.getLocalSourceRepoPath = getLocalSourceRepoPath;
@@ -1 +0,0 @@
1
- export {};