Skip to content

Commit

Permalink
feat(core): lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
PagoNxt-Trade committed Dec 1, 2022
1 parent 35b9256 commit 5c0bc4b
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 68 deletions.
102 changes: 52 additions & 50 deletions docs/guides/2-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,58 +78,60 @@ Usage:

```bash
spectral lint ./reference/**/*.oas*.{json,yml,yaml} --ruleset mycustomruleset.js --scoring-config ./scoringFile.json
```
or
```bash
spectral lint ./reference/**/*.oas*.{json,yml,yaml} -r mycustomruleset.js -s ./scoringFile.json
```


Heres an example of this scoringFile config file:

```
{
"scoringSubtract":
{
"error":
{
1:55,
2:65,
3:75,
6:85,
10:95
}
"warn":
{
1:3,
2:7,
3:10,
6:15,
10:18
}
},
"scoringLetter":
{
"A":75,
"B":65,
"C":55,
"D":45,
"E":0
},
"threshold":50,
"warningsSubtract": true,
"uniqueErrors": false
}
```
```

or

```bash
spectral lint ./reference/**/*.oas*.{json,yml,yaml} -r mycustomruleset.js -s ./scoringFile.json
```

Heres an example of this scoringFile config file:

```
{
"scoringSubtract":
{
"error":
{
1:55,
2:65,
3:75,
6:85,
10:95
}
"warn":
{
1:3,
2:7,
3:10,
6:15,
10:18
}
},
"scoringLetter":
{
"A":75,
"B":65,
"C":55,
"D":45,
"E":0
},
"threshold":50,
"warningsSubtract": true,
"uniqueErrors": false
}
```

Where:
- scoringSubtract : An object with a key/value pair objects for every result level we want to subtract percentage, with the percentage to subtract from number of results on every result type
- scoringLetter : An object with key/value pairs with scoring letter and scoring percentage, that the result must be greater , for this letter
- threshold : A number with minimum percentage value to provide valid the file we are checking
- warningsSubtract : A boolean to setup if accumulate the result types to less the scoring percentage or stop counting on most critical result types
- uniqueErrors : A boolean to setup a count with unique errors or with all of them

Example:
- scoringSubtract : An object with a key/value pair objects for every result level we want to subtract percentage, with the percentage to subtract from number of results on every result type
- scoringLetter : An object with key/value pairs with scoring letter and scoring percentage, that the result must be greater , for this letter
- threshold : A number with minimum percentage value to provide valid the file we are checking
- warningsSubtract : A boolean to setup if accumulate the result types to less the scoring percentage or stop counting on most critical result types
- uniqueErrors : A boolean to setup a count with unique errors or with all of them

Example:

With previous scoring config file, if we have:

Expand All @@ -139,7 +141,7 @@ Where:
4 errors, the scoring is 25% and E
and so on

Output:
Output:

Below your output log you can see the scoring, like:

Expand Down
21 changes: 12 additions & 9 deletions packages/cli/src/commands/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import { formatOutput, writeOutput } from '../services/output';
import { FailSeverity, ILintConfig, OutputFormat } from '../services/config';

import { CLIError } from '../errors';
import { ScoringConfig } from "./../formatters/types";
import { ScoringConfig } from './../formatters/types';
import {
getScoringConfig,
getScoringLevel,
groupBySource,
getCountsBySeverity,
uniqueErrors
uniqueErrors,
} from '../formatters//utils';

const formatOptions = Object.values(OutputFormat);
Expand Down Expand Up @@ -222,7 +222,7 @@ const lintCommand: CommandModule = {
return writeOutput(formattedOutput, output?.[f] ?? '<stdout>');
}),
);
//process.stdout.write(`${scoringThresholdNotEnough(results, scoringConfigData)}\n`);

if (results.length > 0) {
process.exit(
scoringThresholdNotEnough(results, scoringConfigData) ? 1 : severeEnoughToFail(results, failSeverity) ? 1 : 0,
Expand All @@ -232,6 +232,9 @@ const lintCommand: CommandModule = {
process.stdout.write(
`No results with a severity of '${failSeverity}' ${isErrorSeverity ? '' : 'or higher '}found!\n`,
);
if (scoringConfig !== void 0) {
process.stdout.write(`SCORING: (100%)\nPASSED!`);
}
}
} catch (ex) {
fail(isError(ex) ? ex : new Error(String(ex)), config.verbose === true);
Expand Down Expand Up @@ -302,12 +305,12 @@ const scoringThresholdNotEnough = (results: IRuleResult[], scoringConfig: Scorin
groupedUniqueResults = { ...groupBySource(uniqueErrors(results)) };
}
return (
scoringConfig.threshold >
getScoringLevel(
getCountsBySeverity(groupedUniqueResults),
scoringConfig.scoringSubtract,
scoringConfig.warningsSubtract,
)
scoringConfig.threshold >
getScoringLevel(
getCountsBySeverity(groupedUniqueResults),
scoringConfig.scoringSubtract,
scoringConfig.warningsSubtract,
)
);
}
return false;
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/formatters/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Formatter, FormatterOptions } from './types';

import { groupBySource, uniqueErrors, getCountsBySeverity, getScoringText } from './utils';

const { version } = require('../../package.json');
const version = process.env.npm_package_version;

export const json: Formatter = (results: ISpectralDiagnostic[], options: FormatterOptions) => {
let spectralVersion = '';
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/formatters/pretty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import {
groupBySource,
getScoringText,
getCountsBySeverity,
uniqueErrors
uniqueErrors,
} from './utils';

const { version } = require('../../package.json');
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/formatters/stylish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ import {
groupBySource,
getScoringText,
getCountsBySeverity,
uniqueErrors
uniqueErrors,
} from './utils';

const { version } = require('../../package.json');
const version = process.env.npm_package_version;

// -----------------------------------------------------------------------------
// Helpers
Expand Down
13 changes: 8 additions & 5 deletions packages/cli/src/formatters/utils/getScoring.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SEVERITY_MAP } from '@stoplight/spectral-core';
import { DiagnosticSeverity } from '@stoplight/types';
import { ScoringConfig, ScoringTable } from '../types';
import { ScoringConfig, ScoringTable, ScoringSubtract } from '../types';
import * as path from '@stoplight/path';
import fs from 'fs';

Expand Down Expand Up @@ -32,10 +32,13 @@ export const getScoringLevel = (
if (scoringSubtract[scoringKey] !== void 0) {
if (scoring < 100 && !warningsSubtract) return;
let subtractValue = 0;
Object.keys(scoringSubtract[scoringKey]).forEach(
subtractKey =>
(subtractValue = issuesCount[key] >= subtractKey ? scoringSubtract[scoringKey][subtractKey] : subtractValue),
);
Object.keys(scoringSubtract[scoringKey] as ScoringSubtract[]).forEach((subtractKey: string): void => {
subtractValue = (
issuesCount[key] >= subtractKey
? (scoringSubtract[scoringKey] as ScoringSubtract[])[subtractKey]
: subtractValue
) as number;
});
scoring -= subtractValue;
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,4 @@ module.exports = oas;
{bin} lint {document} --ruleset "{asset:ruleset}" --scoring-config "{asset:scoring-config.json}"
====stdout====
No results with a severity of 'error' found!
SCORING: (100%)PASSED!

0 comments on commit 5c0bc4b

Please sign in to comment.