const path = require ( "path" ) ; import ConfigurationError from "./configuration-error" ; import fetch from "./fetch" ; export interface GitHubUserResponse { login : string ; name : string ; html_url : string ; } export interface GitHubIssueResponse { number : number ; title : string ; pull_request ? : { html_url : string ; } ; labels : Array < { name : string ; } > ; user : { login : string ; html_url : string ; } ; } export interface Options { repo : string ; rootPath : string ; cacheDir ? : string ; } export default class GithubAPI { private cacheDir : string | undefined ; private auth : string ; constructor ( config ) { this . cacheDir = config . cacheDir && path . join ( config . rootPath , config . cacheDir , "github" ) ; this . auth = this . getAuthToken ( ) ; if ( ! this . auth ) { throw new ConfigurationError ( "Must provide GITHUB_AUTH" ) ; } } public getBaseIssueUrl ( repo ) { return `https://github.com/${ repo }/issues/` ; } public async getIssueData ( repo , issue ) { return this . _fetch ( `https://api.github.com/repos/${ repo }/issues/${ issue }` ) ; } public async getUserData ( login ) { return this . _fetch ( `https://api.github.com/users/${ login }` ) ; } private async _fetch ( url ) { const res = await fetch ( url , { cachePath : this . cacheDir , headers : { Authorization : `token ${ this . auth }` , } , } ) ; const parsedResponse = await res . json ( ) ; if ( res . ok ) { return parsedResponse ; } throw new ConfigurationError ( `Fetch error: ${ res . statusText }.\n${ JSON . stringify ( parsedResponse ) }` ) ; } private getAuthToken ( ) { return process . env . GITHUB_AUTH || "" ; } }