forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for using Danger on PRs (jestjs#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
- Loading branch information
Showing
4 changed files
with
483 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.