import * as core from '@actions/core' ; import * as github from '@actions/github' ; import { execSync } from 'child_process' ; import fs from 'fs' ; import { CoverageReport } from './Model/CoverageReport' ; import { DiffChecker } from './DiffChecker' ; import { Octokit } from '@octokit/core' ; import { PaginateInterface } from '@octokit/plugin-paginate-rest' ; import { RestEndpointMethods } from '@octokit/plugin-rest-endpoint-methods/dist-types/generated/method-types' ; async function run ( ) { try { const repoName = github . context . repo . repo ; const repoOwner = github . context . repo . owner ; const commitSha = github . context . sha ; const githubToken = core . getInput ( 'accessToken' ) ; const fullCoverage = JSON . parse ( core . getInput ( 'fullCoverageDiff' ) ) ; const commandToRun = core . getInput ( 'runCommand' ) ; const commandAfterSwitch = core . getInput ( 'afterSwitchCommand' ) ; const delta = Number ( core . getInput ( 'delta' ) ) ; const githubClient = github . getOctokit ( githubToken ) ; const prNumber = github . context . issue . number ; const branchNameBase = github . context . payload . pull_request ?. base . ref ; const branchNameHead = github . context . payload . pull_request ?. head . ref ; const useSameComment = JSON . parse ( core . getInput ( 'useSameComment' ) ) ; const commentIdentifier = `<!-- codeCoverageDiffComment -->` ; const deltaCommentIdentifier = `<!-- codeCoverageDeltaComment -->` ; let commentId = null ; execSync ( commandToRun ) ; const codeCoverageNew = < CoverageReport > ( JSON . parse ( fs . readFileSync ( 'coverage-summary.json' ) . toString ( ) ) ) ; execSync ( '/usr/bin/git fetch' ) ; execSync ( '/usr/bin/git stash' ) ; execSync ( `/usr/bin/git checkout --progress --force ${ branchNameBase }` ) ; if ( commandAfterSwitch ) { execSync ( commandAfterSwitch ) ; } execSync ( commandToRun ) ; const codeCoverageOld = < CoverageReport > ( JSON . parse ( fs . readFileSync ( 'coverage-summary.json' ) . toString ( ) ) ) ; const currentDirectory = execSync ( 'pwd' ) . toString ( ) . trim ( ) ; const diffChecker = new DiffChecker ( codeCoverageNew , codeCoverageOld ) ; let messageToPost = `## Test coverage results :test_tube: \n
    Code coverage diff between base branch:${ branchNameBase } and head branch: ${ branchNameHead } \n\n` ; const coverageDetails = diffChecker . getCoverageDetails ( ! fullCoverage , `${ currentDirectory }/` ) ; if ( coverageDetails . length === 0 ) { messageToPost = 'No changes to code coverage between the base branch and the head branch' ; } else { messageToPost += 'Status | File | % Stmts | % Branch | % Funcs | % Lines \n -----|-----|---------|----------|---------|------ \n' ; messageToPost += coverageDetails . join ( '\n' ) ; } messageToPost = `${ commentIdentifier }\nCommit SHA:${ commitSha }\n${ messageToPost }` ; if ( useSameComment ) { commentId = await findComment ( githubClient , repoName , repoOwner , prNumber , commentIdentifier ) ; } await createOrUpdateComment ( commentId , githubClient , repoOwner , repoName , messageToPost , prNumber ) ; if ( diffChecker . checkIfTestCoverageFallsBelowDelta ( delta ) ) { if ( useSameComment ) { commentId = await findComment ( githubClient , repoName , repoOwner , prNumber , deltaCommentIdentifier ) ; } messageToPost = `Current PR reduces the test coverage percentage by ${ delta } for some tests` ; messageToPost = `${ deltaCommentIdentifier }\nCommit SHA:${ commitSha }\n${ messageToPost }` ; await createOrUpdateComment ( commentId , githubClient , repoOwner , repoName , messageToPost , prNumber ) ; throw Error ( messageToPost ) ; } } catch ( error ) { core . setFailed ( error ) ; } } async function createOrUpdateComment ( commentId , githubClient , repoOwner , repoName , messageToPost , prNumber ) { if ( commentId ) { await githubClient . issues . updateComment ( { owner : repoOwner , repo : repoName , comment_id : commentId , body : messageToPost } ) ; } else { await githubClient . issues . createComment ( { repo : repoName , owner : repoOwner , body : messageToPost , issue_number : prNumber } ) ; } } async function findComment ( githubClient , repoName , repoOwner , prNumber , identifier ) { const comments = await githubClient . issues . listComments ( { owner : repoOwner , repo : repoName , issue_number : prNumber } ) ; for ( const comment of comments . data ) { if ( comment . body . startsWith ( identifier ) ) { return comment . id ; } } return 0 ; } run ( ) ;