-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathcherry_pick.js
138 lines (116 loc) · 3.49 KB
/
cherry_pick.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
132
133
134
135
136
137
138
import path from 'node:path';
import { getMetadata } from '../components/metadata.js';
import {
runAsync, runSync
} from './run.js';
import { getNcuDir } from './config.js';
import LandingSession, { LINT_RESULTS } from './landing_session.js';
export default class CherryPick {
constructor(prid, dir, cli, {
owner,
repo,
lint,
includeCVE
} = {}) {
this.prid = prid;
this.cli = cli;
this.dir = dir;
this.options = { owner, repo, lint, includeCVE };
}
get includeCVE() {
return this.options.includeCVE ?? false;
}
get owner() {
return this.options.owner || 'nodejs';
}
get repo() {
return this.options.repo || 'node';
}
get lint() {
return this.options.lint;
}
getCurrentRev() {
return runSync('git', ['rev-parse', 'HEAD']).trim();
}
getStrayCommits(verbose) {
const { upstream, branch } = this;
const ref = `${upstream}/${branch}...HEAD`;
const gitCmd = verbose
? ['log', '--oneline', '--reverse', ref]
: ['rev-list', '--reverse', ref];
const revs = runSync('git', gitCmd).trim();
return revs ? revs.split('\n') : [];
}
get ncuDir() {
return getNcuDir(this.dir);
}
get pullDir() {
return path.resolve(this.ncuDir, `${this.prid}`);
}
async start() {
const { cli } = this;
const metadata = await getMetadata({
prid: this.prid,
owner: this.owner,
repo: this.repo
}, false, cli);
this.expectedCommitShas =
metadata.data.commits.map(({ commit }) => commit.oid);
const amend = await cli.prompt(
'Would you like to amend this PR to the proposal?',
{ default: true }
);
if (!amend) {
return true;
}
try {
const commitInfo = await this.downloadAndPatch();
const cleanLint = await this.validateLint();
if (cleanLint === LINT_RESULTS.FAILED) {
cli.error('Patch still contains lint errors. ' +
'Please fix manually before proceeding');
return false;
} else if (cleanLint === LINT_RESULTS.SUCCESS) {
cli.ok('Lint passed cleanly');
}
return this.amend(metadata.metadata, commitInfo);
} catch (e) {
cli.error(e.message);
return false;
}
}
async amend(metadata, commitInfo) {
const { cli } = this;
const subjects = await runAsync('git',
['log', '--pretty=format:%s', `${commitInfo.base}..${commitInfo.head}`],
{ captureStdout: 'lines' });
if (commitInfo.shas.length !== 1) {
const fixupAll = await cli.prompt(
`${subjects.length} commits from the original PR are going to be` +
'squashed into a single commit. OK to proceed?', {
defaultAnswer: true
});
if (!fixupAll) {
// TODO: add this support?
throw new Error(`There are ${subjects.length} commits in the PR ` +
'and the ammend were not able to succeed');
}
await runAsync('git', ['reset', '--soft', `HEAD~${subjects.length - 1}`]);
await runAsync('git', ['commit', '--amend', '--no-edit']);
}
return LandingSession.prototype.amend.call(this, metadata);
}
readyToAmend() {
return true;
}
startAmending() {
// No-op
}
saveCommitInfo() {
// No-op
}
}
CherryPick.prototype.downloadAndPatch = LandingSession.prototype.downloadAndPatch;
CherryPick.prototype.validateLint = LandingSession.prototype.validateLint;
CherryPick.prototype.getMessagePath = LandingSession.prototype.getMessagePath;
CherryPick.prototype.saveMessage = LandingSession.prototype.saveMessage;