import yargs = require ( 'yargs' ) ; import { logger } from 'gcf-utils' ; import { Octokit } from '@octokit/rest' ; import { getConfig } from '@google-automations/bot-config-utils' ; import * as yaml from 'js-yaml' ; import { readFileSync } from 'fs' ; import { RepoConfig } from './types' ; import { SyncRepoSettings } from './sync-repo-settings' ; import { CONFIG_FILE_NAME } from './config' ; import schema from './schema.json' ; interface Args { file ? : string ; branch ? : string ; 'github-token' : string ; repo : string ; } const sync = { command : '$0' , describe : 'sync repository settings from a remote configuration' , builder ( yargs ) { return yargs . option ( 'file' , { describe : 'path to configuration file' , type : 'string' , } ) . option ( 'branch' , { describe : 'branch to fetch sync-repo-settings.yaml from' , type : 'string' , } ) . option ( 'github-token' , { describe : 'GitHub access token. Can also be set via the `GITHUB_TOKEN` environment variable.' , type : 'string' , coerce : arg => { return arg || process . env . GITHUB_TOKEN || process . env . GH_TOKEN ; } , demand : true , } ) . option ( 'repo' , { describe : 'target repository in the form of owner/repo' , type : 'string' , demand : true , } ) ; } , async handler ( argv ) { const [ owner , repo ] = argv . repo . split ( '/' ) ; const octokit = new Octokit ( { auth : argv [ 'github-token' ] , } ) ; let config ; if ( argv . file ) { const content = readFileSync ( argv . file ) . toString ( 'utf-8' ) ; config = yaml . load ( content ) as RepoConfig ; } else { config = await getConfig < RepoConfig > ( octokit , owner , repo , CONFIG_FILE_NAME , { fallbackToOrgConfig : false , schema : schema } ) ; } await new SyncRepoSettings ( octokit , logger ) . syncRepoSettings ( { repo : argv . repo , config : config || undefined , } ) ; } , } ; export function parser ( ) { return yargs . command ( sync ) . showHelpOnFail ( false ) . strictCommands ( ) ; } if ( require . main === module ) { parser ( ) . parse ( process . argv ) ; }