barecheck / code-coverage-action

GitHub Action that generates code coverage reports
https://barecheck.com
MIT License
57 stars 14 forks source link

Bug: Failed to generate code coverage report for monorepo. #235

Closed hemant-brb closed 1 year ago

hemant-brb commented 1 year ago

Consider a monorepo which have app/web and app/server with their own coverage information at location app/web/coverage/lcov.info and app/server/coverage/lcov.info respectively. (which we can specify through lcov-file param).

However when the script tries to read the changed files coverage information in changeFilesCoverage.js -> getChangedFilesCoverage(), there is an issue in file path comparison logic due to which it doesn't match any files and returns an empty array.

Now, In coverage/lcov.info file path is app/web/**/xyz.js but the script tries to compare this file with the **/xyz.js. While passing the params through GitHub Actions we need to tell the script the workspace path so that the script can use that workspace path + file path to compare.

const getChangedFilesCoverage = async (coverage) => {
  const pullRequestContext = getPullRequestContext();

  if (!pullRequestContext) return coverage.data;

  const octokit = await getOctokit();

  const { repo, owner, pullNumber } = pullRequestContext;
  const changedFiles = await githubApi.getChangedFiles(octokit, {
    repo,
    owner,
    pullNumber
  });

  const changedFilesCoverage = coverage.data.reduce(
    (allFiles, { file, lines }) => {
      const changedFile = changedFiles.find(
        ({ filename }) => filename === file
      );

      if (changedFile) {
        return [
          ...allFiles,
          {
            file,
            url: changedFile.blob_url,
            lines
          }
        ];
      }
      return allFiles;
    },
    []
  );

  return changedFilesCoverage;
};
hemant-brb commented 1 year ago

have added a fix for this issue https://github.com/barecheck/code-coverage-action/pull/236

const getChangedFilesCoverage = async (coverage) => {
  const pullRequestContext = getPullRequestContext();

  if (!pullRequestContext) return coverage.data;

  const octokit = await getOctokit();

  const { repo, owner, pullNumber } = pullRequestContext;
  const changedFiles = await githubApi.getChangedFiles(octokit, {
    repo,
    owner,
    pullNumber
  });

  const workspacePath = getWorkspacePath();
  const changedFilesCoverage = coverage.data.reduce(
    (allFiles, { file, lines }) => {
      const filePath = workspacePath ? `${workspacePath}/${file}` : file;
      const changedFile = changedFiles.find(
        ({ filename }) => filename === filePath
      );

      if (changedFile) {
        return [
          ...allFiles,
          {
            file: filePath,
            url: changedFile.blob_url,
            lines
          }
        ];
      }
      return allFiles;
    },
    []
  );

  return changedFilesCoverage;
};

The Github action:

      - name: Backend Code Coverage report
        id: code-coverage-backend
        uses: ./code-coverage
        with:
          barecheck-github-app-token: ${{ secrets.BARECHECK_GITHUB_APP_TOKEN }}
          lcov-file: "apps/server/coverage/lcov.info"
          send-summary-comment: true
          show-annotations: "warning"
          workspace-path: "apps/server"
          app-name: "Backend"
        env:
          BARECHECK_GITHUB_APP_TOKEN: ${{ secrets.BARECHECK_GITHUB_APP_TOKEN }}          
vitaliimelnychuk commented 1 year ago

That's great @Hemant11111 ! Thanks for doing it 🎉

I just left a few comments, let me know when you're able to take a look and we can merge/release this feature.

vitaliimelnychuk commented 1 year ago

Hey @Hemant11111 ! I just released new version - please, let me know if you see any issues with it