Skip to content

Commit de6d1e2

Browse files
authored
fix: lint during the landing process (#435)
Runs make lint after the patch has been downloaded and applied, and optionally stage and amend updated files if lint fails.
1 parent df5c572 commit de6d1e2

File tree

1 file changed

+54
-8
lines changed

1 file changed

+54
-8
lines changed

lib/landing_session.js

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const path = require('path');
4+
const os = require('os');
45
const {
56
getUnmarkedDeprecations,
67
updateDeprecations
@@ -130,8 +131,19 @@ class LandingSession extends Session {
130131
return command;
131132
}
132133

133-
async suggestAfterPatch(patch) {
134+
async validateLint() {
135+
// The linter is currently only run on non-Windows platforms.
136+
if (os.platform() === 'win32') {
137+
return true;
138+
}
139+
140+
const linted = await runAsync('make', ['lint']);
141+
return linted;
142+
}
143+
144+
async tryCompleteLanding(patch) {
134145
const { cli } = this;
146+
135147
const subjects = patch.match(/Subject: \[PATCH.*?\].*/g);
136148
if (!subjects) {
137149
cli.warn('Cannot get number of commits in the patch. ' +
@@ -154,6 +166,7 @@ class LandingSession extends Session {
154166
if (!canFinal) {
155167
return;
156168
}
169+
157170
return this.final();
158171
}
159172

@@ -167,16 +180,47 @@ class LandingSession extends Session {
167180

168181
async apply() {
169182
const { cli } = this;
183+
184+
// Bail if another landing session is currently in progress.
170185
if (!this.isApplying()) {
171-
cli.warn('This session can not proceed to apply patches, ' +
172-
'run `git node land --abort`');
186+
cli.warn('Landing session already in progress - ' +
187+
'to start a new one run `git node land --abort`');
173188
return;
174189
}
175190
await this.tryResetBranch();
176191

177192
const patch = await this.downloadAndPatch();
193+
194+
const cleanLint = await this.validateLint();
195+
if (!cleanLint) {
196+
const tryFixLint = await cli.prompt(
197+
'Lint failed - try fixing with \'make lint-js-fix\'?');
198+
if (tryFixLint) {
199+
await runAsync('make', ['lint-js-fix']);
200+
const fixed = await this.validateLint();
201+
if (!fixed) {
202+
cli.warn('Patch still contains lint errors. ' +
203+
'Please fix manually before proceeding');
204+
}
205+
}
206+
207+
const correctedLint = await cli.prompt('Corrected all lint errors?');
208+
if (correctedLint) {
209+
await runAsync('git', ['add', '.']);
210+
211+
// Final message will be edited later - don't try to change it here.
212+
await runAsync('git', ['commit', '--amend', '--no-edit']);
213+
} else {
214+
cli.info('Please fix lint errors and then run ' +
215+
'`git node land --amend` followed by ' +
216+
'`git node land --continue`.');
217+
process.exit(1);
218+
}
219+
}
220+
178221
this.startAmending();
179-
await this.suggestAfterPatch(patch);
222+
223+
await this.tryCompleteLanding(patch);
180224
}
181225

182226
async amend() {
@@ -239,7 +283,8 @@ class LandingSession extends Session {
239283
async final() {
240284
const { cli, owner, repo, upstream, branch, prid } = this;
241285

242-
if (!this.readyToFinal()) { // check git rebase/am has been done
286+
// Check that git rebase/am has been completed.
287+
if (!this.readyToFinal()) {
243288
cli.warn('Not yet ready to final');
244289
cli.log('A git rebase/am is in progress.' +
245290
' Please complete it before running git node land --final');
@@ -301,13 +346,14 @@ class LandingSession extends Session {
301346
return this.amend();
302347
}
303348
if (this.isApplying()) {
304-
// We are resolving conflict
349+
// We're still resolving conflicts.
305350
if (this.amInProgress()) {
306351
cli.log('Looks like you are resolving a `git am` conflict');
307352
cli.log('Please run `git status` for help');
308-
} else { // The conflict has been resolved
353+
} else {
354+
// Conflicts has been resolved - amend.
309355
this.startAmending();
310-
return this.suggestAfterPatch(this.patch);
356+
return this.tryCompleteLanding(this.patch);
311357
}
312358
return;
313359
}

0 commit comments

Comments
 (0)