import * as github from '@actions/github' ; import { Octokit } from '@octokit/rest' ; import { inject , injectable } from 'inversify' ; import { isNil } from 'lodash' ; import { Logger , ProgressReporter , ReportMarkdownConvertor , checkRunDetailsTitle , checkRunName , disclaimerText , } from '@accessibility-insights-action/shared' ; import { CombinedReportParameters } from 'accessibility-insights-report' ; import { GitHubIocTypes } from '../ioc/gh-ioc-types' ; type CreateCheckResponseData = { id : number ; } ; type UpdateCheckOutputParameter = { title ? : string ; summary : string ; text ? : string ; } ; @ injectable ( ) export class CheckRunCreator extends ProgressReporter { private a11yCheck : CreateCheckResponseData ; constructor ( @ inject ( ReportMarkdownConvertor ) private readonly reportMarkdownConvertor , @ inject ( Octokit ) private readonly octokit , @ inject ( GitHubIocTypes . Github ) private readonly githubObj , @ inject ( Logger ) private readonly logger ) { super ( ) ; } public async start ( ) { this . logMessage ( 'Creating check run with status as in progress' ) ; const response = await this . invoke ( async ( ) => await this . octokit . checks . create ( { owner : this . githubObj . context . repo . owner , repo : this . githubObj . context . repo . repo , name : checkRunName , status : 'in_progress' , head_sha : isNil ( this . githubObj . context . payload . pull_request ) ? this . githubObj . context . sha : ( this . githubObj . context . payload . pull_request . head as { sha : string ; } ) . sha , } ) ) ; this . a11yCheck = response ?. data ; } public async completeRun ( combinedReportResult ) { this . logMessage ( 'Updating check run with status as completed' ) ; const reportMarkdown = this . reportMarkdownConvertor . convert ( combinedReportResult ) ; this . traceMarkdown ( reportMarkdown ) ; await this . invoke ( async ( ) => await this . octokit . checks . update ( { owner : this . githubObj . context . repo . owner , repo : this . githubObj . context . repo . repo , check_run_id : this . a11yCheck . id , name : checkRunName , status : 'completed' , conclusion : combinedReportResult . results . urlResults . failedUrls > 0 ? 'failure' : 'success' , output : this . getScanOutput ( reportMarkdown ) , } ) ) ; } public async failRun ( ) { this . logMessage ( 'Updating check run with status as failed' ) ; const reportMarkdown = this . reportMarkdownConvertor . getErrorMarkdown ( ) ; this . traceMarkdown ( reportMarkdown ) ; await this . invoke ( async ( ) => await this . octokit . checks . update ( { owner : this . githubObj . context . repo . owner , repo : this . githubObj . context . repo . repo , check_run_id : this . a11yCheck . id , name : checkRunName , status : 'completed' , conclusion : 'failure' , output : { title : checkRunDetailsTitle , summary : disclaimerText , annotations : [ ] , text : reportMarkdown , } , } ) ) ; } private logMessage ( message ) { this . logger . logInfo ( `${ message }` ) ; } private getScanOutput ( text ) { return { title : checkRunDetailsTitle , summary : disclaimerText , text , } ; } }