code-intelligence.com/cifuzz@v0.40.0/dangerfile.js (about) 1 const { danger, warn, markdown } = require("danger"); 2 const { basename, dirname } = require("path"); 3 4 const goFileFilter = fileName => 5 fileName.includes(".go") && 6 !fileName.includes("_test.go") && 7 !fileName.includes("testutil") && 8 !fileName.startsWith("integration-tests") && 9 !fileName.startsWith("e2e-tests"); 10 const testFileFilter = fileName => fileName.includes("_test.go"); 11 12 const createdGoFiles = danger.git.created_files.filter(goFileFilter); 13 const createdTestFiles = danger.git.created_files.filter(testFileFilter); 14 const modifiedGoFiles = danger.git.modified_files.filter(goFileFilter); 15 const modifiedTestFiles = danger.git.modified_files.filter(testFileFilter); 16 17 // Warnings 18 checkDescription(); 19 prSize(); 20 missingTestsForCreatedFiles(); 21 missingTestsForModifiedFiles(); 22 23 // Encouragement 24 newTestsForExistingFiles(); 25 removedMoreCodeThanAdded(); 26 27 function checkDescription() { 28 if (!danger.github.pr.body || danger.github.pr.body.length <= 0) { 29 warn(`This PR doesn't have a description. 30 We recommend following the template to include all necessary information.`); 31 } else { 32 if ( 33 danger.github.pr.body?.includes("Motivation/Context") && 34 danger.github.pr.body?.includes("Description") && 35 danger.github.pr.body?.includes("How to use/reproduce") 36 ) { 37 markdown("Thank you for using the PR template ❤️"); 38 } 39 } 40 } 41 42 function prSize() { 43 if (danger.github.pr.changed_files > 15) { 44 warn(`This PR changes a lot of files (${danger.github.pr.changed_files}). 45 It could be useful to break it up into multiple PRs to keep your changes simple and easy to review.`); 46 } 47 } 48 49 function missingTestsForCreatedFiles() { 50 if (createdGoFiles?.length > 0) { 51 const missingTestsForCreatedGoFiles = createdGoFiles.filter(x => { 52 // Create the test file names for the go file and check 53 // if it can be found in the list of created test files 54 const filePath = dirname(x); 55 const testFile = basename(x).replace(".go", "_test.go"); 56 return !createdTestFiles.includes(`${filePath}/${testFile}`); 57 }); 58 59 // No idea why these extra lines are necessary 60 // but without them the bullet points *sometimes* don't work 61 if (missingTestsForCreatedGoFiles?.length > 0) { 62 warn(` 63 The following created files don't have corresponding test files: 64 - [ ] ${missingTestsForCreatedGoFiles.join("\n - [ ] ")} 65 66 If you checked the file and there is no need for the test, you can tick the checkbox.`); 67 } 68 } 69 } 70 71 function missingTestsForModifiedFiles() { 72 if (modifiedGoFiles?.length > 0) { 73 const missingTestsForModifiedGoFiles = modifiedGoFiles.filter(x => { 74 // Create the test file names for the go file and check 75 // if it can be found in the list of modified or created test files 76 const filePath = dirname(x); 77 const testFile = basename(x).replace(".go", "_test.go"); 78 return ( 79 !modifiedTestFiles.includes(`${filePath}/${testFile}`) && 80 !createdTestFiles.includes(`${filePath}/${testFile}`) 81 ); 82 }); 83 84 // No idea why these extra lines are necessary 85 // but without them the bullet points *sometimes* don't work 86 if (missingTestsForModifiedGoFiles?.length > 0) { 87 warn(` 88 The following files have been modified but their tests have not changed: 89 - [ ] ${missingTestsForModifiedGoFiles.join("\n - [ ] ")} 90 91 If you checked the file and there is no need for the test, you can tick the checkbox.`); 92 } 93 } 94 } 95 96 function newTestsForExistingFiles() { 97 if (createdTestFiles?.length > 0) { 98 const expandedTestCoverage = createdTestFiles.filter(x => { 99 // Create the go file names for the test file and check 100 // if it can be found in the list of created go files 101 const filePath = dirname(x); 102 const file = basename(x).replace("_test.go", ".go"); 103 return !createdGoFiles.includes(`${filePath}/${file}`); 104 }); 105 106 if (expandedTestCoverage?.length > 0) { 107 markdown(`You're a rockstar for creating tests for files without any ⭐`); 108 } 109 } 110 } 111 112 function removedMoreCodeThanAdded() { 113 if (danger.github.pr.deletions > danger.github.pr.additions) { 114 markdown(`You removed more lines of code than you added, nice cleanup 🧹`); 115 } 116 }