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
@@ -0,0 +1,139 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getOptionsFromGithub = void 0;
7
+ const ora_1 = __importDefault(require("ora"));
8
+ const parseRequiredOptions_1 = require("../../../../options/parseRequiredOptions");
9
+ const filterEmpty_1 = require("../../../../utils/filterEmpty");
10
+ const HandledError_1 = require("../../../HandledError");
11
+ const git_1 = require("../../../git");
12
+ const logger_1 = require("../../../logger");
13
+ const apiRequestV4_1 = require("../apiRequestV4");
14
+ const throwOnInvalidAccessToken_1 = require("../throwOnInvalidAccessToken");
15
+ const query_1 = require("./query");
16
+ async function getOptionsFromGithub(optionsFromConfigFiles, optionsFromCliArgs) {
17
+ const options = {
18
+ ...optionsFromConfigFiles,
19
+ ...optionsFromCliArgs,
20
+ };
21
+ const { githubApiBaseUrlV4 } = options;
22
+ const { accessToken, repoName, repoOwner } = (0, parseRequiredOptions_1.getRequiredOptions)(options);
23
+ let res;
24
+ const spinner = (0, ora_1.default)().start('Initializing...');
25
+ try {
26
+ res = await (0, apiRequestV4_1.apiRequestV4)({
27
+ githubApiBaseUrlV4,
28
+ accessToken,
29
+ query: query_1.query,
30
+ variables: { repoOwner, repoName },
31
+ handleError: false,
32
+ });
33
+ spinner.stop();
34
+ }
35
+ catch (e) {
36
+ spinner.stop();
37
+ const error = e;
38
+ (0, throwOnInvalidAccessToken_1.throwOnInvalidAccessToken)({
39
+ error,
40
+ repoName,
41
+ repoOwner,
42
+ });
43
+ const configFileNotFound = error.response?.data.errors?.some((error) => error.type === 'NOT_FOUND' &&
44
+ error.path.join('.') ===
45
+ 'repository.defaultBranchRef.target.history.edges.0.remoteConfig.file');
46
+ if (configFileNotFound && error.response?.data.data) {
47
+ // swallow error if caused by missing config file on Github
48
+ res = error.response.data.data;
49
+ }
50
+ else {
51
+ // throw generic Github error
52
+ throw (0, apiRequestV4_1.handleGithubV4Error)(error);
53
+ }
54
+ }
55
+ // get the original repo (not the fork)
56
+ const repo = res.repository.isFork ? res.repository.parent : res.repository;
57
+ // it is not possible to have a branch named "backport"
58
+ if (repo.ref?.name === 'backport') {
59
+ throw new HandledError_1.HandledError('You must delete the branch "backport" to continue. See https://github.com/sqren/backport/issues/155 for details');
60
+ }
61
+ const historicalRemoteConfigs = repo.defaultBranchRef.target.history.edges;
62
+ const latestRemoteConfig = historicalRemoteConfigs[0]?.remoteConfig;
63
+ const skipRemoteConfig = await getSkipRemoteConfigFile(options, latestRemoteConfig);
64
+ const defaultBranch = repo.defaultBranchRef.name;
65
+ // if no sourceBranch is given, use the `defaultBranch` (normally "master")
66
+ const sourceBranch = options.sourceBranch
67
+ ? options.sourceBranch
68
+ : defaultBranch;
69
+ return {
70
+ sourceBranch,
71
+ ...(skipRemoteConfig ? {} : parseRemoteConfig(latestRemoteConfig)),
72
+ historicalBranchLabelMappings: skipRemoteConfig
73
+ ? []
74
+ : getHistoricalBranchLabelMappings(historicalRemoteConfigs),
75
+ };
76
+ }
77
+ exports.getOptionsFromGithub = getOptionsFromGithub;
78
+ async function getSkipRemoteConfigFile(options, remoteConfig) {
79
+ if (options.forceLocalConfig) {
80
+ logger_1.logger.info('Skipping remote config: `--force-local-config` specified via config file or cli');
81
+ return true;
82
+ }
83
+ if (!remoteConfig) {
84
+ logger_1.logger.info("Skipping remote config: remote config doesn't exist");
85
+ return true;
86
+ }
87
+ const [isLocalConfigModified, isLocalConfigUntracked, localCommitDate] = await Promise.all([
88
+ (0, git_1.isLocalConfigFileModified)(),
89
+ (0, git_1.isLocalConfigFileUntracked)(),
90
+ (0, git_1.getLocalConfigFileCommitDate)(),
91
+ ]);
92
+ if (isLocalConfigUntracked) {
93
+ logger_1.logger.info('Skipping remote config: local config is new');
94
+ return true;
95
+ }
96
+ if (isLocalConfigModified) {
97
+ logger_1.logger.info('Skipping remote config: local config is modified');
98
+ return true;
99
+ }
100
+ if (localCommitDate &&
101
+ localCommitDate > Date.parse(remoteConfig.committedDate)) {
102
+ logger_1.logger.info(`Skipping remote config: local config is newer: ${new Date(localCommitDate).toISOString()} > ${remoteConfig.committedDate}`);
103
+ return true;
104
+ }
105
+ return false;
106
+ }
107
+ function parseRemoteConfig(remoteConfig) {
108
+ if (!remoteConfig) {
109
+ return;
110
+ }
111
+ try {
112
+ logger_1.logger.info('Using remote config');
113
+ return JSON.parse(remoteConfig.file.object.text);
114
+ }
115
+ catch (e) {
116
+ logger_1.logger.info('Parsing remote config failed', e);
117
+ return;
118
+ }
119
+ }
120
+ function getHistoricalBranchLabelMappings(historicalRemoteConfigs) {
121
+ return historicalRemoteConfigs
122
+ .map((edge) => {
123
+ try {
124
+ const remoteConfig = JSON.parse(edge.remoteConfig.file.object.text);
125
+ if (!remoteConfig.branchLabelMapping) {
126
+ return;
127
+ }
128
+ return {
129
+ branchLabelMapping: remoteConfig.branchLabelMapping,
130
+ committedDate: edge.remoteConfig.committedDate,
131
+ };
132
+ }
133
+ catch (e) {
134
+ logger_1.logger.info('Could not get historical remote config', e);
135
+ return;
136
+ }
137
+ })
138
+ .filter(filterEmpty_1.filterNil);
139
+ }
@@ -0,0 +1,126 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const os_1 = __importDefault(require("os"));
7
+ const getDevAccessToken_1 = require("../../../../test/private/getDevAccessToken");
8
+ const _1 = require(".");
9
+ describe('getOptionsFromGithub', () => {
10
+ let devAccessToken;
11
+ beforeAll(async () => {
12
+ devAccessToken = await (0, getDevAccessToken_1.getDevAccessToken)();
13
+ jest.spyOn(os_1.default, 'homedir').mockReturnValue('/myHomeDir');
14
+ });
15
+ describe('access token', () => {
16
+ describe('is invalid', () => {
17
+ it('throws an error', async () => {
18
+ const optionsFromConfigFiles = {
19
+ username: 'sqren',
20
+ accessToken: 'foo',
21
+ githubApiBaseUrlV4: 'https://api.github.com/graphql',
22
+ upstream: 'backport-org/backport-e2e',
23
+ };
24
+ const optionsFromCliArgs = {};
25
+ await expect((0, _1.getOptionsFromGithub)(optionsFromConfigFiles, optionsFromCliArgs)).rejects.toThrowError('Please check your access token and make sure it is valid.\nConfig: /myHomeDir/.backport/config.json');
26
+ });
27
+ });
28
+ describe('is valid', () => {
29
+ it('returns the options', async () => {
30
+ const optionsFromConfigFiles = {
31
+ username: 'sqren',
32
+ accessToken: devAccessToken,
33
+ githubApiBaseUrlV4: 'https://api.github.com/graphql',
34
+ upstream: 'backport-org/backport-e2e',
35
+ };
36
+ const optionsFromCliArgs = {};
37
+ expect(await (0, _1.getOptionsFromGithub)(optionsFromConfigFiles, optionsFromCliArgs)).toEqual({
38
+ branchLabelMapping: {
39
+ '^v(\\d+).(\\d+).\\d+$': '$1.$2',
40
+ '^v7.9.0$': '7.x',
41
+ '^v8.0.0$': 'master',
42
+ },
43
+ sourceBranch: 'master',
44
+ targetBranchChoices: [
45
+ { checked: true, name: 'master' },
46
+ { checked: true, name: '7.x' },
47
+ '7.8',
48
+ ],
49
+ targetPRLabels: ['backport'],
50
+ upstream: 'backport-org/backport-e2e',
51
+ });
52
+ });
53
+ });
54
+ });
55
+ describe('when using fork as upstream', () => {
56
+ it('uses upstream of parent', async () => {
57
+ const optionsFromConfigFiles = {
58
+ username: 'sqren',
59
+ accessToken: devAccessToken,
60
+ githubApiBaseUrlV4: 'https://api.github.com/graphql',
61
+ upstream: 'sqren/backport-e2e',
62
+ };
63
+ const optionsFromCliArgs = {};
64
+ const options = await (0, _1.getOptionsFromGithub)(optionsFromConfigFiles, optionsFromCliArgs);
65
+ await expect(options.upstream).toEqual('backport-org/backport-e2e');
66
+ });
67
+ });
68
+ describe('sourceBranch', () => {
69
+ describe('when no sourceBranch is specified', () => {
70
+ it('uses the default branch of the repo', async () => {
71
+ const optionsFromConfigFiles = {
72
+ username: 'sqren',
73
+ accessToken: devAccessToken,
74
+ githubApiBaseUrlV4: 'https://api.github.com/graphql',
75
+ upstream: 'backport-org/repo-with-non-standard-main-branch',
76
+ };
77
+ const optionsFromCliArgs = {};
78
+ const options = await (0, _1.getOptionsFromGithub)(optionsFromConfigFiles, optionsFromCliArgs);
79
+ await expect(options.sourceBranch).toEqual('my-custom-default-branch');
80
+ });
81
+ });
82
+ describe('when sourceBranch is specified', () => {
83
+ it('uses that as sourceBranch if nothing else is specified', async () => {
84
+ const optionsFromConfigFiles = {
85
+ sourceBranch: 'my-specific-source-branch',
86
+ username: 'sqren',
87
+ accessToken: devAccessToken,
88
+ githubApiBaseUrlV4: 'https://api.github.com/graphql',
89
+ upstream: 'backport-org/repo-with-non-standard-main-branch',
90
+ };
91
+ const optionsFromCliArgs = {};
92
+ const options = await (0, _1.getOptionsFromGithub)(optionsFromConfigFiles, optionsFromCliArgs);
93
+ await expect(options.sourceBranch).toEqual('my-specific-source-branch');
94
+ });
95
+ });
96
+ });
97
+ describe('when a branch named "backport"', () => {
98
+ describe('exists', () => {
99
+ it('shows a warning', async () => {
100
+ const optionsFromConfigFiles = {
101
+ username: 'sqren',
102
+ accessToken: devAccessToken,
103
+ githubApiBaseUrlV4: 'https://api.github.com/graphql',
104
+ upstream: 'backport-org/repo-with-branch-named-backport',
105
+ };
106
+ const optionsFromCliArgs = {};
107
+ await expect(async () => {
108
+ await (0, _1.getOptionsFromGithub)(optionsFromConfigFiles, optionsFromCliArgs);
109
+ }).rejects.toThrowError('You must delete the branch "backport" to continue. See https://github.com/sqren/backport/issues/155 for details');
110
+ });
111
+ });
112
+ });
113
+ describe('when a backportrc.json config was deleted from repo', () => {
114
+ it('does not throw', async () => {
115
+ const optionsFromConfigFiles = {
116
+ username: 'sqren',
117
+ accessToken: devAccessToken,
118
+ githubApiBaseUrlV4: 'https://api.github.com/graphql',
119
+ upstream: 'backport-org/repo-with-backportrc-removed',
120
+ };
121
+ const optionsFromCliArgs = {};
122
+ const options = await (0, _1.getOptionsFromGithub)(optionsFromConfigFiles, optionsFromCliArgs);
123
+ expect(options).toEqual({ sourceBranch: 'main' });
124
+ });
125
+ });
126
+ });
@@ -0,0 +1,34 @@
1
+ export declare type RemoteConfig = {
2
+ committedDate: string;
3
+ file: {
4
+ object: {
5
+ text: string;
6
+ };
7
+ };
8
+ };
9
+ export declare type Repository = {
10
+ ref: {
11
+ name: string;
12
+ } | null;
13
+ defaultBranchRef: {
14
+ name: string;
15
+ target: {
16
+ history: {
17
+ edges: Array<{
18
+ remoteConfig: RemoteConfig;
19
+ }>;
20
+ };
21
+ };
22
+ };
23
+ };
24
+ export interface GithubConfigOptionsResponse {
25
+ repository: {
26
+ isFork: true;
27
+ defaultBranchRef: null;
28
+ parent: Repository;
29
+ } | ({
30
+ isFork: false;
31
+ parent: null;
32
+ } & Repository);
33
+ }
34
+ export declare const query = "\n query GithubConfigOptions($repoOwner: String!, $repoName: String!) {\n repository(owner: $repoOwner, name: $repoName) {\n isFork\n ...Repo\n parent {\n ...Repo\n }\n }\n }\n\n fragment Repo on Repository {\n # check to see if a branch named \"backport\" exists\n ref(qualifiedName: \"refs/heads/backport\") {\n name\n }\n defaultBranchRef {\n name\n target {\n ... on Commit {\n history(first: 20, path: \".backportrc.json\") {\n edges {\n remoteConfig: node {\n committedDate\n file(path: \".backportrc.json\") {\n ... on TreeEntry {\n object {\n ... on Blob {\n text\n }\n }\n }\n }\n }\n }\n }\n }\n }\n }\n }\n";
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.query = void 0;
4
+ exports.query = `
5
+ query GithubConfigOptions($repoOwner: String!, $repoName: String!) {
6
+ repository(owner: $repoOwner, name: $repoName) {
7
+ isFork
8
+ ...Repo
9
+ parent {
10
+ ...Repo
11
+ }
12
+ }
13
+ }
14
+
15
+ fragment Repo on Repository {
16
+ # check to see if a branch named "backport" exists
17
+ ref(qualifiedName: "refs/heads/backport") {
18
+ name
19
+ }
20
+ defaultBranchRef {
21
+ name
22
+ target {
23
+ ... on Commit {
24
+ history(first: 20, path: ".backportrc.json") {
25
+ edges {
26
+ remoteConfig: node {
27
+ committedDate
28
+ file(path: ".backportrc.json") {
29
+ ... on TreeEntry {
30
+ object {
31
+ ... on Blob {
32
+ text
33
+ }
34
+ }
35
+ }
36
+ }
37
+ }
38
+ }
39
+ }
40
+ }
41
+ }
42
+ }
43
+ }
44
+ `;
@@ -1,12 +1,5 @@
1
1
  import { ValidConfigOptions } from '../../../options/options';
2
- import { ExistingTargetPullRequests } from './getExistingTargetPullRequests';
3
2
  export declare function getTargetBranchForLabel({ branchLabelMapping, label, }: {
4
3
  branchLabelMapping: NonNullable<ValidConfigOptions['branchLabelMapping']>;
5
4
  label: string;
6
5
  }): string | undefined;
7
- export declare function getTargetBranchesFromLabels({ sourceBranch, existingTargetPullRequests, branchLabelMapping, labels, }: {
8
- sourceBranch: string;
9
- existingTargetPullRequests: ExistingTargetPullRequests;
10
- branchLabelMapping: ValidConfigOptions['branchLabelMapping'];
11
- labels?: string[];
12
- }): string[];
@@ -1,12 +1,6 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.getTargetBranchesFromLabels = exports.getTargetBranchForLabel = void 0;
7
- const lodash_uniq_1 = __importDefault(require("lodash.uniq"));
8
- const filterEmpty_1 = require("../../../utils/filterEmpty");
9
- const logger_1 = require("../../logger");
3
+ exports.getTargetBranchForLabel = void 0;
10
4
  function getTargetBranchForLabel({ branchLabelMapping, label, }) {
11
5
  // only get first match
12
6
  const result = Object.entries(branchLabelMapping).find(([labelPattern]) => {
@@ -24,22 +18,3 @@ function getTargetBranchForLabel({ branchLabelMapping, label, }) {
24
18
  }
25
19
  }
26
20
  exports.getTargetBranchForLabel = getTargetBranchForLabel;
27
- function getTargetBranchesFromLabels({ sourceBranch, existingTargetPullRequests, branchLabelMapping, labels, }) {
28
- if (!branchLabelMapping || !labels) {
29
- return [];
30
- }
31
- const existingBranches = existingTargetPullRequests.map((pr) => pr.branch);
32
- const targetBranches = labels
33
- .map((label) => getTargetBranchForLabel({ branchLabelMapping, label }))
34
- .filter(filterEmpty_1.filterNil)
35
- .filter((targetBranch) => !existingBranches.includes(targetBranch))
36
- .filter((targetBranch) => targetBranch !== sourceBranch);
37
- logger_1.logger.info('Inputs when calculating target branches:', {
38
- labels,
39
- branchLabelMapping,
40
- existingTargetPullRequests,
41
- });
42
- logger_1.logger.info('Target branches inferred from labels:', targetBranches);
43
- return lodash_uniq_1.default(targetBranches);
44
- }
45
- exports.getTargetBranchesFromLabels = getTargetBranchesFromLabels;
@@ -4,11 +4,11 @@ const getTargetBranchesFromLabels_1 = require("./getTargetBranchesFromLabels");
4
4
  describe('getTargetBranchesFromLabels', () => {
5
5
  it(`should support Kibana's label format`, () => {
6
6
  const sourceBranch = 'master';
7
- const existingTargetPullRequests = [];
7
+ const expectedTargetPullRequests = [];
8
8
  const branchLabelMapping = {
9
9
  'v8.0.0': 'master',
10
10
  '^v7.8.0$': '7.x',
11
- '^v(\\d+).(\\d+).\\d+$': '$1.$2',
11
+ '^v(\\d+).(\\d+).\\d+$': '$1.$2', // all other branches
12
12
  };
13
13
  const labels = [
14
14
  'release_note:fix',
@@ -33,15 +33,15 @@ describe('getTargetBranchesFromLabels', () => {
33
33
  'v7.6.0',
34
34
  'v7.7.0',
35
35
  'v7.8.0',
36
- 'v8.0.0',
36
+ 'v8.0.0', // master
37
37
  ];
38
- const targetBranches = getTargetBranchesFromLabels_1.getTargetBranchesFromLabels({
38
+ const targetBranches = (0, getTargetBranchesFromLabels_1.getTargetBranchesFromLabels)({
39
39
  sourceBranch,
40
- existingTargetPullRequests,
40
+ expectedTargetPullRequests,
41
41
  branchLabelMapping,
42
42
  labels,
43
43
  });
44
- expect(targetBranches).toEqual([
44
+ expect(targetBranches.expected).toEqual([
45
45
  '5.4',
46
46
  '5.5',
47
47
  '5.6',
@@ -67,98 +67,135 @@ describe('getTargetBranchesFromLabels', () => {
67
67
  });
68
68
  it('should only get first match', () => {
69
69
  const sourceBranch = 'master';
70
- const existingTargetPullRequests = [];
70
+ const expectedTargetPullRequests = [];
71
71
  const branchLabelMapping = {
72
72
  'label-2': 'branch-b',
73
73
  'label-(\\d+)': 'branch-$1',
74
74
  };
75
75
  const labels = ['label-2'];
76
- const targetBranches = getTargetBranchesFromLabels_1.getTargetBranchesFromLabels({
76
+ const targetBranches = (0, getTargetBranchesFromLabels_1.getTargetBranchesFromLabels)({
77
77
  sourceBranch,
78
- existingTargetPullRequests,
78
+ expectedTargetPullRequests,
79
79
  labels,
80
80
  branchLabelMapping,
81
81
  });
82
- expect(targetBranches).toEqual(['branch-b']);
82
+ expect(targetBranches).toEqual({
83
+ expected: ['branch-b'],
84
+ missing: ['branch-b'],
85
+ unmerged: [],
86
+ merged: [],
87
+ });
83
88
  });
84
- it('should remove PRs that are already open', () => {
89
+ it('open PRs', () => {
85
90
  const sourceBranch = 'master';
86
- const existingTargetPullRequests = [
91
+ const expectedTargetPullRequests = [
87
92
  { branch: 'branch-3', state: 'OPEN' },
88
93
  ];
89
94
  const branchLabelMapping = {
90
95
  'label-(\\d+)': 'branch-$1',
91
96
  };
92
97
  const labels = ['label-1', 'label-2', 'label-3', 'label-4'];
93
- const targetBranches = getTargetBranchesFromLabels_1.getTargetBranchesFromLabels({
98
+ const targetBranches = (0, getTargetBranchesFromLabels_1.getTargetBranchesFromLabels)({
94
99
  sourceBranch,
95
- existingTargetPullRequests,
100
+ expectedTargetPullRequests,
96
101
  labels,
97
102
  branchLabelMapping,
98
103
  });
99
- expect(targetBranches).toEqual(['branch-1', 'branch-2', 'branch-4']);
104
+ expect(targetBranches).toEqual({
105
+ expected: ['branch-1', 'branch-2', 'branch-3', 'branch-4'],
106
+ missing: ['branch-1', 'branch-2', 'branch-4'],
107
+ unmerged: ['branch-3'],
108
+ merged: [],
109
+ });
100
110
  });
101
- it('should remove PRs that are already merged', () => {
111
+ it('closed PRs', () => {
102
112
  const sourceBranch = 'master';
103
- const existingTargetPullRequests = [
113
+ const expectedTargetPullRequests = [
114
+ { branch: 'branch-3', state: 'CLOSED' },
115
+ ];
116
+ const branchLabelMapping = {
117
+ 'label-(\\d+)': 'branch-$1',
118
+ };
119
+ const labels = ['label-1', 'label-2', 'label-3', 'label-4'];
120
+ const targetBranches = (0, getTargetBranchesFromLabels_1.getTargetBranchesFromLabels)({
121
+ sourceBranch,
122
+ expectedTargetPullRequests,
123
+ labels,
124
+ branchLabelMapping,
125
+ });
126
+ expect(targetBranches).toEqual({
127
+ expected: ['branch-1', 'branch-2', 'branch-3', 'branch-4'],
128
+ missing: ['branch-1', 'branch-2', 'branch-4'],
129
+ unmerged: ['branch-3'],
130
+ merged: [],
131
+ });
132
+ });
133
+ it('merged PRs', () => {
134
+ const sourceBranch = 'master';
135
+ const expectedTargetPullRequests = [
104
136
  { branch: 'branch-2', state: 'MERGED' },
105
137
  ];
106
138
  const branchLabelMapping = {
107
139
  'label-(\\d+)': 'branch-$1',
108
140
  };
109
141
  const labels = ['label-1', 'label-2', 'label-3', 'label-4'];
110
- const targetBranches = getTargetBranchesFromLabels_1.getTargetBranchesFromLabels({
142
+ const targetBranches = (0, getTargetBranchesFromLabels_1.getTargetBranchesFromLabels)({
111
143
  sourceBranch,
112
- existingTargetPullRequests,
144
+ expectedTargetPullRequests,
113
145
  labels,
114
146
  branchLabelMapping,
115
147
  });
116
- expect(targetBranches).toEqual(['branch-1', 'branch-3', 'branch-4']);
148
+ expect(targetBranches).toEqual({
149
+ expected: ['branch-1', 'branch-2', 'branch-3', 'branch-4'],
150
+ missing: ['branch-1', 'branch-3', 'branch-4'],
151
+ unmerged: [],
152
+ merged: ['branch-2'],
153
+ });
117
154
  });
118
155
  it('should remove duplicates', () => {
119
156
  const sourceBranch = 'master';
120
- const existingTargetPullRequests = [];
157
+ const expectedTargetPullRequests = [];
121
158
  const branchLabelMapping = {
122
159
  'label-(\\d+)': 'branch-$1',
123
160
  };
124
161
  const labels = ['label-1', 'label-2', 'label-2'];
125
- const targetBranches = getTargetBranchesFromLabels_1.getTargetBranchesFromLabels({
162
+ const targetBranches = (0, getTargetBranchesFromLabels_1.getTargetBranchesFromLabels)({
126
163
  sourceBranch,
127
- existingTargetPullRequests,
164
+ expectedTargetPullRequests,
128
165
  labels,
129
166
  branchLabelMapping,
130
167
  });
131
- expect(targetBranches).toEqual(['branch-1', 'branch-2']);
168
+ expect(targetBranches.expected).toEqual(['branch-1', 'branch-2']);
132
169
  });
133
170
  it('should ignore non-matching labels', () => {
134
171
  const sourceBranch = 'master';
135
- const existingTargetPullRequests = [];
172
+ const expectedTargetPullRequests = [];
136
173
  const branchLabelMapping = {
137
174
  'label-(\\d+)': 'branch-$1',
138
175
  };
139
176
  const labels = ['label-1', 'label-2', 'foo', 'bar'];
140
- const targetBranches = getTargetBranchesFromLabels_1.getTargetBranchesFromLabels({
177
+ const targetBranches = (0, getTargetBranchesFromLabels_1.getTargetBranchesFromLabels)({
141
178
  sourceBranch,
142
- existingTargetPullRequests,
179
+ expectedTargetPullRequests,
143
180
  labels,
144
181
  branchLabelMapping,
145
182
  });
146
- expect(targetBranches).toEqual(['branch-1', 'branch-2']);
183
+ expect(targetBranches.expected).toEqual(['branch-1', 'branch-2']);
147
184
  });
148
185
  it('should omit empty labels', () => {
149
186
  const sourceBranch = 'master';
150
- const existingTargetPullRequests = [];
187
+ const expectedTargetPullRequests = [];
151
188
  const branchLabelMapping = {
152
189
  'label-2': '',
153
190
  'label-(\\d+)': 'branch-$1',
154
191
  };
155
192
  const labels = ['label-1', 'label-2'];
156
- const targetBranches = getTargetBranchesFromLabels_1.getTargetBranchesFromLabels({
193
+ const targetBranches = (0, getTargetBranchesFromLabels_1.getTargetBranchesFromLabels)({
157
194
  sourceBranch,
158
- existingTargetPullRequests,
195
+ expectedTargetPullRequests,
159
196
  labels,
160
197
  branchLabelMapping,
161
198
  });
162
- expect(targetBranches).toEqual(['branch-1']);
199
+ expect(targetBranches.expected).toEqual(['branch-1']);
163
200
  });
164
201
  });
@@ -1,2 +1,2 @@
1
- import { DataResponse } from '../fetchCommitsByAuthor';
2
- export declare const commitsWithPullRequestsMock: DataResponse;
1
+ import { CommitByAuthorResponse } from '../fetchCommits/fetchCommitsByAuthor';
2
+ export declare const commitsByAuthorMock: CommitByAuthorResponse;