Skip to content

Commit

Permalink
added new build-in report: codacy (codacy.json)
Browse files Browse the repository at this point in the history
  • Loading branch information
cenfun committed Jul 1, 2024
1 parent 3272adf commit c25585e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Changelog

- 2.8.7
- fixed OOM with better GC
- added new build-in report: `codacy` (codacy.json)

- 2.8.6
- improved performance
- fixed warning message for adding empty istanbul coverage
Expand Down
3 changes: 1 addition & 2 deletions lib/converter/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ const handleLinesCoverage = (bytes, locator, ignoredRanges) => {
blank: blankCount,
comment: commentCount
};
// data lines for codecov
// https://docs.codecov.com/docs/codecov-custom-coverage-format
// data lines
const dataLines = {};

// no ignore items
Expand Down
38 changes: 38 additions & 0 deletions lib/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ const getReportGroup = (reports, lcov, dataType) => {

// both
'codecov': 'both',
'codacy': 'both',
'console-details': 'both',
'console-summary': 'both',
'raw': 'both'
Expand Down Expand Up @@ -280,6 +281,42 @@ const handleCodecovReport = async (reportData, reportOptions, options) => {
return Util.relativePath(jsonPath);
};

const handleCodacyReport = async (reportData, reportOptions, options) => {
const codacyOptions = {
outputFile: 'codacy.json',
... reportOptions
};

const jsonPath = path.resolve(options.outputDir, codacyOptions.outputFile);

// https://api.codacy.com/swagger#tocscoveragereport
const fileReports = [];
reportData.files.forEach((item) => {
const { sourcePath, data } = item;

const coverage = {};
for (const [key, value] of Object.entries(data.lines)) {
if (typeof value === 'number') {
coverage[key] = value;
} else {
// partial coverage not supported?
coverage[key] = 0;
}
}

fileReports.push({
filename: sourcePath,
coverage
});
});
const codacyData = {
fileReports
};

await Util.writeFile(jsonPath, JSON.stringify(codacyData));
return Util.relativePath(jsonPath);
};

const handleConsoleDetailsReport = (reportData, reportOptions, options) => {
const cdOptions = {
maxCols: 50,
Expand Down Expand Up @@ -464,6 +501,7 @@ const generateCoverageReports = async (dataList, sourceCache, options) => {

const buildInBothReports = {
'codecov': handleCodecovReport,
'codacy': handleCodacyReport,
'console-details': handleConsoleDetailsReport,
'console-summary': handleConsoleSummaryReport,
'raw': handleRawReport
Expand Down
5 changes: 4 additions & 1 deletion lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ declare namespace MCR {
['codecov'] | ["codecov", {
outputFile?: string;
}] |
['codacy'] | ["codacy", {
outputFile?: string;
}] |
['console-summary'] | ['console-summary', {
metrics?: Array<"bytes" | "statements" | "branches" | "functions" | "lines">;
}] |
Expand Down Expand Up @@ -190,7 +193,7 @@ declare namespace MCR {
branches?: CoverageRange[];
functions?: CoverageRange[];
ignores?: IgnoredRange[];
/** codecov json format */
/** common json format */
lines: {
[key: string]: number | string;
};
Expand Down

0 comments on commit c25585e

Please sign in to comment.