|
| 1 | +import { danger, fail, warn, message } from 'danger'; |
| 2 | +import { includes } from 'lodash'; |
| 3 | +import * as fs from 'fs'; |
| 4 | + |
| 5 | +// Setup |
| 6 | +const pr = danger.github.pr; |
| 7 | +const modified = danger.git.modified_files; |
| 8 | +const bodyAndTitle = (pr.body + pr.title).toLowerCase(); |
| 9 | + |
| 10 | +// Custom modifiers for people submitting PRs to be able to say "skip this" |
| 11 | +const trivialPR = bodyAndTitle.includes('trivial'); |
| 12 | +const acceptedNoTests = bodyAndTitle.includes('skip new tests'); |
| 13 | + |
| 14 | +const typescriptOnly = (file: string) => includes(file, '.ts'); |
| 15 | +const filesOnly = (file: string) => |
| 16 | + fs.existsSync(file) && fs.lstatSync(file).isFile(); |
| 17 | + |
| 18 | +// Custom subsets of known files |
| 19 | +const modifiedAppFiles = modified |
| 20 | + .filter(p => includes(p, 'src/')) |
| 21 | + .filter(p => filesOnly(p) && typescriptOnly(p)); |
| 22 | + |
| 23 | +// Takes a list of file paths, and converts it into clickable links |
| 24 | +const linkableFiles = paths => { |
| 25 | + const repoURL = danger.github.pr.head.repo.html_url; |
| 26 | + const ref = danger.github.pr.head.ref; |
| 27 | + const links = paths.map(path => { |
| 28 | + return createLink(`${repoURL}/blob/${ref}/${path}`, path); |
| 29 | + }); |
| 30 | + return toSentence(links); |
| 31 | +}; |
| 32 | + |
| 33 | +// ["1", "2", "3"] to "1, 2 and 3" |
| 34 | +const toSentence = (array: Array<string>): string => { |
| 35 | + if (array.length === 1) { |
| 36 | + return array[0]; |
| 37 | + } |
| 38 | + return array.slice(0, array.length - 1).join(', ') + ' and ' + array.pop(); |
| 39 | +}; |
| 40 | + |
| 41 | +// ("/href/thing", "name") to "<a href="/href/thing">name</a>" |
| 42 | +const createLink = (href: string, text: string): string => |
| 43 | + `<a href='${href}'>${text}</a>`; |
| 44 | + |
| 45 | +// Raise about missing code inside files |
| 46 | +const raiseIssueAboutPaths = ( |
| 47 | + type: Function, |
| 48 | + paths: string[], |
| 49 | + codeToInclude: string |
| 50 | +) => { |
| 51 | + if (paths.length > 0) { |
| 52 | + const files = linkableFiles(paths); |
| 53 | + const strict = '<code>' + codeToInclude + '</code>'; |
| 54 | + type(`Please ensure that ${strict} is enabled on: ${files}`); |
| 55 | + } |
| 56 | +}; |
| 57 | + |
| 58 | +// Rules |
| 59 | + |
| 60 | +// make sure someone else reviews these changes |
| 61 | +const someoneAssigned = danger.github.pr.assignee; |
| 62 | +if (someoneAssigned === null) { |
| 63 | + warn( |
| 64 | + 'Please assign someone to merge this PR, and optionally include people who should review.' |
| 65 | + ); |
| 66 | +} |
| 67 | + |
| 68 | +// When there are app-changes and it's not a PR marked as trivial, expect |
| 69 | +// there to be CHANGELOG changes. |
| 70 | +const changelogChanges = includes(modified, 'CHANGELOG.md'); |
| 71 | +if (modifiedAppFiles.length > 0 && !trivialPR && !changelogChanges) { |
| 72 | + fail('No CHANGELOG added.'); |
| 73 | +} |
| 74 | + |
| 75 | +// No PR is too small to warrant a paragraph or two of summary |
| 76 | +if (pr.body.length === 0) { |
| 77 | + fail('Please add a description to your PR.'); |
| 78 | +} |
| 79 | + |
| 80 | +const hasAppChanges = modifiedAppFiles.length > 0; |
| 81 | + |
| 82 | +const testChanges = modifiedAppFiles.filter( |
| 83 | + filepath => filepath.includes('__tests__') || filepath.includes('test') |
| 84 | +); |
| 85 | +const hasTestChanges = testChanges.length > 0; |
| 86 | + |
| 87 | +// Warn when there is a big PR |
| 88 | +const bigPRThreshold = 500; |
| 89 | +if (danger.github.pr.additions + danger.github.pr.deletions > bigPRThreshold) { |
| 90 | + warn(':exclamation: Big PR'); |
| 91 | +} |
| 92 | + |
| 93 | +// Warn if there are library changes, but not tests |
| 94 | +if (hasAppChanges && !hasTestChanges) { |
| 95 | + warn( |
| 96 | + "There are library changes, but not tests. That's OK as long as you're refactoring existing code" |
| 97 | + ); |
| 98 | +} |
| 99 | + |
| 100 | +// Be careful of leaving testing shortcuts in the codebase |
| 101 | +const onlyTestFiles = testChanges.filter(x => { |
| 102 | + const content = fs.readFileSync(x).toString(); |
| 103 | + return ( |
| 104 | + content.includes('it.only') || |
| 105 | + content.includes('describe.only') || |
| 106 | + content.includes('fdescribe') || |
| 107 | + content.includes('fit(') |
| 108 | + ); |
| 109 | +}); |
| 110 | +raiseIssueAboutPaths(fail, onlyTestFiles, 'an `only` was left in the test'); |
0 commit comments