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,31 @@
1
+ import { AxiosResponse } from 'axios';
2
+ import { DocumentNode } from 'graphql';
3
+ interface GithubError {
4
+ type?: string;
5
+ path?: string[];
6
+ locations?: {
7
+ line: number;
8
+ column: number;
9
+ }[];
10
+ message: string;
11
+ }
12
+ export interface GithubV4Response<DataResponse> {
13
+ data: DataResponse;
14
+ errors?: GithubError[];
15
+ }
16
+ declare type Variables = Record<string, string | number | null>;
17
+ export declare function apiRequestV4<DataResponse>({ githubApiBaseUrlV4, accessToken, query, variables, }: {
18
+ githubApiBaseUrlV4?: string;
19
+ accessToken: string;
20
+ query: DocumentNode;
21
+ variables?: Variables;
22
+ }): Promise<DataResponse>;
23
+ declare type AxiosGithubResponse<DataResponse> = AxiosResponse<GithubV4Response<DataResponse | null>, any>;
24
+ export declare class GithubV4Exception<DataResponse> extends Error {
25
+ githubResponse: AxiosGithubResponse<DataResponse> & {
26
+ request: undefined;
27
+ };
28
+ constructor(githubResponse: AxiosGithubResponse<DataResponse>, errorMessage?: string);
29
+ }
30
+ export declare function getQueryName(query: DocumentNode): string;
31
+ export {};
@@ -0,0 +1,71 @@
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.getQueryName = exports.GithubV4Exception = exports.apiRequestV4 = void 0;
7
+ const axios_1 = __importDefault(require("axios"));
8
+ const printer_1 = require("graphql/language/printer");
9
+ const BackportError_1 = require("../../BackportError");
10
+ const logger_1 = require("../../logger");
11
+ async function apiRequestV4({ githubApiBaseUrlV4 = 'https://api.github.com/graphql', accessToken, query, variables, }) {
12
+ try {
13
+ const response = await axios_1.default.post(githubApiBaseUrlV4, { query: (0, printer_1.print)(query), variables }, {
14
+ headers: {
15
+ 'Content-Type': 'application/json',
16
+ Authorization: `bearer ${accessToken}`,
17
+ },
18
+ });
19
+ if (response.data.errors) {
20
+ throw new GithubV4Exception(response);
21
+ }
22
+ addDebugLogs({
23
+ githubApiBaseUrlV4,
24
+ query,
25
+ variables,
26
+ githubResponse: response,
27
+ });
28
+ return response.data.data;
29
+ }
30
+ catch (e) {
31
+ if (axios_1.default.isAxiosError(e) && e.response) {
32
+ addDebugLogs({
33
+ githubApiBaseUrlV4,
34
+ query,
35
+ variables,
36
+ githubResponse: e.response,
37
+ });
38
+ throw new GithubV4Exception(e.response, e.message);
39
+ }
40
+ throw e;
41
+ }
42
+ }
43
+ exports.apiRequestV4 = apiRequestV4;
44
+ function addDebugLogs({ githubApiBaseUrlV4, query, variables, githubResponse, }) {
45
+ const gqlQueryName = getQueryName(query);
46
+ logger_1.logger.info(`POST ${githubApiBaseUrlV4} (name:${gqlQueryName}, status: ${githubResponse.status})`);
47
+ logger_1.logger.info(`Query name ${gqlQueryName}`);
48
+ logger_1.logger.info(`Query: ${query}`);
49
+ logger_1.logger.info('Variables:', variables);
50
+ logger_1.logger.info('Response headers:', githubResponse.headers);
51
+ logger_1.logger.info('Response data:', githubResponse.data);
52
+ }
53
+ class GithubV4Exception extends Error {
54
+ constructor(githubResponse, errorMessage) {
55
+ const githubMessage = githubResponse.data.errors
56
+ ?.map((error) => error.message)
57
+ .join(',');
58
+ const message = `${errorMessage ?? githubMessage ?? 'Unknown error'} (Github API v4)`;
59
+ super(message);
60
+ Error.captureStackTrace(this, BackportError_1.BackportError);
61
+ this.name = 'GithubV4Exception';
62
+ this.message = message;
63
+ this.githubResponse = { ...githubResponse, request: undefined };
64
+ }
65
+ }
66
+ exports.GithubV4Exception = GithubV4Exception;
67
+ function getQueryName(query) {
68
+ //@ts-expect-error
69
+ return query.definitions[0].name?.value;
70
+ }
71
+ exports.getQueryName = getQueryName;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,109 @@
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 graphql_tag_1 = __importDefault(require("graphql-tag"));
7
+ const nock_1 = __importDefault(require("nock"));
8
+ const nockHelpers_1 = require("../../../test/nockHelpers");
9
+ const BackportError_1 = require("../../BackportError");
10
+ const apiRequestV4_1 = require("./apiRequestV4");
11
+ describe('apiRequestV4', () => {
12
+ afterEach(() => {
13
+ nock_1.default.cleanAll();
14
+ });
15
+ describe('when request succeeds', () => {
16
+ let res;
17
+ let commitsByAuthorCalls;
18
+ beforeEach(async () => {
19
+ commitsByAuthorCalls = (0, nockHelpers_1.mockGqlRequest)({
20
+ name: 'MyQuery',
21
+ statusCode: 200,
22
+ body: { data: { viewer: { login: 'sqren' } } },
23
+ });
24
+ res = await (0, apiRequestV4_1.apiRequestV4)({
25
+ accessToken: 'myAccessToken',
26
+ githubApiBaseUrlV4: 'http://localhost/graphql',
27
+ query: (0, graphql_tag_1.default) `
28
+ query MyQuery {
29
+ viewer {
30
+ login
31
+ }
32
+ }
33
+ `,
34
+ variables: { foo: 'bar' },
35
+ });
36
+ });
37
+ it('should return correct response', async () => {
38
+ expect(res).toEqual({ viewer: { login: 'sqren' } });
39
+ });
40
+ it('should call with correct args', async () => {
41
+ expect(commitsByAuthorCalls).toMatchInlineSnapshot(`
42
+ Array [
43
+ Object {
44
+ "query": "query MyQuery {
45
+ viewer {
46
+ login
47
+ }
48
+ }",
49
+ "variables": Object {
50
+ "foo": "bar",
51
+ },
52
+ },
53
+ ]
54
+ `);
55
+ });
56
+ });
57
+ describe('when request fails with error messages', () => {
58
+ beforeEach(() => {
59
+ (0, nockHelpers_1.mockGqlRequest)({
60
+ name: 'MyQuery',
61
+ statusCode: 200,
62
+ body: {
63
+ errors: [{ message: 'some error' }, { message: 'some other error' }],
64
+ },
65
+ });
66
+ });
67
+ it('should return error containing the error messages', async () => {
68
+ return expect((0, apiRequestV4_1.apiRequestV4)({
69
+ accessToken: 'myAccessToken',
70
+ githubApiBaseUrlV4: 'http://localhost/graphql',
71
+ query: (0, graphql_tag_1.default) `
72
+ query MyQuery {
73
+ viewer {
74
+ login
75
+ }
76
+ }
77
+ `,
78
+ variables: {
79
+ foo: 'bar',
80
+ },
81
+ })).rejects.toThrowError(new BackportError_1.BackportError(`some error,some other error (Github API v4)`));
82
+ });
83
+ });
84
+ describe('when request fails without error messages', () => {
85
+ beforeEach(() => {
86
+ (0, nockHelpers_1.mockGqlRequest)({
87
+ name: 'MyQuery',
88
+ statusCode: 500,
89
+ body: { data: { foo: 'bar' } },
90
+ });
91
+ });
92
+ it('should return parsed github error', async () => {
93
+ return expect((0, apiRequestV4_1.apiRequestV4)({
94
+ accessToken: 'myAccessToken',
95
+ githubApiBaseUrlV4: 'http://localhost/graphql',
96
+ query: (0, graphql_tag_1.default) `
97
+ query MyQuery {
98
+ viewer {
99
+ login
100
+ }
101
+ }
102
+ `,
103
+ variables: {
104
+ foo: 'bar',
105
+ },
106
+ })).rejects.toThrowError('Request failed with status code 500');
107
+ });
108
+ });
109
+ });
@@ -0,0 +1,2 @@
1
+ import { ValidConfigOptions } from '../../../options/options';
2
+ export declare function disablePullRequestAutoMerge(options: ValidConfigOptions, pullNumber: number): Promise<number | undefined>;
@@ -0,0 +1,32 @@
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.disablePullRequestAutoMerge = void 0;
7
+ const graphql_tag_1 = __importDefault(require("graphql-tag"));
8
+ const FetchPullRequestId_1 = require("./FetchPullRequestId");
9
+ const apiRequestV4_1 = require("./apiRequestV4");
10
+ async function disablePullRequestAutoMerge(options, pullNumber) {
11
+ const { accessToken, githubApiBaseUrlV4 } = options;
12
+ const pullRequestId = await (0, FetchPullRequestId_1.fetchPullRequestId)(options, pullNumber);
13
+ const query = (0, graphql_tag_1.default) `
14
+ mutation DisablePullRequestAutoMerge($pullRequestId: ID!) {
15
+ disablePullRequestAutoMerge(input: { pullRequestId: $pullRequestId }) {
16
+ pullRequest {
17
+ number
18
+ }
19
+ }
20
+ }
21
+ `;
22
+ const res = await (0, apiRequestV4_1.apiRequestV4)({
23
+ githubApiBaseUrlV4,
24
+ accessToken,
25
+ query,
26
+ variables: {
27
+ pullRequestId,
28
+ },
29
+ });
30
+ return res.disablePullRequestAutoMerge.pullRequest?.number;
31
+ }
32
+ exports.disablePullRequestAutoMerge = disablePullRequestAutoMerge;
@@ -0,0 +1,2 @@
1
+ import { ValidConfigOptions } from '../../../options/options';
2
+ export declare function enablePullRequestAutoMerge(options: ValidConfigOptions, targetPullRequestNumber: number): Promise<number | undefined>;
@@ -0,0 +1,50 @@
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.enablePullRequestAutoMerge = void 0;
7
+ const graphql_tag_1 = __importDefault(require("graphql-tag"));
8
+ const ora_1 = require("../../../lib/ora");
9
+ const logger_1 = require("../../logger");
10
+ const FetchPullRequestId_1 = require("./FetchPullRequestId");
11
+ const apiRequestV4_1 = require("./apiRequestV4");
12
+ async function enablePullRequestAutoMerge(options, targetPullRequestNumber) {
13
+ const { accessToken, githubApiBaseUrlV4, autoMergeMethod = 'merge', } = options;
14
+ const text = `Enabling auto merging via ${options.autoMergeMethod}`;
15
+ logger_1.logger.info(text);
16
+ const spinner = (0, ora_1.ora)(options.interactive, text).start();
17
+ const pullRequestId = await (0, FetchPullRequestId_1.fetchPullRequestId)(options, targetPullRequestNumber);
18
+ const query = (0, graphql_tag_1.default) `
19
+ mutation EnablePullRequestAutoMerge(
20
+ $pullRequestId: ID!
21
+ $mergeMethod: PullRequestMergeMethod!
22
+ ) {
23
+ enablePullRequestAutoMerge(
24
+ input: { pullRequestId: $pullRequestId, mergeMethod: $mergeMethod }
25
+ ) {
26
+ pullRequest {
27
+ number
28
+ }
29
+ }
30
+ }
31
+ `;
32
+ try {
33
+ const res = await (0, apiRequestV4_1.apiRequestV4)({
34
+ githubApiBaseUrlV4,
35
+ accessToken,
36
+ query,
37
+ variables: {
38
+ pullRequestId,
39
+ mergeMethod: autoMergeMethod.toUpperCase(),
40
+ },
41
+ });
42
+ spinner.succeed();
43
+ return res.enablePullRequestAutoMerge.pullRequest?.number;
44
+ }
45
+ catch (e) {
46
+ spinner.fail();
47
+ logger_1.logger.info(`Could not enable auto merging for ${targetPullRequestNumber}`, e);
48
+ }
49
+ }
50
+ exports.enablePullRequestAutoMerge = enablePullRequestAutoMerge;
@@ -0,0 +1,112 @@
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 crypto_1 = __importDefault(require("crypto"));
7
+ const rest_1 = require("@octokit/rest");
8
+ const getDevAccessToken_1 = require("../../../test/private/getDevAccessToken");
9
+ const createPullRequest_1 = require("../v3/createPullRequest");
10
+ const disablePullRequestAutoMerge_1 = require("./disablePullRequestAutoMerge");
11
+ const enablePullRequestAutoMerge_1 = require("./enablePullRequestAutoMerge");
12
+ const fetchPullRequestAutoMergeMethod_1 = require("./fetchPullRequestAutoMergeMethod");
13
+ // The test repo requires auto-merge being enabled in options, as well as all merge types enabled (merge, squash, rebase)
14
+ // The test pull requests should be open, and not currently able to be merged (e.g. because it requires an approval),
15
+ // otherwise it will be merged when auto-merge is turned on
16
+ const TEST_REPO_OWNER = 'backport-org';
17
+ const TEST_REPO_NAME = 'repo-with-auto-merge-enabled';
18
+ // commit to create new branch from
19
+ // https://github.com/backport-org/repo-with-auto-merge-enabled/commit/1a88d210f90a22e2a06253c5760909833dc820e9
20
+ const COMMIT_SHA = '1a88d210f90a22e2a06253c5760909833dc820e9';
21
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
22
+ async function closePr(octokit, pullNumber) {
23
+ await octokit.pulls.update({
24
+ owner: TEST_REPO_OWNER,
25
+ repo: TEST_REPO_NAME,
26
+ pull_number: pullNumber,
27
+ state: 'closed',
28
+ });
29
+ }
30
+ async function createPr(options, branchName) {
31
+ const prPayload = {
32
+ base: 'main',
33
+ head: branchName,
34
+ body: 'testing...',
35
+ owner: TEST_REPO_OWNER,
36
+ repo: TEST_REPO_NAME,
37
+ title: 'my pr title',
38
+ };
39
+ const { number } = await (0, createPullRequest_1.createPullRequest)({ options, prPayload });
40
+ return number;
41
+ }
42
+ async function deleteBranch(octokit, branchName) {
43
+ await octokit.git.deleteRef({
44
+ owner: TEST_REPO_OWNER,
45
+ repo: TEST_REPO_NAME,
46
+ ref: `heads/${branchName}`,
47
+ });
48
+ }
49
+ async function createBranch(octokit, branchName, sha) {
50
+ await octokit.git.createRef({
51
+ owner: TEST_REPO_OWNER,
52
+ repo: TEST_REPO_NAME,
53
+ ref: `refs/heads/${branchName}`,
54
+ sha,
55
+ });
56
+ }
57
+ // causes flaky test runs on CI because parallel builds are racing each other
58
+ // might also be an issue with Github's API being slow at updating after a mutation happens
59
+ // eslint-disable-next-line jest/no-disabled-tests
60
+ describe.skip('enablePullRequestAutoMerge', () => {
61
+ let pullNumber;
62
+ let branchName;
63
+ let octokit;
64
+ let options;
65
+ beforeAll(async () => {
66
+ const accessToken = (0, getDevAccessToken_1.getDevAccessToken)();
67
+ const randomString = crypto_1.default.randomBytes(4).toString('hex');
68
+ branchName = `test-${randomString}`;
69
+ options = {
70
+ accessToken,
71
+ repoOwner: TEST_REPO_OWNER,
72
+ repoName: TEST_REPO_NAME,
73
+ };
74
+ octokit = new rest_1.Octokit({ auth: accessToken });
75
+ await createBranch(octokit, branchName, COMMIT_SHA);
76
+ pullNumber = await createPr(options, branchName);
77
+ });
78
+ // cleanup
79
+ afterAll(async () => {
80
+ await closePr(octokit, pullNumber);
81
+ await deleteBranch(octokit, branchName);
82
+ });
83
+ // reset auto-merge state between runs
84
+ afterEach(async () => {
85
+ await (0, disablePullRequestAutoMerge_1.disablePullRequestAutoMerge)(options, pullNumber);
86
+ });
87
+ it('should initially have auto-merge disabled', async () => {
88
+ const autoMergeMethod = await (0, fetchPullRequestAutoMergeMethod_1.fetchPullRequestAutoMergeMethod)(options, pullNumber);
89
+ expect(autoMergeMethod).toBe(undefined);
90
+ });
91
+ it('should enable auto-merge via merge', async () => {
92
+ await (0, enablePullRequestAutoMerge_1.enablePullRequestAutoMerge)({ ...options, autoMergeMethod: 'merge' }, pullNumber);
93
+ // ensure Github API reflects the change before querying
94
+ await sleep(100);
95
+ const autoMergeMethod = await (0, fetchPullRequestAutoMergeMethod_1.fetchPullRequestAutoMergeMethod)(options, pullNumber);
96
+ expect(autoMergeMethod).toBe('MERGE');
97
+ });
98
+ it('should not enable auto-merge via rebase because it is disallowed', async () => {
99
+ await (0, enablePullRequestAutoMerge_1.enablePullRequestAutoMerge)({ ...options, autoMergeMethod: 'rebase' }, pullNumber);
100
+ // ensure Github API reflects the change before querying
101
+ await sleep(100);
102
+ const autoMergeMethod = await (0, fetchPullRequestAutoMergeMethod_1.fetchPullRequestAutoMergeMethod)(options, pullNumber);
103
+ expect(autoMergeMethod).toBe(undefined);
104
+ });
105
+ it('should enable auto-merge via squash', async () => {
106
+ await (0, enablePullRequestAutoMerge_1.enablePullRequestAutoMerge)({ ...options, autoMergeMethod: 'squash' }, pullNumber);
107
+ // ensure Github API reflects the change before querying
108
+ await sleep(100);
109
+ const autoMergeMethod = await (0, fetchPullRequestAutoMergeMethod_1.fetchPullRequestAutoMergeMethod)(options, pullNumber);
110
+ expect(autoMergeMethod).toBe('SQUASH');
111
+ });
112
+ });
@@ -0,0 +1,10 @@
1
+ export interface AuthorIdResponse {
2
+ user: {
3
+ id: string;
4
+ };
5
+ }
6
+ export declare function fetchAuthorId({ accessToken, author, githubApiBaseUrlV4, }: {
7
+ accessToken: string;
8
+ author: string | null;
9
+ githubApiBaseUrlV4?: string;
10
+ }): Promise<string | null>;
@@ -0,0 +1,28 @@
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.fetchAuthorId = void 0;
7
+ const graphql_tag_1 = __importDefault(require("graphql-tag"));
8
+ const apiRequestV4_1 = require("./apiRequestV4");
9
+ async function fetchAuthorId({ accessToken, author, githubApiBaseUrlV4 = 'https://api.github.com/graphql', }) {
10
+ if (author === null) {
11
+ return null;
12
+ }
13
+ const query = (0, graphql_tag_1.default) `
14
+ query AuthorId($author: String!) {
15
+ user(login: $author) {
16
+ id
17
+ }
18
+ }
19
+ `;
20
+ const res = await (0, apiRequestV4_1.apiRequestV4)({
21
+ githubApiBaseUrlV4,
22
+ accessToken,
23
+ query,
24
+ variables: { author },
25
+ });
26
+ return res.user.id;
27
+ }
28
+ exports.fetchAuthorId = fetchAuthorId;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const getDevAccessToken_1 = require("../../../test/private/getDevAccessToken");
4
+ const fetchAuthorId_1 = require("./fetchAuthorId");
5
+ const accessToken = (0, getDevAccessToken_1.getDevAccessToken)();
6
+ describe('fetchAuthorId', () => {
7
+ describe('author = null', () => {
8
+ it('returns null', async () => {
9
+ const options = {
10
+ accessToken,
11
+ author: null,
12
+ };
13
+ expect(await (0, fetchAuthorId_1.fetchAuthorId)(options)).toEqual(null);
14
+ });
15
+ });
16
+ describe('author is "sqren"', () => {
17
+ it('returns author id', async () => {
18
+ const options = {
19
+ accessToken,
20
+ author: 'sqren',
21
+ };
22
+ expect(await (0, fetchAuthorId_1.fetchAuthorId)(options)).toEqual('MDQ6VXNlcjIwOTk2Ng==');
23
+ });
24
+ });
25
+ });
@@ -0,0 +1,93 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const getDevAccessToken_1 = require("../../../../test/private/getDevAccessToken");
4
+ const fetchCommitByPullNumber_1 = require("./fetchCommitByPullNumber");
5
+ const fetchCommitBySha_1 = require("./fetchCommitBySha");
6
+ const fetchCommitsByAuthor_1 = require("./fetchCommitsByAuthor");
7
+ const fetchPullRequestsBySearchQuery_1 = require("./fetchPullRequestsBySearchQuery");
8
+ const accessToken = (0, getDevAccessToken_1.getDevAccessToken)();
9
+ describe('allFetchers', () => {
10
+ let commitByAuthor;
11
+ beforeEach(async () => {
12
+ const commitsByAuthor = await (0, fetchCommitsByAuthor_1.fetchCommitsByAuthor)({
13
+ accessToken,
14
+ author: 'sqren',
15
+ maxNumber: 1,
16
+ repoName: 'kibana',
17
+ repoOwner: 'elastic',
18
+ sourceBranch: 'main',
19
+ dateSince: '2021-01-10T00:00:00Z',
20
+ dateUntil: '2022-01-01T00:00:00Z',
21
+ commitPaths: [],
22
+ });
23
+ commitByAuthor = commitsByAuthor[0];
24
+ });
25
+ it('matches commitByAuthor with commitByPullNumber', async () => {
26
+ if (!commitByAuthor.sourcePullRequest) {
27
+ throw new Error('Missing pull number!');
28
+ }
29
+ const commitByPullNumber = await (0, fetchCommitByPullNumber_1.fetchCommitByPullNumber)({
30
+ repoOwner: 'elastic',
31
+ repoName: 'kibana',
32
+ accessToken,
33
+ pullNumber: commitByAuthor.sourcePullRequest.number,
34
+ sourceBranch: 'master',
35
+ });
36
+ expect(commitByAuthor).toEqual(commitByPullNumber);
37
+ });
38
+ it('matches commitByAuthor with commitBySha', async () => {
39
+ const commitBySha = await (0, fetchCommitBySha_1.fetchCommitBySha)({
40
+ repoOwner: 'elastic',
41
+ repoName: 'kibana',
42
+ accessToken,
43
+ sha: commitByAuthor.sourceCommit.sha,
44
+ sourceBranch: 'main',
45
+ });
46
+ expect(commitByAuthor).toEqual(commitBySha);
47
+ });
48
+ it('matches commitByAuthor with commitBySearchQuery', async () => {
49
+ const commitsBySearchQuery = await (0, fetchPullRequestsBySearchQuery_1.fetchPullRequestsBySearchQuery)({
50
+ repoOwner: 'elastic',
51
+ repoName: 'kibana',
52
+ accessToken,
53
+ maxNumber: 1,
54
+ prFilter: `created:2021-12-20..2021-12-20`,
55
+ sourceBranch: 'main',
56
+ author: 'sqren',
57
+ });
58
+ const commitBySearchQuery = commitsBySearchQuery[0];
59
+ expect(commitByAuthor).toEqual(commitBySearchQuery);
60
+ });
61
+ it('returns correct response for commitByAuthor', async () => {
62
+ const expectedCommit = {
63
+ author: { email: 'soren.louv@elastic.co', name: 'Søren Louv-Jansen' },
64
+ sourceCommit: {
65
+ committedDate: '2021-12-20T14:20:16Z',
66
+ message: '[APM] Add note about synthtrace to APM docs (#121633)',
67
+ sha: 'd421ddcf6157150596581c7885afa3690cec6339',
68
+ },
69
+ sourcePullRequest: {
70
+ number: 121633,
71
+ url: 'https://github.com/elastic/kibana/pull/121633',
72
+ mergeCommit: {
73
+ message: '[APM] Add note about synthtrace to APM docs (#121633)',
74
+ sha: 'd421ddcf6157150596581c7885afa3690cec6339',
75
+ },
76
+ },
77
+ sourceBranch: 'main',
78
+ expectedTargetPullRequests: [
79
+ {
80
+ branch: '8.0',
81
+ mergeCommit: {
82
+ message: '[APM] Add note about synthtrace to APM docs (#121633) (#121643)\n\nCo-authored-by: Søren Louv-Jansen <soren.louv@elastic.co>',
83
+ sha: '842adfdeb5541b059231857522f9009771a46107',
84
+ },
85
+ number: 121643,
86
+ state: 'MERGED',
87
+ url: 'https://github.com/elastic/kibana/pull/121643',
88
+ },
89
+ ],
90
+ };
91
+ expect(commitByAuthor).toEqual(expectedCommit);
92
+ });
93
+ });
@@ -0,0 +1,11 @@
1
+ import { ValidConfigOptions } from '../../../../options/options';
2
+ import { Commit } from '../../../sourceCommit/parseSourceCommit';
3
+ export declare function fetchCommitByPullNumber(options: {
4
+ accessToken: string;
5
+ branchLabelMapping?: ValidConfigOptions['branchLabelMapping'];
6
+ githubApiBaseUrlV4?: string;
7
+ pullNumber: number;
8
+ repoName: string;
9
+ repoOwner: string;
10
+ sourceBranch: string;
11
+ }): Promise<Commit>;
@@ -0,0 +1,58 @@
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.fetchCommitByPullNumber = 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 fetchCommitByPullNumber(options) {
13
+ const { accessToken, githubApiBaseUrlV4 = 'https://api.github.com/graphql', pullNumber, repoName, repoOwner, } = options;
14
+ const query = (0, graphql_tag_1.default) `
15
+ query CommitByPullNumber(
16
+ $repoOwner: String!
17
+ $repoName: String!
18
+ $pullNumber: Int!
19
+ ) {
20
+ repository(owner: $repoOwner, name: $repoName) {
21
+ pullRequest(number: $pullNumber) {
22
+ mergeCommit {
23
+ ...SourceCommitWithTargetPullRequestFragment
24
+ }
25
+ }
26
+ }
27
+ }
28
+
29
+ ${parseSourceCommit_1.SourceCommitWithTargetPullRequestFragment}
30
+ `;
31
+ let res;
32
+ try {
33
+ res = await (0, apiRequestV4_1.apiRequestV4)({
34
+ githubApiBaseUrlV4,
35
+ accessToken,
36
+ query,
37
+ variables: {
38
+ repoOwner,
39
+ repoName,
40
+ pullNumber,
41
+ },
42
+ });
43
+ }
44
+ catch (e) {
45
+ //@ts-expect-error
46
+ res = (0, remoteConfig_1.swallowMissingConfigFileException)(e);
47
+ }
48
+ const pullRequestNode = res.repository.pullRequest;
49
+ if (!pullRequestNode) {
50
+ throw new BackportError_1.BackportError(`The PR #${pullNumber} does not exist`);
51
+ }
52
+ const sourceCommit = pullRequestNode.mergeCommit;
53
+ if (sourceCommit === null) {
54
+ throw new BackportError_1.BackportError(`The PR #${pullNumber} is not merged`);
55
+ }
56
+ return (0, parseSourceCommit_1.parseSourceCommit)({ options, sourceCommit });
57
+ }
58
+ exports.fetchCommitByPullNumber = fetchCommitByPullNumber;