From 38293ec607aaff02c3b32b8a2f9cc837a26b7975 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge-Andr=C3=A9=20Fulgencio=20Esteves?= Date: Tue, 4 Mar 2025 08:43:29 +0100 Subject: [PATCH 1/4] Add support for diff coverage reporting and update workflow inputs --- .github/workflows/test.yml | 1 + action.yml | 7 +- diff-coverage.json | 215 +++++++++++++++++++++++++++++++++++++ src/coverage.ts | 26 ++++- src/index.ts | 6 +- 5 files changed, 249 insertions(+), 6 deletions(-) create mode 100644 diff-coverage.json diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 201938f..996f08c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -20,3 +20,4 @@ jobs: 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..2701b86 --- /dev/null +++ b/diff-coverage.json @@ -0,0 +1,215 @@ +{ + "report_name": "XML", + "diff_name": "origin/develop...HEAD, staged and unstaged changes", + "src_stats": { + "model/room.py": { + "percent_covered": 90.9090909090909, + "violation_lines": [ + 368 + ], + "covered_lines": [ + 12, + 367, + 370, + 371, + 372, + 373, + 378, + 379, + 380, + 381 + ], + "violations": [ + [ + 368, + null + ] + ] + }, + "test.py": { + "percent_covered": 0.0, + "violation_lines": [ + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 10, + 12, + 13, + 16, + 18, + 21, + 23, + 26, + 28, + 31, + 33, + 36, + 38, + 39, + 42, + 44, + 45, + 48, + 50, + 51, + 54, + 56, + 59, + 61, + 62, + 63 + ], + "covered_lines": [], + "violations": [ + [ + 1, + null + ], + [ + 2, + null + ], + [ + 3, + null + ], + [ + 4, + null + ], + [ + 5, + null + ], + [ + 6, + null + ], + [ + 7, + null + ], + [ + 10, + null + ], + [ + 12, + null + ], + [ + 13, + null + ], + [ + 16, + null + ], + [ + 18, + null + ], + [ + 21, + null + ], + [ + 23, + null + ], + [ + 26, + null + ], + [ + 28, + null + ], + [ + 31, + null + ], + [ + 33, + null + ], + [ + 36, + null + ], + [ + 38, + null + ], + [ + 39, + null + ], + [ + 42, + null + ], + [ + 44, + null + ], + [ + 45, + null + ], + [ + 48, + null + ], + [ + 50, + null + ], + [ + 51, + null + ], + [ + 54, + null + ], + [ + 56, + null + ], + [ + 59, + null + ], + [ + 61, + null + ], + [ + 62, + null + ], + [ + 63, + null + ] + ] + }, + "project_sync/callbacks/model/room_monitoring.py": { + "percent_covered": 100.0, + "violation_lines": [], + "covered_lines": [ + 22 + ], + "violations": [] + } + }, + "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..7c7e9a4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,6 +10,9 @@ async function run(): Promise { const coverageFile: string = core.getInput('coverageFile', {required: true}) core.debug(`coverageFile: ${coverageFile}`) + const diffCoverageFile: string = core.getInput('diffCoverageFile', {required: true}) + core.debug(`diffCoverageFile: ${diffCoverageFile}`) + const eventName = context.eventName if (eventName !== 'pull_request') { core.info(`action support only pull requests but event is ${eventName}`) @@ -24,7 +27,8 @@ async function run(): Promise { core.info(`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) { From 7f688d49a6fcc2064da565ee54df234a25f2cc61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge-Andr=C3=A9=20Fulgencio=20Esteves?= Date: Tue, 4 Mar 2025 08:51:03 +0100 Subject: [PATCH 2/4] Refactor coverage logging and update diff coverage file handling --- diff-coverage.json | 189 +-------------------------------------------- src/index.ts | 6 +- 2 files changed, 6 insertions(+), 189 deletions(-) diff --git a/diff-coverage.json b/diff-coverage.json index 2701b86..39f5cc5 100644 --- a/diff-coverage.json +++ b/diff-coverage.json @@ -2,11 +2,9 @@ "report_name": "XML", "diff_name": "origin/develop...HEAD, staged and unstaged changes", "src_stats": { - "model/room.py": { - "percent_covered": 90.9090909090909, - "violation_lines": [ - 368 - ], + "src/coverage.ts": { + "percent_covered": 69.9090909090909, + "violation_lines": [], "covered_lines": [ 12, 367, @@ -25,187 +23,6 @@ null ] ] - }, - "test.py": { - "percent_covered": 0.0, - "violation_lines": [ - 1, - 2, - 3, - 4, - 5, - 6, - 7, - 10, - 12, - 13, - 16, - 18, - 21, - 23, - 26, - 28, - 31, - 33, - 36, - 38, - 39, - 42, - 44, - 45, - 48, - 50, - 51, - 54, - 56, - 59, - 61, - 62, - 63 - ], - "covered_lines": [], - "violations": [ - [ - 1, - null - ], - [ - 2, - null - ], - [ - 3, - null - ], - [ - 4, - null - ], - [ - 5, - null - ], - [ - 6, - null - ], - [ - 7, - null - ], - [ - 10, - null - ], - [ - 12, - null - ], - [ - 13, - null - ], - [ - 16, - null - ], - [ - 18, - null - ], - [ - 21, - null - ], - [ - 23, - null - ], - [ - 26, - null - ], - [ - 28, - null - ], - [ - 31, - null - ], - [ - 33, - null - ], - [ - 36, - null - ], - [ - 38, - null - ], - [ - 39, - null - ], - [ - 42, - null - ], - [ - 44, - null - ], - [ - 45, - null - ], - [ - 48, - null - ], - [ - 50, - null - ], - [ - 51, - null - ], - [ - 54, - null - ], - [ - 56, - null - ], - [ - 59, - null - ], - [ - 61, - null - ], - [ - 62, - null - ], - [ - 63, - null - ] - ] - }, - "project_sync/callbacks/model/room_monitoring.py": { - "percent_covered": 100.0, - "violation_lines": [], - "covered_lines": [ - 22 - ], - "violations": [] } }, "total_num_lines": 45, diff --git a/src/index.ts b/src/index.ts index 7c7e9a4..0f4e06b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,10 +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', {required: true}) - core.debug(`diffCoverageFile: ${diffCoverageFile}`) + const diffCoverageFile: string = core.getInput('diffCoverageFile') + core.info(`diffCoverageFile: ${diffCoverageFile}`) const eventName = context.eventName if (eventName !== 'pull_request') { From a60b771ac01dd88f06b9782fb155f3fb4e17a357 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge-Andr=C3=A9=20Fulgencio=20Esteves?= Date: Tue, 4 Mar 2025 09:21:34 +0100 Subject: [PATCH 3/4] change ref --- .github/workflows/test.yml | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 996f08c..eba7923 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -2,18 +2,20 @@ 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 }} From 5f22ff3a0eef4fb93e60af61858150f31506dd2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge-Andr=C3=A9=20Fulgencio=20Esteves?= Date: Tue, 4 Mar 2025 09:24:24 +0100 Subject: [PATCH 4/4] Update log message for commit comparison to improve clarity --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 0f4e06b..104ebee 100644 --- a/src/index.ts +++ b/src/index.ts @@ -24,7 +24,7 @@ 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 diffReport = readFile(diffCoverageFile)