import { includes } from 'lodash' ; import * as fs from 'fs' ; const pr = danger . github . pr ; const commits = danger . github . commits ; const modified = danger . git . modified_files ; const bodyAndTitle = ( pr . body + pr . title ) . toLowerCase ( ) ; const trivialPR = bodyAndTitle . includes ( 'trivial' ) ; const acceptedNoTests = bodyAndTitle . includes ( 'skip new tests' ) ; const typescriptOnly = ( file ) => includes ( file , '.ts' ) ; const filesOnly = ( file ) => fs . existsSync ( file ) && fs . lstatSync ( file ) . isFile ( ) ; const modifiedAppFiles = modified . filter ( p => includes ( p , 'src' ) || includes ( p , '__tests__' ) ) . filter ( p => filesOnly ( p ) && typescriptOnly ( p ) ) ; const linkableFiles = paths => { 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 ) ; } ; const toSentence = ( array ) : string => { if ( array . length === 1 ) { return array [ 0 ] ; } return array . slice ( 0 , array . length - 1 ) . join ( ', ' ) + ' and ' + array . pop ( ) ; } ; const createLink = ( href , text ) : string => `<a href='${ href }'>${ text }</a>` ; const raiseIssueAboutPaths = ( type , paths , codeToInclude ) => { if ( paths . length > 0 ) { const files = linkableFiles ( paths ) ; const strict = '<code>' + codeToInclude + '</code>' ; type ( `Please ensure that ${ strict } is enabled on: ${ files }` ) ; } } ; const authors = commits . map ( x => x . author . login ) ; const isBot = authors . some ( x => [ 'greenkeeper' , 'renovate' ] . indexOf ( x ) > - 1 ) ; if ( ! isBot ) { if ( pr . body . length === 0 ) { fail ( 'Please add a description to your PR.' ) ; } const hasAppChanges = modifiedAppFiles . length > 0 ; const testChanges = modifiedAppFiles . filter ( filepath => filepath . includes ( 'test' ) ) ; const hasTestChanges = testChanges . length > 0 ; const bigPRThreshold = 500 ; if ( danger . github . pr . additions + danger . github . pr . deletions > bigPRThreshold ) { warn ( ':exclamation: Big PR' ) ; } if ( hasAppChanges && ! hasTestChanges ) { warn ( "There are library changes, but not tests. That's OK as long as you're refactoring existing code" ) ; } const onlyTestFiles = testChanges . filter ( x => { const content = fs . readFileSync ( x ) . toString ( ) ; return ( content . includes ( 'it.only' ) || content . includes ( 'describe.only' ) || content . includes ( 'fdescribe' ) || content . includes ( 'fit(' ) ) ; } ) ; raiseIssueAboutPaths ( fail , onlyTestFiles , 'an `only` was left in the test' ) ; }