Skip to content

Commit

Permalink
Adds support for using Danger on PRs (jestjs#2508)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
orta authored and cpojer committed Jan 7, 2017
1 parent b135885 commit 2b81d21
Show file tree
Hide file tree
Showing 4 changed files with 483 additions and 16 deletions.
1 change: 1 addition & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
.*/vendor/jsonlint/.*
.*/examples/.*
.*/website/.*
.*/dangerfile.js

[options]
module.name_mapper='^types/\(.*\)$' -> '<PROJECT_ROOT>/types/\1.js'
Expand Down
75 changes: 75 additions & 0 deletions dangerfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// @flow

const {danger, fail, warn} = require('danger');
const fs = require('fs');

// Takes a list of file paths, and converts it into clickable links
const linkableFiles = (paths: Array<string>): string => {
const repoURL = danger.github.pr.head.repo.html_url;
const ref = danger.github.pr.head.ref;
const links = paths.map(path => {
return createLink(`${repoURL}/blob/${ref}/${path}`, path);
});
return toSentence(links);
};

// ["1", "2", "3"] to "1, 2 and 3"
const toSentence = (array: Array<string>) : string => {
if (array.length === 1) { return array[0]; }
return array.slice(0, array.length - 1).join(', ') + ' and ' + array.pop();
};

// ("/href/thing", "name") to "<a href="/href/thing">name</a>"
const createLink = (href: string, text: string): string =>
`<a href='${href}'>${text}</a>`;

const newJsFiles = danger.git.created_files.filter(path => path.endsWith('js'));

// New JS files should have the FB copyright header + flow
const facebookLicenseHeaderComponents = [
'Copyright \(c\) .*, Facebook, Inc. All rights reserved.',
'This source code is licensed under the BSD-style license found in the',
'LICENSE file in the root directory of this source tree. An additional grant',
'of patent rights can be found in the PATENTS file in the same directory.',
];

const noFBCopyrightFiles = newJsFiles.filter(filepath => {
const content = fs.readFileSync(filepath).toString();
for (const line of facebookLicenseHeaderComponents) {
if (!content.match(new RegExp(line))) {
return true;
}
}
return false;
});

if (noFBCopyrightFiles.length > 0) {
const files = linkableFiles(noFBCopyrightFiles);
fail(`New JS files do not have the Facebook copyright header: ${files}`);
}

// Ensure the use of Flow and 'use strict';
const noFlowFiles = newJsFiles.filter(filepath => {
const content = fs.readFileSync(filepath).toString();
return content.includes('@flow') && content.includes('use strict');
});

if (noFlowFiles.length > 0) {
const files = linkableFiles(noFlowFiles);
const flow = '<code>@flow</code>';
const strict = "<code>'use strict'</code>";
warn(`Please ensure that ${flow} and ${strict} are enabled on: ${files}`);
}

// No merge from master commmits
// TODO: blocked by https://github.com/danger/danger-js/issues/81

// Warns if there are changes to package.json without changes to yarn.lock.

const packageChanged = danger.git.modified_files.includes('package.json');
const lockfileChanged = danger.git.modified_files.includes('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>`);
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"babel-plugin-transform-flow-strip-types": "^6.18.0",
"chalk": "^1.1.3",
"codecov": "^1.0.1",
"danger": "^0.8.0",
"eslint": "^3.11.1",
"eslint-plugin-babel": "^4.0.0",
"eslint-plugin-flow-vars": "^0.5.0",
Expand All @@ -21,6 +22,7 @@
"istanbul-api": "^1.1.0",
"istanbul-lib-coverage": "^1.0.0",
"jasmine-reporters": "^2.2.0",
"jest-runtime": "file:./packages/jest-runtime/",
"jsdom": "^9.9.1",
"left-pad": "^1.1.1",
"lerna": "2.0.0-beta.32",
Expand All @@ -43,7 +45,7 @@
"postinstall": "node ./scripts/postinstall.js && node ./scripts/build.js",
"publish": "yarn run build-clean && yarn run build && lerna publish",
"test": "yarn run typecheck && yarn run lint && yarn run build && yarn run jest && yarn run test-examples",
"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",
"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",
"test-examples": "node scripts/test_examples.js",
"typecheck": "flow check",
"watch": "yarn run build; node ./scripts/watch.js"
Expand Down
Loading

0 comments on commit 2b81d21

Please sign in to comment.