forked from trussworks/react-uswds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdangerfile.ts
71 lines (62 loc) · 2.64 KB
/
dangerfile.ts
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
import { includes } from 'lodash'
import { danger, warn } from 'danger'
// No PR is too small to include a description of why you made a change
if (danger.github && danger.github.pr.body.length < 10) {
warn('Please include a description of your PR changes.')
}
// Load all modified and new files
const allFiles = danger.git.modified_files.concat(danger.git.created_files)
// Request changes to package source code to also include changes to tests.
const hasCodeChanges = allFiles.some((p) => !!p.match(/src\/.*\.[jt]sx?/))
const hasTestChanges = allFiles.some((p) => !!p.match(/src\/.*\.test\.[jt]sx?/))
if (hasCodeChanges && !hasTestChanges) {
warn(
'This PR does not include changes to tests, even though it affects source code.'
)
}
// Make sure to export new components (src/components/*.[jt]sx)
const hasNewComponents = danger.git.created_files.some(
(p) => !!p.match(/src\/components\/.*\.[jt]sx/)
)
const hasEntrypointChanges = includes(allFiles, 'src/index.ts')
if (hasNewComponents && !hasEntrypointChanges) {
const message = `It looks like there are new component (JSX/TSX) files, but the entrypoint (index.ts) has not changed.`
const idea = `Did you forget to export new components from the library entrypoint?`
warn(`${message} - <em>${idea}</em>`)
}
// Require new src/components files to include changes to storybook
const hasStorybookChanges = allFiles.some(
(p) => !!p.match(/src\/.*\.stories\.[jt]sx?/)
)
if (hasCodeChanges && !hasStorybookChanges) {
warn(
'This PR does not include changes to storybook, even though it affects component code.'
)
}
// Request update of yarn.lock if package.json changed but yarn.lock isn't
const packageChanged = includes(allFiles, 'package.json')
const lockfileChanged = includes(allFiles, 'yarn.lock')
if (packageChanged && !lockfileChanged) {
const message = 'Changes were made to package.json, but not to yarn.lock'
const idea = 'Perhaps you need to run `yarn install`?'
warn(`${message} - <i>${idea}</i>`)
}
// Ensure we have access to github for these checks
let isYarnAuditMissing = false
if (danger.github) {
const prBody = danger.github.pr.body
if (lockfileChanged && danger.github.pr.user.type == 'User') {
isYarnAuditMissing = !(
includes(prBody, 'vulnerabilities found') &&
includes(prBody, 'Packages audited:')
)
}
}
// Encourage adding `yarn audit` output on package change
if (isYarnAuditMissing) {
const message =
'Changes were made to yarn.lock, but no plain text yarn audit output was found in PR description.'
const idea =
'Can you run `yarn audit` in your branch and paste the results inside a markdown code block?'
warn(`${message} - <i>${idea}</i>`)
}