cenfun / monocart-coverage-reports

A code coverage tool to generate native V8 reports or Istanbul reports.
MIT License
31 stars 5 forks source link

[Question] Show file coverage for touched files only #64

Closed fnebenfuehr closed 1 month ago

fnebenfuehr commented 1 month ago

Is there a way to generate markdown summary-details for only the files that are being touched by the PR. I am trying to create a similar experience that vitest-coverage-report-action gives you? Also the line reference with github integration is pretty neat without deploying v8 report. Although I like the v8 report.

Appreciate your fantastic work.

cenfun commented 1 month ago

Is it not working with the report markdown-summary and markdown-details? see example run: https://github.com/cenfun/monocart-coverage-reports/actions/runs/10097130722 image

only the files that are being touched by the PR

Do you mean you need a filter?

fnebenfuehr commented 1 month ago

markdown-summary and markdown-details are working fine. However, I would like to filter markdown-details to only show relevant files, specifically new or changed files within the context of a PR. Currently, it seems to show all files.

cenfun commented 1 month ago

Please try new version monocart-coverage-reports@2.10.0

const getPullRequestChanges = async () => {

if (!github.context.payload.pull_request) {
    // console.log(Object.keys(github.context));
    return [];
}

// console.log('pull_request', github.context.payload.pull_request);

/**
 * env:
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
 */

const octokit = github.getOctokit(process.env.GITHUB_TOKEN);
const prNumber = github.context.payload.pull_request.number;
core.startGroup(`Fetching list of changed files for PR#${prNumber} from Github API`);

const iterator = octokit.paginate.iterator(
    octokit.rest.pulls.listFiles, {
        owner: github.context.repo.owner,
        repo: github.context.repo.repo,
        pull_number: prNumber,
        per_page: 100
    }
);

const files = [];
for await (const response of iterator) {
    core.info(`Received ${response.data.length} items`);

    for (const file of response.data) {
        core.debug(`[${file.status}] ${file.filename}`);
        if (['added', 'modified'].includes(file.status)) {
            files.push(file.filename);
        }
    }
}

return files;

};

- Then using `filter` option in report `markdown-details` 
```js

const prChanges = await getPullRequestChanges();
const filter = (file) => {
        for (const p of prChanges) {
            if (file.sourcePath.includes(p)) {
                return true;
            }
        }
};
const coverageOptions = {
        reports: [
            ['markdown-details', {
                // color: 'Tex',
                baseUrl: 'https://cenfun.github.io/monocart-coverage-reports/pr/#page=',
                metrics: ['bytes', 'lines'],
                filter
            }]
        ]

    };

see details:

groinder commented 1 month ago

This filter option is awesome I'd love to see it for all reporters!

cenfun commented 1 month ago

If it's all the reports including the summary report, then I recommend using entryFilter and sourceFilter.

https://github.com/cenfun/monocart-coverage-reports?tab=readme-ov-file#filtering-results