import meow from 'meow' ; import { Octokit } from '@octokit/rest' ; import { exportToBigQuery } from './export' ; import { getPolicy , GitHubRepo } from './policy' ; import { getChanger } from './changer' ; import { openIssue } from './issue' ; interface Flags { repo ? : string ; search ? : string ; export ? : boolean ; autofix ? : boolean ; report ? : boolean ; } const cli = meow ( `
	Usage
	  $ policy

	Options
    --repo        Filter by a given repository
    --search      Provide a GitHub repository search filter to limit the repositories used
    --export      Export the results to BigQuery.
    --autofix     Where possible, submit a PR to fix any issues.
    --report      Create or update a GitHub Issue documenting the gaps

	Examples
    $ policy [--repo][--search QUERY][--autofix][--report]

` , { flags : { repo : { type : 'string' } , search : { type : 'string' } , autofix : { type : 'boolean' } , report : { type : 'boolean' } , } , } ) ; export async function main ( cli ) { const auth = process . env . GITHUB_TOKEN || process . env . GH_TOKEN ; const flags = cli . flags as Flags ; if ( ! auth ) { throw new Error ( 'The GITHUB_TOKEN or GH_TOKEN env var must be set with a personal access token' ) ; } const repos = [ ] ; const octokit = new Octokit ( { auth } ) ; const policy = getPolicy ( octokit , console ) ; if ( flags . repo ) { const repo = await policy . getRepo ( flags . repo ) ; repos . push ( repo ) ; } else if ( flags . search ) { const r = await policy . getRepos ( flags . search ) ; repos . push ( ... r ) ; } else { cli . showHelp ( ) ; return ; } for ( const repo of repos ) { const res = await policy . checkRepoPolicy ( repo ) ; console . log ( res ) ; if ( flags . export ) { await exportToBigQuery ( res ) ; } if ( flags . autofix ) { const changer = getChanger ( octokit , repo ) ; await changer . submitFixes ( res ) ; } if ( flags . report ) { await openIssue ( octokit , res ) ; } } } if ( module === require . main ) { main ( cli ) ; }