backport 7.4.0 → 8.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (244) hide show
  1. package/README.md +15 -16
  2. package/dist/backportRun.d.ts +12 -6
  3. package/dist/backportRun.js +64 -62
  4. package/dist/entrypoint.cli.js +4 -8
  5. package/dist/entrypoint.module.d.ts +13 -6
  6. package/dist/entrypoint.module.js +14 -20
  7. package/dist/entrypoint.module.private.test.js +47 -346
  8. package/dist/errors/BackportError.d.ts +19 -0
  9. package/dist/errors/BackportError.js +29 -0
  10. package/dist/lib/BackportError.d.ts +19 -0
  11. package/dist/lib/BackportError.js +29 -0
  12. package/dist/lib/cherrypickAndCreateTargetPullRequest.d.ts +11 -0
  13. package/dist/lib/cherrypickAndCreateTargetPullRequest.js +242 -0
  14. package/dist/lib/cherrypickAndCreateTargetPullRequest.test.d.ts +1 -0
  15. package/dist/lib/cherrypickAndCreateTargetPullRequest.test.js +321 -0
  16. package/dist/lib/child-process-promisified.d.ts +25 -0
  17. package/dist/lib/child-process-promisified.js +60 -0
  18. package/dist/lib/env.d.ts +9 -0
  19. package/dist/lib/env.js +33 -0
  20. package/dist/lib/env.test.d.ts +1 -0
  21. package/dist/lib/env.test.js +26 -0
  22. package/dist/lib/getCommits.d.ts +2 -0
  23. package/dist/lib/getCommits.js +75 -0
  24. package/dist/lib/getCommitsWithoutBackports.d.ts +11 -0
  25. package/dist/lib/getCommitsWithoutBackports.js +68 -0
  26. package/dist/lib/getCommitsWithoutBackports.test.d.ts +1 -0
  27. package/dist/lib/getCommitsWithoutBackports.test.js +335 -0
  28. package/dist/lib/getGitConfigAuthor.d.ts +6 -0
  29. package/dist/lib/getGitConfigAuthor.js +15 -0
  30. package/dist/lib/getTargetBranches.d.ts +5 -0
  31. package/dist/lib/getTargetBranches.js +67 -0
  32. package/dist/lib/getTargetBranches.test.d.ts +1 -0
  33. package/dist/lib/getTargetBranches.test.js +210 -0
  34. package/dist/lib/git.d.ts +77 -0
  35. package/dist/lib/git.js +439 -0
  36. package/dist/lib/git.private.test.d.ts +1 -0
  37. package/dist/lib/git.private.test.js +618 -0
  38. package/dist/lib/git.test.d.ts +1 -0
  39. package/dist/lib/git.test.js +580 -0
  40. package/dist/lib/github/commitFormatters.d.ts +3 -0
  41. package/dist/lib/github/commitFormatters.js +17 -0
  42. package/dist/lib/github/commitFormatters.test.d.ts +1 -0
  43. package/dist/lib/github/commitFormatters.test.js +11 -0
  44. package/dist/lib/github/v3/addAssigneesToPullRequest.d.ts +2 -0
  45. package/dist/lib/github/v3/addAssigneesToPullRequest.js +32 -0
  46. package/dist/lib/github/v3/addAssigneesToPullRequest.test.d.ts +1 -0
  47. package/dist/lib/github/v3/addAssigneesToPullRequest.test.js +27 -0
  48. package/dist/lib/github/v3/addLabelsToPullRequest.d.ts +2 -0
  49. package/dist/lib/github/v3/addLabelsToPullRequest.js +30 -0
  50. package/dist/lib/github/v3/addReviewersToPullRequest.d.ts +2 -0
  51. package/dist/lib/github/v3/addReviewersToPullRequest.js +32 -0
  52. package/dist/lib/github/v3/createPullRequest.d.ts +29 -0
  53. package/dist/lib/github/v3/createPullRequest.js +91 -0
  54. package/dist/lib/github/v3/createPullRequest.test.d.ts +1 -0
  55. package/dist/lib/github/v3/createPullRequest.test.js +377 -0
  56. package/dist/lib/github/v3/createStatusComment.d.ts +11 -0
  57. package/dist/lib/github/v3/createStatusComment.js +138 -0
  58. package/dist/lib/github/v3/createStatusComment.test.d.ts +1 -0
  59. package/dist/lib/github/v3/createStatusComment.test.js +566 -0
  60. package/dist/lib/github/v3/getGithubV3ErrorMessage.d.ts +14 -0
  61. package/dist/lib/github/v3/getGithubV3ErrorMessage.js +16 -0
  62. package/dist/lib/github/v4/FetchPullRequestId.d.ts +2 -0
  63. package/dist/lib/github/v4/FetchPullRequestId.js +36 -0
  64. package/dist/lib/github/v4/apiRequestV4.d.ts +31 -0
  65. package/dist/lib/github/v4/apiRequestV4.js +71 -0
  66. package/dist/lib/github/v4/apiRequestV4.test.d.ts +1 -0
  67. package/dist/lib/github/v4/apiRequestV4.test.js +109 -0
  68. package/dist/lib/github/v4/disablePullRequestAutoMerge.d.ts +2 -0
  69. package/dist/lib/github/v4/disablePullRequestAutoMerge.js +32 -0
  70. package/dist/lib/github/v4/enablePullRequestAutoMerge.d.ts +2 -0
  71. package/dist/lib/github/v4/enablePullRequestAutoMerge.js +50 -0
  72. package/dist/lib/github/v4/enablePullRequestAutoMerge.private.test.d.ts +1 -0
  73. package/dist/lib/github/v4/enablePullRequestAutoMerge.private.test.js +112 -0
  74. package/dist/lib/github/v4/fetchAuthorId.d.ts +10 -0
  75. package/dist/lib/github/v4/fetchAuthorId.js +28 -0
  76. package/dist/lib/github/v4/fetchAuthorId.private.test.d.ts +1 -0
  77. package/dist/lib/github/v4/fetchAuthorId.private.test.js +25 -0
  78. package/dist/lib/github/v4/fetchCommits/allFetchers.private.test.d.ts +1 -0
  79. package/dist/lib/github/v4/fetchCommits/allFetchers.private.test.js +93 -0
  80. package/dist/lib/github/v4/fetchCommits/fetchCommitByPullNumber.d.ts +11 -0
  81. package/dist/lib/github/v4/fetchCommits/fetchCommitByPullNumber.js +58 -0
  82. package/dist/lib/github/v4/fetchCommits/fetchCommitByPullNumber.private.test.d.ts +1 -0
  83. package/dist/lib/github/v4/fetchCommits/fetchCommitByPullNumber.private.test.js +136 -0
  84. package/dist/lib/github/v4/fetchCommits/fetchCommitBySha.d.ts +11 -0
  85. package/dist/lib/github/v4/fetchCommits/fetchCommitBySha.js +48 -0
  86. package/dist/lib/github/v4/fetchCommits/fetchCommitBySha.private.test.d.ts +1 -0
  87. package/dist/lib/github/v4/fetchCommits/fetchCommitBySha.private.test.js +118 -0
  88. package/dist/lib/github/v4/fetchCommits/fetchCommitsByAuthor.d.ts +29 -0
  89. package/dist/lib/github/v4/fetchCommits/fetchCommitsByAuthor.js +110 -0
  90. package/dist/lib/github/v4/fetchCommits/fetchCommitsByAuthor.private.test.d.ts +1 -0
  91. package/dist/lib/github/v4/fetchCommits/fetchCommitsByAuthor.private.test.js +196 -0
  92. package/dist/lib/github/v4/fetchCommits/fetchCommitsByAuthor.test.d.ts +1 -0
  93. package/dist/lib/github/v4/fetchCommits/fetchCommitsByAuthor.test.js +157 -0
  94. package/dist/lib/github/v4/fetchCommits/fetchPullRequestBySearchQuery.private.test.d.ts +1 -0
  95. package/dist/lib/github/v4/fetchCommits/fetchPullRequestBySearchQuery.private.test.js +107 -0
  96. package/dist/lib/github/v4/fetchCommits/fetchPullRequestsBySearchQuery.d.ts +12 -0
  97. package/dist/lib/github/v4/fetchCommits/fetchPullRequestsBySearchQuery.js +69 -0
  98. package/dist/lib/github/v4/fetchExistingPullRequest.d.ts +9 -0
  99. package/dist/lib/github/v4/fetchExistingPullRequest.js +64 -0
  100. package/dist/lib/github/v4/fetchExistingPullRequest.private.test.d.ts +1 -0
  101. package/dist/lib/github/v4/fetchExistingPullRequest.private.test.js +44 -0
  102. package/dist/lib/github/v4/fetchPullRequestAutoMergeMethod.d.ts +2 -0
  103. package/dist/lib/github/v4/fetchPullRequestAutoMergeMethod.js +39 -0
  104. package/dist/lib/github/v4/fetchRemoteProjectConfig.d.ts +8 -0
  105. package/dist/lib/github/v4/fetchRemoteProjectConfig.js +49 -0
  106. package/dist/lib/github/v4/fetchRemoteProjectConfig.private.test.d.ts +1 -0
  107. package/dist/lib/github/v4/fetchRemoteProjectConfig.private.test.js +49 -0
  108. package/dist/lib/github/v4/getOptionsFromGithub/getOptionsFromGithub.d.ts +72 -0
  109. package/dist/lib/github/v4/getOptionsFromGithub/getOptionsFromGithub.js +78 -0
  110. package/dist/lib/github/v4/getOptionsFromGithub/getOptionsFromGithub.private.test.d.ts +1 -0
  111. package/dist/lib/github/v4/getOptionsFromGithub/getOptionsFromGithub.private.test.js +116 -0
  112. package/dist/lib/github/v4/getOptionsFromGithub/query.d.ts +16 -0
  113. package/dist/lib/github/v4/getOptionsFromGithub/query.js +29 -0
  114. package/dist/lib/github/v4/getRepoOwnerAndNameFromGitRemotes.d.ts +29 -0
  115. package/dist/lib/github/v4/getRepoOwnerAndNameFromGitRemotes.js +61 -0
  116. package/dist/lib/github/v4/getRepoOwnerAndNameFromGitRemotes.private.test.d.ts +1 -0
  117. package/dist/lib/github/v4/getRepoOwnerAndNameFromGitRemotes.private.test.js +72 -0
  118. package/dist/lib/github/v4/mocks/commitsByAuthorMock.d.ts +2 -0
  119. package/dist/lib/github/v4/mocks/commitsByAuthorMock.js +176 -0
  120. package/dist/lib/github/v4/throwOnInvalidAccessToken.d.ts +7 -0
  121. package/dist/lib/github/v4/throwOnInvalidAccessToken.js +45 -0
  122. package/dist/lib/github/v4/throwOnInvalidAccessToken.test.d.ts +1 -0
  123. package/dist/lib/github/v4/throwOnInvalidAccessToken.test.js +87 -0
  124. package/dist/lib/github/v4/validateTargetBranches.d.ts +14 -0
  125. package/dist/lib/github/v4/validateTargetBranches.js +46 -0
  126. package/dist/lib/github/v4/validateTargetBranches.private.test.d.ts +1 -0
  127. package/dist/lib/github/v4/validateTargetBranches.private.test.js +51 -0
  128. package/dist/lib/logger.d.ts +11 -0
  129. package/dist/lib/logger.js +80 -0
  130. package/dist/lib/ora.d.ts +2 -0
  131. package/dist/lib/ora.js +21 -0
  132. package/dist/lib/prompt.test.d.ts +1 -0
  133. package/dist/lib/prompt.test.js +139 -0
  134. package/dist/lib/prompts.d.ts +17 -0
  135. package/dist/lib/prompts.js +132 -0
  136. package/dist/lib/remoteConfig.d.ts +20 -0
  137. package/dist/lib/remoteConfig.js +52 -0
  138. package/dist/lib/sequentially.d.ts +1 -0
  139. package/dist/lib/sequentially.js +10 -0
  140. package/dist/lib/setupRepo.d.ts +2 -0
  141. package/dist/lib/setupRepo.js +56 -0
  142. package/dist/lib/setupRepo.test.d.ts +1 -0
  143. package/dist/lib/setupRepo.test.js +278 -0
  144. package/dist/lib/sourceCommit/getExpectedTargetPullRequests.d.ts +16 -0
  145. package/dist/lib/sourceCommit/getExpectedTargetPullRequests.js +102 -0
  146. package/dist/lib/sourceCommit/getExpectedTargetPullRequests.test.d.ts +1 -0
  147. package/dist/lib/sourceCommit/getExpectedTargetPullRequests.test.js +467 -0
  148. package/dist/lib/sourceCommit/getMockSourceCommit.d.ts +26 -0
  149. package/dist/lib/sourceCommit/getMockSourceCommit.js +103 -0
  150. package/dist/lib/sourceCommit/parseSourceCommit.d.ts +108 -0
  151. package/dist/lib/sourceCommit/parseSourceCommit.js +123 -0
  152. package/dist/lib/sourceCommit/parseSourceCommit.test.d.ts +1 -0
  153. package/dist/lib/sourceCommit/parseSourceCommit.test.js +226 -0
  154. package/dist/options/ConfigOptions.d.ts +4 -2
  155. package/dist/options/cliArgs.d.ts +14 -6
  156. package/dist/options/cliArgs.js +50 -22
  157. package/dist/options/cliArgs.test.js +128 -4
  158. package/dist/options/config/config.d.ts +5 -4
  159. package/dist/options/config/config.js +3 -5
  160. package/dist/options/config/config.test.js +0 -1
  161. package/dist/options/config/globalConfig.d.ts +5 -3
  162. package/dist/options/config/globalConfig.js +17 -14
  163. package/dist/options/config/readConfigFile.d.ts +5 -3
  164. package/dist/options/config/readConfigFile.js +2 -2
  165. package/dist/options/options.d.ts +41 -5
  166. package/dist/options/options.js +24 -24
  167. package/dist/options/options.test.js +117 -55
  168. package/dist/runSequentially.d.ts +14 -9
  169. package/dist/runSequentially.js +27 -12
  170. package/dist/scripts/postinstall.js +4 -3
  171. package/dist/scripts/postinstall.test.js +6 -2
  172. package/dist/services/BackportError.d.ts +19 -0
  173. package/dist/services/BackportError.js +29 -0
  174. package/dist/services/HandledBackportError.d.ts +19 -0
  175. package/dist/services/HandledBackportError.js +29 -0
  176. package/dist/services/HandledError.d.ts +2 -2
  177. package/dist/services/HandledError.js +7 -7
  178. package/dist/services/child-process-promisified.d.ts +19 -3
  179. package/dist/services/child-process-promisified.js +46 -17
  180. package/dist/services/git.d.ts +6 -7
  181. package/dist/services/git.integration.test.js +381 -65
  182. package/dist/services/git.js +189 -123
  183. package/dist/services/git.private.test.d.ts +1 -0
  184. package/dist/services/git.private.test.js +618 -0
  185. package/dist/services/git.test.js +167 -124
  186. package/dist/services/github/v3/addAssigneesToPullRequest.js +1 -1
  187. package/dist/services/github/v3/addLabelsToPullRequest.js +1 -1
  188. package/dist/services/github/v3/addReviewersToPullRequest.js +2 -1
  189. package/dist/services/github/v3/createPullRequest.js +4 -2
  190. package/dist/services/github/v3/createStatusComment.js +16 -17
  191. package/dist/services/github/v3/createStatusComment.test.js +20 -9
  192. package/dist/services/github/v4/apiRequestV4.js +2 -2
  193. package/dist/services/github/v4/apiRequestV4.test.js +2 -2
  194. package/dist/services/github/v4/enablePullRequestAutoMerge.js +2 -4
  195. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.js +4 -3
  196. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.private.test.js +5 -1
  197. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.js +3 -2
  198. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.private.test.js +5 -1
  199. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.js +4 -3
  200. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.private.test.js +5 -1
  201. package/dist/services/github/v4/fetchCommits/fetchPullRequestsBySearchQuery.js +3 -2
  202. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.js +2 -2
  203. package/dist/services/github/v4/getRepoOwnerAndNameFromGitRemotes.private.test.js +5 -1
  204. package/dist/services/github/v4/throwOnInvalidAccessToken.js +5 -5
  205. package/dist/services/logger.js +5 -1
  206. package/dist/services/test.d.ts +1 -0
  207. package/dist/services/test.js +23 -0
  208. package/dist/state.d.ts +0 -0
  209. package/dist/state.js +1 -0
  210. package/dist/test/e2e/cli/commit-author.private.test.js +4 -4
  211. package/dist/test/e2e/cli/different-merge-strategies.private.test.js +27 -25
  212. package/dist/test/e2e/cli/entrypoint.cli.private.test.js +53 -75
  213. package/dist/test/e2e/cli/error-handling-interactive-mode.private.test.d.ts +1 -0
  214. package/dist/test/e2e/cli/error-handling-interactive-mode.private.test.js +80 -0
  215. package/dist/test/e2e/cli/error-handling-non-interactive-mode.private.test.d.ts +1 -0
  216. package/dist/test/e2e/cli/error-handling-non-interactive-mode.private.test.js +199 -0
  217. package/dist/test/e2e/cli/interactive-error-handling.private.test.d.ts +1 -0
  218. package/dist/test/e2e/cli/interactive-error-handling.private.test.js +80 -0
  219. package/dist/test/e2e/cli/non-interactive-error-handling.private.test.d.ts +1 -0
  220. package/dist/test/e2e/cli/non-interactive-error-handling.private.test.js +198 -0
  221. package/dist/test/e2e/cli/repo-with-backportrc-removed.private.test.js +28 -29
  222. package/dist/test/e2e/cli/runBackportViaCli.d.ts +6 -2
  223. package/dist/test/e2e/cli/runBackportViaCli.js +44 -23
  224. package/dist/test/e2e/cli/test-that-repo-can-be-cloned.private.test.js +22 -14
  225. package/dist/test/e2e/module/entrypoint.module.mutation.test.js +28 -21
  226. package/dist/test/handleUnbackportedPullRequests.private.test.js +11 -8
  227. package/dist/test/sandbox.js +5 -1
  228. package/dist/test/setupFiles/automatic-mocks.js +6 -2
  229. package/dist/ui/cherrypickAndCreateTargetPullRequest.js +4 -4
  230. package/dist/ui/cherrypickAndCreateTargetPullRequest.test.js +7 -159
  231. package/dist/ui/getCommits.js +2 -2
  232. package/dist/ui/getCommitsWithoutBackports.test.js +5 -1
  233. package/dist/ui/getGitConfigAuthor.js +1 -1
  234. package/dist/ui/getTargetBranches.js +3 -3
  235. package/dist/ui/getTargetBranches.test.js +5 -1
  236. package/dist/ui/ora.d.ts +1 -8
  237. package/dist/ui/setupRepo.js +3 -3
  238. package/dist/ui/setupRepo.test.js +98 -61
  239. package/dist/utils/isErrnoError.d.ts +2 -0
  240. package/dist/utils/isErrnoError.js +7 -0
  241. package/dist/utils/packageVersion.d.ts +1 -1
  242. package/dist/utils/packageVersion.js +1 -1
  243. package/package.json +19 -19
  244. package/yarn.lock +472 -701
@@ -0,0 +1,7 @@
1
+ import { GithubV4Exception } from './apiRequestV4';
2
+ export declare function throwOnInvalidAccessToken({ error, repoOwner, repoName, globalConfigFile, }: {
3
+ error: GithubV4Exception<unknown>;
4
+ repoOwner: string;
5
+ repoName: string;
6
+ globalConfigFile?: string;
7
+ }): undefined;
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.throwOnInvalidAccessToken = void 0;
4
+ const lodash_1 = require("lodash");
5
+ const maybe_1 = require("../../../utils/maybe");
6
+ const BackportError_1 = require("../../BackportError");
7
+ const env_1 = require("../../env");
8
+ function throwOnInvalidAccessToken({ error, repoOwner, repoName, globalConfigFile, }) {
9
+ function getSSOAuthUrl(ssoHeader) {
10
+ const matches = ssoHeader?.match(/url=(.*)/);
11
+ if (matches) {
12
+ return matches[1];
13
+ }
14
+ }
15
+ const statusCode = error.githubResponse.status;
16
+ switch (statusCode) {
17
+ case 200: {
18
+ const repoNotFound = error.githubResponse.data.errors?.some((error) => error.type === 'NOT_FOUND' && error.path?.join('.') === 'repository');
19
+ const grantedScopes = error.githubResponse.headers['x-oauth-scopes'] || '';
20
+ const requiredScopes = error.githubResponse.headers['x-accepted-oauth-scopes'] || '';
21
+ const ssoHeader = (0, maybe_1.maybe)(error.githubResponse.headers['x-github-sso']);
22
+ if (repoNotFound) {
23
+ const hasRequiredScopes = (0, lodash_1.isEmpty)((0, lodash_1.difference)(requiredScopes.split(','), grantedScopes.split(',')));
24
+ // user does not have permission to the repo
25
+ if (!hasRequiredScopes) {
26
+ throw new BackportError_1.BackportError(`You do not have access to the repository "${repoOwner}/${repoName}". Please make sure your access token has the required scopes.\n\nRequired scopes: ${requiredScopes}\nAccess token scopes: ${grantedScopes}`);
27
+ }
28
+ // repo does not exist
29
+ throw new BackportError_1.BackportError(`The repository "${repoOwner}/${repoName}" doesn't exist`);
30
+ }
31
+ const repoAccessForbidden = error.githubResponse.data.errors?.some((error) => error.type === 'FORBIDDEN');
32
+ const ssoAuthUrl = getSSOAuthUrl(ssoHeader);
33
+ // user does not have permissions
34
+ if (repoAccessForbidden && ssoAuthUrl) {
35
+ throw new BackportError_1.BackportError(`Please follow the link to authorize your personal access token with SSO:\n\n${ssoAuthUrl}`);
36
+ }
37
+ break;
38
+ }
39
+ case 401:
40
+ throw new BackportError_1.BackportError(`Please check your access token and make sure it is valid.\nConfig: ${(0, env_1.getGlobalConfigPath)(globalConfigFile)}`);
41
+ default:
42
+ return undefined;
43
+ }
44
+ }
45
+ exports.throwOnInvalidAccessToken = throwOnInvalidAccessToken;
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const throwOnInvalidAccessToken_1 = require("./throwOnInvalidAccessToken");
4
+ describe('throwOnInvalidAccessToken', () => {
5
+ describe('when status code is', () => {
6
+ it('should handle invalid access token', () => {
7
+ const error = {
8
+ githubResponse: {
9
+ status: 401,
10
+ headers: {},
11
+ },
12
+ };
13
+ return expect(() => (0, throwOnInvalidAccessToken_1.throwOnInvalidAccessToken)({
14
+ repoOwner: 'elastic',
15
+ repoName: 'kibana',
16
+ error,
17
+ })).toThrowError('Please check your access token and make sure it is valid');
18
+ });
19
+ it('should handle SSO error', () => {
20
+ const error = {
21
+ githubResponse: {
22
+ status: 200,
23
+ headers: { 'x-github-sso': 'required; url=https://ssourl.com' },
24
+ data: {
25
+ errors: [{ type: 'FORBIDDEN' }],
26
+ },
27
+ },
28
+ };
29
+ return expect(() => (0, throwOnInvalidAccessToken_1.throwOnInvalidAccessToken)({
30
+ repoOwner: 'elastic',
31
+ repoName: 'kibana',
32
+ error,
33
+ })).toThrowErrorMatchingSnapshot();
34
+ });
35
+ it('should handle non-existing repo', () => {
36
+ const error = {
37
+ githubResponse: {
38
+ status: 200,
39
+ headers: {
40
+ 'x-oauth-scopes': 'a,b,c',
41
+ 'x-accepted-oauth-scopes': 'a,b,c',
42
+ },
43
+ data: {
44
+ errors: [{ type: 'NOT_FOUND', path: ['repository'] }],
45
+ },
46
+ },
47
+ };
48
+ return expect(() => (0, throwOnInvalidAccessToken_1.throwOnInvalidAccessToken)({
49
+ repoOwner: 'elastic',
50
+ repoName: 'kibana',
51
+ error,
52
+ })).toThrowError(`The repository "elastic/kibana" doesn't exist`);
53
+ });
54
+ it('should handle insufficient permissions (oauth scopes)', () => {
55
+ const error = {
56
+ githubResponse: {
57
+ status: 200,
58
+ headers: {
59
+ 'x-oauth-scopes': 'a,b',
60
+ 'x-accepted-oauth-scopes': 'a,b,c',
61
+ },
62
+ data: {
63
+ errors: [{ type: 'NOT_FOUND', path: ['repository'] }],
64
+ },
65
+ },
66
+ };
67
+ return expect(() => (0, throwOnInvalidAccessToken_1.throwOnInvalidAccessToken)({
68
+ repoOwner: 'elastic',
69
+ repoName: 'kibana',
70
+ error,
71
+ })).toThrowErrorMatchingSnapshot();
72
+ });
73
+ it('should not handle unknown cases', () => {
74
+ const error = {
75
+ githubResponse: {
76
+ status: 500,
77
+ headers: {},
78
+ },
79
+ };
80
+ return expect((0, throwOnInvalidAccessToken_1.throwOnInvalidAccessToken)({
81
+ repoOwner: 'elastic',
82
+ repoName: 'kibana',
83
+ error,
84
+ })).toBe(undefined);
85
+ });
86
+ });
87
+ });
@@ -0,0 +1,14 @@
1
+ export interface TargetBranchResponse {
2
+ repository: {
3
+ ref: {
4
+ id: string;
5
+ } | null;
6
+ };
7
+ }
8
+ export declare function validateTargetBranches({ accessToken, repoName, repoOwner, targetBranches, githubApiBaseUrlV4, }: {
9
+ accessToken: string;
10
+ repoOwner: string;
11
+ repoName: string;
12
+ targetBranches?: string[];
13
+ githubApiBaseUrlV4?: string;
14
+ }): Promise<void>;
@@ -0,0 +1,46 @@
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.validateTargetBranches = void 0;
7
+ const graphql_tag_1 = __importDefault(require("graphql-tag"));
8
+ const BackportError_1 = require("../../BackportError");
9
+ const apiRequestV4_1 = require("./apiRequestV4");
10
+ async function fetchTargetBranch({ accessToken, repoName, repoOwner, branchName, githubApiBaseUrlV4 = 'https://api.github.com/graphql', }) {
11
+ const query = (0, graphql_tag_1.default) `
12
+ query doesBranchExist(
13
+ $repoOwner: String!
14
+ $repoName: String!
15
+ $branchName: String!
16
+ ) {
17
+ repository(owner: $repoOwner, name: $repoName) {
18
+ ref(qualifiedName: $branchName) {
19
+ id
20
+ }
21
+ }
22
+ }
23
+ `;
24
+ const res = await (0, apiRequestV4_1.apiRequestV4)({
25
+ githubApiBaseUrlV4,
26
+ accessToken,
27
+ query,
28
+ variables: { repoOwner, repoName, branchName },
29
+ });
30
+ if (!res.repository.ref) {
31
+ throw new BackportError_1.BackportError(`The branch "${branchName}" does not exist`);
32
+ }
33
+ return res.repository.ref;
34
+ }
35
+ async function validateTargetBranches({ accessToken, repoName, repoOwner, targetBranches = [], githubApiBaseUrlV4, }) {
36
+ await Promise.all(targetBranches.map((targetBranch) => {
37
+ return fetchTargetBranch({
38
+ accessToken,
39
+ repoName,
40
+ repoOwner,
41
+ branchName: targetBranch,
42
+ githubApiBaseUrlV4,
43
+ });
44
+ }));
45
+ }
46
+ exports.validateTargetBranches = validateTargetBranches;
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const getDevAccessToken_1 = require("../../../test/private/getDevAccessToken");
4
+ const validateTargetBranches_1 = require("./validateTargetBranches");
5
+ const accessToken = (0, getDevAccessToken_1.getDevAccessToken)();
6
+ describe('validateTargetBranches', () => {
7
+ describe('no branches', () => {
8
+ it('resolves', async () => {
9
+ const options = {
10
+ repoOwner: 'backport-org',
11
+ repoName: 'repo-with-target-branches',
12
+ accessToken,
13
+ targetBranches: [],
14
+ };
15
+ expect(await (0, validateTargetBranches_1.validateTargetBranches)(options)).toEqual(undefined);
16
+ });
17
+ });
18
+ describe('one invalid and one valid branch', () => {
19
+ it('throws', async () => {
20
+ const options = {
21
+ repoOwner: 'backport-org',
22
+ repoName: 'repo-with-target-branches',
23
+ accessToken,
24
+ targetBranches: ['production', 'foo'],
25
+ };
26
+ await expect(() => (0, validateTargetBranches_1.validateTargetBranches)(options)).rejects.toThrowError('The branch "foo" does not exist');
27
+ });
28
+ });
29
+ describe('two valid branches', () => {
30
+ it('resolves', async () => {
31
+ const options = {
32
+ repoOwner: 'backport-org',
33
+ repoName: 'repo-with-target-branches',
34
+ accessToken,
35
+ targetBranches: ['production', 'staging'],
36
+ };
37
+ expect(await (0, validateTargetBranches_1.validateTargetBranches)(options)).toEqual(undefined);
38
+ });
39
+ });
40
+ describe('two invalid branches', () => {
41
+ it('throws', async () => {
42
+ const options = {
43
+ repoOwner: 'backport-org',
44
+ repoName: 'repo-with-target-branches',
45
+ accessToken,
46
+ targetBranches: ['foo', 'bar'],
47
+ };
48
+ await expect(() => (0, validateTargetBranches_1.validateTargetBranches)(options)).rejects.toThrow(/The branch "(foo|bar)" does not exist/);
49
+ });
50
+ });
51
+ });
@@ -0,0 +1,11 @@
1
+ import winston from 'winston';
2
+ export declare let logger: winston.Logger;
3
+ export declare function initLogger({ interactive, accessToken, logFilePath, }: {
4
+ interactive: boolean;
5
+ accessToken?: string;
6
+ logFilePath?: string;
7
+ }): winston.Logger;
8
+ export declare function consoleLog(message: string): void;
9
+ export declare function setAccessToken(accessToken: string): void;
10
+ export declare function redactAccessToken(str: string): string;
11
+ export declare type LogLevel = 'error' | 'warn' | 'info' | 'verbose' | 'debug';
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.redactAccessToken = exports.setAccessToken = exports.consoleLog = exports.initLogger = exports.logger = void 0;
27
+ const winston_1 = __importStar(require("winston"));
28
+ const env_1 = require("./env");
29
+ let _accessToken;
30
+ let _interactive;
31
+ function initLogger({ interactive, accessToken, logFilePath, }) {
32
+ _accessToken = accessToken;
33
+ _interactive = interactive;
34
+ exports.logger = winston_1.default.createLogger({
35
+ format: winston_1.format.combine(winston_1.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
36
+ // Format the metadata object
37
+ winston_1.format.metadata({
38
+ fillExcept: ['message', 'level', 'timestamp', 'label'],
39
+ })),
40
+ transports: logFilePath
41
+ ? [fileTransport({ logLevel: 'debug', logFilePath })]
42
+ : [
43
+ fileTransport({ logLevel: 'info' }),
44
+ fileTransport({ logLevel: 'debug' }),
45
+ ],
46
+ });
47
+ return exports.logger;
48
+ }
49
+ exports.initLogger = initLogger;
50
+ function fileTransport({ logFilePath, logLevel, }) {
51
+ return new winston_1.default.transports.File({
52
+ filename: (0, env_1.getLogfilePath)({ logFilePath, logLevel }),
53
+ level: logLevel,
54
+ format: winston_1.format.json({
55
+ replacer: (key, value) => {
56
+ return typeof value === 'string' ? redactAccessToken(value) : value;
57
+ },
58
+ }),
59
+ });
60
+ }
61
+ // wrapper around console.log
62
+ function consoleLog(message) {
63
+ if (_interactive) {
64
+ // eslint-disable-next-line no-console
65
+ console.log(redactAccessToken(message));
66
+ }
67
+ }
68
+ exports.consoleLog = consoleLog;
69
+ function setAccessToken(accessToken) {
70
+ _accessToken = accessToken;
71
+ }
72
+ exports.setAccessToken = setAccessToken;
73
+ function redactAccessToken(str) {
74
+ // `redactAccessToken` might be called before access token is set
75
+ if (_accessToken) {
76
+ return str.replaceAll(_accessToken, '<REDACTED>');
77
+ }
78
+ return str;
79
+ }
80
+ exports.redactAccessToken = redactAccessToken;
@@ -0,0 +1,2 @@
1
+ import oraOriginal from 'ora';
2
+ export declare function ora(interactive: boolean | undefined, text?: string | undefined): oraOriginal.Ora;
@@ -0,0 +1,21 @@
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.ora = void 0;
7
+ const ora_1 = __importDefault(require("ora"));
8
+ /* eslint-disable @typescript-eslint/no-empty-function */
9
+ const oraMock = {
10
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
11
+ start: (text) => oraMock,
12
+ succeed: () => { },
13
+ stop: () => { },
14
+ fail: () => { },
15
+ stopAndPersist: () => { },
16
+ set text(value) { },
17
+ };
18
+ function ora(interactive, text) {
19
+ return interactive ? (0, ora_1.default)({ text }) : oraMock;
20
+ }
21
+ exports.ora = ora;
@@ -0,0 +1 @@
1
+ export {};
@@ -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
+ const strip_ansi_1 = __importDefault(require("strip-ansi"));
7
+ const prompts_1 = require("./prompts");
8
+ describe('prompt', () => {
9
+ describe('getChoicesForCommitPrompt', () => {
10
+ it('should display status badges via `expectedTargetPullRequests`', () => {
11
+ const commits = [
12
+ {
13
+ author: { email: 'soren.louv@elastic.co', name: 'Søren Louv-Jansen' },
14
+ sourceBranch: 'master',
15
+ sourceCommit: {
16
+ committedDate: '1',
17
+ sha: 'ae9d51b7fe3ee6f30d0d196c782e0dcabb7ac5ff',
18
+ message: '[APM] Remove log-log descriptions from correlation charts (#119700)',
19
+ },
20
+ sourcePullRequest: {
21
+ number: 119700,
22
+ url: 'foo',
23
+ mergeCommit: {
24
+ sha: 'ae9d51b7fe3ee6f30d0d196c782e0dcabb7ac5ff',
25
+ message: '[APM] Remove log-log descriptions from correlation charts (#119700)',
26
+ },
27
+ },
28
+ expectedTargetPullRequests: [
29
+ { number: 120178, branch: '8.0', state: 'MERGED' },
30
+ { number: 120179, branch: '7.16', state: 'MERGED' },
31
+ { number: 120179, branch: '7.15', state: 'NOT_CREATED' },
32
+ ],
33
+ },
34
+ ];
35
+ const choices = (0, prompts_1.getChoicesForCommitPrompt)(commits, false).map(({ name, short }) => ({ name: (0, strip_ansi_1.default)(name), short }));
36
+ expect(choices).toEqual([
37
+ {
38
+ name: '1. [APM] Remove log-log descriptions from correlation charts (#119700) 8.0, 7.16, 7.15',
39
+ short: '#119700 (ae9d51b7)',
40
+ },
41
+ ]);
42
+ });
43
+ it('should list choices', () => {
44
+ const commits = [
45
+ {
46
+ author: { email: 'soren.louv@elastic.co', name: 'Søren Louv-Jansen' },
47
+ sourceBranch: 'master',
48
+ sourceCommit: {
49
+ committedDate: '',
50
+ sha: 'b1b491959dab47aeb83c88ee2accb2db46d23793',
51
+ message: '[APM] Prefer service.name for logs correlation (#120694)',
52
+ },
53
+ sourcePullRequest: {
54
+ number: 120694,
55
+ url: 'foo',
56
+ mergeCommit: {
57
+ sha: 'b1b491959dab47aeb83c88ee2accb2db46d23793',
58
+ message: '[APM] Prefer service.name for logs correlation (#120694)',
59
+ },
60
+ },
61
+ expectedTargetPullRequests: [],
62
+ },
63
+ {
64
+ author: { email: 'soren.louv@elastic.co', name: 'Søren Louv-Jansen' },
65
+ sourceBranch: 'master',
66
+ sourceCommit: {
67
+ committedDate: '',
68
+ sha: 'b1bb4a93959f19a653b9cfb207a5c6acb6559482',
69
+ message: '[APM] Disable telemetry in agent config endpoint (#120106)',
70
+ },
71
+ sourcePullRequest: {
72
+ number: 120106,
73
+ url: 'foo',
74
+ mergeCommit: {
75
+ sha: 'b1bb4a93959f19a653b9cfb207a5c6acb6559482',
76
+ message: '[APM] Disable telemetry in agent config endpoint (#120106)',
77
+ },
78
+ },
79
+ expectedTargetPullRequests: [],
80
+ },
81
+ {
82
+ author: { email: 'soren.louv@elastic.co', name: 'Søren Louv-Jansen' },
83
+ sourceBranch: 'master',
84
+ sourceCommit: {
85
+ committedDate: '',
86
+ sha: '434f6e6a88faf24dc1ea41f9f726db78e46355a7',
87
+ message: '[APM] Remove index_pattern.json and add custom field formatters (#119915)',
88
+ },
89
+ sourcePullRequest: {
90
+ number: 119915,
91
+ url: 'foo',
92
+ mergeCommit: {
93
+ sha: '434f6e6a88faf24dc1ea41f9f726db78e46355a7',
94
+ message: '[APM] Remove index_pattern.json and add custom field formatters (#119915)',
95
+ },
96
+ },
97
+ expectedTargetPullRequests: [],
98
+ },
99
+ {
100
+ author: { email: 'soren.louv@elastic.co', name: 'Søren Louv-Jansen' },
101
+ sourceBranch: 'master',
102
+ sourceCommit: {
103
+ committedDate: '',
104
+ sha: 'ae9d51b7fe3ee6f30d0d196c782e0dcabb7ac5ff',
105
+ message: '[APM] Remove log-log descriptions from correlation charts (#119700)',
106
+ },
107
+ sourcePullRequest: {
108
+ number: 119700,
109
+ url: 'foo',
110
+ mergeCommit: {
111
+ sha: 'ae9d51b7fe3ee6f30d0d196c782e0dcabb7ac5ff',
112
+ message: '[APM] Remove log-log descriptions from correlation charts (#119700)',
113
+ },
114
+ },
115
+ expectedTargetPullRequests: [],
116
+ },
117
+ ];
118
+ const choices = (0, prompts_1.getChoicesForCommitPrompt)(commits, false).map(({ name, short }) => ({ name: (0, strip_ansi_1.default)(name), short }));
119
+ expect(choices).toEqual([
120
+ {
121
+ name: '1. [APM] Prefer service.name for logs correlation (#120694) ',
122
+ short: '#120694 (b1b49195)',
123
+ },
124
+ {
125
+ name: '2. [APM] Disable telemetry in agent config endpoint (#120106) ',
126
+ short: '#120106 (b1bb4a93)',
127
+ },
128
+ {
129
+ name: '3. [APM] Remove index_pattern.json and add custom field formatters (#119915) ',
130
+ short: '#119915 (434f6e6a)',
131
+ },
132
+ {
133
+ name: '4. [APM] Remove log-log descriptions from correlation charts (#119700) ',
134
+ short: '#119700 (ae9d51b7)',
135
+ },
136
+ ]);
137
+ });
138
+ });
139
+ });
@@ -0,0 +1,17 @@
1
+ import { TargetBranchChoice } from '../options/ConfigOptions';
2
+ import { Commit } from './sourceCommit/parseSourceCommit';
3
+ export declare function getChoicesForCommitPrompt(commits: Commit[], showDetails: boolean): {
4
+ name: string;
5
+ short: string;
6
+ value: Commit;
7
+ }[];
8
+ export declare function promptForCommits({ commitChoices, isMultipleChoice, showDetails, }: {
9
+ commitChoices: Commit[];
10
+ isMultipleChoice: boolean;
11
+ showDetails: boolean;
12
+ }): Promise<Commit[]>;
13
+ export declare function promptForTargetBranches({ targetBranchChoices, isMultipleChoice, }: {
14
+ targetBranchChoices: TargetBranchChoice[];
15
+ isMultipleChoice: boolean;
16
+ }): Promise<string[]>;
17
+ export declare function confirmPrompt(message: string): Promise<boolean>;