Skip to content

Commit 2b81d21

Browse files
ortacpojer
authored andcommitted
Adds support for using Danger on PRs (#2508)
* Initial Dangerfile, adds a rule for including flow and the FB header * [Danger] Use a local path for jest-runtime instead of the downloaded NPM package * [Danger] Makes the FB copyright header check correct * just run danger * Update Danger
1 parent b135885 commit 2b81d21

File tree

4 files changed

+483
-16
lines changed

4 files changed

+483
-16
lines changed

.flowconfig

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
.*/vendor/jsonlint/.*
66
.*/examples/.*
77
.*/website/.*
8+
.*/dangerfile.js
89

910
[options]
1011
module.name_mapper='^types/\(.*\)$' -> '<PROJECT_ROOT>/types/\1.js'

dangerfile.js

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// @flow
2+
3+
const {danger, fail, warn} = require('danger');
4+
const fs = require('fs');
5+
6+
// Takes a list of file paths, and converts it into clickable links
7+
const linkableFiles = (paths: Array<string>): string => {
8+
const repoURL = danger.github.pr.head.repo.html_url;
9+
const ref = danger.github.pr.head.ref;
10+
const links = paths.map(path => {
11+
return createLink(`${repoURL}/blob/${ref}/${path}`, path);
12+
});
13+
return toSentence(links);
14+
};
15+
16+
// ["1", "2", "3"] to "1, 2 and 3"
17+
const toSentence = (array: Array<string>) : string => {
18+
if (array.length === 1) { return array[0]; }
19+
return array.slice(0, array.length - 1).join(', ') + ' and ' + array.pop();
20+
};
21+
22+
// ("/href/thing", "name") to "<a href="/href/thing">name</a>"
23+
const createLink = (href: string, text: string): string =>
24+
`<a href='${href}'>${text}</a>`;
25+
26+
const newJsFiles = danger.git.created_files.filter(path => path.endsWith('js'));
27+
28+
// New JS files should have the FB copyright header + flow
29+
const facebookLicenseHeaderComponents = [
30+
'Copyright \(c\) .*, Facebook, Inc. All rights reserved.',
31+
'This source code is licensed under the BSD-style license found in the',
32+
'LICENSE file in the root directory of this source tree. An additional grant',
33+
'of patent rights can be found in the PATENTS file in the same directory.',
34+
];
35+
36+
const noFBCopyrightFiles = newJsFiles.filter(filepath => {
37+
const content = fs.readFileSync(filepath).toString();
38+
for (const line of facebookLicenseHeaderComponents) {
39+
if (!content.match(new RegExp(line))) {
40+
return true;
41+
}
42+
}
43+
return false;
44+
});
45+
46+
if (noFBCopyrightFiles.length > 0) {
47+
const files = linkableFiles(noFBCopyrightFiles);
48+
fail(`New JS files do not have the Facebook copyright header: ${files}`);
49+
}
50+
51+
// Ensure the use of Flow and 'use strict';
52+
const noFlowFiles = newJsFiles.filter(filepath => {
53+
const content = fs.readFileSync(filepath).toString();
54+
return content.includes('@flow') && content.includes('use strict');
55+
});
56+
57+
if (noFlowFiles.length > 0) {
58+
const files = linkableFiles(noFlowFiles);
59+
const flow = '<code>@flow</code>';
60+
const strict = "<code>'use strict'</code>";
61+
warn(`Please ensure that ${flow} and ${strict} are enabled on: ${files}`);
62+
}
63+
64+
// No merge from master commmits
65+
// TODO: blocked by https://github.com/danger/danger-js/issues/81
66+
67+
// Warns if there are changes to package.json without changes to yarn.lock.
68+
69+
const packageChanged = danger.git.modified_files.includes('package.json');
70+
const lockfileChanged = danger.git.modified_files.includes('yarn.lock');
71+
if (packageChanged && !lockfileChanged) {
72+
const message = 'Changes were made to package.json, but not to yarn.lock';
73+
const idea = 'Perhaps you need to run `yarn install`?';
74+
warn(`${message} - <i>${idea}</i>`);
75+
}

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"babel-plugin-transform-flow-strip-types": "^6.18.0",
1111
"chalk": "^1.1.3",
1212
"codecov": "^1.0.1",
13+
"danger": "^0.8.0",
1314
"eslint": "^3.11.1",
1415
"eslint-plugin-babel": "^4.0.0",
1516
"eslint-plugin-flow-vars": "^0.5.0",
@@ -21,6 +22,7 @@
2122
"istanbul-api": "^1.1.0",
2223
"istanbul-lib-coverage": "^1.0.0",
2324
"jasmine-reporters": "^2.2.0",
25+
"jest-runtime": "file:./packages/jest-runtime/",
2426
"jsdom": "^9.9.1",
2527
"left-pad": "^1.1.1",
2628
"lerna": "2.0.0-beta.32",
@@ -43,7 +45,7 @@
4345
"postinstall": "node ./scripts/postinstall.js && node ./scripts/build.js",
4446
"publish": "yarn run build-clean && yarn run build && lerna publish",
4547
"test": "yarn run typecheck && yarn run lint && yarn run build && yarn run jest && yarn run test-examples",
46-
"test-ci": "yarn run typecheck && yarn run lint && yarn run build && yarn run jest-coverage -- -i && yarn run test-examples && node scripts/mapCoverage.js && codecov",
48+
"test-ci": "yarn run typecheck && yarn run lint && yarn run build && yarn run jest-coverage -- -i && yarn run test-examples && node scripts/mapCoverage.js && codecov && yarn run danger",
4749
"test-examples": "node scripts/test_examples.js",
4850
"typecheck": "flow check",
4951
"watch": "yarn run build; node ./scripts/watch.js"

0 commit comments

Comments
 (0)