import { Probot , ProbotOctokit } from 'probot' ; import { describe , it , beforeEach , afterEach } from 'mocha' ; import nock from 'nock' ; import * as sinon from 'sinon' ; import { handler } from '../src/repo-metadata-lint' ; import { logger } from 'gcf-utils' ; import * as fileIterator from '../src/file-iterator' ; import { assert } from 'console' ; async function * emptyIterator ( ) { } async function * failingIterator ( ) { yield [ './foo' , JSON . stringify ( { library_type : 'BATMAN_LIB' , } ) , ] ; } nock . disableNetConnect ( ) ; const sandbox = sinon . createSandbox ( ) ; describe ( 'repo-metadata-lint' , ( ) => { let probot ; beforeEach ( ( ) => { probot = new Probot ( { githubToken : 'abc123' , Octokit : ProbotOctokit . defaults ( { retry : { enabled : false } , throttle : { enabled : false } , } ) , } ) ; probot . load ( handler ) ; } ) ; afterEach ( ( ) => { nock . cleanAll ( ) ; sandbox . restore ( ) ; } ) ; describe ( 'schedule.repository' , ( ) => { it ( 'handles schedule.repository event' , async ( ) => { const FileIterator = sandbox . stub ( fileIterator , 'FileIterator' ) ; FileIterator . prototype . repoMetadata = sandbox . stub ( ) . returns ( emptyIterator ( ) ) ; const infoStub = sandbox . stub ( logger , 'info' ) ; const githubApiRequests = nock ( 'https://api.github.com' ) . get ( '/repos/foo-org/foo-repo/issues?labels=repo-metadata%3A%20lint' ) . reply ( 200 , [ ] ) ; await probot . receive ( { name : 'schedule.repository' as any , payload : { organization : { login : 'foo-org' } , repository : { name : 'foo-repo' , owner : { login : 'bar-login' } } , cron_org : 'foo-org' , } as any , id : 'abc123' , } ) ; sandbox . assert . calledWith ( infoStub , sinon . match ( /no validation errors/ ) ) ; githubApiRequests . done ( ) ; } ) ; it ( 'opens an issue on failure' , async ( ) => { const FileIterator = sandbox . stub ( fileIterator , 'FileIterator' ) ; FileIterator . prototype . repoMetadata = sandbox . stub ( ) . returns ( failingIterator ( ) ) ; const infoStub = sandbox . stub ( logger , 'info' ) ; const githubApiRequests = nock ( 'https://api.github.com' ) . get ( '/repos/foo-org/foo-repo/issues?labels=repo-metadata%3A%20lint' ) . reply ( 200 , [ ] ) . post ( '/repos/foo-org/foo-repo/issues' , ( post ) => { assert ( post . body . includes ( 'library_type must be equal to one of' ) ) ; return true ; } ) . reply ( 200 ) ; await probot . receive ( { name : 'schedule.repository' as any , payload : { organization : { login : 'foo-org' } , repository : { name : 'foo-repo' , owner : { login : 'bar-login' } } , cron_org : 'foo-org' , } as any , id : 'abc123' , } ) ; sandbox . assert . calledWith ( infoStub , sinon . match ( /1 validation errors/ ) ) ; githubApiRequests . done ( ) ; } ) ; } ) ; } ) ;