import { logger } from 'gcf-utils' ; import { ErrorMessageText } from './error-message-text' ; import { OctokitType } from './utils/octokit-util' ; import { ValidationResult } from './validate' ; const LINT_LABEL = 'repo-metadata: lint' ; const ISSUE_LABELS = [ LINT_LABEL , 'type: process' ] ; export class IssueOpener { octokit : OctokitType ; owner : string ; repo : string ; constructor ( owner , repo , octokit ) { this . octokit = octokit ; this . owner = owner ; this . repo = repo ; } async open ( results ) { let update = false ; let updateIssueNumber = 0 ; const repoMetadataIssues = ( await this . octokit . rest . issues . listForRepo ( { owner : this . owner , repo : this . repo , labels : LINT_LABEL , } ) ) . data ; if ( repoMetadataIssues . length ) { const issue = repoMetadataIssues [ 0 ] ; update = true ; updateIssueNumber = issue . number ; if ( ErrorMessageText . eql ( issue . body || '' , results ) ) { logger . info ( `${ this . owner }/${ this . repo } has identical issue ${ issue . number }` ) ; return ; } } if ( results . length === 0 ) { logger . info ( `${ this . owner }/${ this . repo } has no validation errors` ) ; if ( update ) { await this . octokit . issues . update ( { owner : this . owner , repo : this . repo , issue_number : updateIssueNumber , state : 'closed' , } ) ; } return ; } const title = `Your .repo-metadata.json file${ results . length > 1 ? 's have' : ' has' } a problem 🤒` ; if ( update ) { await this . octokit . issues . update ( { owner : this . owner , repo : this . repo , title , body : ErrorMessageText . forIssueBody ( results ) , issue_number : updateIssueNumber , } ) ; } else { await this . octokit . issues . create ( { owner : this . owner , repo : this . repo , title , body : ErrorMessageText . forIssueBody ( results ) , labels : ISSUE_LABELS , } ) ; } } }