import { Context } from 'probot' ; import { Buffer } from 'buffer' ; export async function closeIssue ( context ) { const params = { owner : context . payload . repository . owner . login , repo : context . payload . repository . name , issue_number : context . payload . issue . number } ; await context . github . issues . update ( { ... params , state : 'closed' } ) ; } export async function createLabelIfNotExist ( context , label ) { try { const l = await context . github . issues . getLabel ( { owner : context . payload . repository . owner . login , repo : context . payload . repository . name , name : label } ) ; } catch ( e ) { if ( e . hasOwnProperty ( 'status' ) && e . status === 404 ) { await context . github . issues . createLabel ( { owner : context . payload . repository . owner . login , repo : context . payload . repository . name , name : label , color : 'aa1414' } ) ; } else { throw e ; } } } export async function addLabel ( context , label ) { await createLabelIfNotExist ( context , label ) ; await context . github . issues . addLabels ( { owner : context . payload . repository . owner . login , repo : context . payload . repository . name , number : context . payload . issue . number , labels : [ label ] } ) ; } export async function createComment ( context , comment ) { const params = { owner : context . payload . repository . owner . login , repo : context . payload . repository . name , number : context . payload . issue . number } ; await context . github . issues . createComment ( { body : comment , ... params } ) ; } export async function getContent ( context , path ) { const params = { owner : context . payload . repository . owner . login , repo : context . payload . repository . name } ; const resp = await context . github . repos . getContents ( { path , ... params } ) ; if ( ! ( resp . data instanceof Array ) && 'content' in resp . data && resp . data . content ) { return Buffer . from ( resp . data . content , resp . data . encoding ) . toString ( 'utf-8' ) ; } else { return null ; } }