Skip to content

Commit

Permalink
feat: be able to generate a sonar report
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmanuelDemey committed Mar 21, 2023
1 parent 27b6f57 commit 78d5f82
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 3 deletions.
11 changes: 9 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@ At the end of the readme, we will explain how to generate the lighthouse and eco
| srcEcoIndex | string | Option is used for defined ecoIndex reports path |
| srcLighthouse | string | Option is used for defined lighthouse reports path |
| h | boolean | Option is used for see informations cli |
| reports | string | Option is used for defined format reports after task unique value possible used is "html" |
| reports | string | Option is used for defined the format of the generated report. Possible vaue "html" or "sonar" |
| lang | string | Option is used for translated report values possible used is "fr-FR" or "en-GB" default is "en-GB" |
| v | boolean | Option is used for verbose task |
| config | string | Option is used for define configuration file |
| pass | number | Option is used for define limit pass |
| fail | number | Option is used for define limit fail |
| m | boolean | Option is used for minify file output it's true by default |
| sonarFilePath | string | Option is used when generating the sonar report, in order to make the issue visible on SonarCloud |

## Example usage

```bash
node ./cli.js --srcLighthouse="C:\Workspace\reports\lighthouse" --srcEcoIndex="C:\Workspace\reports\ecoindex" --reports="html"
node ./src/cli.js --srcLighthouse="C:\Workspace\reports\lighthouse" --srcEcoIndex="C:\Workspace\reports\ecoindex" --reports="html"
```

You can also used this module programmatically
Expand Down Expand Up @@ -88,3 +89,9 @@ cd cypress-demo
npm i
npx cypress run -b chrome
```

## Sonar report

This tool can also generate a external sonar report you can add to the Sonar configuration (via the sonar.externalIssuesReportPaths option).

You need to define the path to one of your file managed by Sonar, in order to make the rule visible in Sonar Cloud.
6 changes: 6 additions & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const optionDefinitions = [
{ name: "pass", type: Number, multiple: false },
{ name: "fail", type: Number, multiple: false },
{ name: "Minify", alias: "m", type: Boolean },
{ name: "sonarFilePath", type: String },
];

const sections = [
Expand Down Expand Up @@ -75,6 +76,11 @@ const sections = [
typeLabel: "{underline bool}",
description: "used minify file",
},
{
name: "sonarFilePath",
typeLabel: "{underline string}",
description: "the file to a static file managed by sonar",
},
],
},
];
Expand Down
5 changes: 4 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const aggregatorServiceLighthouse = require("./lighthouse/aggregatorService");
const aggregatorServiceEcoIndex = require("./ecoIndex/aggregatorService");
const aggregatorGlobalService = require("./globlalAggregation/aggregatorService");
const { generateReports } = require("./reporters/generatorReports");
const { generateReports, generateReportsSonar } = require("./reporters/generatorReports");

module.exports = async (options) => {
if (!options?.pass) {
Expand All @@ -17,6 +17,9 @@ module.exports = async (options) => {
if (options.reports === "html") {
await generateReports(options, resultsGlobal);
}
if (options.reports === "sonar") {
await generateReportsSonar(options, resultsGlobal);
}

return resultsGlobal;
};
55 changes: 55 additions & 0 deletions src/reporters/generatorReports.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,60 @@ const computeCssClassForMetrics = require("./utils/computeCssClassForMetrics");
const pageInErrorOrWarning = require("./utils/displayPageErrorIcon");
const { statusGreen } = require("./utils/statusGreen");
const { statusPerPage } = require("./utils/statusPerPage");
const { cwd } = require("process");
const { dirname } = require("path");



const generateReportsSonar = async (options, results) => {
if (!options.sonarFilePath) {
console.error("You should define the sonarFilePath property");
process.exit(1);
}
if (!options.outputPath) {
console.error("You should define the outputPath property");
process.exit(1);
}

const issues = [];

const addIssues = (value, ruleId, name) => {
if (options.fail > value) {
issues.push({
engineId: "eco-index",
ruleId,
severity: "MAJOR",
type: "BUG",
primaryLocation: {
message: `You ${name} (${value}) is below the configured threshold (${options.fail})`,
filePath: options.sonarFilePath,
},
});
} else {
if (options.fail <= value && value < options.pass) {
issues.push({
engineId: "eco-index",
ruleId,
severity: "MINOR",
type: "BUG",
primaryLocation: {
message: `You ${name} (${value}) is below the configured threshold (${options.pass})`,
filePath: options.sonarFilePath,
},
});
}
}
};

addIssues(results.ecoIndex, "eco-index-below-threshold", "ecoindex");
addIssues(results.performance, "performance-below-threshold", "performance");
addIssues(results.accessibility, "accessibility-below-threshold", "accessibility");
addIssues(results.bestPractices, "bestPractices-below-threshold", "bestPractices");

fs.mkdirSync(dirname(path.resolve(cwd(), options.outputPath)), {recursive: true});
fs.writeFileSync(path.resolve(cwd(), options.outputPath), JSON.stringify({ issues }));
};

const generateReports = async (options, results) => {
if (!options?.pass) {
options.pass = 90;
Expand Down Expand Up @@ -341,6 +395,7 @@ const generateCSSClassBasedOnValue = (value, { pass, fail }) => {

module.exports = {
generateReports,
generateReportsSonar,
populateTemplatePerformance,
populateTemplateAccessibility,
populateTemplateBestPractices,
Expand Down

0 comments on commit 78d5f82

Please sign in to comment.