-
Notifications
You must be signed in to change notification settings - Fork 0
/
dangerfile.js
111 lines (96 loc) · 4.99 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
const { danger, warn, fail, message } = require('danger');
const fs = require('fs');
const MAX_COMMIT_MESSAGE_LENGTH = 72;
const SMOKE_TEST_BRANCH = 'smoke/';
const SMOKE_TEST_WORKFLOW_FILE_PATH = '.github/workflows/smoke-tests.yml';
if (danger.github && danger.github.pr) {
const ghCommits = danger.github.commits;
for (const { commit } of ghCommits) {
const { message, url } = commit;
const [firstLine] = message.split('\n', 1);
const firstLineRegex = /^(feat|fix|chore|test|docs|refactor|revert)(\(.*\))?:(.+)$/;
if (!firstLineRegex.test(firstLine)) {
fail(
`"[${firstLine}](${url})" is not using a valid commit message format. For commit guidelines, see: [CONTRIBUTING](https://github.com/snyk/snyk/blob/master/CONTRIBUTING.md#creating-commits).`,
);
}
if (firstLine.length >= MAX_COMMIT_MESSAGE_LENGTH) {
warn(
`"[${firstLine}](${url})" is too long. Keep the first line of your commit message under ${MAX_COMMIT_MESSAGE_LENGTH} characters.`,
);
}
}
// Forgotten tests check
const modifiedTest =
danger.git.modified_files.some((f) => f.startsWith('test/')) ||
danger.git.created_files.some((f) => f.startsWith('test/'));
const modifiedSrc =
danger.git.modified_files.some((f) => f.startsWith('src/')) ||
danger.git.created_files.some((f) => f.startsWith('src/'));
if (modifiedSrc && !modifiedTest) {
// TODO: let's be careful about wording here. Maybe including Contributing guidelines and project goals document here
warn(
"You've modified files in `src/` directory, but haven't updated anything in test folder. Is there something that could be tested?",
);
}
// `.spec.ts` is always used for Jest tests
// `.test.ts` is normally used for Tap tests and but there are also `.spec.ts` files which are used be Tap tests in test/acceptance.
// either way, we should warn about new `.test.ts` or `.spec.ts` files being created outside the `/test/jest` folder
const newTestFiles = danger.git.created_files.filter((f) => {
const inTestFolder = f.startsWith('test/');
const isATestFile = f.includes('.test.ts') || f.includes('.spec.ts');
const inJestFolder = f.startsWith('test/jest/');
const inFixturesFolder = f.startsWith('test/fixtures/');
return inTestFolder && isATestFile && !inJestFolder && !inFixturesFolder;
});
if (newTestFiles.length) {
const joinedFileList = newTestFiles.map((f) => '- `' + f + '`').join('\n');
const msg = `Looks like you added a new Tap test. Consider making it a Jest test instead. See files in \`test/jest/(unit|acceptance)\` for examples. Files found:\n${joinedFileList}`;
warn(msg);
}
// Smoke test modification check
const modifiedSmokeTest =
danger.git.modified_files.some((f) => f.startsWith('test/smoke/')) ||
danger.git.created_files.some((f) => f.startsWith('test/smoke/')) ||
danger.git.modified_files.includes(SMOKE_TEST_WORKFLOW_FILE_PATH);
const isOnSmokeTestBranch = danger.github.pr.head.ref.startsWith(
SMOKE_TEST_BRANCH,
);
if (modifiedSmokeTest && !isOnSmokeTestBranch) {
message(
`You are modifying something in \`test/smoke\` directory, yet you are not on the branch starting with ${SMOKE_TEST_BRANCH}. You can prefix your branch with ${SMOKE_TEST_BRANCH} and Smoke tests will trigger for this PR.`,
);
}
// Enforce usage of ES6 modules
const filesUsingNodeJSImportExport = danger.git.modified_files
.filter((filePath) => {
if (filePath.endsWith('.js')) {
return false;
}
const fileContent = fs.readFileSync(filePath, 'utf8');
return (
fileContent.includes('module.exports') ||
fileContent.includes('= require(')
);
})
.map((filePath) => `- \`${filePath}\``)
.join('\n');
if (filesUsingNodeJSImportExport) {
const message =
"Since the CLI is unifying on a standard and improved tooling, we're starting to migrate old-style `import`s and `export`s to ES6 ones.\nA file you've modified is using either `module.exports` or `require()`. If you can, please update them to ES6 [import syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) and [export syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export).\n Files found:\n" +
filesUsingNodeJSImportExport;
warn(message);
}
// Warn if changes to help files are created in snyk/snyk repo instead of snyk/user-docs
const modifiedHelp = danger.git.modified_files.some((f) =>
f.startsWith('help/'),
);
const createdHelp = danger.git.created_files.some((f) =>
f.startsWith('help/'),
);
if (modifiedHelp || createdHelp) {
warn(
'Please make changes to `snyk help` text in [Gitbook](https://docs.snyk.io/snyk-cli/commands). Changes will be automatically synchronised to Snyk CLI as a [scheduled PR](https://github.com/snyk/snyk/actions/workflows/sync-cli-help-to-user-docs.yml).\nFor more information, see: [`help/README.md`](https://github.com/snyk/snyk/tree/master/help/README.md).',
);
}
}