backport 8.0.0 → 8.1.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 (205) hide show
  1. package/README.md +4 -4
  2. package/dist/backportRun.d.ts +5 -4
  3. package/dist/backportRun.js +41 -42
  4. package/dist/entrypoint.cli.js +3 -7
  5. package/dist/entrypoint.module.d.ts +7 -5
  6. package/dist/entrypoint.module.js +11 -9
  7. package/dist/entrypoint.module.private.test.js +4 -4
  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 +13 -0
  13. package/dist/lib/cherrypickAndCreateTargetPullRequest.js +238 -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 +58 -0
  18. package/dist/lib/env.d.ts +7 -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 +206 -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 +136 -0
  58. package/dist/lib/github/v3/createStatusComment.test.d.ts +1 -0
  59. package/dist/lib/github/v3/createStatusComment.test.js +461 -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/logger.d.ts +9 -0
  125. package/dist/lib/logger.js +74 -0
  126. package/dist/lib/ora.d.ts +2 -0
  127. package/dist/lib/ora.js +21 -0
  128. package/dist/lib/prompt.test.d.ts +1 -0
  129. package/dist/lib/prompt.test.js +139 -0
  130. package/dist/lib/prompts.d.ts +17 -0
  131. package/dist/lib/prompts.js +132 -0
  132. package/dist/lib/remoteConfig.d.ts +20 -0
  133. package/dist/lib/remoteConfig.js +52 -0
  134. package/dist/lib/sequentially.d.ts +1 -0
  135. package/dist/lib/sequentially.js +10 -0
  136. package/dist/lib/setupRepo.d.ts +2 -0
  137. package/dist/lib/setupRepo.js +56 -0
  138. package/dist/lib/setupRepo.test.d.ts +1 -0
  139. package/dist/lib/setupRepo.test.js +278 -0
  140. package/dist/lib/sourceCommit/getExpectedTargetPullRequests.d.ts +16 -0
  141. package/dist/lib/sourceCommit/getExpectedTargetPullRequests.js +102 -0
  142. package/dist/lib/sourceCommit/getExpectedTargetPullRequests.test.d.ts +1 -0
  143. package/dist/lib/sourceCommit/getExpectedTargetPullRequests.test.js +467 -0
  144. package/dist/lib/sourceCommit/getMockSourceCommit.d.ts +26 -0
  145. package/dist/lib/sourceCommit/getMockSourceCommit.js +103 -0
  146. package/dist/lib/sourceCommit/parseSourceCommit.d.ts +108 -0
  147. package/dist/lib/sourceCommit/parseSourceCommit.js +123 -0
  148. package/dist/lib/sourceCommit/parseSourceCommit.test.d.ts +1 -0
  149. package/dist/lib/sourceCommit/parseSourceCommit.test.js +226 -0
  150. package/dist/options/ConfigOptions.d.ts +4 -2
  151. package/dist/options/cliArgs.d.ts +15 -12
  152. package/dist/options/cliArgs.js +34 -19
  153. package/dist/options/cliArgs.test.js +125 -1
  154. package/dist/options/config/config.d.ts +5 -4
  155. package/dist/options/config/config.js +3 -5
  156. package/dist/options/config/config.test.js +0 -1
  157. package/dist/options/config/globalConfig.d.ts +3 -1
  158. package/dist/options/config/globalConfig.js +5 -5
  159. package/dist/options/config/readConfigFile.d.ts +9 -7
  160. package/dist/options/config/readConfigFile.js +2 -2
  161. package/dist/options/options.d.ts +43 -7
  162. package/dist/options/options.js +20 -24
  163. package/dist/options/options.test.js +112 -54
  164. package/dist/runSequentially.d.ts +10 -7
  165. package/dist/runSequentially.js +5 -5
  166. package/dist/scripts/postinstall.js +3 -3
  167. package/dist/scripts/postinstall.test.js +1 -1
  168. package/dist/services/BackportError.d.ts +19 -0
  169. package/dist/services/BackportError.js +29 -0
  170. package/dist/services/HandledBackportError.d.ts +19 -0
  171. package/dist/services/HandledBackportError.js +29 -0
  172. package/dist/services/HandledError.d.ts +1 -1
  173. package/dist/services/HandledError.js +5 -5
  174. package/dist/services/git.js +7 -7
  175. package/dist/services/github/v3/createPullRequest.js +2 -2
  176. package/dist/services/github/v3/createStatusComment.js +2 -2
  177. package/dist/services/github/v3/createStatusComment.test.js +5 -4
  178. package/dist/services/github/v4/apiRequestV4.js +2 -2
  179. package/dist/services/github/v4/apiRequestV4.test.js +2 -2
  180. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.js +3 -3
  181. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.js +2 -2
  182. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.js +3 -3
  183. package/dist/services/github/v4/fetchCommits/fetchPullRequestsBySearchQuery.js +2 -2
  184. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.js +2 -2
  185. package/dist/services/github/v4/throwOnInvalidAccessToken.js +5 -5
  186. package/dist/state.d.ts +0 -0
  187. package/dist/state.js +1 -0
  188. package/dist/test/e2e/cli/commit-author.private.test.js +4 -4
  189. package/dist/test/e2e/cli/different-merge-strategies.private.test.js +26 -24
  190. package/dist/test/e2e/cli/entrypoint.cli.private.test.js +189 -140
  191. package/dist/test/e2e/cli/repo-with-backportrc-removed.private.test.js +12 -14
  192. package/dist/test/e2e/cli/runBackportViaCli.d.ts +5 -2
  193. package/dist/test/e2e/cli/runBackportViaCli.js +42 -29
  194. package/dist/test/e2e/cli/test-that-repo-can-be-cloned.private.test.js +22 -14
  195. package/dist/test/e2e/module/entrypoint.module.mutation.test.js +2 -2
  196. package/dist/test/handleUnbackportedPullRequests.private.test.js +3 -2
  197. package/dist/test/setupFiles/automatic-mocks.js +1 -1
  198. package/dist/ui/cherrypickAndCreateTargetPullRequest.js +3 -3
  199. package/dist/ui/getCommits.js +2 -2
  200. package/dist/ui/getTargetBranches.js +3 -3
  201. package/dist/ui/setupRepo.js +2 -2
  202. package/dist/utils/packageVersion.d.ts +1 -1
  203. package/dist/utils/packageVersion.js +1 -1
  204. package/package.json +12 -12
  205. package/yarn.lock +112 -65
@@ -0,0 +1,136 @@
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
+ const graphql_1 = require("graphql");
27
+ const getDevAccessToken_1 = require("../../../../test/private/getDevAccessToken");
28
+ const apiRequestV4Module = __importStar(require("../apiRequestV4"));
29
+ const fetchCommitByPullNumber_1 = require("./fetchCommitByPullNumber");
30
+ const accessToken = (0, getDevAccessToken_1.getDevAccessToken)();
31
+ describe('fetchCommitByPullNumber', () => {
32
+ describe('snapshot request/response', () => {
33
+ let spy;
34
+ let commit;
35
+ beforeEach(async () => {
36
+ spy = jest.spyOn(apiRequestV4Module, 'apiRequestV4');
37
+ commit = await (0, fetchCommitByPullNumber_1.fetchCommitByPullNumber)({
38
+ repoOwner: 'elastic',
39
+ repoName: 'kibana',
40
+ accessToken,
41
+ pullNumber: 121633,
42
+ sourceBranch: 'master',
43
+ });
44
+ });
45
+ it('makes the right queries', () => {
46
+ const queries = spy.mock.calls.reduce((acc, call) => {
47
+ const query = call[0].query;
48
+ const name = apiRequestV4Module.getQueryName(query);
49
+ return { ...acc, [name]: (0, graphql_1.print)(query) };
50
+ }, {});
51
+ const queryNames = Object.keys(queries);
52
+ expect(queryNames).toEqual(['CommitByPullNumber']);
53
+ queryNames.forEach((name) => {
54
+ expect(queries[name]).toMatchSnapshot(`Query: ${name}`);
55
+ });
56
+ });
57
+ it('returns the correct response', async () => {
58
+ expect(commit).toMatchSnapshot();
59
+ });
60
+ });
61
+ describe('when PR was merged', () => {
62
+ it('is returned', async () => {
63
+ const options = {
64
+ accessToken,
65
+ pullNumber: 5,
66
+ repoName: 'backport-e2e',
67
+ repoOwner: 'backport-org',
68
+ sourceBranch: 'main',
69
+ };
70
+ const expectedCommit = {
71
+ author: { email: 'sorenlouv@gmail.com', name: 'Søren Louv-Jansen' },
72
+ sourceCommit: {
73
+ committedDate: '2020-08-15T12:40:19Z',
74
+ message: 'Add 🍏 emoji (#5)',
75
+ sha: 'ee8c492334cef1ca077a56addb79a26f79821d2f',
76
+ },
77
+ sourcePullRequest: {
78
+ number: 5,
79
+ url: 'https://github.com/backport-org/backport-e2e/pull/5',
80
+ mergeCommit: {
81
+ message: 'Add 🍏 emoji (#5)',
82
+ sha: 'ee8c492334cef1ca077a56addb79a26f79821d2f',
83
+ },
84
+ },
85
+ sourceBranch: 'master',
86
+ expectedTargetPullRequests: [
87
+ {
88
+ branch: '7.x',
89
+ state: 'MERGED',
90
+ number: 6,
91
+ url: 'https://github.com/backport-org/backport-e2e/pull/6',
92
+ mergeCommit: {
93
+ message: 'Add 🍏 emoji (#5) (#6)',
94
+ sha: '4bcd876d4ceaa73cf437bfc89b74d1a4e704c0a6',
95
+ },
96
+ },
97
+ {
98
+ branch: '7.8',
99
+ state: 'MERGED',
100
+ number: 7,
101
+ url: 'https://github.com/backport-org/backport-e2e/pull/7',
102
+ mergeCommit: {
103
+ message: 'Add 🍏 emoji (#5) (#7)',
104
+ sha: '46cd6f9999effdf894a36dbc7db90e890f4be840',
105
+ },
106
+ },
107
+ ],
108
+ };
109
+ expect(await (0, fetchCommitByPullNumber_1.fetchCommitByPullNumber)(options)).toEqual(expectedCommit);
110
+ });
111
+ });
112
+ describe('when PR is still open', () => {
113
+ it('throws an error', async () => {
114
+ const options = {
115
+ accessToken,
116
+ pullNumber: 11,
117
+ repoName: 'backport-e2e',
118
+ repoOwner: 'backport-org',
119
+ sourceBranch: 'main',
120
+ };
121
+ await expect((0, fetchCommitByPullNumber_1.fetchCommitByPullNumber)(options)).rejects.toThrowError(`The PR #11 is not merged`);
122
+ });
123
+ });
124
+ describe('when PR does not exist', () => {
125
+ it('throws an error', async () => {
126
+ const options = {
127
+ accessToken,
128
+ pullNumber: 9999999999999,
129
+ repoName: 'backport-e2e',
130
+ repoOwner: 'backport-org',
131
+ sourceBranch: 'main',
132
+ };
133
+ await expect((0, fetchCommitByPullNumber_1.fetchCommitByPullNumber)(options)).rejects.toThrowError(`Could not resolve to a PullRequest with the number of 9999999999999. (Github API v4)`);
134
+ });
135
+ });
136
+ });
@@ -0,0 +1,11 @@
1
+ import { ValidConfigOptions } from '../../../../options/options';
2
+ import { Commit } from '../../../sourceCommit/parseSourceCommit';
3
+ export declare function fetchCommitBySha(options: {
4
+ accessToken: string;
5
+ branchLabelMapping?: ValidConfigOptions['branchLabelMapping'];
6
+ githubApiBaseUrlV4?: string;
7
+ repoName: string;
8
+ repoOwner: string;
9
+ sha: string;
10
+ sourceBranch: string;
11
+ }): Promise<Commit>;
@@ -0,0 +1,48 @@
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.fetchCommitBySha = void 0;
7
+ const graphql_tag_1 = __importDefault(require("graphql-tag"));
8
+ const BackportError_1 = require("../../../BackportError");
9
+ const remoteConfig_1 = require("../../../remoteConfig");
10
+ const parseSourceCommit_1 = require("../../../sourceCommit/parseSourceCommit");
11
+ const apiRequestV4_1 = require("../apiRequestV4");
12
+ async function fetchCommitBySha(options) {
13
+ const { accessToken, githubApiBaseUrlV4 = 'https://api.github.com/graphql', repoName, repoOwner, sha, sourceBranch, } = options;
14
+ const query = (0, graphql_tag_1.default) `
15
+ query CommitsBySha($repoOwner: String!, $repoName: String!, $sha: String!) {
16
+ repository(owner: $repoOwner, name: $repoName) {
17
+ object(expression: $sha) {
18
+ ...SourceCommitWithTargetPullRequestFragment
19
+ }
20
+ }
21
+ }
22
+
23
+ ${parseSourceCommit_1.SourceCommitWithTargetPullRequestFragment}
24
+ `;
25
+ let res;
26
+ try {
27
+ res = await (0, apiRequestV4_1.apiRequestV4)({
28
+ githubApiBaseUrlV4,
29
+ accessToken,
30
+ query,
31
+ variables: {
32
+ repoOwner,
33
+ repoName,
34
+ sha,
35
+ },
36
+ });
37
+ }
38
+ catch (e) {
39
+ //@ts-expect-error
40
+ res = (0, remoteConfig_1.swallowMissingConfigFileException)(e);
41
+ }
42
+ const sourceCommit = res.repository.object;
43
+ if (!sourceCommit) {
44
+ throw new BackportError_1.BackportError(`No commit found on branch "${sourceBranch}" with sha "${sha}"`);
45
+ }
46
+ return (0, parseSourceCommit_1.parseSourceCommit)({ options, sourceCommit });
47
+ }
48
+ exports.fetchCommitBySha = fetchCommitBySha;
@@ -0,0 +1,118 @@
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
+ const graphql_1 = require("graphql");
27
+ const getDevAccessToken_1 = require("../../../../test/private/getDevAccessToken");
28
+ const apiRequestV4Module = __importStar(require("../apiRequestV4"));
29
+ const fetchCommitBySha_1 = require("./fetchCommitBySha");
30
+ const accessToken = (0, getDevAccessToken_1.getDevAccessToken)();
31
+ describe('fetchCommitBySha', () => {
32
+ describe('snapshot request/response', () => {
33
+ let spy;
34
+ let commit;
35
+ beforeEach(async () => {
36
+ spy = jest.spyOn(apiRequestV4Module, 'apiRequestV4');
37
+ commit = await (0, fetchCommitBySha_1.fetchCommitBySha)({
38
+ repoOwner: 'elastic',
39
+ repoName: 'kibana',
40
+ accessToken,
41
+ sha: 'd421ddcf6157150596581c7885afa3690cec6339',
42
+ sourceBranch: 'main',
43
+ });
44
+ });
45
+ it('makes the right queries', () => {
46
+ const queries = spy.mock.calls.reduce((acc, call) => {
47
+ const query = call[0].query;
48
+ //@ts-expect-error
49
+ const name = query.definitions[0].name.value;
50
+ return { ...acc, [name]: (0, graphql_1.print)(query) };
51
+ }, {});
52
+ const queryNames = Object.keys(queries);
53
+ expect(queryNames).toEqual(['CommitsBySha']);
54
+ queryNames.forEach((name) => {
55
+ expect(queries[name]).toMatchSnapshot(`Query: ${name}`);
56
+ });
57
+ });
58
+ it('returns the correct response', async () => {
59
+ expect(commit).toMatchSnapshot();
60
+ });
61
+ });
62
+ it('should return single commit with pull request', async () => {
63
+ const expectedCommit = {
64
+ author: { email: 'sorenlouv@gmail.com', name: 'Søren Louv-Jansen' },
65
+ sourceCommit: {
66
+ committedDate: '2020-07-07T20:40:28Z',
67
+ message: '[APM] Add API tests (#70740)',
68
+ sha: 'cb6fbc0e1b406675724181a3e9f59459b5f8f892',
69
+ },
70
+ sourcePullRequest: {
71
+ number: 70740,
72
+ url: 'https://github.com/elastic/kibana/pull/70740',
73
+ mergeCommit: {
74
+ message: '[APM] Add API tests (#70740)',
75
+ sha: 'cb6fbc0e1b406675724181a3e9f59459b5f8f892',
76
+ },
77
+ },
78
+ sourceBranch: 'master',
79
+ expectedTargetPullRequests: [
80
+ {
81
+ branch: '7.x',
82
+ state: 'MERGED',
83
+ number: 71014,
84
+ url: 'https://github.com/elastic/kibana/pull/71014',
85
+ mergeCommit: {
86
+ message: '[APM] Add API tests (#70740) (#71014)',
87
+ sha: 'd15242be682582e58cd69f6b7707565434e99293',
88
+ },
89
+ },
90
+ ],
91
+ };
92
+ await expect(await (0, fetchCommitBySha_1.fetchCommitBySha)({
93
+ repoOwner: 'elastic',
94
+ repoName: 'kibana',
95
+ accessToken,
96
+ sha: 'cb6fbc0',
97
+ sourceBranch: 'master',
98
+ })).toEqual(expectedCommit);
99
+ });
100
+ it('throws if sha does not exist', async () => {
101
+ await expect((0, fetchCommitBySha_1.fetchCommitBySha)({
102
+ repoOwner: 'elastic',
103
+ repoName: 'kibana',
104
+ accessToken,
105
+ sha: 'fc22f59',
106
+ sourceBranch: 'main',
107
+ })).rejects.toThrowError('No commit found on branch "main" with sha "fc22f59"');
108
+ });
109
+ it('throws if sha is invalid', async () => {
110
+ await expect((0, fetchCommitBySha_1.fetchCommitBySha)({
111
+ repoOwner: 'elastic',
112
+ repoName: 'kibana',
113
+ accessToken,
114
+ sha: 'myCommitSha',
115
+ sourceBranch: 'main',
116
+ })).rejects.toThrowError('No commit found on branch "main" with sha "myCommitSha"');
117
+ });
118
+ });
@@ -0,0 +1,29 @@
1
+ import { ValidConfigOptions } from '../../../../options/options';
2
+ import { Commit, SourceCommitWithTargetPullRequest } from '../../../sourceCommit/parseSourceCommit';
3
+ export declare function fetchCommitsByAuthor(options: {
4
+ accessToken: string;
5
+ author: string | null;
6
+ branchLabelMapping?: ValidConfigOptions['branchLabelMapping'];
7
+ commitPaths?: string[];
8
+ dateSince: string | null;
9
+ dateUntil: string | null;
10
+ githubApiBaseUrlV4?: string;
11
+ maxNumber?: number;
12
+ onlyMissing?: boolean;
13
+ repoName: string;
14
+ repoOwner: string;
15
+ sourceBranch: string;
16
+ }): Promise<Commit[]>;
17
+ export interface CommitByAuthorResponse {
18
+ repository: {
19
+ ref: {
20
+ target: {
21
+ history: {
22
+ edges: Array<{
23
+ node: SourceCommitWithTargetPullRequest;
24
+ }>;
25
+ };
26
+ };
27
+ } | null;
28
+ };
29
+ }
@@ -0,0 +1,110 @@
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.fetchCommitsByAuthor = void 0;
7
+ const graphql_tag_1 = __importDefault(require("graphql-tag"));
8
+ const lodash_1 = require("lodash");
9
+ const filterEmpty_1 = require("../../../../utils/filterEmpty");
10
+ const filterUnmergedCommits_1 = require("../../../../utils/filterUnmergedCommits");
11
+ const BackportError_1 = require("../../../BackportError");
12
+ const remoteConfig_1 = require("../../../remoteConfig");
13
+ const parseSourceCommit_1 = require("../../../sourceCommit/parseSourceCommit");
14
+ const apiRequestV4_1 = require("../apiRequestV4");
15
+ const fetchAuthorId_1 = require("../fetchAuthorId");
16
+ async function fetchByCommitPath({ options, authorId, commitPath, }) {
17
+ const { accessToken, githubApiBaseUrlV4 = 'https://api.github.com/graphql', maxNumber = 10, repoName, repoOwner, sourceBranch, dateSince, dateUntil, } = options;
18
+ const query = (0, graphql_tag_1.default) `
19
+ query CommitsByAuthor(
20
+ $repoOwner: String!
21
+ $repoName: String!
22
+ $maxNumber: Int!
23
+ $sourceBranch: String!
24
+ $authorId: ID
25
+ $commitPath: String
26
+ $dateSince: GitTimestamp
27
+ $dateUntil: GitTimestamp
28
+ ) {
29
+ repository(owner: $repoOwner, name: $repoName) {
30
+ ref(qualifiedName: $sourceBranch) {
31
+ target {
32
+ ... on Commit {
33
+ history(
34
+ first: $maxNumber
35
+ author: { id: $authorId }
36
+ path: $commitPath
37
+ since: $dateSince
38
+ until: $dateUntil
39
+ ) {
40
+ edges {
41
+ node {
42
+ ...SourceCommitWithTargetPullRequestFragment
43
+ }
44
+ }
45
+ }
46
+ }
47
+ }
48
+ }
49
+ }
50
+ }
51
+
52
+ ${parseSourceCommit_1.SourceCommitWithTargetPullRequestFragment}
53
+ `;
54
+ const variables = {
55
+ repoOwner,
56
+ repoName,
57
+ sourceBranch,
58
+ maxNumber,
59
+ authorId,
60
+ commitPath,
61
+ dateSince,
62
+ dateUntil,
63
+ };
64
+ try {
65
+ return await (0, apiRequestV4_1.apiRequestV4)({
66
+ githubApiBaseUrlV4,
67
+ accessToken,
68
+ query,
69
+ variables,
70
+ });
71
+ }
72
+ catch (e) {
73
+ //@ts-expect-error
74
+ return (0, remoteConfig_1.swallowMissingConfigFileException)(e);
75
+ }
76
+ }
77
+ async function fetchCommitsByAuthor(options) {
78
+ const { sourceBranch, commitPaths = [] } = options;
79
+ const authorId = await (0, fetchAuthorId_1.fetchAuthorId)(options);
80
+ const responses = await Promise.all((0, lodash_1.isEmpty)(commitPaths)
81
+ ? [fetchByCommitPath({ options, authorId, commitPath: null })]
82
+ : commitPaths.map((commitPath) => fetchByCommitPath({ options, authorId, commitPath })));
83
+ // we only need to check if the first item is `null` (if the first is `null` they all are)
84
+ if (responses[0].repository.ref === null) {
85
+ throw new BackportError_1.BackportError(`The upstream branch "${sourceBranch}" does not exist. Try specifying a different branch with "--source-branch <your-branch>"`);
86
+ }
87
+ const commits = responses
88
+ .flatMap((res) => {
89
+ return res.repository.ref?.target.history.edges.map((edge) => {
90
+ const sourceCommit = edge.node;
91
+ return (0, parseSourceCommit_1.parseSourceCommit)({ options, sourceCommit });
92
+ });
93
+ })
94
+ .filter(filterEmpty_1.filterNil);
95
+ // terminate if not commits were found
96
+ if ((0, lodash_1.isEmpty)(commits)) {
97
+ const pathText = commitPaths.length > 0 ? ` touching files in path: "${commitPaths}"` : '';
98
+ const errorText = options.author
99
+ ? `There are no commits by "${options.author}" in this repository${pathText}. Try with \`--all\` for commits by all users or \`--author=<username>\` for commits from a specific user`
100
+ : `There are no commits in this repository${pathText}`;
101
+ throw new BackportError_1.BackportError(errorText);
102
+ }
103
+ const commitsUnique = (0, lodash_1.uniqBy)(commits, (c) => c.sourceCommit.sha);
104
+ const commitsSorted = (0, lodash_1.orderBy)(commitsUnique, (c) => c.sourceCommit.committedDate, 'desc');
105
+ if (options.onlyMissing) {
106
+ return commitsSorted.filter(filterUnmergedCommits_1.filterUnmergedCommits);
107
+ }
108
+ return commitsSorted;
109
+ }
110
+ exports.fetchCommitsByAuthor = fetchCommitsByAuthor;
@@ -0,0 +1,196 @@
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
+ const graphql_1 = require("graphql");
27
+ const getDevAccessToken_1 = require("../../../../test/private/getDevAccessToken");
28
+ const apiRequestV4Module = __importStar(require("../apiRequestV4"));
29
+ const fetchCommitsByAuthor_1 = require("./fetchCommitsByAuthor");
30
+ const accessToken = (0, getDevAccessToken_1.getDevAccessToken)();
31
+ describe('fetchCommitsByAuthor', () => {
32
+ describe('snapshot request/response', () => {
33
+ let spy;
34
+ let commits;
35
+ beforeEach(async () => {
36
+ spy = jest.spyOn(apiRequestV4Module, 'apiRequestV4');
37
+ commits = await (0, fetchCommitsByAuthor_1.fetchCommitsByAuthor)({
38
+ accessToken,
39
+ author: 'sqren',
40
+ maxNumber: 10,
41
+ repoName: 'kibana',
42
+ repoOwner: 'elastic',
43
+ sourceBranch: 'main',
44
+ dateSince: '2021-01-10T00:00:00Z',
45
+ dateUntil: '2022-01-01T00:00:00Z',
46
+ commitPaths: [],
47
+ });
48
+ });
49
+ it('makes the right queries', () => {
50
+ const queries = spy.mock.calls.reduce((acc, call) => {
51
+ const query = call[0].query;
52
+ //@ts-expect-error
53
+ const name = query.definitions[0].name.value;
54
+ return { ...acc, [name]: (0, graphql_1.print)(query) };
55
+ }, {});
56
+ const queryNames = Object.keys(queries);
57
+ expect(queryNames).toEqual(['AuthorId', 'CommitsByAuthor']);
58
+ queryNames.forEach((name) => {
59
+ expect(queries[name]).toMatchSnapshot(`Query: ${name}`);
60
+ });
61
+ });
62
+ it('returns the correct response', async () => {
63
+ expect(commits).toMatchSnapshot();
64
+ });
65
+ });
66
+ describe('commitPaths', () => {
67
+ const getOptions = () => ({
68
+ accessToken,
69
+ author: 'sqren',
70
+ maxNumber: 10,
71
+ repoName: 'repo-with-different-commit-paths',
72
+ repoOwner: 'backport-org',
73
+ sourceBranch: 'main',
74
+ dateSince: null,
75
+ dateUntil: null,
76
+ });
77
+ const getCommitMessages = (commits) => {
78
+ return commits.map((c) => c.sourceCommit.message.replace(/(\r\n|\n|\r)/gm, ''));
79
+ };
80
+ it('returns all commits', async () => {
81
+ const commits = await (0, fetchCommitsByAuthor_1.fetchCommitsByAuthor)({
82
+ ...getOptions(),
83
+ commitPaths: [],
84
+ });
85
+ const commitMessages = getCommitMessages(commits);
86
+ expect(commitMessages).toEqual([
87
+ 'Edit all lyrics',
88
+ 'Update .backportrc.json',
89
+ 'Edit "Take on me"',
90
+ 'Add backportrc.json',
91
+ 'Add "Bohemian Rhapsody""',
92
+ 'Add "99 Luftballons"',
93
+ 'Add "Take on me"',
94
+ ]);
95
+ });
96
+ it('only returns commits related to "99-luftballons.txt"', async () => {
97
+ const commits = await (0, fetchCommitsByAuthor_1.fetchCommitsByAuthor)({
98
+ ...getOptions(),
99
+ commitPaths: ['lyrics/99-luftballons.txt'],
100
+ });
101
+ const commitMessages = getCommitMessages(commits);
102
+ expect(commitMessages).toEqual([
103
+ 'Edit all lyrics',
104
+ 'Add "99 Luftballons"',
105
+ ]);
106
+ });
107
+ it('only returns commits related to "take-on-me.txt"', async () => {
108
+ const commits = await (0, fetchCommitsByAuthor_1.fetchCommitsByAuthor)({
109
+ ...getOptions(),
110
+ commitPaths: ['lyrics/take-on-me.txt'],
111
+ });
112
+ const commitMessages = getCommitMessages(commits);
113
+ expect(commitMessages).toEqual([
114
+ 'Edit all lyrics',
115
+ 'Edit "Take on me"',
116
+ 'Add "Take on me"',
117
+ ]);
118
+ });
119
+ it('removes duplicates and order by `committedDate`', async () => {
120
+ const commits = await (0, fetchCommitsByAuthor_1.fetchCommitsByAuthor)({
121
+ ...getOptions(),
122
+ commitPaths: [
123
+ 'lyrics/99-luftballons.txt',
124
+ 'lyrics/take-on-me.txt',
125
+ 'lyrics/bohemian-rhapsody.txt',
126
+ ],
127
+ });
128
+ const commitMessages = getCommitMessages(commits);
129
+ expect(commitMessages).toEqual([
130
+ 'Edit all lyrics',
131
+ 'Edit "Take on me"',
132
+ 'Add "Bohemian Rhapsody""',
133
+ 'Add "99 Luftballons"',
134
+ 'Add "Take on me"',
135
+ ]);
136
+ });
137
+ });
138
+ describe('expectedTargetPullRequests', () => {
139
+ let res;
140
+ beforeEach(async () => {
141
+ res = await (0, fetchCommitsByAuthor_1.fetchCommitsByAuthor)({
142
+ accessToken,
143
+ commitPaths: [],
144
+ maxNumber: 10,
145
+ repoName: 'backport-e2e',
146
+ repoOwner: 'backport-org',
147
+ sourceBranch: 'master',
148
+ author: null,
149
+ dateSince: null,
150
+ dateUntil: null,
151
+ });
152
+ });
153
+ it('returns related OPEN PRs', async () => {
154
+ const commitWithOpenPR = res.find((commit) => commit.sourcePullRequest?.number === 9);
155
+ expect(commitWithOpenPR?.expectedTargetPullRequests).toEqual([
156
+ {
157
+ branch: '7.8',
158
+ state: 'OPEN',
159
+ number: 10,
160
+ url: 'https://github.com/backport-org/backport-e2e/pull/10',
161
+ },
162
+ ]);
163
+ });
164
+ it('returns related MERGED PRs', async () => {
165
+ const commitWithMergedPRs = res.find((commit) => commit.sourcePullRequest?.number === 5);
166
+ expect(commitWithMergedPRs?.expectedTargetPullRequests).toEqual([
167
+ {
168
+ branch: '7.x',
169
+ state: 'MERGED',
170
+ number: 6,
171
+ url: 'https://github.com/backport-org/backport-e2e/pull/6',
172
+ mergeCommit: {
173
+ message: 'Add 🍏 emoji (#5) (#6)',
174
+ sha: '4bcd876d4ceaa73cf437bfc89b74d1a4e704c0a6',
175
+ },
176
+ },
177
+ {
178
+ branch: '7.8',
179
+ state: 'MERGED',
180
+ number: 7,
181
+ url: 'https://github.com/backport-org/backport-e2e/pull/7',
182
+ mergeCommit: {
183
+ message: 'Add 🍏 emoji (#5) (#7)',
184
+ sha: '46cd6f9999effdf894a36dbc7db90e890f4be840',
185
+ },
186
+ },
187
+ ]);
188
+ });
189
+ it('returns missing pull requests', async () => {
190
+ const commitWithoutPRs = res.find((commit) => commit.sourcePullRequest?.number === 8);
191
+ expect(commitWithoutPRs?.expectedTargetPullRequests).toEqual([
192
+ { branch: '7.x', state: 'NOT_CREATED' },
193
+ ]);
194
+ });
195
+ });
196
+ });