backport 5.6.6 → 6.1.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 (234) hide show
  1. package/README.md +186 -50
  2. package/dist/entrypoint.cli.js +3 -1
  3. package/dist/entrypoint.module.d.ts +18 -5
  4. package/dist/entrypoint.module.js +42 -12
  5. package/dist/entrypoint.module.private.test.d.ts +1 -0
  6. package/dist/entrypoint.module.private.test.js +34 -0
  7. package/dist/ink.d.ts +1 -0
  8. package/dist/ink.js +49 -0
  9. package/dist/main.d.ts +11 -6
  10. package/dist/main.js +40 -25
  11. package/dist/options/ConfigOptions.d.ts +28 -13
  12. package/dist/options/cliArgs.d.ts +36 -35
  13. package/dist/options/cliArgs.js +91 -52
  14. package/dist/options/cliArgs.test.js +35 -43
  15. package/dist/options/config/config.d.ts +35 -57
  16. package/dist/options/config/config.js +5 -39
  17. package/dist/options/config/config.test.js +11 -27
  18. package/dist/options/config/getOptionsFromGit.d.ts +3 -2
  19. package/dist/options/config/getOptionsFromGit.js +1 -3
  20. package/dist/options/config/globalConfig.d.ts +2 -51
  21. package/dist/options/config/globalConfig.js +9 -12
  22. package/dist/options/config/globalConfig.test.js +7 -5
  23. package/dist/options/config/projectConfig.d.ts +2 -51
  24. package/dist/options/config/projectConfig.js +2 -2
  25. package/dist/options/config/projectConfig.test.js +53 -16
  26. package/dist/options/config/readConfigFile.d.ts +44 -41
  27. package/dist/options/config/readConfigFile.js +30 -4
  28. package/dist/options/options.d.ts +59 -31
  29. package/dist/options/options.js +103 -10
  30. package/dist/options/options.test.js +266 -79
  31. package/dist/options/parseRequiredOptions.d.ts +19 -14
  32. package/dist/options/parseRequiredOptions.js +9 -15
  33. package/dist/options/parseRequiredOptions.test.js +30 -26
  34. package/dist/runSequentially.d.ts +20 -0
  35. package/dist/runSequentially.js +41 -0
  36. package/dist/runSequentially.test.d.ts +1 -0
  37. package/dist/runSequentially.test.js +196 -0
  38. package/dist/runWithOptions.js +7 -14
  39. package/dist/runWithOptions.test.js +138 -17
  40. package/dist/scripts/generate-gql.d.ts +1 -0
  41. package/dist/scripts/generate-gql.js +15 -0
  42. package/dist/scripts/postinstall.js +4 -4
  43. package/dist/scripts/postinstall.test.js +9 -2
  44. package/dist/scripts/runPostinstall.js +1 -1
  45. package/dist/services/HandledError.d.ts +11 -1
  46. package/dist/services/HandledError.js +2 -1
  47. package/dist/services/child-process-promisified.d.ts +1 -1
  48. package/dist/services/child-process-promisified.js +1 -1
  49. package/dist/services/env.js +5 -5
  50. package/dist/services/env.test.js +11 -4
  51. package/dist/services/fs-promisified.js +4 -4
  52. package/dist/services/getExpectedTarget.d.ts +9 -0
  53. package/dist/services/getExpectedTarget.js +77 -0
  54. package/dist/services/getExpectedTargetPullRequests.d.ts +9 -0
  55. package/dist/services/getExpectedTargetPullRequests.js +77 -0
  56. package/dist/services/git.d.ts +20 -6
  57. package/dist/services/git.integration.test.d.ts +1 -0
  58. package/dist/services/git.integration.test.js +168 -0
  59. package/dist/services/git.js +93 -67
  60. package/dist/services/git.test.js +68 -68
  61. package/dist/services/github/commitFormatters.d.ts +3 -7
  62. package/dist/services/github/commitFormatters.js +10 -19
  63. package/dist/services/github/commitFormatters.test.js +2 -24
  64. package/dist/services/github/v3/addAssigneesToPullRequest.d.ts +1 -1
  65. package/dist/services/github/v3/addAssigneesToPullRequest.js +3 -8
  66. package/dist/services/github/v3/addAssigneesToPullRequest.test.js +6 -8
  67. package/dist/services/github/v3/addLabelsToPullRequest.d.ts +1 -1
  68. package/dist/services/github/v3/addLabelsToPullRequest.js +2 -6
  69. package/dist/services/github/v3/addReviewersToPullRequest.d.ts +2 -0
  70. package/dist/services/github/v3/addReviewersToPullRequest.js +34 -0
  71. package/dist/services/github/v3/createPullRequest.d.ts +3 -2
  72. package/dist/services/github/v3/createPullRequest.js +31 -25
  73. package/dist/services/github/v3/createPullRequest.test.d.ts +1 -0
  74. package/dist/services/github/v3/createPullRequest.test.js +110 -0
  75. package/dist/services/github/v3/createStatusComment.d.ts +11 -0
  76. package/dist/services/github/v3/createStatusComment.js +95 -0
  77. package/dist/services/github/v3/createStatusComment.test.d.ts +1 -0
  78. package/dist/services/github/v3/createStatusComment.test.js +209 -0
  79. package/dist/services/github/v4/FetchPullRequestId.js +1 -1
  80. package/dist/services/github/v4/apiRequestV4.d.ts +2 -2
  81. package/dist/services/github/v4/apiRequestV4.js +6 -8
  82. package/dist/services/github/v4/apiRequestV4.test.js +7 -7
  83. package/dist/services/github/v4/disablePullRequestAutoMerge.js +3 -4
  84. package/dist/services/github/v4/enablePullRequestAutoMerge.js +9 -8
  85. package/dist/services/github/v4/enablePullRequestAutoMerge.private.test.js +93 -33
  86. package/dist/services/github/v4/fetchAuthorId.d.ts +5 -2
  87. package/dist/services/github/v4/fetchAuthorId.js +6 -7
  88. package/dist/services/github/v4/fetchAuthorId.private.test.js +6 -9
  89. package/dist/services/github/v4/fetchCommitByPullNumber.js +11 -9
  90. package/dist/services/github/v4/fetchCommitByPullNumber.private.test.js +4 -4
  91. package/dist/services/github/v4/fetchCommitBySha.js +14 -12
  92. package/dist/services/github/v4/fetchCommitBySha.private.test.js +4 -4
  93. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.d.ts +12 -0
  94. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.js +46 -0
  95. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.private.test.d.ts +1 -0
  96. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.private.test.js +70 -0
  97. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.d.ts +12 -0
  98. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.js +36 -0
  99. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.private.test.d.ts +1 -0
  100. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.private.test.js +55 -0
  101. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.d.ts +27 -0
  102. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.js +88 -0
  103. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.private.test.d.ts +1 -0
  104. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.private.test.js +128 -0
  105. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.test.d.ts +1 -0
  106. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.test.js +125 -0
  107. package/dist/services/github/v4/fetchCommits/fetchCommitsInDateRange.d.ts +28 -0
  108. package/dist/services/github/v4/fetchCommits/fetchCommitsInDateRange.js +52 -0
  109. package/dist/services/github/v4/fetchCommits/fetchProjectConfig.d.ts +3 -0
  110. package/dist/services/github/v4/fetchCommits/fetchProjectConfig.js +55 -0
  111. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.d.ts +10 -0
  112. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.js +49 -0
  113. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.private.test.d.ts +1 -0
  114. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.private.test.js +77 -0
  115. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.test.d.ts +1 -0
  116. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.test.js +252 -0
  117. package/dist/services/github/v4/fetchCommitsByAuthor.d.ts +2 -1
  118. package/dist/services/github/v4/fetchCommitsByAuthor.js +81 -70
  119. package/dist/services/github/v4/fetchCommitsByAuthor.private.test.js +78 -5
  120. package/dist/services/github/v4/fetchCommitsByAuthor.test.js +29 -21
  121. package/dist/services/github/v4/fetchDefaultBranch.js +1 -1
  122. package/dist/services/github/v4/fetchExistingPullRequest.js +2 -3
  123. package/dist/services/github/v4/fetchExistingPullRequest.private.test.js +3 -5
  124. package/dist/services/github/v4/fetchMergedPullRequests.d.ts +18 -19
  125. package/dist/services/github/v4/fetchMergedPullRequests.js +22 -69
  126. package/dist/services/github/v4/fetchMergedPullRequests.private.test.js +8 -9
  127. package/dist/services/github/v4/fetchProjectConfig.d.ts +8 -0
  128. package/dist/services/github/v4/fetchProjectConfig.js +56 -0
  129. package/dist/services/github/v4/fetchProjectConfig.private.test.d.ts +1 -0
  130. package/dist/services/github/v4/fetchProjectConfig.private.test.js +52 -0
  131. package/dist/services/github/v4/fetchProjectConfig.test.d.ts +1 -0
  132. package/dist/services/github/v4/fetchProjectConfig.test.js +37 -0
  133. package/dist/services/github/v4/fetchPullRequestAutoMergeMethod.js +2 -3
  134. package/dist/services/github/v4/fetchPullRequestBySearchQuery.js +12 -10
  135. package/dist/services/github/v4/fetchPullRequestBySearchQuery.private.test.js +3 -3
  136. package/dist/services/github/v4/fetchPullRequestBySearchQuery.test.js +2 -2
  137. package/dist/services/github/v4/getExistingTargetPullRequests.d.ts +4 -58
  138. package/dist/services/github/v4/getExistingTargetPullRequests.js +3 -58
  139. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.d.ts +67 -0
  140. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.js +130 -0
  141. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.private.test.d.ts +1 -0
  142. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.private.test.js +130 -0
  143. package/dist/services/github/v4/getOptionsFromGithub/index.d.ts +19 -11
  144. package/dist/services/github/v4/getOptionsFromGithub/index.js +52 -22
  145. package/dist/services/github/v4/getOptionsFromGithub/index.private.test.js +46 -7
  146. package/dist/services/github/v4/getOptionsFromGithub/query.d.ts +16 -16
  147. package/dist/services/github/v4/getOptionsFromGithub/query.js +16 -15
  148. package/dist/services/github/v4/getTargetBranchesFromLabels.d.ts +0 -7
  149. package/dist/services/github/v4/getTargetBranchesFromLabels.js +1 -26
  150. package/dist/services/github/v4/getTargetBranchesFromLabels.test.js +69 -32
  151. package/dist/services/github/v4/mocks/commitsByAuthorMock.d.ts +2 -2
  152. package/dist/services/github/v4/mocks/commitsByAuthorMock.js +39 -25
  153. package/dist/services/github/v4/mocks/fetchPullRequestBySearchQueryMock.d.ts +1 -1
  154. package/dist/services/github/v4/mocks/fetchPullRequestBySearchQueryMock.js +10 -0
  155. package/dist/services/github/v4/mocks/getCommitsByAuthorMock.d.ts +1 -1
  156. package/dist/services/github/v4/mocks/getCommitsByAuthorMock.js +2 -0
  157. package/dist/services/github/v4/mocks/getPullRequestNodeMock.d.ts +1 -1
  158. package/dist/services/github/v4/mocks/getPullRequestNodeMock.js +1 -0
  159. package/dist/services/github/v4/throwOnInvalidAccessToken.js +10 -15
  160. package/dist/services/github/v4/throwOnInvalidAccessToken.test.js +7 -7
  161. package/dist/services/logger.d.ts +3 -3
  162. package/dist/services/logger.js +16 -15
  163. package/dist/services/prompt.test.d.ts +1 -0
  164. package/dist/services/prompt.test.js +87 -0
  165. package/dist/services/prompts.d.ts +10 -4
  166. package/dist/services/prompts.js +85 -22
  167. package/dist/services/sourceCommit/getExpectedTargetPullRequests.d.ts +9 -0
  168. package/dist/services/sourceCommit/getExpectedTargetPullRequests.js +92 -0
  169. package/dist/services/sourceCommit/getExpectedTargetPullRequests.test.d.ts +1 -0
  170. package/dist/services/sourceCommit/getExpectedTargetPullRequests.test.js +413 -0
  171. package/dist/services/sourceCommit/getMockSourceCommit.d.ts +21 -0
  172. package/dist/services/sourceCommit/getMockSourceCommit.js +67 -0
  173. package/dist/services/sourceCommit/getTargetBranchesFromLabels.d.ts +5 -0
  174. package/dist/services/sourceCommit/getTargetBranchesFromLabels.js +20 -0
  175. package/dist/services/sourceCommit/getTargetBranchesFromLabels.test.d.ts +1 -0
  176. package/dist/services/sourceCommit/getTargetBranchesFromLabels.test.js +201 -0
  177. package/dist/services/sourceCommit/parseSourceCommit.d.ts +82 -0
  178. package/dist/services/sourceCommit/parseSourceCommit.js +96 -0
  179. package/dist/services/sourceCommit/parseSourceCommit.test.d.ts +1 -0
  180. package/dist/services/sourceCommit/parseSourceCommit.test.js +229 -0
  181. package/dist/services/sourceCommit/sourceCommit.d.ts +78 -0
  182. package/dist/services/sourceCommit/sourceCommit.js +86 -0
  183. package/dist/services/sourceCommit/sourceCommit.test.d.ts +1 -0
  184. package/dist/services/sourceCommit/sourceCommit.test.js +287 -0
  185. package/dist/services/sourceCommit.d.ts +78 -0
  186. package/dist/services/sourceCommit.js +86 -0
  187. package/dist/services/sourceCommit.test.d.ts +1 -0
  188. package/dist/services/sourceCommit.test.js +280 -0
  189. package/dist/test/inquirer.private.test.js +55 -51
  190. package/dist/test/integration/createSpies.js +6 -6
  191. package/dist/test/integration/envConstants.js +5 -6
  192. package/dist/test/integration/helpers.js +1 -1
  193. package/dist/test/integration/integration.private.test.d.ts +1 -0
  194. package/dist/test/integration/integration.private.test.js +327 -0
  195. package/dist/test/integration/integration.test.js +28 -29
  196. package/dist/test/mockConfigFiles.d.ts +5 -0
  197. package/dist/test/mockConfigFiles.js +37 -0
  198. package/dist/test/nockHelpers.d.ts +1 -1
  199. package/dist/test/nockHelpers.js +7 -11
  200. package/dist/test/private/getDevAccessToken.js +1 -1
  201. package/dist/test/setupFiles/automatic-mocks.d.ts +0 -1
  202. package/dist/test/setupFiles/automatic-mocks.js +1 -51
  203. package/dist/types/Commit.d.ts +85 -0
  204. package/dist/types/Commit.js +139 -0
  205. package/dist/types/commitNode.d.ts +89 -0
  206. package/dist/types/commitNode.js +139 -0
  207. package/dist/types/commitNode.test.d.ts +1 -0
  208. package/dist/types/commitNode.test.js +255 -0
  209. package/dist/types/commitWithAssociatedPullRequests.d.ts +86 -0
  210. package/dist/types/commitWithAssociatedPullRequests.js +145 -0
  211. package/dist/ui/cherrypickAndCreatePullRequest.test.js +346 -76
  212. package/dist/ui/cherrypickAndCreateTargetPullRequest.d.ts +2 -1
  213. package/dist/ui/cherrypickAndCreateTargetPullRequest.js +95 -67
  214. package/dist/ui/cherrypickAndCreateTargetPullRequest.test.d.ts +1 -0
  215. package/dist/ui/cherrypickAndCreateTargetPullRequest.test.js +468 -0
  216. package/dist/ui/getCommits.d.ts +1 -1
  217. package/dist/ui/getCommits.js +55 -25
  218. package/dist/ui/getCommitsWithoutBackports.d.ts +11 -0
  219. package/dist/ui/getCommitsWithoutBackports.js +53 -0
  220. package/dist/ui/getTargetBranches.d.ts +3 -2
  221. package/dist/ui/getTargetBranches.js +33 -18
  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 +11 -10
  228. package/dist/ui/maybeSetupRepo.test.js +54 -4
  229. package/dist/utils/excludeUndefined.d.ts +6 -0
  230. package/dist/utils/excludeUndefined.js +7 -0
  231. package/dist/utils/excludeUndefined.test.d.ts +1 -0
  232. package/dist/utils/excludeUndefined.test.js +24 -0
  233. package/package.json +37 -51
  234. package/yarn.lock +2414 -2993
@@ -1,5 +1,5 @@
1
1
  import { ValidConfigOptions } from '../../../options/options';
2
- import { Commit } from '../../../types/Commit';
2
+ import { Commit } from '../../sourceCommit/parseSourceCommit';
3
3
  export interface PullRequestPayload {
4
4
  owner: string;
5
5
  repo: string;
@@ -15,8 +15,9 @@ export declare function createPullRequest({ options, prPayload, }: {
15
15
  }): Promise<{
16
16
  url: string;
17
17
  number: number;
18
+ didUpdate: boolean;
18
19
  }>;
19
- export declare function getBody({ options, commits, targetBranch, }: {
20
+ export declare function getPullRequestBody({ options, commits, targetBranch, }: {
20
21
  options: ValidConfigOptions;
21
22
  commits: Commit[];
22
23
  targetBranch: string;
@@ -3,27 +3,18 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.getTitle = exports.getBody = exports.createPullRequest = void 0;
6
+ exports.getTitle = exports.getPullRequestBody = exports.createPullRequest = void 0;
7
7
  const rest_1 = require("@octokit/rest");
8
- const chalk_1 = __importDefault(require("chalk"));
9
8
  const ora_1 = __importDefault(require("ora"));
10
9
  const HandledError_1 = require("../../HandledError");
11
10
  const logger_1 = require("../../logger");
11
+ const commitFormatters_1 = require("../commitFormatters");
12
12
  const fetchExistingPullRequest_1 = require("../v4/fetchExistingPullRequest");
13
13
  const getGithubV3ErrorMessage_1 = require("./getGithubV3ErrorMessage");
14
14
  async function createPullRequest({ options, prPayload, }) {
15
15
  logger_1.logger.info(`Creating PR with title: "${prPayload.title}". ${prPayload.head} -> ${prPayload.base}`);
16
- const { accessToken, dryRun, githubApiBaseUrlV3 } = options;
17
- const spinner = ora_1.default(`Creating pull request`).start();
18
- if (dryRun) {
19
- spinner.succeed('Dry run: Creating pull request');
20
- // output PR summary
21
- logger_1.consoleLog(chalk_1.default.bold('\nPull request summary:'));
22
- logger_1.consoleLog(`Branch: ${prPayload.head} -> ${prPayload.base}`);
23
- logger_1.consoleLog(`Title: ${prPayload.title}`);
24
- logger_1.consoleLog(`Body: ${prPayload.body}\n`);
25
- return { url: 'example_url', number: 1337 };
26
- }
16
+ const { accessToken, githubApiBaseUrlV3 } = options;
17
+ const spinner = (0, ora_1.default)(`Creating pull request`).start();
27
18
  try {
28
19
  const octokit = new rest_1.Octokit({
29
20
  auth: accessToken,
@@ -35,18 +26,23 @@ async function createPullRequest({ options, prPayload, }) {
35
26
  return {
36
27
  url: res.data.html_url,
37
28
  number: res.data.number,
29
+ didUpdate: false,
38
30
  };
39
31
  }
40
32
  catch (e) {
41
33
  // retrieve url for existing
42
34
  try {
43
- const existingPR = await fetchExistingPullRequest_1.fetchExistingPullRequest({
35
+ const existingPR = await (0, fetchExistingPullRequest_1.fetchExistingPullRequest)({
44
36
  options,
45
37
  prPayload,
46
38
  });
47
39
  if (existingPR) {
48
40
  spinner.succeed('Updating existing pull request');
49
- return existingPR;
41
+ return {
42
+ url: existingPR.url,
43
+ number: existingPR.number,
44
+ didUpdate: true,
45
+ };
50
46
  }
51
47
  }
52
48
  catch (e) {
@@ -54,25 +50,35 @@ async function createPullRequest({ options, prPayload, }) {
54
50
  // swallow error
55
51
  }
56
52
  spinner.fail();
57
- throw new HandledError_1.HandledError(`Could not create pull request: ${getGithubV3ErrorMessage_1.getGithubV3ErrorMessage(e)}`);
53
+ throw new HandledError_1.HandledError(`Could not create pull request: ${(0, getGithubV3ErrorMessage_1.getGithubV3ErrorMessage)(e)}`);
58
54
  }
59
55
  }
60
56
  exports.createPullRequest = createPullRequest;
61
- function getBody({ options, commits, targetBranch, }) {
57
+ function getPullRequestBody({ options, commits, targetBranch, }) {
58
+ const bodySuffix = options.prDescription ? `\n${options.prDescription}` : '';
62
59
  const commitMessages = commits
63
- .map((commit) => ` - ${commit.formattedMessage}`)
60
+ .map((c) => {
61
+ const message = c.pullNumber
62
+ ? `#${c.pullNumber}`
63
+ : `${(0, commitFormatters_1.getFirstLine)(c.originalMessage)} (${(0, commitFormatters_1.getShortSha)(c.sha)})`;
64
+ return ` - ${message}`;
65
+ })
64
66
  .join('\n');
65
- const bodySuffix = options.prDescription
66
- ? `\n\n${options.prDescription}`
67
- : '';
68
- return `Backports the following commits to ${targetBranch}:\n${commitMessages}${bodySuffix}`;
67
+ return `# Backport
68
+
69
+ This is an automatic backport to ${targetBranch} of:
70
+ ${commitMessages}
71
+
72
+ ### Questions ?
73
+ Please refer to the [Backport tool documentation](https://github.com/sqren/backport)
74
+ ${bodySuffix}`;
69
75
  }
70
- exports.getBody = getBody;
76
+ exports.getPullRequestBody = getPullRequestBody;
71
77
  function getTitle({ options, commits, targetBranch, }) {
72
78
  const commitMessages = commits
73
- .map((commit) => commit.formattedMessage)
79
+ .map((commit) => (0, commitFormatters_1.getFirstLine)(commit.originalMessage))
74
80
  .join(' | ');
75
- return options.prTitle
81
+ return (options.prTitle ?? '[{targetBranch}] {commitMessages}')
76
82
  .replace('{targetBranch}', targetBranch)
77
83
  .replace('{commitMessages}', commitMessages)
78
84
  .slice(0, 240);
@@ -0,0 +1,110 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const createPullRequest_1 = require("./createPullRequest");
4
+ describe('getPullRequestBody', () => {
5
+ it('when single pull request is backported', () => {
6
+ expect((0, createPullRequest_1.getPullRequestBody)({
7
+ options: {},
8
+ commits: [
9
+ {
10
+ pullNumber: 55,
11
+ sha: 'abcdefghi',
12
+ originalMessage: 'My commit message',
13
+ },
14
+ ],
15
+ targetBranch: '7.x',
16
+ })).toMatchInlineSnapshot(`
17
+ "# Backport
18
+
19
+ This is an automatic backport to 7.x of:
20
+ - #55
21
+
22
+ ### Questions ?
23
+ Please refer to the [Backport tool documentation](https://github.com/sqren/backport)
24
+ "
25
+ `);
26
+ });
27
+ it('when a single commit (non pull request) is backported', () => {
28
+ expect((0, createPullRequest_1.getPullRequestBody)({
29
+ options: {},
30
+ commits: [
31
+ {
32
+ sha: 'abcdefghijklmw',
33
+ originalMessage: 'My commit message',
34
+ },
35
+ ],
36
+ targetBranch: '7.x',
37
+ })).toMatchInlineSnapshot(`
38
+ "# Backport
39
+
40
+ This is an automatic backport to 7.x of:
41
+ - My commit message (abcdefgh)
42
+
43
+ ### Questions ?
44
+ Please refer to the [Backport tool documentation](https://github.com/sqren/backport)
45
+ "
46
+ `);
47
+ });
48
+ it('when multiple commits are backported', () => {
49
+ expect((0, createPullRequest_1.getPullRequestBody)({
50
+ options: {},
51
+ commits: [
52
+ {
53
+ pullNumber: 55,
54
+ sha: 'abcdefghijklm',
55
+ originalMessage: 'My commit message',
56
+ },
57
+ {
58
+ sha: 'qwertyuiop',
59
+ originalMessage: 'Another commit message',
60
+ },
61
+ ],
62
+ targetBranch: '7.x',
63
+ })).toMatchInlineSnapshot(`
64
+ "# Backport
65
+
66
+ This is an automatic backport to 7.x of:
67
+ - #55
68
+ - Another commit message (qwertyui)
69
+
70
+ ### Questions ?
71
+ Please refer to the [Backport tool documentation](https://github.com/sqren/backport)
72
+ "
73
+ `);
74
+ });
75
+ });
76
+ describe('getTitle', () => {
77
+ it('when the default title is used', () => {
78
+ expect((0, createPullRequest_1.getTitle)({
79
+ options: {},
80
+ commits: [
81
+ {
82
+ pullNumber: 55,
83
+ sha: 'abcdefghi',
84
+ originalMessage: 'My commit message',
85
+ },
86
+ {
87
+ pullNumber: 56,
88
+ sha: 'jklmnopqr',
89
+ originalMessage: 'Another commit message',
90
+ },
91
+ ],
92
+ targetBranch: '7.x',
93
+ })).toEqual('[7.x] My commit message | Another commit message');
94
+ });
95
+ it('when a custom PR title is used', () => {
96
+ expect((0, createPullRequest_1.getTitle)({
97
+ options: {
98
+ prTitle: 'Branch: "{targetBranch}". Messages: {commitMessages}',
99
+ },
100
+ commits: [
101
+ {
102
+ pullNumber: 55,
103
+ sha: 'abcdefghi',
104
+ originalMessage: 'My commit message',
105
+ },
106
+ ],
107
+ targetBranch: '7.x',
108
+ })).toEqual('Branch: "7.x". Messages: My commit message');
109
+ });
110
+ });
@@ -0,0 +1,11 @@
1
+ import { BackportResponse } from '../../../main';
2
+ import { ValidConfigOptions } from '../../../options/options';
3
+ export declare function createStatusComment({ options, backportResponse, }: {
4
+ options: ValidConfigOptions;
5
+ backportResponse: BackportResponse;
6
+ }): Promise<void>;
7
+ export declare function getCommentBody({ options, pullNumber, backportResponse, }: {
8
+ options: ValidConfigOptions;
9
+ pullNumber: number;
10
+ backportResponse: BackportResponse;
11
+ }): string;
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getCommentBody = exports.createStatusComment = void 0;
4
+ const rest_1 = require("@octokit/rest");
5
+ const logger_1 = require("../../logger");
6
+ const commitFormatters_1 = require("../commitFormatters");
7
+ async function createStatusComment({ options, backportResponse, }) {
8
+ const { githubApiBaseUrlV3, repoName, repoOwner, accessToken } = options;
9
+ try {
10
+ const octokit = new rest_1.Octokit({
11
+ auth: accessToken,
12
+ baseUrl: githubApiBaseUrlV3,
13
+ log: logger_1.logger,
14
+ });
15
+ // don't post update if there are no new pull requests created
16
+ if (backportResponse.status === 'success') {
17
+ const hasOnlyUpdates = backportResponse.results.every((result) => result.status === 'success' && result.didUpdate);
18
+ if (hasOnlyUpdates) {
19
+ return;
20
+ }
21
+ }
22
+ await Promise.all(backportResponse.commits.map((commit) => {
23
+ if (!commit.pullNumber) {
24
+ return;
25
+ }
26
+ const body = getCommentBody({
27
+ options,
28
+ pullNumber: commit.pullNumber,
29
+ backportResponse,
30
+ });
31
+ return octokit.issues.createComment({
32
+ owner: repoOwner,
33
+ repo: repoName,
34
+ issue_number: commit.pullNumber,
35
+ body,
36
+ });
37
+ }));
38
+ }
39
+ catch (e) {
40
+ logger_1.logger.info(`Could not create status comment `, e.stack);
41
+ }
42
+ }
43
+ exports.createStatusComment = createStatusComment;
44
+ function getCommentBody({ options, pullNumber, backportResponse, }) {
45
+ const { repoName, repoOwner, autoMerge } = options;
46
+ const backportPRCommand = `\n### How to fix\nRe-run the backport manually:\n\`\`\`\n${options.backportBinary} --pr ${pullNumber}\n\`\`\``;
47
+ const supportSection = '\n\n### Questions ?\nPlease refer to the [Backport tool documentation](https://github.com/sqren/backport)';
48
+ if (backportResponse.status === 'failure') {
49
+ return `## 💔 Backport failed
50
+ The pull request could not be backported due to the following error:
51
+ \`${backportResponse.errorMessage}\`
52
+ ${backportPRCommand}${supportSection}`;
53
+ }
54
+ const tableBody = backportResponse.results
55
+ .map((result) => {
56
+ if (result.status === 'success') {
57
+ return [
58
+ '✅',
59
+ result.targetBranch,
60
+ `[<img src="https://img.shields.io/github/pulls/detail/state/${repoOwner}/${repoName}/${result.pullRequestNumber}">](${result.pullRequestUrl})`,
61
+ ];
62
+ }
63
+ if (result.error.meta?.type === 'commitsWithoutBackports') {
64
+ const conflictingPullRequests = result.error.meta.commitsWithoutBackports.map((c) => {
65
+ return ` - [${(0, commitFormatters_1.getFirstLine)(c.commit.originalMessage)}](${c.commit.pullUrl})`;
66
+ });
67
+ const conflictSection = `<br><br>You might need to backport the following PRs to ${result.targetBranch}:<br>${conflictingPullRequests.join('<br>')}`;
68
+ return [
69
+ '❌',
70
+ result.targetBranch,
71
+ `**Backport failed because of merge conflicts**${conflictingPullRequests.length > 0 ? conflictSection : ''}`,
72
+ ];
73
+ }
74
+ return ['❌', result.targetBranch, result.errorMessage];
75
+ })
76
+ .map((line) => line.join('|'))
77
+ .join('|\n|');
78
+ const table = backportResponse.results.length
79
+ ? `| Status | Branch | Result |\n|:------:|:------:|:------|\n|${tableBody}|`
80
+ : '';
81
+ const didAllBackportsSucceed = backportResponse.results.every((r) => r.status === 'success');
82
+ const didAnyBackportsSucceed = backportResponse.results.some((r) => r.status === 'success');
83
+ const header = didAllBackportsSucceed
84
+ ? '## 💚 All backports created successfully'
85
+ : '## 💔 Some backports could not be created';
86
+ const autoMergeMessage = autoMerge && didAnyBackportsSucceed
87
+ ? '\nNote: Successful backport PRs will be merged automatically after passing CI.'
88
+ : '';
89
+ const backportPRCommandMessage = !didAllBackportsSucceed
90
+ ? `${backportPRCommand}`
91
+ : '';
92
+ return (0, logger_1.redactAccessToken)(`${header}\n\n${table}
93
+ ${backportPRCommandMessage}${autoMergeMessage}${supportSection}`);
94
+ }
95
+ exports.getCommentBody = getCommentBody;
@@ -0,0 +1,209 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const HandledError_1 = require("../../HandledError");
4
+ const createStatusComment_1 = require("./createStatusComment");
5
+ describe('getCommentBody', () => {
6
+ it('when an unknown error occurs', () => {
7
+ expect((0, createStatusComment_1.getCommentBody)({
8
+ options: {},
9
+ pullNumber: 55,
10
+ backportResponse: {
11
+ status: 'failure',
12
+ errorMessage: 'A terrible error occured',
13
+ },
14
+ })).toMatchInlineSnapshot(`
15
+ "## 💔 Backport failed
16
+ The pull request could not be backported due to the following error:
17
+ \`A terrible error occured\`
18
+
19
+ To backport manually run: \`node scripts/backport --pr 55\`.
20
+ For more info read the [Backport documentation](https://github.com/sqren/backport#backport)
21
+ "
22
+ `);
23
+ });
24
+ it('when all backports succeed', () => {
25
+ expect((0, createStatusComment_1.getCommentBody)({
26
+ options: {
27
+ repoName: 'kibana',
28
+ repoOwner: 'elastic',
29
+ autoMerge: true,
30
+ },
31
+ pullNumber: 55,
32
+ backportResponse: {
33
+ status: 'success',
34
+ results: [
35
+ {
36
+ status: 'success',
37
+ targetBranch: '7.x',
38
+ pullRequestNumber: 55,
39
+ pullRequestUrl: 'url-to-pr',
40
+ },
41
+ {
42
+ status: 'success',
43
+ targetBranch: '7.1',
44
+ pullRequestNumber: 66,
45
+ pullRequestUrl: 'url-to-pr',
46
+ },
47
+ ],
48
+ },
49
+ })).toMatchInlineSnapshot(`
50
+ "## 💚 All backports created successfully
51
+
52
+ | Status | Branch | Result |
53
+ |:------:|:------:|:------:|
54
+ |✅|7.x|[<img src=\\"https://img.shields.io/github/pulls/detail/state/elastic/kibana/55\\">](url-to-pr)|
55
+ |✅|7.1|[<img src=\\"https://img.shields.io/github/pulls/detail/state/elastic/kibana/66\\">](url-to-pr)|
56
+
57
+ Note: Successful backport PRs will be merged automatically after passing CI.
58
+ "
59
+ `);
60
+ });
61
+ it('when all backports fail', () => {
62
+ expect((0, createStatusComment_1.getCommentBody)({
63
+ options: {
64
+ repoName: 'kibana',
65
+ repoOwner: 'elastic',
66
+ autoMerge: true,
67
+ },
68
+ pullNumber: 55,
69
+ backportResponse: {
70
+ status: 'success',
71
+ results: [
72
+ {
73
+ status: 'failure',
74
+ targetBranch: '7.x',
75
+ error: new Error('Boom!'),
76
+ errorMessage: 'My boom error!',
77
+ },
78
+ {
79
+ status: 'failure',
80
+ targetBranch: '7.1',
81
+ error: new Error('Boom!'),
82
+ errorMessage: 'My boom error!',
83
+ },
84
+ ],
85
+ },
86
+ })).toMatchInlineSnapshot(`
87
+ "## 💔 Some backports could not be created
88
+
89
+ | Status | Branch | Result |
90
+ |:------:|:------:|:------:|
91
+ |❌|7.x|My boom error!|
92
+ |❌|7.1|My boom error!|
93
+
94
+ To backport manually run: \`node scripts/backport --pr 55\`.
95
+ For more info read the [Backport documentation](https://github.com/sqren/backport#backport)
96
+
97
+ "
98
+ `);
99
+ });
100
+ it('when some backports fail', () => {
101
+ expect((0, createStatusComment_1.getCommentBody)({
102
+ options: {
103
+ repoName: 'kibana',
104
+ repoOwner: 'elastic',
105
+ autoMerge: true,
106
+ },
107
+ pullNumber: 55,
108
+ backportResponse: {
109
+ status: 'success',
110
+ results: [
111
+ {
112
+ status: 'success',
113
+ targetBranch: '7.x',
114
+ pullRequestNumber: 55,
115
+ pullRequestUrl: 'url-to-pr-55',
116
+ },
117
+ {
118
+ status: 'failure',
119
+ targetBranch: '7.1',
120
+ error: new Error('Boom!'),
121
+ errorMessage: 'My boom error!',
122
+ },
123
+ ],
124
+ },
125
+ })).toMatchInlineSnapshot(`
126
+ "## 💔 Some backports could not be created
127
+
128
+ | Status | Branch | Result |
129
+ |:------:|:------:|:------:|
130
+ |✅|7.x|[<img src=\\"https://img.shields.io/github/pulls/detail/state/elastic/kibana/55\\">](url-to-pr-55)|
131
+ |❌|7.1|My boom error!|
132
+
133
+ To backport manually run: \`node scripts/backport --pr 55\`.
134
+ For more info read the [Backport documentation](https://github.com/sqren/backport#backport)
135
+
136
+ Note: Successful backport PRs will be merged automatically after passing CI.
137
+ "
138
+ `);
139
+ });
140
+ it('when some backports fail due to conflicts', () => {
141
+ expect((0, createStatusComment_1.getCommentBody)({
142
+ options: {
143
+ repoName: 'kibana',
144
+ repoOwner: 'elastic',
145
+ autoMerge: true,
146
+ },
147
+ pullNumber: 55,
148
+ backportResponse: {
149
+ status: 'success',
150
+ results: [
151
+ {
152
+ status: 'success',
153
+ targetBranch: '7.x',
154
+ pullRequestNumber: 55,
155
+ pullRequestUrl: 'url-to-pr-55',
156
+ },
157
+ {
158
+ status: 'failure',
159
+ targetBranch: '7.1',
160
+ errorMessage: 'My boom error!',
161
+ error: new HandledError_1.HandledError('Boom!', {
162
+ type: 'commitsWithoutBackports',
163
+ commitsWithoutBackports: [
164
+ {
165
+ //@ts-expect-error
166
+ commit: {
167
+ pullNumber: 5,
168
+ pullUrl: 'url-to-pr-5',
169
+ },
170
+ },
171
+ ],
172
+ }),
173
+ },
174
+ {
175
+ status: 'failure',
176
+ targetBranch: '7.2',
177
+ errorMessage: 'My boom error!',
178
+ error: new HandledError_1.HandledError('Boom!', {
179
+ type: 'commitsWithoutBackports',
180
+ commitsWithoutBackports: [
181
+ {
182
+ //@ts-expect-error
183
+ commit: {
184
+ sha: 'acdefg',
185
+ originalMessage: 'Some long message\nin multiple lines',
186
+ },
187
+ },
188
+ ],
189
+ }),
190
+ },
191
+ ],
192
+ },
193
+ })).toMatchInlineSnapshot(`
194
+ "## 💔 Some backports could not be created
195
+
196
+ | Status | Branch | Result |
197
+ |:------:|:------:|:------:|
198
+ |✅|7.x|[<img src=\\"https://img.shields.io/github/pulls/detail/state/elastic/kibana/55\\">](url-to-pr-55)|
199
+ |❌|7.1|Could not backport due to conflicts, possibly caused by the following unbackported commits:<br> - [#5](url-to-pr-5)|
200
+ |❌|7.2|Could not backport due to conflicts, possibly caused by the following unbackported commits:<br> - Some long message (acdefg)|
201
+
202
+ To backport manually run: \`node scripts/backport --pr 55\`.
203
+ For more info read the [Backport documentation](https://github.com/sqren/backport#backport)
204
+
205
+ Note: Successful backport PRs will be merged automatically after passing CI.
206
+ "
207
+ `);
208
+ });
209
+ });
@@ -17,7 +17,7 @@ async function fetchPullRequestId(options, pullNumber) {
17
17
  }
18
18
  }
19
19
  `;
20
- const prResponse = await apiRequestV4_1.apiRequestV4({
20
+ const prResponse = await (0, apiRequestV4_1.apiRequestV4)({
21
21
  githubApiBaseUrlV4,
22
22
  accessToken,
23
23
  query: prQuery,
@@ -13,7 +13,7 @@ export interface GithubV4Response<DataResponse> {
13
13
  }[];
14
14
  }
15
15
  export declare function apiRequestV4<DataResponse>({ githubApiBaseUrlV4, accessToken, query, variables, handleError, }: {
16
- githubApiBaseUrlV4: string;
16
+ githubApiBaseUrlV4?: string;
17
17
  accessToken: string;
18
18
  query: string;
19
19
  variables?: {
@@ -21,4 +21,4 @@ export declare function apiRequestV4<DataResponse>({ githubApiBaseUrlV4, accessT
21
21
  };
22
22
  handleError?: boolean;
23
23
  }): Promise<DataResponse>;
24
- export declare function handleGithubV4Error(e: AxiosError<GithubV4Response<unknown>>): HandledError;
24
+ export declare function handleGithubV4Error(e: AxiosError<GithubV4Response<unknown>>): HandledError | AxiosError<GithubV4Response<unknown>, any>;
@@ -7,8 +7,7 @@ exports.handleGithubV4Error = exports.apiRequestV4 = void 0;
7
7
  const axios_1 = __importDefault(require("axios"));
8
8
  const HandledError_1 = require("../../HandledError");
9
9
  const logger_1 = require("../../logger");
10
- async function apiRequestV4({ githubApiBaseUrlV4, accessToken, query, variables, handleError = true, }) {
11
- var _a, _b, _c;
10
+ async function apiRequestV4({ githubApiBaseUrlV4 = 'https://api.github.com/graphql', accessToken, query, variables, handleError = true, }) {
12
11
  try {
13
12
  const response = await axios_1.default.post(githubApiBaseUrlV4, { query, variables }, {
14
13
  headers: {
@@ -30,11 +29,11 @@ async function apiRequestV4({ githubApiBaseUrlV4, accessToken, query, variables,
30
29
  return response.data.data;
31
30
  }
32
31
  catch (e) {
33
- logger_1.logger.info(`POST ${githubApiBaseUrlV4} (status: ${(_a = e.response) === null || _a === void 0 ? void 0 : _a.status})`);
32
+ logger_1.logger.info(`POST ${githubApiBaseUrlV4} (status: ${e.response?.status})`);
34
33
  logger_1.logger.info('Query:', query);
35
34
  logger_1.logger.info('Variables:', variables);
36
- logger_1.logger.verbose('Response headers:', (_b = e.response) === null || _b === void 0 ? void 0 : _b.headers);
37
- logger_1.logger.info('Response data:', (_c = e.response) === null || _c === void 0 ? void 0 : _c.data);
35
+ logger_1.logger.verbose('Response headers:', e.response?.headers);
36
+ logger_1.logger.info('Response data:', e.response?.data);
38
37
  if (handleError) {
39
38
  throw handleGithubV4Error(e);
40
39
  }
@@ -43,12 +42,11 @@ async function apiRequestV4({ githubApiBaseUrlV4, accessToken, query, variables,
43
42
  }
44
43
  exports.apiRequestV4 = apiRequestV4;
45
44
  function handleGithubV4Error(e) {
46
- var _a, _b;
47
45
  // re-throw unknown error
48
- if (!((_a = e.response) === null || _a === void 0 ? void 0 : _a.data)) {
46
+ if (!e.response?.data) {
49
47
  return e;
50
48
  }
51
- const errorMessages = (_b = e.response.data.errors) === null || _b === void 0 ? void 0 : _b.map((error) => error.message);
49
+ const errorMessages = e.response.data.errors?.map((error) => error.message);
52
50
  if (errorMessages) {
53
51
  return new HandledError_1.HandledError(`${errorMessages.join(', ')} (Unhandled Github v4 error)`);
54
52
  }