-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathcherry_pick.js
More file actions
153 lines (129 loc) · 3.75 KB
/
cherry_pick.js
File metadata and controls
153 lines (129 loc) · 3.75 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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,
upstream,
gpgSign,
lint,
includeCVE,
cveIds,
vulnCveMap
} = {}) {
this.prid = prid;
this.cli = cli;
this.dir = dir;
this.upstream = upstream;
this.gpgSign = gpgSign;
this.options = { owner, repo, lint, includeCVE, cveIds, vulnCveMap };
}
get includeCVE() {
return this.options.includeCVE ?? false;
}
get cveIds() {
return this.options.cveIds ?? null;
}
get vulnCveMap() {
return this.options.vulnCveMap ?? null;
}
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');
}
this.metadata = metadata.metadata;
return this.amend(commitInfo);
} catch (e) {
cli.error(e.message);
return false;
}
}
async amend(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);
}
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;