import { Probot } from 'probot' ; import { logger } from 'gcf-utils' ; import { getPolicy } from './policy' ; import { exportToBigQuery } from './export' ; import { getChanger } from './changer' ; import { openIssue } from './issue' ; export const allowedOrgs = [ 'googleapis' , 'googlecloudplatform' ] ; export function policyBot ( app ) { app . on ( [ 'schedule.repository' as any ] , async ( context ) => { const owner = context . payload . organization . login ; const name = context . payload . repository . name ; const repo = `${ owner }/${ name }` ; if ( ! allowedOrgs . includes ( owner . toLowerCase ( ) ) ) { logger . info ( `skipping run for ${ repo }` ) ; return ; } const policy = getPolicy ( context . octokit , logger ) ; const repoMetadata = await policy . getRepo ( repo ) ; if ( repoMetadata . private || repoMetadata . archived ) { return ; } if ( owner . toLowerCase ( ) === 'googlecloudplatform' ) { const topics = repoMetadata . topics || [ ] ; if ( ! ( topics . includes ( 'samples' ) || topics . includes ( 'libraries' ) ) ) { return ; } } const result = await policy . checkRepoPolicy ( repoMetadata ) ; await exportToBigQuery ( result ) ; try { await openIssue ( context . octokit , result ) ; } catch ( e ) { const err = e as Error ; err . message = `Error opening GitHub issue: ${ err . message }` ; logger . error ( err ) ; } try { const changer = getChanger ( context . octokit , repoMetadata , logger ) ; await changer . submitFixes ( result ) ; } catch ( e ) { const err = e as Error ; logger . error ( err ) ; } } ) ; }