backport 8.8.0 → 8.9.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.
- package/README.md +9 -3
- package/dist/entrypoint.module.d.ts +3 -1
- package/dist/entrypoint.module.js +6 -4
- package/dist/lib/cherrypickAndCreateTargetPullRequest/autoMergeNowOrLater.d.ts +2 -0
- package/dist/lib/cherrypickAndCreateTargetPullRequest/autoMergeNowOrLater.js +52 -0
- package/dist/lib/cherrypickAndCreateTargetPullRequest/cherrypickAndCreateTargetPullRequest.js +2 -2
- package/dist/lib/getCommits.js +2 -1
- package/dist/lib/github/v3/mergePullRequest.d.ts +6 -0
- package/dist/lib/github/v3/mergePullRequest.js +19 -0
- package/dist/lib/github/v4/enablePullRequestAutoMerge.js +10 -27
- package/dist/lib/github/v4/enablePullRequestAutoMerge.mutation.test.js +87 -58
- package/dist/lib/github/v4/fetchCommits/allFetchers.private.test.js +2 -2
- package/dist/lib/github/v4/fetchCommits/fetchCommitByPullNumber.d.ts +2 -2
- package/dist/lib/github/v4/fetchCommits/fetchCommitByPullNumber.js +34 -10
- package/dist/lib/github/v4/fetchCommits/fetchCommitByPullNumber.private.test.js +8 -8
- package/dist/lib/github/v4/fetchCommits/fetchCommitsForRebaseAndMergeStrategy.d.ts +9 -0
- package/dist/lib/github/v4/fetchCommits/fetchCommitsForRebaseAndMergeStrategy.js +78 -0
- package/dist/lib/github/v4/{fetchRemoteProjectConfig.private.test.d.ts → fetchCommits/fetchCommitsForRebaseAndMergeStrategy.private.test.d.ts} +0 -0
- package/dist/lib/github/v4/fetchCommits/fetchCommitsForRebaseAndMergeStrategy.private.test.js +29 -0
- package/dist/lib/sourceCommit/getPullRequestStates.d.ts +7 -0
- package/dist/lib/sourceCommit/getPullRequestStates.js +2 -1
- package/dist/test/e2e/cli/different-merge-strategies.private.test.js +57 -23
- package/dist/utils/packageVersion.d.ts +1 -1
- package/dist/utils/packageVersion.js +1 -1
- package/package.json +13 -13
- package/yarn.lock +266 -254
- package/dist/lib/github/v4/fetchRemoteProjectConfig.d.ts +0 -8
- package/dist/lib/github/v4/fetchRemoteProjectConfig.js +0 -49
- package/dist/lib/github/v4/fetchRemoteProjectConfig.private.test.js +0 -49
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
|
-
|
|
82
|
-
|
|
83
|
-
|
|
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
|
|
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import { BackportResponse } from './backportRun';
|
|
2
|
+
import { getOptionsFromGithub } from './lib/github/v4/getOptionsFromGithub/getOptionsFromGithub';
|
|
2
3
|
import type { Commit } from './lib/sourceCommit/parseSourceCommit';
|
|
3
4
|
import { ConfigFileOptions } from './options/ConfigOptions';
|
|
4
5
|
import { ValidConfigOptions } from './options/options';
|
|
5
6
|
export type { HandledErrorResult, SuccessResult, UnhandledErrorResult, } from './runSequentially';
|
|
6
7
|
export type { BackportAbortResponse, BackportFailureResponse, BackportResponse, BackportSuccessResponse, } from './backportRun';
|
|
8
|
+
export { getTargetBranchFromLabel } from './lib/sourceCommit/getPullRequestStates';
|
|
7
9
|
export { BackportError } from './lib/BackportError';
|
|
8
10
|
export type { Commit } from './lib/sourceCommit/parseSourceCommit';
|
|
9
11
|
export type { ConfigFileOptions } from './options/ConfigOptions';
|
|
10
|
-
export { fetchRemoteProjectConfig as getRemoteProjectConfig } from './lib/github/v4/fetchRemoteProjectConfig';
|
|
11
12
|
export { getGlobalConfig as getLocalGlobalConfig } from './options/config/globalConfig';
|
|
13
|
+
export { getOptionsFromGithub };
|
|
12
14
|
export declare function backportRun({ options, processArgs, exitCodeOnFailure, }: {
|
|
13
15
|
options?: ConfigFileOptions;
|
|
14
16
|
processArgs?: string[];
|
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getCommits = exports.backportRun = exports.
|
|
3
|
+
exports.getCommits = exports.backportRun = exports.getOptionsFromGithub = exports.getLocalGlobalConfig = exports.BackportError = exports.getTargetBranchFromLabel = void 0;
|
|
4
4
|
const backportRun_1 = require("./backportRun");
|
|
5
5
|
const fetchCommitByPullNumber_1 = require("./lib/github/v4/fetchCommits/fetchCommitByPullNumber");
|
|
6
6
|
const fetchCommitBySha_1 = require("./lib/github/v4/fetchCommits/fetchCommitBySha");
|
|
7
7
|
const fetchCommitsByAuthor_1 = require("./lib/github/v4/fetchCommits/fetchCommitsByAuthor");
|
|
8
8
|
const fetchPullRequestsBySearchQuery_1 = require("./lib/github/v4/fetchCommits/fetchPullRequestsBySearchQuery");
|
|
9
9
|
const getOptionsFromGithub_1 = require("./lib/github/v4/getOptionsFromGithub/getOptionsFromGithub");
|
|
10
|
+
Object.defineProperty(exports, "getOptionsFromGithub", { enumerable: true, get: function () { return getOptionsFromGithub_1.getOptionsFromGithub; } });
|
|
10
11
|
const logger_1 = require("./lib/logger");
|
|
11
12
|
const excludeUndefined_1 = require("./utils/excludeUndefined");
|
|
13
|
+
var getPullRequestStates_1 = require("./lib/sourceCommit/getPullRequestStates");
|
|
14
|
+
Object.defineProperty(exports, "getTargetBranchFromLabel", { enumerable: true, get: function () { return getPullRequestStates_1.getTargetBranchFromLabel; } });
|
|
12
15
|
var BackportError_1 = require("./lib/BackportError");
|
|
13
16
|
Object.defineProperty(exports, "BackportError", { enumerable: true, get: function () { return BackportError_1.BackportError; } });
|
|
14
|
-
var fetchRemoteProjectConfig_1 = require("./lib/github/v4/fetchRemoteProjectConfig");
|
|
15
|
-
Object.defineProperty(exports, "getRemoteProjectConfig", { enumerable: true, get: function () { return fetchRemoteProjectConfig_1.fetchRemoteProjectConfig; } });
|
|
16
17
|
var globalConfig_1 = require("./options/config/globalConfig");
|
|
17
18
|
Object.defineProperty(exports, "getLocalGlobalConfig", { enumerable: true, get: function () { return globalConfig_1.getGlobalConfig; } });
|
|
18
19
|
function backportRun({ options = {}, processArgs = [], exitCodeOnFailure = true, }) {
|
|
@@ -30,11 +31,12 @@ async function getCommits(options) {
|
|
|
30
31
|
const pullNumbers = Array.isArray(options.pullNumber)
|
|
31
32
|
? options.pullNumber
|
|
32
33
|
: [options.pullNumber];
|
|
33
|
-
|
|
34
|
+
const nestedCommits = await Promise.all(pullNumbers.map((pullNumber) => (0, fetchCommitByPullNumber_1.fetchCommitsByPullNumber)({
|
|
34
35
|
...optionsFromGithub,
|
|
35
36
|
...options,
|
|
36
37
|
pullNumber,
|
|
37
38
|
})));
|
|
39
|
+
return nestedCommits.flat();
|
|
38
40
|
}
|
|
39
41
|
if (options.sha) {
|
|
40
42
|
const shas = Array.isArray(options.sha) ? options.sha : [options.sha];
|
|
@@ -0,0 +1,52 @@
|
|
|
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: Could not enable auto merge for PR "#${pullNumber}" due to ${e.message}`);
|
|
26
|
+
const isMissingStatusChecks = e.githubResponse.data.errors?.some((e) => e.type === 'UNPROCESSABLE' &&
|
|
27
|
+
e.message.includes('Branch does not have required protected branch rules'));
|
|
28
|
+
// if auto merge cannot be enabled due to missing status checks, the PR should be merged immediately
|
|
29
|
+
if (!isMissingStatusChecks) {
|
|
30
|
+
throw e;
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
await (0, mergePullRequest_1.mergePullRequest)(options, pullNumber);
|
|
34
|
+
spinner.text =
|
|
35
|
+
'Auto-merge: Pull request was merged without waiting for status checks';
|
|
36
|
+
}
|
|
37
|
+
catch (e) {
|
|
38
|
+
if (!(e instanceof Error)) {
|
|
39
|
+
throw new Error(`Unknown error: ${e}`);
|
|
40
|
+
}
|
|
41
|
+
logger_1.logger.error(`Auto merge: Could not merge PR "#${pullNumber}" immediately due to ${e.message}`, e);
|
|
42
|
+
throw e;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
spinner.succeed();
|
|
46
|
+
}
|
|
47
|
+
catch (e) {
|
|
48
|
+
logger_1.logger.warn(`Auto merge: An error occurred ${e}`);
|
|
49
|
+
spinner.fail();
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
exports.autoMergeNowOrLater = autoMergeNowOrLater;
|
package/dist/lib/cherrypickAndCreateTargetPullRequest/cherrypickAndCreateTargetPullRequest.js
CHANGED
|
@@ -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,
|
|
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) {
|
package/dist/lib/getCommits.js
CHANGED
|
@@ -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
|
|
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;
|
|
@@ -5,19 +5,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
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,15 @@ async function enablePullRequestAutoMerge(options, targetPullRequestNumber) {
|
|
|
33
24
|
}
|
|
34
25
|
}
|
|
35
26
|
`;
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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;
|
|
@@ -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) {
|
|
@@ -35,9 +36,9 @@ async function closePr(octokit, pullNumber) {
|
|
|
35
36
|
state: 'closed',
|
|
36
37
|
});
|
|
37
38
|
}
|
|
38
|
-
async function createPr(options, branchName) {
|
|
39
|
+
async function createPr(options, branchName, baseBranch) {
|
|
39
40
|
const prPayload = {
|
|
40
|
-
base:
|
|
41
|
+
base: baseBranch,
|
|
41
42
|
head: branchName,
|
|
42
43
|
body: 'testing...',
|
|
43
44
|
owner: TEST_REPO_OWNER,
|
|
@@ -73,66 +74,94 @@ 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
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
if (sha) {
|
|
83
|
+
describe('create a PR for 7.x which has status checks', () => {
|
|
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
99
|
await createBranch(octokit, branchName, sha);
|
|
100
|
-
|
|
101
|
-
|
|
100
|
+
pullNumber = await createPr(options, branchName, '7.x');
|
|
101
|
+
});
|
|
102
|
+
// cleanup
|
|
103
|
+
afterAll(async () => {
|
|
104
|
+
await closePr(octokit, pullNumber);
|
|
105
|
+
await deleteBranch(octokit, branchName);
|
|
106
|
+
await resetReference(octokit);
|
|
107
|
+
});
|
|
108
|
+
// reset auto-merge state between runs
|
|
109
|
+
afterEach(async () => {
|
|
110
|
+
await (0, disablePullRequestAutoMerge_1.disablePullRequestAutoMerge)(options, pullNumber);
|
|
111
|
+
});
|
|
112
|
+
it('should initially have auto-merge disabled', async () => {
|
|
113
|
+
const autoMergeMethod = await (0, fetchPullRequestAutoMergeMethod_1.fetchPullRequestAutoMergeMethod)(options, pullNumber);
|
|
114
|
+
expect(autoMergeMethod).toBe(undefined);
|
|
115
|
+
});
|
|
116
|
+
it('should enable auto-merge via merge', async () => {
|
|
117
|
+
await (0, enablePullRequestAutoMerge_1.enablePullRequestAutoMerge)({ ...options, autoMergeMethod: 'merge' }, pullNumber);
|
|
118
|
+
// ensure Github API reflects the change before querying
|
|
119
|
+
await sleep(100);
|
|
120
|
+
const autoMergeMethod = await (0, fetchPullRequestAutoMergeMethod_1.fetchPullRequestAutoMergeMethod)(options, pullNumber);
|
|
121
|
+
expect(autoMergeMethod).toBe('MERGE');
|
|
122
|
+
});
|
|
123
|
+
it('should not enable auto-merge via rebase because it is disallowed', async () => {
|
|
124
|
+
await expect(async () => await (0, enablePullRequestAutoMerge_1.enablePullRequestAutoMerge)({ ...options, autoMergeMethod: 'rebase' }, pullNumber)).rejects.toThrowErrorMatchingInlineSnapshot(`"[\\"Merge method rebase merging is not allowed on this repository\\"] (Github API v4)"`);
|
|
125
|
+
// ensure Github API reflects the change before querying
|
|
126
|
+
await sleep(100);
|
|
127
|
+
const autoMergeMethod = await (0, fetchPullRequestAutoMergeMethod_1.fetchPullRequestAutoMergeMethod)(options, pullNumber);
|
|
128
|
+
expect(autoMergeMethod).toBe(undefined);
|
|
129
|
+
});
|
|
130
|
+
it('should enable auto-merge via squash', async () => {
|
|
131
|
+
await (0, enablePullRequestAutoMerge_1.enablePullRequestAutoMerge)({ ...options, autoMergeMethod: 'squash' }, pullNumber);
|
|
132
|
+
// ensure Github API reflects the change before querying
|
|
133
|
+
await sleep(100);
|
|
134
|
+
const autoMergeMethod = await (0, fetchPullRequestAutoMergeMethod_1.fetchPullRequestAutoMergeMethod)(options, pullNumber);
|
|
135
|
+
expect(autoMergeMethod).toBe('SQUASH');
|
|
136
|
+
});
|
|
102
137
|
});
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
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');
|
|
138
|
+
describe('create a PR for 6.x which does not have status checks', () => {
|
|
139
|
+
let pullNumber;
|
|
140
|
+
let branchName;
|
|
141
|
+
let octokit;
|
|
142
|
+
let options;
|
|
143
|
+
beforeAll(async () => {
|
|
144
|
+
const randomString = crypto_1.default.randomBytes(4).toString('hex');
|
|
145
|
+
branchName = `test-${randomString}`;
|
|
146
|
+
options = {
|
|
147
|
+
accessToken,
|
|
148
|
+
repoOwner: TEST_REPO_OWNER,
|
|
149
|
+
repoName: TEST_REPO_NAME,
|
|
150
|
+
};
|
|
151
|
+
octokit = new rest_1.Octokit({ auth: accessToken });
|
|
152
|
+
await resetReference(octokit);
|
|
153
|
+
const sha = await addCommit(octokit);
|
|
154
|
+
await createBranch(octokit, branchName, sha);
|
|
155
|
+
pullNumber = await createPr(options, branchName, '6.x');
|
|
156
|
+
});
|
|
157
|
+
// cleanup
|
|
158
|
+
afterAll(async () => {
|
|
159
|
+
await closePr(octokit, pullNumber);
|
|
160
|
+
await deleteBranch(octokit, branchName);
|
|
161
|
+
await resetReference(octokit);
|
|
162
|
+
});
|
|
163
|
+
it('should not be able to enable auto-merge via merge', async () => {
|
|
164
|
+
await expect(async () => await (0, enablePullRequestAutoMerge_1.enablePullRequestAutoMerge)({ ...options, autoMergeMethod: 'merge' }, pullNumber)).rejects.toThrowErrorMatchingInlineSnapshot(`"[\\"Pull request Pull request is in clean status\\"] (Github API v4)"`);
|
|
165
|
+
});
|
|
137
166
|
});
|
|
138
167
|
});
|
|
@@ -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.
|
|
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
|
|
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.
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
52
|
-
if (
|
|
66
|
+
const { mergeCommit } = pullRequestNode;
|
|
67
|
+
if (mergeCommit === null) {
|
|
53
68
|
throw new BackportError_1.BackportError(`The PR #${pullNumber} is not merged`);
|
|
54
69
|
}
|
|
55
|
-
|
|
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.
|
|
81
|
+
exports.fetchCommitsByPullNumber = fetchCommitsByPullNumber;
|
|
@@ -31,10 +31,10 @@ const accessToken = (0, getDevAccessToken_1.getDevAccessToken)();
|
|
|
31
31
|
describe('fetchCommitByPullNumber', () => {
|
|
32
32
|
describe('snapshot request/response', () => {
|
|
33
33
|
let spy;
|
|
34
|
-
let
|
|
34
|
+
let commits;
|
|
35
35
|
beforeEach(async () => {
|
|
36
36
|
spy = jest.spyOn(apiRequestV4Module, 'apiRequestV4');
|
|
37
|
-
|
|
37
|
+
commits = await (0, fetchCommitByPullNumber_1.fetchCommitsByPullNumber)({
|
|
38
38
|
repoOwner: 'elastic',
|
|
39
39
|
repoName: 'kibana',
|
|
40
40
|
accessToken,
|
|
@@ -49,17 +49,17 @@ describe('fetchCommitByPullNumber', () => {
|
|
|
49
49
|
return { ...acc, [name]: (0, graphql_1.print)(query) };
|
|
50
50
|
}, {});
|
|
51
51
|
const queryNames = Object.keys(queries);
|
|
52
|
-
expect(queryNames).toEqual(['CommitByPullNumber']);
|
|
52
|
+
expect(queryNames).toEqual(['CommitByPullNumber', 'CommitsBySha']);
|
|
53
53
|
queryNames.forEach((name) => {
|
|
54
54
|
expect(queries[name]).toMatchSnapshot(`Query: ${name}`);
|
|
55
55
|
});
|
|
56
56
|
});
|
|
57
57
|
it('returns the correct response', async () => {
|
|
58
|
-
expect(
|
|
58
|
+
expect(commits).toMatchSnapshot();
|
|
59
59
|
});
|
|
60
60
|
});
|
|
61
61
|
describe('when PR was merged', () => {
|
|
62
|
-
it('is returned', async () => {
|
|
62
|
+
it('the pull request response is returned', async () => {
|
|
63
63
|
const options = {
|
|
64
64
|
accessToken,
|
|
65
65
|
pullNumber: 5,
|
|
@@ -132,7 +132,7 @@ describe('fetchCommitByPullNumber', () => {
|
|
|
132
132
|
},
|
|
133
133
|
],
|
|
134
134
|
};
|
|
135
|
-
expect(await (0, fetchCommitByPullNumber_1.
|
|
135
|
+
expect(await (0, fetchCommitByPullNumber_1.fetchCommitsByPullNumber)(options)).toEqual([expectedCommit]);
|
|
136
136
|
});
|
|
137
137
|
});
|
|
138
138
|
describe('when PR is still open', () => {
|
|
@@ -144,7 +144,7 @@ describe('fetchCommitByPullNumber', () => {
|
|
|
144
144
|
repoOwner: 'backport-org',
|
|
145
145
|
sourceBranch: 'main',
|
|
146
146
|
};
|
|
147
|
-
await expect((0, fetchCommitByPullNumber_1.
|
|
147
|
+
await expect((0, fetchCommitByPullNumber_1.fetchCommitsByPullNumber)(options)).rejects.toThrowError(`The PR #11 is not merged`);
|
|
148
148
|
});
|
|
149
149
|
});
|
|
150
150
|
describe('when PR does not exist', () => {
|
|
@@ -156,7 +156,7 @@ describe('fetchCommitByPullNumber', () => {
|
|
|
156
156
|
repoOwner: 'backport-org',
|
|
157
157
|
sourceBranch: 'main',
|
|
158
158
|
};
|
|
159
|
-
await expect((0, fetchCommitByPullNumber_1.
|
|
159
|
+
await expect((0, fetchCommitByPullNumber_1.fetchCommitsByPullNumber)(options)).rejects.toThrowError(`Could not resolve to a PullRequest with the number of 9999999999999. (Github API v4)`);
|
|
160
160
|
});
|
|
161
161
|
});
|
|
162
162
|
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Commit } from '../../../../entrypoint.module';
|
|
2
|
+
export declare function fetchCommitsForRebaseAndMergeStrategy(options: {
|
|
3
|
+
accessToken: string;
|
|
4
|
+
githubApiBaseUrlV4?: string;
|
|
5
|
+
pullNumber: number;
|
|
6
|
+
repoName: string;
|
|
7
|
+
repoOwner: string;
|
|
8
|
+
sourceBranch: string;
|
|
9
|
+
}, commitsTotalCount: number): Promise<Commit[] | undefined>;
|