diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 201938f..eba7923 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -2,21 +2,24 @@ name: 'Get Cover Build' on: ['pull_request'] jobs: - build: # make sure build/ci work properly - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - run: | - npm install - - run: | - npm run all + # build: # make sure build/ci work properly + # runs-on: ubuntu-latest + # steps: + # - uses: actions/checkout@v3 + # - run: | + # npm install + # - run: | + # npm run all test: # make sure the action works on a clean machine without building runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 + with: + ref: ${{ github.head_ref }} - uses: ./ with: token: ${{ secrets.GITHUB_TOKEN }} thresholdNew: 0.8 thresholdModified: 0.0 coverageFile: coverage.xml + diffCoverageFile: diff-coverage.json diff --git a/action.yml b/action.yml index f81bc10..1ac39d5 100644 --- a/action.yml +++ b/action.yml @@ -8,6 +8,9 @@ inputs: token: required: true description: 'github token' + diffCoverageFile: + required: false + description: 'local path to a diff coverage xml file like the output of diff-cover coverage.xml' thresholdAll: required: false description: the coverage threshold for average over all files [0,1] @@ -27,5 +30,5 @@ runs: using: 'node20' main: 'dist/index.js' branding: - icon: 'umbrella' - color: 'purple' + icon: 'umbrella' + color: 'purple' diff --git a/diff-coverage.json b/diff-coverage.json new file mode 100644 index 0000000..39f5cc5 --- /dev/null +++ b/diff-coverage.json @@ -0,0 +1,32 @@ +{ + "report_name": "XML", + "diff_name": "origin/develop...HEAD, staged and unstaged changes", + "src_stats": { + "src/coverage.ts": { + "percent_covered": 69.9090909090909, + "violation_lines": [], + "covered_lines": [ + 12, + 367, + 370, + 371, + 372, + 373, + 378, + 379, + 380, + 381 + ], + "violations": [ + [ + 368, + null + ] + ] + } + }, + "total_num_lines": 45, + "total_num_violations": 34, + "total_percent_covered": 24, + "num_changed_lines": 95 +} \ No newline at end of file diff --git a/src/coverage.ts b/src/coverage.ts index 6589089..7bd3cde 100644 --- a/src/coverage.ts +++ b/src/coverage.ts @@ -23,14 +23,18 @@ export type FilesCoverage = { modifiedCover?: Coverage[] } -export function parseCoverageReport(report: string, files: CommitsComparison): FilesCoverage { +export function parseCoverageReport(report: string, diffReport: string, files: CommitsComparison): FilesCoverage { const threshAll = parseFloat(core.getInput('thresholdAll')) const avgCover = parseAverageCoverage(report, threshAll) const source = core.getInput('sourceDir') || parseSource(report) const threshModified = parseFloat(core.getInput('thresholdModified')) - const modifiedCover = parseFilesCoverage(report, source, files.modifiedFiles, threshModified) - + let modifiedCover: Coverage[] | undefined + if (diffReport === '') { + modifiedCover = parseFilesCoverage(report, source, files.modifiedFiles, threshModified) + } else { + modifiedCover = parseFilesCoverage(diffReport, source, files.modifiedFiles, threshModified) + } const threshNew = parseFloat(core.getInput('thresholdNew')) const newCover = parseFilesCoverage(report, source, files.newFiles, threshNew) return {averageCover: avgCover, newCover, modifiedCover} @@ -56,6 +60,22 @@ export function parseFilesCoverage( return coverages?.filter(cover => cover.cover >= 0) } +export function parseDiffCoverageReport( + report: string, + source: string, + files: string[] | undefined, + threshold: number +): Coverage[] | undefined { + const jsonReport = JSON.parse(report) + const coverages = files?.map(file => { + const fileName = escapeRegExp(file.replace(`${source}/`, '')) + const fileReport = jsonReport.src_stats[fileName] + const cover = fileReport?.percent_covered ?? -1 + return {file, cover, pass: cover >= threshold} + }) + return coverages?.filter(cover => cover.cover >= 0) +} + export function parseSource(report: string): string { const regex = new RegExp(`.*(?.*).*`) const match = report.match(regex) diff --git a/src/index.ts b/src/index.ts index 8c1f1fc..104ebee 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,7 +8,10 @@ import readFile from './readFile' async function run(): Promise { try { const coverageFile: string = core.getInput('coverageFile', {required: true}) - core.debug(`coverageFile: ${coverageFile}`) + core.info(`coverageFile: ${coverageFile}`) + + const diffCoverageFile: string = core.getInput('diffCoverageFile') + core.info(`diffCoverageFile: ${diffCoverageFile}`) const eventName = context.eventName if (eventName !== 'pull_request') { @@ -21,10 +24,11 @@ async function run(): Promise { core.info(`comparing commits: base ${base} <> head ${head}`) const files = await compareCommits(base, head) - core.info(`git new files: ${JSON.stringify(files.newFiles)} modified files: ${JSON.stringify(files.modifiedFiles)}`) + core.info(`can you see me ? git new files: ${JSON.stringify(files.newFiles)} modified files: ${JSON.stringify(files.modifiedFiles)}`) const report = readFile(coverageFile) - const filesCoverage = parseCoverageReport(report, files) + const diffReport = readFile(diffCoverageFile) + const filesCoverage = parseCoverageReport(report, diffReport, files) const passOverall = scorePr(filesCoverage) if (!passOverall) {