backport 8.6.0 → 8.7.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/dist/entrypoint.module.private.test.js +78 -52
- package/dist/lib/{cherrypickAndCreateTargetPullRequest.d.ts → cherrypickAndCreateTargetPullRequest/cherrypickAndCreateTargetPullRequest.d.ts} +2 -2
- package/dist/lib/cherrypickAndCreateTargetPullRequest/cherrypickAndCreateTargetPullRequest.js +98 -0
- package/dist/lib/{cherrypickAndCreateTargetPullRequest.test.d.ts → cherrypickAndCreateTargetPullRequest/cherrypickAndCreateTargetPullRequest.test.d.ts} +0 -0
- package/dist/lib/{cherrypickAndCreateTargetPullRequest.test.js → cherrypickAndCreateTargetPullRequest/cherrypickAndCreateTargetPullRequest.test.js} +12 -10
- package/dist/lib/cherrypickAndCreateTargetPullRequest/getBackportBranchName.d.ts +2 -0
- package/dist/lib/cherrypickAndCreateTargetPullRequest/getBackportBranchName.js +22 -0
- package/dist/lib/{getCommitsWithoutBackports.d.ts → cherrypickAndCreateTargetPullRequest/getCommitsWithoutBackports.d.ts} +2 -2
- package/dist/lib/{getCommitsWithoutBackports.js → cherrypickAndCreateTargetPullRequest/getCommitsWithoutBackports.js} +6 -6
- package/dist/lib/{getCommitsWithoutBackports.test.d.ts → cherrypickAndCreateTargetPullRequest/getCommitsWithoutBackports.test.d.ts} +0 -0
- package/dist/lib/{getCommitsWithoutBackports.test.js → cherrypickAndCreateTargetPullRequest/getCommitsWithoutBackports.test.js} +24 -17
- package/dist/lib/cherrypickAndCreateTargetPullRequest/getMergeCommit.d.ts +3 -0
- package/dist/lib/cherrypickAndCreateTargetPullRequest/getMergeCommit.js +17 -0
- package/dist/lib/cherrypickAndCreateTargetPullRequest/getTargetPRLabels.d.ts +7 -0
- package/dist/lib/cherrypickAndCreateTargetPullRequest/getTargetPRLabels.js +33 -0
- package/dist/lib/cherrypickAndCreateTargetPullRequest/getTargetPRLabels.test.d.ts +1 -0
- package/dist/lib/cherrypickAndCreateTargetPullRequest/getTargetPRLabels.test.js +166 -0
- package/dist/lib/cherrypickAndCreateTargetPullRequest/waitForCherrypick.d.ts +3 -0
- package/dist/lib/cherrypickAndCreateTargetPullRequest/waitForCherrypick.js +154 -0
- package/dist/lib/getSourceBranchFromCommits.d.ts +2 -0
- package/dist/lib/getSourceBranchFromCommits.js +10 -0
- package/dist/lib/getTargetBranches.js +2 -3
- package/dist/lib/getTargetBranches.test.js +6 -4
- package/dist/lib/git.test.js +1 -1
- package/dist/lib/github/v3/addAssigneesToPullRequest.d.ts +11 -2
- package/dist/lib/github/v3/addAssigneesToPullRequest.js +9 -1
- package/dist/lib/github/v3/addAssigneesToPullRequest.test.js +5 -2
- package/dist/lib/github/v3/addLabelsToPullRequest.d.ts +10 -2
- package/dist/lib/github/v3/addLabelsToPullRequest.js +5 -1
- package/dist/lib/github/v3/addReviewersToPullRequest.d.ts +1 -1
- package/dist/lib/github/v3/addReviewersToPullRequest.js +5 -1
- package/dist/lib/github/v3/createPullRequest.js +7 -1
- package/dist/lib/github/v3/createPullRequest.test.js +22 -13
- package/dist/lib/github/v3/createStatusComment.test.js +4 -2
- package/dist/lib/github/v4/enablePullRequestAutoMerge.js +4 -0
- package/dist/lib/github/v4/fetchCommits/allFetchers.private.test.js +10 -1
- package/dist/lib/github/v4/fetchCommits/fetchCommitByPullNumber.private.test.js +5 -1
- package/dist/lib/github/v4/fetchCommits/fetchCommitBySha.private.test.js +3 -1
- package/dist/lib/github/v4/fetchCommits/fetchCommitsByAuthor.private.test.js +10 -4
- package/dist/lib/github/v4/fetchCommits/fetchCommitsByAuthor.test.js +7 -5
- package/dist/lib/github/v4/fetchCommits/fetchPullRequestBySearchQuery.private.test.js +8 -2
- package/dist/lib/github/v4/getOptionsFromGithub/getOptionsFromGithub.js +6 -5
- package/dist/lib/prompt.test.js +14 -6
- package/dist/lib/prompts.js +2 -2
- package/dist/lib/sourceCommit/getPullRequestStates.d.ts +3 -2
- package/dist/lib/sourceCommit/getPullRequestStates.js +13 -20
- package/dist/lib/sourceCommit/getPullRequestStates.test.js +69 -28
- package/dist/lib/sourceCommit/parseSourceCommit.d.ts +2 -1
- package/dist/lib/sourceCommit/parseSourceCommit.js +15 -7
- package/dist/lib/sourceCommit/parseSourceCommit.test.js +15 -4
- package/dist/runSequentially.js +5 -5
- package/dist/test/e2e/cli/commit-author.private.test.js +3 -3
- package/dist/test/e2e/cli/different-merge-strategies.private.test.js +6 -2
- package/dist/test/e2e/cli/gracefully-handles-corrupt-repo.private.test.js +5 -1
- package/dist/test/e2e/cli/repo-with-changing-branchLabelMapping.private.test.js +2 -2
- package/dist/test/e2e/cli/repo-with-programatically-added-commits.mutation.test.js +1 -1
- package/dist/test/e2e/cli/test-that-repo-can-be-cloned.private.test.js +6 -2
- package/dist/test/e2e/module/entrypoint.module.mutation.test.js +4 -4
- package/dist/test/handleUnbackportedPullRequests.private.test.js +3 -1
- package/dist/utils/filterUnmergedCommits.js +1 -1
- package/dist/utils/packageVersion.d.ts +1 -1
- package/dist/utils/packageVersion.js +1 -1
- package/package.json +7 -7
- package/yarn.lock +75 -75
- package/dist/lib/cherrypickAndCreateTargetPullRequest.js +0 -255
|
@@ -1,255 +0,0 @@
|
|
|
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.cherrypickAndCreateTargetPullRequest = void 0;
|
|
7
|
-
const chalk_1 = __importDefault(require("chalk"));
|
|
8
|
-
const lodash_1 = require("lodash");
|
|
9
|
-
const BackportError_1 = require("./BackportError");
|
|
10
|
-
const author_1 = require("./author");
|
|
11
|
-
const child_process_promisified_1 = require("./child-process-promisified");
|
|
12
|
-
const env_1 = require("./env");
|
|
13
|
-
const getCommitsWithoutBackports_1 = require("./getCommitsWithoutBackports");
|
|
14
|
-
const git_1 = require("./git");
|
|
15
|
-
const commitFormatters_1 = require("./github/commitFormatters");
|
|
16
|
-
const addAssigneesToPullRequest_1 = require("./github/v3/addAssigneesToPullRequest");
|
|
17
|
-
const addLabelsToPullRequest_1 = require("./github/v3/addLabelsToPullRequest");
|
|
18
|
-
const addReviewersToPullRequest_1 = require("./github/v3/addReviewersToPullRequest");
|
|
19
|
-
const createPullRequest_1 = require("./github/v3/createPullRequest");
|
|
20
|
-
const enablePullRequestAutoMerge_1 = require("./github/v4/enablePullRequestAutoMerge");
|
|
21
|
-
const fetchCommitBySha_1 = require("./github/v4/fetchCommits/fetchCommitBySha");
|
|
22
|
-
const validateTargetBranch_1 = require("./github/v4/validateTargetBranch");
|
|
23
|
-
const logger_1 = require("./logger");
|
|
24
|
-
const ora_1 = require("./ora");
|
|
25
|
-
const prompts_1 = require("./prompts");
|
|
26
|
-
const sequentially_1 = require("./sequentially");
|
|
27
|
-
function getSourceBranchFromCommits(commits) {
|
|
28
|
-
// sourceBranch should be the same for all commits, so picking `sourceBranch` from the first commit should be fine 🤞
|
|
29
|
-
// this is specifically needed when backporting a PR like `backport --pr 123` and the source PR was merged to a non-default (aka non-master) branch.
|
|
30
|
-
const { sourceBranch } = commits[0];
|
|
31
|
-
return sourceBranch;
|
|
32
|
-
}
|
|
33
|
-
async function cherrypickAndCreateTargetPullRequest({ options, commits, targetBranch, }) {
|
|
34
|
-
const backportBranch = getBackportBranchName(targetBranch, commits);
|
|
35
|
-
const repoForkOwner = (0, git_1.getRepoForkOwner)(options);
|
|
36
|
-
(0, logger_1.consoleLog)(`\n${chalk_1.default.bold(`Backporting to ${targetBranch}:`)}`);
|
|
37
|
-
await (0, validateTargetBranch_1.validateTargetBranch)({ ...options, branchName: targetBranch });
|
|
38
|
-
await (0, git_1.createBackportBranch)({
|
|
39
|
-
options,
|
|
40
|
-
sourceBranch: getSourceBranchFromCommits(commits),
|
|
41
|
-
targetBranch,
|
|
42
|
-
backportBranch,
|
|
43
|
-
});
|
|
44
|
-
const commitsFlattened = (0, lodash_1.flatten)(await Promise.all(commits.map((c) => getMergeCommits(options, c))));
|
|
45
|
-
await (0, sequentially_1.sequentially)(commitsFlattened, (commit) => waitForCherrypick(options, commit, targetBranch));
|
|
46
|
-
if (options.dryRun) {
|
|
47
|
-
(0, ora_1.ora)(options.interactive).succeed('Dry run complete');
|
|
48
|
-
return { url: 'https://localhost/dry-run', didUpdate: false, number: 1337 };
|
|
49
|
-
}
|
|
50
|
-
await (0, git_1.pushBackportBranch)({ options, backportBranch });
|
|
51
|
-
await (0, git_1.deleteBackportBranch)({ options, backportBranch });
|
|
52
|
-
const prPayload = {
|
|
53
|
-
owner: options.repoOwner,
|
|
54
|
-
repo: options.repoName,
|
|
55
|
-
title: (0, createPullRequest_1.getTitle)({ options, commits, targetBranch }),
|
|
56
|
-
body: (0, createPullRequest_1.getPullRequestBody)({ options, commits, targetBranch }),
|
|
57
|
-
head: `${repoForkOwner}:${backportBranch}`,
|
|
58
|
-
base: targetBranch, // eg. 7.x
|
|
59
|
-
};
|
|
60
|
-
const targetPullRequest = await (0, createPullRequest_1.createPullRequest)({ options, prPayload });
|
|
61
|
-
// add assignees to target pull request
|
|
62
|
-
const assignees = options.autoAssign
|
|
63
|
-
? [options.authenticatedUsername]
|
|
64
|
-
: options.assignees;
|
|
65
|
-
if (options.assignees.length > 0) {
|
|
66
|
-
await (0, addAssigneesToPullRequest_1.addAssigneesToPullRequest)(options, targetPullRequest.number, assignees);
|
|
67
|
-
}
|
|
68
|
-
// add reviewers to target pull request
|
|
69
|
-
if (options.reviewers.length > 0) {
|
|
70
|
-
await (0, addReviewersToPullRequest_1.addReviewersToPullRequest)(options, targetPullRequest.number, options.reviewers);
|
|
71
|
-
}
|
|
72
|
-
// add labels to target pull request
|
|
73
|
-
if (options.targetPRLabels.length > 0) {
|
|
74
|
-
await (0, addLabelsToPullRequest_1.addLabelsToPullRequest)(options, targetPullRequest.number, options.targetPRLabels);
|
|
75
|
-
}
|
|
76
|
-
// make PR auto mergable
|
|
77
|
-
if (options.autoMerge) {
|
|
78
|
-
await (0, enablePullRequestAutoMerge_1.enablePullRequestAutoMerge)(options, targetPullRequest.number);
|
|
79
|
-
}
|
|
80
|
-
// add labels to source pull requests
|
|
81
|
-
if (options.sourcePRLabels.length > 0) {
|
|
82
|
-
const promises = commits.map((commit) => {
|
|
83
|
-
if (commit.sourcePullRequest) {
|
|
84
|
-
return (0, addLabelsToPullRequest_1.addLabelsToPullRequest)(options, commit.sourcePullRequest.number, options.sourcePRLabels);
|
|
85
|
-
}
|
|
86
|
-
});
|
|
87
|
-
await Promise.all(promises);
|
|
88
|
-
}
|
|
89
|
-
(0, logger_1.consoleLog)(`View pull request: ${targetPullRequest.url}`);
|
|
90
|
-
return targetPullRequest;
|
|
91
|
-
}
|
|
92
|
-
exports.cherrypickAndCreateTargetPullRequest = cherrypickAndCreateTargetPullRequest;
|
|
93
|
-
async function getMergeCommits(options, commit) {
|
|
94
|
-
const { sha } = commit.sourceCommit;
|
|
95
|
-
if (!options.mainline) {
|
|
96
|
-
const isMergeCommit = await (0, git_1.getIsMergeCommit)(options, sha);
|
|
97
|
-
if (isMergeCommit) {
|
|
98
|
-
const shas = await (0, git_1.getShasInMergeCommit)(options, sha);
|
|
99
|
-
return Promise.all(shas.reverse().map((sha) => (0, fetchCommitBySha_1.fetchCommitBySha)({ ...options, sha })));
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
return [commit];
|
|
103
|
-
}
|
|
104
|
-
/*
|
|
105
|
-
* Returns the name of the backport branch without remote name
|
|
106
|
-
*
|
|
107
|
-
* Examples:
|
|
108
|
-
* For a single PR: `backport/7.x/pr-1234`
|
|
109
|
-
* For a single commit: `backport/7.x/commit-abcdef`
|
|
110
|
-
* For multiple: `backport/7.x/pr-1234_commit-abcdef`
|
|
111
|
-
*/
|
|
112
|
-
function getBackportBranchName(targetBranch, commits) {
|
|
113
|
-
const refValues = commits
|
|
114
|
-
.map((commit) => commit.sourcePullRequest
|
|
115
|
-
? `pr-${commit.sourcePullRequest.number}`
|
|
116
|
-
: `commit-${(0, commitFormatters_1.getShortSha)(commit.sourceCommit.sha)}`)
|
|
117
|
-
.join('_')
|
|
118
|
-
.slice(0, 200);
|
|
119
|
-
return `backport/${targetBranch}/${refValues}`;
|
|
120
|
-
}
|
|
121
|
-
async function waitForCherrypick(options, commit, targetBranch) {
|
|
122
|
-
const spinnerText = `Cherry-picking: ${chalk_1.default.greenBright((0, commitFormatters_1.getFirstLine)(commit.sourceCommit.message))}`;
|
|
123
|
-
const cherrypickSpinner = (0, ora_1.ora)(options.interactive, spinnerText).start();
|
|
124
|
-
const commitAuthor = (0, author_1.getCommitAuthor)({ options, commit });
|
|
125
|
-
await cherrypickAndHandleConflicts({
|
|
126
|
-
options,
|
|
127
|
-
commit,
|
|
128
|
-
commitAuthor,
|
|
129
|
-
targetBranch,
|
|
130
|
-
cherrypickSpinner,
|
|
131
|
-
});
|
|
132
|
-
// Conflicts should be resolved and files staged at this point
|
|
133
|
-
try {
|
|
134
|
-
// Run `git commit` in case conflicts were not manually committed
|
|
135
|
-
await (0, git_1.commitChanges)({ options, commit, commitAuthor });
|
|
136
|
-
cherrypickSpinner.succeed();
|
|
137
|
-
}
|
|
138
|
-
catch (e) {
|
|
139
|
-
cherrypickSpinner.fail();
|
|
140
|
-
throw e;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
async function cherrypickAndHandleConflicts({ options, commit, commitAuthor, targetBranch, cherrypickSpinner, }) {
|
|
144
|
-
const mergedTargetPullRequest = commit.pullRequestStates.find((pr) => pr.state === 'MERGED' && pr.branch === targetBranch);
|
|
145
|
-
let conflictingFiles;
|
|
146
|
-
let unstagedFiles;
|
|
147
|
-
let needsResolving;
|
|
148
|
-
try {
|
|
149
|
-
({ conflictingFiles, unstagedFiles, needsResolving } = await (0, git_1.cherrypick)({
|
|
150
|
-
options,
|
|
151
|
-
sha: commit.sourceCommit.sha,
|
|
152
|
-
mergedTargetPullRequest,
|
|
153
|
-
commitAuthor,
|
|
154
|
-
}));
|
|
155
|
-
// no conflicts encountered
|
|
156
|
-
if (!needsResolving) {
|
|
157
|
-
return;
|
|
158
|
-
}
|
|
159
|
-
// cherrypick failed due to conflicts
|
|
160
|
-
cherrypickSpinner.fail();
|
|
161
|
-
}
|
|
162
|
-
catch (e) {
|
|
163
|
-
cherrypickSpinner.fail();
|
|
164
|
-
throw e;
|
|
165
|
-
}
|
|
166
|
-
const repoPath = (0, env_1.getRepoPath)(options);
|
|
167
|
-
// resolve conflicts automatically
|
|
168
|
-
if (options.autoFixConflicts) {
|
|
169
|
-
const autoResolveSpinner = (0, ora_1.ora)(options.interactive, 'Attempting to resolve conflicts automatically').start();
|
|
170
|
-
const didAutoFix = await options.autoFixConflicts({
|
|
171
|
-
files: conflictingFiles.map((f) => f.absolute),
|
|
172
|
-
directory: repoPath,
|
|
173
|
-
logger: logger_1.logger,
|
|
174
|
-
targetBranch,
|
|
175
|
-
});
|
|
176
|
-
// conflicts were automatically resolved
|
|
177
|
-
if (didAutoFix) {
|
|
178
|
-
autoResolveSpinner.succeed();
|
|
179
|
-
return;
|
|
180
|
-
}
|
|
181
|
-
autoResolveSpinner.fail();
|
|
182
|
-
}
|
|
183
|
-
const conflictingFilesRelative = conflictingFiles
|
|
184
|
-
.map((f) => f.relative)
|
|
185
|
-
.slice(0, 50);
|
|
186
|
-
const commitsWithoutBackports = await (0, getCommitsWithoutBackports_1.getCommitsWithoutBackports)({
|
|
187
|
-
options,
|
|
188
|
-
commit,
|
|
189
|
-
targetBranch,
|
|
190
|
-
conflictingFiles: conflictingFilesRelative,
|
|
191
|
-
});
|
|
192
|
-
if (!options.interactive) {
|
|
193
|
-
throw new BackportError_1.BackportError({
|
|
194
|
-
code: 'merge-conflict-exception',
|
|
195
|
-
commitsWithoutBackports,
|
|
196
|
-
conflictingFiles: conflictingFilesRelative,
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
(0, logger_1.consoleLog)(chalk_1.default.bold('\nThe commit could not be backported due to conflicts\n'));
|
|
200
|
-
(0, logger_1.consoleLog)(`Please fix the conflicts in ${repoPath}`);
|
|
201
|
-
if (commitsWithoutBackports.length > 0) {
|
|
202
|
-
(0, logger_1.consoleLog)(chalk_1.default.italic(`Hint: Before fixing the conflicts manually you should consider backporting the following pull requests to "${targetBranch}":`));
|
|
203
|
-
(0, logger_1.consoleLog)(`${commitsWithoutBackports.map((c) => c.formatted).join('\n')}\n\n`);
|
|
204
|
-
}
|
|
205
|
-
/*
|
|
206
|
-
* Commit could not be cleanly cherrypicked: Initiating conflict resolution
|
|
207
|
-
*/
|
|
208
|
-
if (options.editor) {
|
|
209
|
-
await (0, child_process_promisified_1.spawnPromise)(options.editor, [repoPath], options.cwd);
|
|
210
|
-
}
|
|
211
|
-
// list files with conflict markers + unstaged files and require user to resolve them
|
|
212
|
-
await listConflictingAndUnstagedFiles({
|
|
213
|
-
retries: 0,
|
|
214
|
-
options,
|
|
215
|
-
conflictingFiles: conflictingFiles.map((f) => f.absolute),
|
|
216
|
-
unstagedFiles,
|
|
217
|
-
});
|
|
218
|
-
}
|
|
219
|
-
async function listConflictingAndUnstagedFiles({ retries, options, conflictingFiles, unstagedFiles, }) {
|
|
220
|
-
const hasUnstagedFiles = !(0, lodash_1.isEmpty)((0, lodash_1.difference)(unstagedFiles, conflictingFiles));
|
|
221
|
-
const hasConflictingFiles = !(0, lodash_1.isEmpty)(conflictingFiles);
|
|
222
|
-
if (!hasConflictingFiles && !hasUnstagedFiles) {
|
|
223
|
-
return;
|
|
224
|
-
}
|
|
225
|
-
// add divider between prompts
|
|
226
|
-
if (retries > 0) {
|
|
227
|
-
(0, logger_1.consoleLog)('\n----------------------------------------\n');
|
|
228
|
-
}
|
|
229
|
-
const header = chalk_1.default.reset(`Fix the following conflicts manually:`);
|
|
230
|
-
// show conflict section if there are conflicting files
|
|
231
|
-
const conflictSection = hasConflictingFiles
|
|
232
|
-
? `Conflicting files:\n${chalk_1.default.reset(conflictingFiles.map((file) => ` - ${file}`).join('\n'))}`
|
|
233
|
-
: '';
|
|
234
|
-
const unstagedSection = hasUnstagedFiles
|
|
235
|
-
? `Unstaged files:\n${chalk_1.default.reset(unstagedFiles.map((file) => ` - ${file}`).join('\n'))}`
|
|
236
|
-
: '';
|
|
237
|
-
const didConfirm = await (0, prompts_1.confirmPrompt)(`${header}\n\n${conflictSection}\n${unstagedSection}\n\nPress ENTER when the conflicts are resolved and files are staged`);
|
|
238
|
-
if (!didConfirm) {
|
|
239
|
-
throw new BackportError_1.BackportError({ code: 'abort-conflict-resolution-exception' });
|
|
240
|
-
}
|
|
241
|
-
const MAX_RETRIES = 100;
|
|
242
|
-
if (retries++ > MAX_RETRIES) {
|
|
243
|
-
throw new Error(`Maximum number of retries (${MAX_RETRIES}) exceeded`);
|
|
244
|
-
}
|
|
245
|
-
const [_conflictingFiles, _unstagedFiles] = await Promise.all([
|
|
246
|
-
(0, git_1.getConflictingFiles)(options),
|
|
247
|
-
(0, git_1.getUnstagedFiles)(options),
|
|
248
|
-
]);
|
|
249
|
-
await listConflictingAndUnstagedFiles({
|
|
250
|
-
retries,
|
|
251
|
-
options,
|
|
252
|
-
conflictingFiles: _conflictingFiles.map((file) => file.absolute),
|
|
253
|
-
unstagedFiles: _unstagedFiles,
|
|
254
|
-
});
|
|
255
|
-
}
|