-
Notifications
You must be signed in to change notification settings - Fork 3
/
dangerfile.js
574 lines (479 loc) · 12.1 KB
/
dangerfile.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
// @flow
// danger (removed by danger)
import {danger, schedule, markdown, warn, fail} from 'danger'
// danger plugins
import yarn from 'danger-plugin-yarn'
// utilities
import uniq from 'lodash/uniq'
import findIndex from 'lodash/findIndex'
async function main() {
const taskName = String(process.env.task)
switch (taskName) {
case 'ANDROID':
await runAndroid()
break
case 'IOS':
await runiOS()
break
case 'JS-general':
await runJSのGeneral()
await yarn()
break
case 'JS-data':
await runJSのData()
break
case 'JS-flow':
await runJSのFlow()
break
case 'JS-jest':
await runJSのJest()
break
case 'JS-lint':
await runJSのLint()
break
case 'JS-prettier':
await runJSのPrettier()
break
case 'GREENKEEPER':
case 'JS-bundle-android':
case 'JS-bundle-ios':
break
default:
warn(`Unknown task name "${taskName}"; Danger cannot report anything.`)
}
}
//
// task=ANDROID
//
async function runAndroid() {
const logFile = readLogFile('./logs/build').split('\n')
const buildStatus = readLogFile('./logs/build-status')
if (buildStatus !== '0') {
fastlaneBuildLogTail(logFile, 'Android Build Failed')
// returning early here because if the build fails, there's nothing to analyze
return
}
if (!(await didNativeDependencyChange())) {
// nothing changed to make this worth analyzing
return
}
// we do not currently do any android analysis
}
//
// task=IOS
//
async function runiOS() {
const logFile = readLogFile('./logs/build').split('\n')
const buildStatus = readLogFile('./logs/build-status')
if (buildStatus !== '0') {
// returning early here because if the build fails, there's nothing to analyze
fastlaneBuildLogTail(logFile, 'iOS Build Failed')
return
}
if (!(await didNativeDependencyChange())) {
// nothing changed to make this worth analyzing
return
}
markdown('## iOS Report')
// tee the "fastlane" output to a log, and run the analysis script
// to report back the longest compilation units
const analysisFile = readFile('./logs/analysis')
markdown(
h.details(
h.summary('Analysis of slow build times (>20s)'),
m.code({}, analysisFile),
),
)
}
//
// task=JS-data
//
async function runJSのData() {
await runJSのDataのData()
await runJSのDataのBusData()
}
function runJSのDataのData() {
const dataValidationLog = readLogFile('./logs/validate-data')
if (!dataValidationLog) {
return
}
if (!isBadDataValidationLog(dataValidationLog)) {
return
}
fileLog("Something's up with the data.", dataValidationLog)
}
function runJSのDataのBusData() {
const busDataValidationLog = readLogFile('./logs/validate-bus-data')
if (!busDataValidationLog) {
return
}
if (!isBadDataValidationLog(busDataValidationLog)) {
return
}
fileLog("🚌 Something's up with the bus routes.", busDataValidationLog)
}
//
// task=JS-general
//
async function runJSのGeneral() {
await flowAnnotated()
await bigPr()
await exclusionaryTests()
await xcodeproj()
}
// New js files should have `@flow` at the top
function flowAnnotated() {
danger.git.created_files
.filter(path => path.endsWith('.js'))
// except for those in /flow-typed
.filter(filepath => !filepath.includes('flow-typed'))
.filter(filepath => !readFile(filepath).includes('@flow'))
.forEach(file =>
warn(`<code>${file}</code> has no <code>@flow</code> annotation!`),
)
}
// Warn if tests have been enabled to the exclusion of all others
function exclusionaryTests() {
danger.git.created_files
.filter(filepath => filepath.endsWith('.test.js'))
.map(filepath => ({filepath, content: readFile(filepath)}))
.filter(
({content}) =>
content.includes('it.only') || content.includes('describe.only'),
)
.forEach(({filepath}) =>
warn(
`An <code>only</code> was left in ${filepath} – no other tests can run.`,
),
)
}
// Warn when PR size is large (mainly for hawken)
function bigPr() {
const bigPRThreshold = 400 // lines
const thisPRSize = danger.github.pr.additions + danger.github.pr.deletions
if (thisPRSize > bigPRThreshold) {
warn(
h.details(
h.summary(
`Big PR! We like to try and keep PRs under ${bigPRThreshold} lines, and this one was ${thisPRSize} lines.`,
),
h.p(
'If the PR contains multiple logical changes, splitting each change into a separate PR will allow a faster, easier, and more thorough review.',
),
),
)
}
}
// Remind us to check the xcodeproj, if it's changed
async function xcodeproj() {
const pbxprojChanged = danger.git.modified_files.find(filepath =>
filepath.endsWith('project.pbxproj'),
)
if (!pbxprojChanged) {
return
}
warn('The Xcode project file changed. Maintainers, double-check the changes!')
await pbxprojBlankLine()
await pbxprojLeadingZeros()
await pbxprojDuplicateLinkingPaths()
}
// Warn about a blank line that Xcode will re-insert if we remove
function pbxprojBlankLine() {
const pbxprojPath = danger.git.modified_files.find(filepath =>
filepath.endsWith('project.pbxproj'),
)
const pbxproj = readFile(pbxprojPath).split('\n')
if (pbxproj[7] === '') {
return
}
warn('Line 8 of the .pbxproj must be an empty line to match Xcode')
}
// Warn about numbers that `react-native link` removes leading 0s on
function pbxprojLeadingZeros() {
const pbxprojPath = danger.git.modified_files.find(filepath =>
filepath.endsWith('project.pbxproj'),
)
const pbxproj = readFile(pbxprojPath).split('\n')
const numericLineNames = [
/^\s+LastSwiftUpdateCheck\s/,
/^\s+LastUpgradeCheck\s/,
/^\s+LastSwiftMigration\s/,
]
const isLineWithoutLeadingZero = line =>
numericLineNames.some(nline => nline.test(line) && / [^0]\d+;$/.test(line))
const numericLinesWithoutLeadingZeros = pbxproj
.filter(isLineWithoutLeadingZero)
.map(line => line.trim())
if (!numericLinesWithoutLeadingZeros.length) {
return
}
warn(
h.details(
h.summary('Some lines in the .pbxproj lost their leading 0s.'),
h.p('Xcode likes to put them back, so we try to keep them around.'),
h.ul(...numericLinesWithoutLeadingZeros.map(line => h.li(h.code(line)))),
),
)
}
// Warn about duplicate entries in the linking paths after a `react-native link`
async function pbxprojDuplicateLinkingPaths() {
const pbxprojPath = danger.git.modified_files.find(filepath =>
filepath.endsWith('project.pbxproj'),
)
const xcodeproj = await parseXcodeProject(pbxprojPath)
const buildConfig = xcodeproj.project.objects.XCBuildConfiguration
const duplicateSearchPaths = Object.entries(buildConfig)
.filter(([_, val]) => typeof val === 'object')
.filter(([_, val]) => val.buildSettings.LIBRARY_SEARCH_PATHS)
.filter(([_, val]) => {
const searchPaths = val.buildSettings.LIBRARY_SEARCH_PATHS
return uniq(searchPaths).length !== searchPaths.length
})
if (!duplicateSearchPaths.length) {
return
}
fail(
h.details(
h.summary(
'Some of the Xcode <code>LIBRARY_SEARCH_PATHS</code> have duplicate entries. Please remove the duplicates. Thanks!',
),
h.p(
'This is easiest to do by editing the project.pbxproj directly, IMHO. These keys all live under the <code>XCBuildConfiguration</code> section.',
),
h.ul(...duplicateSearchPaths.map(([key]) => h.li(h.code(key)))),
),
)
}
//
// task=JS-flow
//
function runJSのFlow() {
const flowLog = readLogFile('./logs/flow')
if (!flowLog) {
return
}
if (flowLog === 'Found 0 errors') {
return
}
fileLog('Flow would like to interject about types…', flowLog)
}
//
// JS-jest
//
function runJSのJest() {
const jestLog = readLogFile('./logs/jest')
if (!jestLog) {
return
}
if (!jestLog.includes('FAIL')) {
return
}
const lines = getRelevantLinesJest(jestLog)
fileLog('Some Jest tests failed. Take a peek?', lines.join('\n'))
}
function getRelevantLinesJest(logContents) {
const file = logContents.split('\n')
const startIndex = findIndex(
file,
l => l.trim() === 'Summary of all failing tests',
)
const endIndex = findIndex(
file,
l => l.trim() === 'Ran all test suites.',
startIndex,
)
return file.slice(startIndex + 1, endIndex - 1)
}
//
// JS-lint
//
function runJSのLint() {
const eslintLog = readLogFile('./logs/eslint')
if (!eslintLog) {
return
}
fileLog('Eslint had a thing to say!', eslintLog)
}
//
// JS-prettier
//
function runJSのPrettier() {
const prettierLog = readLogFile('./logs/prettier')
if (!prettierLog) {
return
}
fileLog('Prettier made some changes', prettierLog, {lang: 'diff'})
}
//
// Utilities
//
import fs from 'fs'
import childProcess from 'child_process'
import stripAnsi from 'strip-ansi'
import directoryTree from 'directory-tree'
import xcode from 'xcode'
import util from 'util'
const execFile = util.promisify(childProcess.execFile)
function fastlaneBuildLogTail(log, message) {
const n = 150
const logToPost = log.slice(-n).map(stripAnsi).join('\n')
fail(
h.details(
h.summary(message),
h.p(`Last ${n} lines`),
m.code({}, logToPost),
),
)
}
const h = new Proxy(
{},
{
get(_, property) {
return function (...children) {
if (!children.length) {
return `<${property}>`
}
return `<${property}>${children.join('\n')}</${property}>`
}
},
},
)
const m = {
code(attrs, ...children) {
return (
'\n' +
'```' +
(attrs.language || '') +
'\n' +
children.join('\n') +
'\n' +
'```' +
'\n'
)
},
json(blob) {
return m.code({language: 'json'}, JSON.stringify(blob, null, 2))
},
}
function readFile(filename) {
try {
return fs.readFileSync(filename, 'utf-8')
} catch (err) {
fail(
h.details(
h.summary(`Could not read <code>${filename}</code>`),
m.json(err),
),
)
return ''
}
}
function readLogFile(filename) {
return readFile(filename).trim()
}
// eslint-disable-next-line no-unused-vars
function readJsonLogFile(filename) {
try {
return JSON.parse(readFile(filename))
} catch (err) {
fail(
h.details(
h.summary(`Could not read the log file at <code>${filename}</code>`),
m.json(err),
),
)
return []
}
}
function isBadDataValidationLog(log) {
return log.split('\n').some(l => !l.endsWith('is valid'))
}
function fileLog(name, log, {lang = null} = {}) {
return markdown(
`## ${name}
${m.code({language: lang}, log)}`,
)
}
function parseXcodeProject(pbxprojPath) {
return new Promise((resolve, reject) => {
const project = xcode.project(pbxprojPath)
// I think this can be called twice from .parse, which is an error for a Promise
let resolved = false
project.parse((error, data) => {
if (resolved) {
return
}
resolved = true
if (error) {
reject(error)
}
resolve(data)
})
})
}
// eslint-disable-next-line no-unused-vars
async function listZip(filepath) {
try {
const {stdout} = await execFile('unzip', ['-l', filepath])
const lines = stdout.split('\n')
const parsed = lines.slice(3, -3).map(line => {
const length = parseInt(line.slice(0, 9).trim(), 10)
// const datetime = line.slice(12, 28)
const filepath = line.slice(30).trim()
const type = filepath.endsWith('/') ? 'folder' : 'file'
return {size: length, filepath, type}
})
const zipSize = parsed.reduce((sum, current) => current.size + sum, 0)
return {files: parsed, size: zipSize}
} catch (err) {
fail(
h.details(
h.summary(`Could not examine the ZIP file at <code>${filepath}</code>`),
m.json(err),
),
)
}
}
function listDirectory(dirpath) {
try {
return fs.readdirSync(dirpath)
} catch (err) {
fail(h.details(h.summary(`${h.code(dirpath)} does not exist`), m.json(err)))
return []
}
}
// eslint-disable-next-line no-unused-vars
function listDirectoryTree(dirpath) {
try {
const exists = fs.accessSync(dirpath, fs.F_OK)
if (!exists) {
fail(
h.details(
h.summary(`Could not access <code>${dirpath}</code>`),
m.code({}, listDirectory(dirpath).join('\n')),
),
)
}
return directoryTree(dirpath)
} catch (err) {
fail(
h.details(
h.summary('<code>listDirectoryTree</code> threw an error'),
m.json(err),
),
)
return {}
}
}
async function didNativeDependencyChange() {
const diff = await danger.git.JSONDiffForFile('package.json')
if (!diff.dependencies && !diff.devDependencies) {
return false
}
// If we need to, we can add more heuristics here in the future
return true
}
//
// Run the file
//
schedule(main)