Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 5 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -27,5 +30,5 @@ runs:
using: 'node20'
main: 'dist/index.js'
branding:
icon: 'umbrella'
color: 'purple'
icon: 'umbrella'
color: 'purple'
32 changes: 32 additions & 0 deletions diff-coverage.json
Original file line number Diff line number Diff line change
@@ -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
}
26 changes: 23 additions & 3 deletions src/coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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(`.*<source>(?<source>.*)</source>.*`)
const match = report.match(regex)
Expand Down
10 changes: 7 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import readFile from './readFile'
async function run(): Promise<void> {
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') {
Expand All @@ -21,10 +24,11 @@ async function run(): Promise<void> {

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) {
Expand Down