backport 8.9.0-beta.1 → 8.9.1

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 (24) hide show
  1. package/README.md +9 -3
  2. package/dist/entrypoint.module.js +2 -1
  3. package/dist/lib/cherrypickAndCreateTargetPullRequest/autoMergeNowOrLater.d.ts +2 -0
  4. package/dist/lib/cherrypickAndCreateTargetPullRequest/autoMergeNowOrLater.js +42 -0
  5. package/dist/lib/cherrypickAndCreateTargetPullRequest/cherrypickAndCreateTargetPullRequest.js +2 -2
  6. package/dist/lib/getCommits.js +2 -1
  7. package/dist/lib/github/v3/mergePullRequest.d.ts +6 -0
  8. package/dist/lib/github/v3/mergePullRequest.js +19 -0
  9. package/dist/lib/github/v4/enablePullRequestAutoMerge.d.ts +4 -0
  10. package/dist/lib/github/v4/enablePullRequestAutoMerge.js +18 -28
  11. package/dist/lib/github/v4/enablePullRequestAutoMerge.mutation.test.js +168 -62
  12. package/dist/lib/github/v4/fetchCommits/allFetchers.private.test.js +2 -2
  13. package/dist/lib/github/v4/fetchCommits/fetchCommitByPullNumber.d.ts +2 -2
  14. package/dist/lib/github/v4/fetchCommits/fetchCommitByPullNumber.js +34 -10
  15. package/dist/lib/github/v4/fetchCommits/fetchCommitByPullNumber.private.test.js +8 -8
  16. package/dist/lib/github/v4/fetchCommits/fetchCommitsForRebaseAndMergeStrategy.d.ts +9 -0
  17. package/dist/lib/github/v4/fetchCommits/fetchCommitsForRebaseAndMergeStrategy.js +78 -0
  18. package/dist/lib/github/v4/fetchCommits/fetchCommitsForRebaseAndMergeStrategy.private.test.d.ts +1 -0
  19. package/dist/lib/github/v4/fetchCommits/fetchCommitsForRebaseAndMergeStrategy.private.test.js +29 -0
  20. package/dist/test/e2e/cli/different-merge-strategies.private.test.js +57 -23
  21. package/dist/utils/packageVersion.d.ts +1 -1
  22. package/dist/utils/packageVersion.js +1 -1
  23. package/package.json +13 -13
  24. package/yarn.lock +266 -254
package/README.md CHANGED
@@ -46,12 +46,17 @@ Add a [project config](/docs/config-file-options.md#project-config-backportrcjso
46
46
  ```js
47
47
  // .backportrc.json
48
48
  {
49
+ // Required
49
50
  "repoOwner": "elastic",
50
51
  "repoName": "kibana",
51
52
 
52
53
  // the branches available to backport to
53
54
  "targetBranchChoices": ["main", "6.3", "6.2", "6.1", "6.0"],
54
55
 
56
+ // Optional: automatically merge backport PR
57
+ "autoMerge": true,
58
+ "autoMergeMethod": "squash",
59
+
55
60
  // Optional: Automatically detect which branches a pull request should be backported to based on the pull request labels.
56
61
  // In this case, adding the label "backport-to-production" will backport the PR to the "production" branch
57
62
  "branchLabelMapping": {
@@ -78,9 +83,10 @@ npx backport
78
83
  _This will start an interactive prompt. You can use your keyboards arrow keys to choose options, `<space>` to select checkboxes and `<enter>` to proceed._
79
84
 
80
85
  ## Documentation
81
- - [Config file options](/docs/config-file-options.md)
82
- - [CLI options](/docs/cli-options.md)
83
- - [Module API](/docs/api.md)
86
+
87
+ - [Config file options](/docs/config-file-options.md)
88
+ - [CLI options](/docs/cli-options.md)
89
+ - [Module API](/docs/api.md)
84
90
 
85
91
  ### What is backporting?
86
92
 
@@ -31,11 +31,12 @@ async function getCommits(options) {
31
31
  const pullNumbers = Array.isArray(options.pullNumber)
32
32
  ? options.pullNumber
33
33
  : [options.pullNumber];
34
- return Promise.all(pullNumbers.map((pullNumber) => (0, fetchCommitByPullNumber_1.fetchCommitByPullNumber)({
34
+ const nestedCommits = await Promise.all(pullNumbers.map((pullNumber) => (0, fetchCommitByPullNumber_1.fetchCommitsByPullNumber)({
35
35
  ...optionsFromGithub,
36
36
  ...options,
37
37
  pullNumber,
38
38
  })));
39
+ return nestedCommits.flat();
39
40
  }
40
41
  if (options.sha) {
41
42
  const shas = Array.isArray(options.sha) ? options.sha : [options.sha];
@@ -0,0 +1,2 @@
1
+ import { ValidConfigOptions } from '../../options/options';
2
+ export declare function autoMergeNowOrLater(options: ValidConfigOptions, pullNumber: number): Promise<void>;
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.autoMergeNowOrLater = void 0;
4
+ const mergePullRequest_1 = require("../github/v3/mergePullRequest");
5
+ const apiRequestV4_1 = require("../github/v4/apiRequestV4");
6
+ const enablePullRequestAutoMerge_1 = require("../github/v4/enablePullRequestAutoMerge");
7
+ const logger_1 = require("../logger");
8
+ const ora_1 = require("../ora");
9
+ async function autoMergeNowOrLater(options, pullNumber) {
10
+ const text = `Auto-merge: Enabling via "${options.autoMergeMethod}"`;
11
+ logger_1.logger.info(text);
12
+ const spinner = (0, ora_1.ora)(options.interactive, text).start();
13
+ if (options.dryRun) {
14
+ spinner.succeed();
15
+ return;
16
+ }
17
+ try {
18
+ try {
19
+ await (0, enablePullRequestAutoMerge_1.enablePullRequestAutoMerge)(options, pullNumber);
20
+ }
21
+ catch (e) {
22
+ if (!(e instanceof apiRequestV4_1.GithubV4Exception)) {
23
+ throw e;
24
+ }
25
+ logger_1.logger.info(`Auto merge: Failed to enable auto merge for PR "#${pullNumber}" due to ${e.message}`);
26
+ const { isMissingStatusChecks } = (0, enablePullRequestAutoMerge_1.parseGithubError)(e);
27
+ if (!isMissingStatusChecks) {
28
+ throw e;
29
+ }
30
+ // if auto merge cannot be enabled due to missing status checks, the PR should be merged immediately
31
+ logger_1.logger.info('Auto merge: Attempting to merge immediately');
32
+ await (0, mergePullRequest_1.mergePullRequest)(options, pullNumber);
33
+ spinner.text = 'Auto-merge: Pull request was merged immediately';
34
+ }
35
+ spinner.succeed();
36
+ }
37
+ catch (e) {
38
+ logger_1.logger.warn(`Auto merge: An error occurred ${e}`);
39
+ spinner.fail();
40
+ }
41
+ }
42
+ exports.autoMergeNowOrLater = autoMergeNowOrLater;
@@ -12,10 +12,10 @@ const addAssigneesToPullRequest_1 = require("../github/v3/addAssigneesToPullRequ
12
12
  const addLabelsToPullRequest_1 = require("../github/v3/addLabelsToPullRequest");
13
13
  const addReviewersToPullRequest_1 = require("../github/v3/addReviewersToPullRequest");
14
14
  const createPullRequest_1 = require("../github/v3/createPullRequest");
15
- const enablePullRequestAutoMerge_1 = require("../github/v4/enablePullRequestAutoMerge");
16
15
  const validateTargetBranch_1 = require("../github/v4/validateTargetBranch");
17
16
  const logger_1 = require("../logger");
18
17
  const sequentially_1 = require("../sequentially");
18
+ const autoMergeNowOrLater_1 = require("./autoMergeNowOrLater");
19
19
  const getBackportBranchName_1 = require("./getBackportBranchName");
20
20
  const getMergeCommit_1 = require("./getMergeCommit");
21
21
  const getTargetPRLabels_1 = require("./getTargetPRLabels");
@@ -77,7 +77,7 @@ async function cherrypickAndCreateTargetPullRequest({ options, commits, targetBr
77
77
  }
78
78
  // make PR auto mergable
79
79
  if (options.autoMerge) {
80
- await (0, enablePullRequestAutoMerge_1.enablePullRequestAutoMerge)(options, targetPullRequest.number);
80
+ await (0, autoMergeNowOrLater_1.autoMergeNowOrLater)(options, targetPullRequest.number);
81
81
  }
82
82
  // add labels to source pull requests
83
83
  if (options.sourcePRLabels.length > 0) {
@@ -38,7 +38,8 @@ async function getCommits(options) {
38
38
  spinner.text = `Loading pull request ${pullNumbers
39
39
  .map((pullNumber) => `#${pullNumber}`)
40
40
  .join(', ')}`;
41
- const commits = await Promise.all(pullNumbers.map((pullNumber) => (0, fetchCommitByPullNumber_1.fetchCommitByPullNumber)({ ...options, pullNumber })));
41
+ const nestedCommits = await Promise.all(pullNumbers.map((pullNumber) => (0, fetchCommitByPullNumber_1.fetchCommitsByPullNumber)({ ...options, pullNumber })));
42
+ const commits = nestedCommits.flat();
42
43
  // add styles to make it look like a prompt question
43
44
  spinner.stopAndPersist(getOraPersistsOption('Select pull request', commits
44
45
  .map((commit) => (0, commitFormatters_1.getFirstLine)(commit.sourceCommit.message))
@@ -0,0 +1,6 @@
1
+ import { ValidConfigOptions } from '../../../options/options';
2
+ export declare function mergePullRequest(options: ValidConfigOptions, pullNumber: number): Promise<import("@octokit/types").OctokitResponse<{
3
+ sha: string;
4
+ merged: boolean;
5
+ message: string;
6
+ }, 200>>;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.mergePullRequest = void 0;
4
+ const rest_1 = require("@octokit/rest");
5
+ const logger_1 = require("../../logger");
6
+ function mergePullRequest(options, pullNumber) {
7
+ const { accessToken, githubApiBaseUrlV3 } = options;
8
+ const octokit = new rest_1.Octokit({
9
+ auth: accessToken,
10
+ baseUrl: githubApiBaseUrlV3,
11
+ log: logger_1.logger,
12
+ });
13
+ return octokit.request('PUT /repos/{owner}/{repo}/pulls/{pull_number}/merge', {
14
+ owner: options.repoOwner,
15
+ repo: options.repoName,
16
+ pull_number: pullNumber,
17
+ });
18
+ }
19
+ exports.mergePullRequest = mergePullRequest;
@@ -1,2 +1,6 @@
1
1
  import { ValidConfigOptions } from '../../../options/options';
2
+ import { GithubV4Exception } from './apiRequestV4';
2
3
  export declare function enablePullRequestAutoMerge(options: ValidConfigOptions, targetPullRequestNumber: number): Promise<number | undefined>;
4
+ export declare function parseGithubError(e: GithubV4Exception<any>): {
5
+ isMissingStatusChecks: boolean | undefined;
6
+ };
@@ -3,21 +3,12 @@ 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.enablePullRequestAutoMerge = void 0;
6
+ exports.parseGithubError = exports.enablePullRequestAutoMerge = void 0;
7
7
  const graphql_tag_1 = __importDefault(require("graphql-tag"));
8
- const ora_1 = require("../../../lib/ora");
9
- const logger_1 = require("../../logger");
10
8
  const FetchPullRequestId_1 = require("./FetchPullRequestId");
11
9
  const apiRequestV4_1 = require("./apiRequestV4");
12
10
  async function enablePullRequestAutoMerge(options, targetPullRequestNumber) {
13
11
  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
- if (options.dryRun) {
18
- spinner.succeed();
19
- return;
20
- }
21
12
  const pullRequestId = await (0, FetchPullRequestId_1.fetchPullRequestId)(options, targetPullRequestNumber);
22
13
  const query = (0, graphql_tag_1.default) `
23
14
  mutation EnablePullRequestAutoMerge(
@@ -33,23 +24,22 @@ async function enablePullRequestAutoMerge(options, targetPullRequestNumber) {
33
24
  }
34
25
  }
35
26
  `;
36
- try {
37
- const res = await (0, apiRequestV4_1.apiRequestV4)({
38
- githubApiBaseUrlV4,
39
- accessToken,
40
- query,
41
- variables: {
42
- pullRequestId,
43
- mergeMethod: autoMergeMethod.toUpperCase(),
44
- },
45
- });
46
- spinner.succeed();
47
- return res.enablePullRequestAutoMerge.pullRequest?.number;
48
- }
49
- catch (e) {
50
- const err = e;
51
- spinner.fail();
52
- logger_1.logger.info(`Could not enable auto merging for ${targetPullRequestNumber} due to ${err.message}`, e);
53
- }
27
+ const res = await (0, apiRequestV4_1.apiRequestV4)({
28
+ githubApiBaseUrlV4,
29
+ accessToken,
30
+ query,
31
+ variables: {
32
+ pullRequestId,
33
+ mergeMethod: autoMergeMethod.toUpperCase(),
34
+ },
35
+ });
36
+ return res.enablePullRequestAutoMerge.pullRequest?.number;
54
37
  }
55
38
  exports.enablePullRequestAutoMerge = enablePullRequestAutoMerge;
39
+ function parseGithubError(e) {
40
+ const isMissingStatusChecks = e.githubResponse.data.errors?.some((e) => e.type === 'UNPROCESSABLE' &&
41
+ (e.message.includes('Branch does not have required protected branch rules') ||
42
+ e.message.includes('Pull request is in clean status')));
43
+ return { isMissingStatusChecks };
44
+ }
45
+ exports.parseGithubError = parseGithubError;
@@ -16,6 +16,7 @@ const fetchPullRequestAutoMergeMethod_1 = require("./fetchPullRequestAutoMergeMe
16
16
  const TEST_REPO_OWNER = 'backport-org';
17
17
  const TEST_REPO_NAME = 'repo-with-auto-merge-enabled';
18
18
  const INITIAL_SHA = '70aa879411e95b6662f8ddcb80a944fc4444579f';
19
+ const accessToken = (0, getDevAccessToken_1.getDevAccessToken)();
19
20
  jest.setTimeout(10000);
20
21
  const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
21
22
  function resetReference(octokit) {
@@ -27,7 +28,7 @@ function resetReference(octokit) {
27
28
  force: true,
28
29
  });
29
30
  }
30
- async function closePr(octokit, pullNumber) {
31
+ async function closePr({ octokit, pullNumber, }) {
31
32
  await octokit.pulls.update({
32
33
  owner: TEST_REPO_OWNER,
33
34
  repo: TEST_REPO_NAME,
@@ -35,10 +36,10 @@ async function closePr(octokit, pullNumber) {
35
36
  state: 'closed',
36
37
  });
37
38
  }
38
- async function createPr(options, branchName) {
39
+ async function createPr({ options, headBranch, baseBranch, }) {
39
40
  const prPayload = {
40
- base: '7.x',
41
- head: branchName,
41
+ base: baseBranch,
42
+ head: headBranch,
42
43
  body: 'testing...',
43
44
  owner: TEST_REPO_OWNER,
44
45
  repo: TEST_REPO_NAME,
@@ -47,14 +48,14 @@ async function createPr(options, branchName) {
47
48
  const { number } = await (0, createPullRequest_1.createPullRequest)({ options, prPayload });
48
49
  return number;
49
50
  }
50
- async function deleteBranch(octokit, branchName) {
51
+ async function deleteBranch({ octokit, branchName, }) {
51
52
  await octokit.git.deleteRef({
52
53
  owner: TEST_REPO_OWNER,
53
54
  repo: TEST_REPO_NAME,
54
55
  ref: `heads/${branchName}`,
55
56
  });
56
57
  }
57
- async function createBranch(octokit, branchName, sha) {
58
+ async function createBranch({ octokit, branchName, sha, }) {
58
59
  await octokit.git.createRef({
59
60
  owner: TEST_REPO_OWNER,
60
61
  repo: TEST_REPO_NAME,
@@ -73,66 +74,171 @@ async function addCommit(octokit) {
73
74
  branch: 'main',
74
75
  });
75
76
  const sha = res.data.commit.sha;
77
+ if (!sha) {
78
+ throw new Error('Missing sha');
79
+ }
76
80
  return sha;
77
81
  }
78
- // Error: cannot have more than 100 pull requests with the same head_sha
79
- // TODO: have unique head sha for every test run
80
- // eslint-disable-next-line jest/no-disabled-tests
81
82
  describe('enablePullRequestAutoMerge', () => {
82
- let pullNumber;
83
- let branchName;
84
- let octokit;
85
- let options;
86
- beforeAll(async () => {
87
- const accessToken = (0, getDevAccessToken_1.getDevAccessToken)();
88
- const randomString = crypto_1.default.randomBytes(4).toString('hex');
89
- branchName = `test-${randomString}`;
90
- options = {
91
- accessToken,
92
- repoOwner: TEST_REPO_OWNER,
93
- repoName: TEST_REPO_NAME,
94
- };
95
- octokit = new rest_1.Octokit({ auth: accessToken });
96
- await resetReference(octokit);
97
- const sha = await addCommit(octokit);
98
- if (sha) {
99
- await createBranch(octokit, branchName, sha);
100
- }
101
- pullNumber = await createPr(options, branchName);
83
+ describe('create a PR and enable auto-merge against "approvals-required-branch"', () => {
84
+ let pullNumber;
85
+ let branchName;
86
+ let octokit;
87
+ let options;
88
+ beforeAll(async () => {
89
+ const randomString = crypto_1.default.randomBytes(4).toString('hex');
90
+ branchName = `test-${randomString}`;
91
+ options = {
92
+ accessToken,
93
+ repoOwner: TEST_REPO_OWNER,
94
+ repoName: TEST_REPO_NAME,
95
+ };
96
+ octokit = new rest_1.Octokit({ auth: accessToken });
97
+ await resetReference(octokit);
98
+ const sha = await addCommit(octokit);
99
+ await createBranch({ octokit, branchName, sha });
100
+ pullNumber = await createPr({
101
+ options,
102
+ headBranch: branchName,
103
+ baseBranch: 'approvals-required-branch',
104
+ });
105
+ });
106
+ // cleanup
107
+ afterAll(async () => {
108
+ await closePr({ octokit, pullNumber });
109
+ await deleteBranch({ octokit, branchName });
110
+ await resetReference(octokit);
111
+ });
112
+ // reset auto-merge state between runs
113
+ afterEach(async () => {
114
+ await (0, disablePullRequestAutoMerge_1.disablePullRequestAutoMerge)(options, pullNumber);
115
+ });
116
+ it('should initially have auto-merge disabled', async () => {
117
+ const autoMergeMethod = await (0, fetchPullRequestAutoMergeMethod_1.fetchPullRequestAutoMergeMethod)(options, pullNumber);
118
+ expect(autoMergeMethod).toBe(undefined);
119
+ });
120
+ it('should enable auto-merge via merge', async () => {
121
+ await (0, enablePullRequestAutoMerge_1.enablePullRequestAutoMerge)({ ...options, autoMergeMethod: 'merge' }, pullNumber);
122
+ // ensure Github API reflects the change before querying
123
+ await sleep(100);
124
+ const autoMergeMethod = await (0, fetchPullRequestAutoMergeMethod_1.fetchPullRequestAutoMergeMethod)(options, pullNumber);
125
+ expect(autoMergeMethod).toBe('MERGE');
126
+ });
127
+ it('should fail when enabling auto-merge via rebase because it is disallowed', async () => {
128
+ let errorMessage;
129
+ let isMissingStatusChecks;
130
+ try {
131
+ await (0, enablePullRequestAutoMerge_1.enablePullRequestAutoMerge)({ ...options, autoMergeMethod: 'rebase' }, pullNumber);
132
+ }
133
+ catch (e) {
134
+ const err = e;
135
+ const res = (0, enablePullRequestAutoMerge_1.parseGithubError)(err);
136
+ errorMessage = err.message;
137
+ isMissingStatusChecks = res.isMissingStatusChecks;
138
+ }
139
+ expect(isMissingStatusChecks).toBe(false);
140
+ expect(errorMessage).toMatchInlineSnapshot(`"[\\"Merge method rebase merging is not allowed on this repository\\"] (Github API v4)"`);
141
+ // ensure Github API reflects the change before querying
142
+ await sleep(100);
143
+ const autoMergeMethod = await (0, fetchPullRequestAutoMergeMethod_1.fetchPullRequestAutoMergeMethod)(options, pullNumber);
144
+ expect(autoMergeMethod).toBe(undefined);
145
+ });
146
+ it('should enable auto-merge via squash', async () => {
147
+ await (0, enablePullRequestAutoMerge_1.enablePullRequestAutoMerge)({ ...options, autoMergeMethod: 'squash' }, pullNumber);
148
+ // ensure Github API reflects the change before querying
149
+ await sleep(100);
150
+ const autoMergeMethod = await (0, fetchPullRequestAutoMergeMethod_1.fetchPullRequestAutoMergeMethod)(options, pullNumber);
151
+ expect(autoMergeMethod).toBe('SQUASH');
152
+ });
102
153
  });
103
- // cleanup
104
- afterAll(async () => {
105
- await closePr(octokit, pullNumber);
106
- await deleteBranch(octokit, branchName);
107
- await resetReference(octokit);
154
+ describe('when creating a PR against "no-checks-required-branch"', () => {
155
+ let pullNumber;
156
+ let branchName;
157
+ let octokit;
158
+ let options;
159
+ beforeAll(async () => {
160
+ const randomString = crypto_1.default.randomBytes(4).toString('hex');
161
+ branchName = `test-${randomString}`;
162
+ options = {
163
+ accessToken,
164
+ repoOwner: TEST_REPO_OWNER,
165
+ repoName: TEST_REPO_NAME,
166
+ };
167
+ octokit = new rest_1.Octokit({ auth: accessToken });
168
+ await resetReference(octokit);
169
+ const sha = await addCommit(octokit);
170
+ await createBranch({ octokit, branchName, sha });
171
+ pullNumber = await createPr({
172
+ options,
173
+ headBranch: branchName,
174
+ baseBranch: 'no-checks-required-branch',
175
+ });
176
+ });
177
+ // cleanup
178
+ afterAll(async () => {
179
+ await closePr({ octokit, pullNumber });
180
+ await deleteBranch({ octokit, branchName });
181
+ await resetReference(octokit);
182
+ });
183
+ it('should not be possible to enable auto-merge', async () => {
184
+ let isMissingStatusChecks;
185
+ let errorMessage;
186
+ try {
187
+ await (0, enablePullRequestAutoMerge_1.enablePullRequestAutoMerge)({ ...options, autoMergeMethod: 'merge' }, pullNumber);
188
+ }
189
+ catch (e) {
190
+ const err = e;
191
+ const res = (0, enablePullRequestAutoMerge_1.parseGithubError)(err);
192
+ errorMessage = err.message;
193
+ isMissingStatusChecks = res.isMissingStatusChecks;
194
+ }
195
+ expect(errorMessage).toMatchInlineSnapshot(`"[\\"Pull request Pull request is in clean status\\"] (Github API v4)"`);
196
+ expect(isMissingStatusChecks).toBe(true);
197
+ });
108
198
  });
109
- // reset auto-merge state between runs
110
- afterEach(async () => {
111
- await (0, disablePullRequestAutoMerge_1.disablePullRequestAutoMerge)(options, pullNumber);
112
- });
113
- it('should initially have auto-merge disabled', async () => {
114
- const autoMergeMethod = await (0, fetchPullRequestAutoMergeMethod_1.fetchPullRequestAutoMergeMethod)(options, pullNumber);
115
- expect(autoMergeMethod).toBe(undefined);
116
- });
117
- it('should enable auto-merge via merge', async () => {
118
- await (0, enablePullRequestAutoMerge_1.enablePullRequestAutoMerge)({ ...options, autoMergeMethod: 'merge' }, pullNumber);
119
- // ensure Github API reflects the change before querying
120
- await sleep(100);
121
- const autoMergeMethod = await (0, fetchPullRequestAutoMergeMethod_1.fetchPullRequestAutoMergeMethod)(options, pullNumber);
122
- expect(autoMergeMethod).toBe('MERGE');
123
- });
124
- it('should not enable auto-merge via rebase because it is disallowed', async () => {
125
- await (0, enablePullRequestAutoMerge_1.enablePullRequestAutoMerge)({ ...options, autoMergeMethod: 'rebase' }, pullNumber);
126
- // ensure Github API reflects the change before querying
127
- await sleep(100);
128
- const autoMergeMethod = await (0, fetchPullRequestAutoMergeMethod_1.fetchPullRequestAutoMergeMethod)(options, pullNumber);
129
- expect(autoMergeMethod).toBe(undefined);
130
- });
131
- it('should enable auto-merge via squash', async () => {
132
- await (0, enablePullRequestAutoMerge_1.enablePullRequestAutoMerge)({ ...options, autoMergeMethod: 'squash' }, pullNumber);
133
- // ensure Github API reflects the change before querying
134
- await sleep(100);
135
- const autoMergeMethod = await (0, fetchPullRequestAutoMergeMethod_1.fetchPullRequestAutoMergeMethod)(options, pullNumber);
136
- expect(autoMergeMethod).toBe('SQUASH');
199
+ describe('when createing a PR against "status-checks-required-branch"', () => {
200
+ let pullNumber;
201
+ let branchName;
202
+ let octokit;
203
+ let options;
204
+ beforeAll(async () => {
205
+ const randomString = crypto_1.default.randomBytes(4).toString('hex');
206
+ branchName = `test-${randomString}`;
207
+ options = {
208
+ accessToken,
209
+ repoOwner: TEST_REPO_OWNER,
210
+ repoName: TEST_REPO_NAME,
211
+ };
212
+ octokit = new rest_1.Octokit({ auth: accessToken });
213
+ await resetReference(octokit);
214
+ const sha = await addCommit(octokit);
215
+ await createBranch({ octokit, branchName, sha });
216
+ pullNumber = await createPr({
217
+ options,
218
+ headBranch: branchName,
219
+ baseBranch: 'status-checks-required-branch',
220
+ });
221
+ });
222
+ // cleanup
223
+ afterAll(async () => {
224
+ await closePr({ octokit, pullNumber });
225
+ await deleteBranch({ octokit, branchName });
226
+ await resetReference(octokit);
227
+ });
228
+ it('should not be possible to enable auto-merge', async () => {
229
+ let isMissingStatusChecks;
230
+ let errorMessage;
231
+ try {
232
+ await (0, enablePullRequestAutoMerge_1.enablePullRequestAutoMerge)({ ...options, autoMergeMethod: 'merge' }, pullNumber);
233
+ }
234
+ catch (e) {
235
+ const err = e;
236
+ const res = (0, enablePullRequestAutoMerge_1.parseGithubError)(err);
237
+ errorMessage = err.message;
238
+ isMissingStatusChecks = res.isMissingStatusChecks;
239
+ }
240
+ expect(errorMessage).toMatchInlineSnapshot(`"[\\"Pull request Pull request is in clean status\\"] (Github API v4)"`);
241
+ expect(isMissingStatusChecks).toBe(true);
242
+ });
137
243
  });
138
244
  });
@@ -27,14 +27,14 @@ describe('allFetchers', () => {
27
27
  if (!commitByAuthor.sourcePullRequest) {
28
28
  throw new Error('Missing pull number!');
29
29
  }
30
- const commitByPullNumber = await (0, fetchCommitByPullNumber_1.fetchCommitByPullNumber)({
30
+ const commitByPullNumber = await (0, fetchCommitByPullNumber_1.fetchCommitsByPullNumber)({
31
31
  repoOwner: 'elastic',
32
32
  repoName: 'kibana',
33
33
  accessToken,
34
34
  pullNumber: commitByAuthor.sourcePullRequest.number,
35
35
  sourceBranch: 'master',
36
36
  });
37
- expect(commitByAuthor).toEqual(commitByPullNumber);
37
+ expect(commitByAuthor).toEqual(commitByPullNumber[0]);
38
38
  });
39
39
  it('matches commitByAuthor with commitBySha', async () => {
40
40
  const commitBySha = await (0, fetchCommitBySha_1.fetchCommitBySha)({
@@ -1,6 +1,6 @@
1
1
  import { ValidConfigOptions } from '../../../../options/options';
2
2
  import { Commit } from '../../../sourceCommit/parseSourceCommit';
3
- export declare function fetchCommitByPullNumber(options: {
3
+ export declare function fetchCommitsByPullNumber(options: {
4
4
  accessToken: string;
5
5
  branchLabelMapping?: ValidConfigOptions['branchLabelMapping'];
6
6
  githubApiBaseUrlV4?: string;
@@ -8,4 +8,4 @@ export declare function fetchCommitByPullNumber(options: {
8
8
  repoName: string;
9
9
  repoOwner: string;
10
10
  sourceBranch: string;
11
- }): Promise<Commit>;
11
+ }): Promise<Commit[]>;
@@ -3,13 +3,14 @@ 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.fetchCommitByPullNumber = void 0;
6
+ exports.fetchCommitsByPullNumber = void 0;
7
7
  const graphql_tag_1 = __importDefault(require("graphql-tag"));
8
8
  const BackportError_1 = require("../../../BackportError");
9
9
  const remoteConfig_1 = require("../../../remoteConfig");
10
- const parseSourceCommit_1 = require("../../../sourceCommit/parseSourceCommit");
11
10
  const apiRequestV4_1 = require("../apiRequestV4");
12
- async function fetchCommitByPullNumber(options) {
11
+ const fetchCommitBySha_1 = require("./fetchCommitBySha");
12
+ const fetchCommitsForRebaseAndMergeStrategy_1 = require("./fetchCommitsForRebaseAndMergeStrategy");
13
+ async function fetchCommitsByPullNumber(options) {
13
14
  const { accessToken, githubApiBaseUrlV4 = 'https://api.github.com/graphql', pullNumber, repoName, repoOwner, } = options;
14
15
  const query = (0, graphql_tag_1.default) `
15
16
  query CommitByPullNumber(
@@ -19,14 +20,28 @@ async function fetchCommitByPullNumber(options) {
19
20
  ) {
20
21
  repository(owner: $repoOwner, name: $repoName) {
21
22
  pullRequest(number: $pullNumber) {
23
+ # used to determine if "Rebase and Merge" strategy was used
24
+ commits {
25
+ totalCount
26
+ }
27
+
22
28
  mergeCommit {
23
- ...SourceCommitWithTargetPullRequestFragment
29
+ oid
30
+
31
+ # used to determine if "Rebase and Merge" strategy was used
32
+ committedDate
33
+ history(first: 2) {
34
+ edges {
35
+ node {
36
+ message
37
+ committedDate
38
+ }
39
+ }
40
+ }
24
41
  }
25
42
  }
26
43
  }
27
44
  }
28
-
29
- ${parseSourceCommit_1.SourceCommitWithTargetPullRequestFragment}
30
45
  `;
31
46
  let res;
32
47
  try {
@@ -48,10 +63,19 @@ async function fetchCommitByPullNumber(options) {
48
63
  if (!pullRequestNode) {
49
64
  throw new BackportError_1.BackportError(`The PR #${pullNumber} does not exist`);
50
65
  }
51
- const sourceCommit = pullRequestNode.mergeCommit;
52
- if (sourceCommit === null) {
66
+ const { mergeCommit } = pullRequestNode;
67
+ if (mergeCommit === null) {
53
68
  throw new BackportError_1.BackportError(`The PR #${pullNumber} is not merged`);
54
69
  }
55
- return (0, parseSourceCommit_1.parseSourceCommit)({ options, sourceCommit });
70
+ const possiblyRebaseAndMergeStrategy = pullRequestNode.commits.totalCount > 0 &&
71
+ mergeCommit.history.edges.every((c) => c.node.committedDate === mergeCommit.committedDate);
72
+ if (possiblyRebaseAndMergeStrategy) {
73
+ const commits = await (0, fetchCommitsForRebaseAndMergeStrategy_1.fetchCommitsForRebaseAndMergeStrategy)(options, pullRequestNode.commits.totalCount);
74
+ if (commits) {
75
+ return commits;
76
+ }
77
+ }
78
+ const commit = await (0, fetchCommitBySha_1.fetchCommitBySha)({ ...options, sha: mergeCommit.oid });
79
+ return [commit];
56
80
  }
57
- exports.fetchCommitByPullNumber = fetchCommitByPullNumber;
81
+ exports.fetchCommitsByPullNumber = fetchCommitsByPullNumber;