-
-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(formatters): add markdown formatter (#2662)
* feat(formatters): add markdown formatter - Format results as a markdown table - New utility function getDocumentationUrl.ts that builds the documentation's url from the rule or the ruleset * feat(cli): add markdown output to the cli * chore(deps): use custom markdown table generator markdown-table-ts is not compatible with node12 --------- Co-authored-by: jb.muscat <jb.muscat@criteo.com> Co-authored-by: Nauman <mnaumanali94@gmail.com>
- Loading branch information
1 parent
1895c96
commit b5edf5e
Showing
14 changed files
with
236 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
| Code | Path | Message | Severity | Start | End | Source | | ||
| ---------------------------------------------------------------------- | ---------------------------- | -------------------------------------------- | -------- | ----- | ---- | --------------------------------------------------- | | ||
| [operation-description](https://rule-documentation-url.com) | paths.\/pets.get.description | paths.\/pets.get.description is not truthy | Error | 1:0 | 10:1 | .\/src\/\_\_tests\_\_\/fixtures\/petstore.oas2.yaml | | ||
| [operation-tags](https://ruleset-documentation-url.com#operation-tags) | paths.\/pets.get.tags | paths.\/pets.get.tags is not truthy | Warning | 11:0 | 20:1 | .\/src\/\_\_tests\_\_\/fixtures\/petstore.oas2.yaml | | ||
| rule-from-other-ruleset | paths | i should not have any documentation url link | Warning | 21:0 | 30:1 | .\/src\/\_\_tests\_\_\/fixtures\/petstore.oas2.yaml | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { DiagnosticSeverity } from '@stoplight/types'; | ||
import type { IRuleResult } from '@stoplight/spectral-core'; | ||
import { FormatterContext } from '../types'; | ||
import { markdown } from '../markdown'; | ||
|
||
const results: IRuleResult[] = [ | ||
{ | ||
code: 'operation-description', | ||
message: 'paths./pets.get.description is not truthy', | ||
path: ['paths', '/pets', 'get', 'description'], | ||
severity: DiagnosticSeverity.Error, | ||
source: './src/__tests__/fixtures/petstore.oas2.yaml', | ||
range: { | ||
start: { | ||
line: 1, | ||
character: 0, | ||
}, | ||
end: { | ||
line: 10, | ||
character: 1, | ||
}, | ||
}, | ||
}, | ||
{ | ||
code: 'operation-tags', | ||
message: 'paths./pets.get.tags is not truthy', | ||
path: ['paths', '/pets', 'get', 'tags'], | ||
severity: DiagnosticSeverity.Warning, | ||
source: './src/__tests__/fixtures/petstore.oas2.yaml', | ||
range: { | ||
start: { | ||
line: 11, | ||
character: 0, | ||
}, | ||
end: { | ||
line: 20, | ||
character: 1, | ||
}, | ||
}, | ||
}, | ||
{ | ||
code: 'rule-from-other-ruleset', | ||
message: 'i should not have any documentation url link', | ||
path: ['paths'], | ||
severity: DiagnosticSeverity.Warning, | ||
source: './src/__tests__/fixtures/petstore.oas2.yaml', | ||
range: { | ||
start: { | ||
line: 21, | ||
character: 0, | ||
}, | ||
end: { | ||
line: 30, | ||
character: 1, | ||
}, | ||
}, | ||
}, | ||
]; | ||
|
||
const context = { | ||
ruleset: { | ||
rules: { | ||
'operation-description': { | ||
documentationUrl: 'https://rule-documentation-url.com', | ||
owner: { | ||
definition: { | ||
documentationUrl: 'https://ruleset-documentation-url.com', | ||
}, | ||
}, | ||
}, | ||
'operation-tags': { | ||
documentationUrl: '', //nothing | ||
owner: { | ||
definition: { | ||
documentationUrl: 'https://ruleset-documentation-url.com', | ||
}, | ||
}, | ||
}, | ||
'rule-from-other-ruleset': { | ||
documentationUrl: '', //nothing | ||
owner: { | ||
definition: { | ||
documentationUrl: '', //nothing | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} as unknown as FormatterContext; | ||
|
||
const expectedMd = String.raw` | ||
| Code | Path | Message | Severity | Start | End | Source | | ||
| ---------------------------------------------------------------------- | ---------------------------- | -------------------------------------------- | -------- | ----- | ---- | --------------------------------------------------- | | ||
| [operation-description](https://rule-documentation-url.com) | paths.\/pets.get.description | paths.\/pets.get.description is not truthy | Error | 1:0 | 10:1 | .\/src\/\_\_tests\_\_\/fixtures\/petstore.oas2.yaml | | ||
| [operation-tags](https://ruleset-documentation-url.com#operation-tags) | paths.\/pets.get.tags | paths.\/pets.get.tags is not truthy | Warning | 11:0 | 20:1 | .\/src\/\_\_tests\_\_\/fixtures\/petstore.oas2.yaml | | ||
| rule-from-other-ruleset | paths | i should not have any documentation url link | Warning | 21:0 | 30:1 | .\/src\/\_\_tests\_\_\/fixtures\/petstore.oas2.yaml | | ||
`; | ||
|
||
describe('Markdown formatter', () => { | ||
test('should format as markdown table', () => { | ||
const CRLF = '\r\n'; | ||
const md = markdown(results, { failSeverity: DiagnosticSeverity.Warning }, context); | ||
|
||
// We normalize the line-breaks and trailing whitespaces because the expected markdown file is can be created on a Windows machine | ||
// and prettier instert a line break automatically | ||
const normalizedMd = md.replace(new RegExp(CRLF, 'g'), '\n').trim(); | ||
const normalizedExpectedMd = expectedMd.replace(new RegExp(CRLF, 'g'), '\n').trim(); | ||
|
||
expect(normalizedMd).toEqual(normalizedExpectedMd); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { printPath, PrintStyle } from '@stoplight/spectral-runtime'; | ||
import { Formatter, FormatterContext } from './types'; | ||
import { groupBySource } from './utils'; | ||
import { DiagnosticSeverity } from '@stoplight/types'; | ||
import markdownEscape from 'markdown-escape'; | ||
import { getRuleDocumentationUrl } from './utils/getDocumentationUrl'; | ||
|
||
export const markdown: Formatter = (results, { failSeverity }, ctx?: FormatterContext) => { | ||
const groupedResults = groupBySource(results); | ||
|
||
const lines: string[][] = []; | ||
for (const [source, validationResults] of Object.entries(groupedResults)) { | ||
validationResults.sort((a, b) => a.range.start.line - b.range.start.line); | ||
|
||
if (validationResults.length > 0) { | ||
const filteredValidationResults = validationResults.filter(result => result.severity <= failSeverity); | ||
|
||
for (const result of filteredValidationResults) { | ||
const ruleDocumentationUrl = getRuleDocumentationUrl(result.code, ctx); | ||
const codeWithOptionalLink = | ||
ruleDocumentationUrl != null | ||
? `[${result.code.toString()}](${ruleDocumentationUrl})` | ||
: result.code.toString(); | ||
const escapedPath = markdownEscape(printPath(result.path, PrintStyle.Dot)); | ||
const escapedMessage = markdownEscape(result.message); | ||
const severityString = DiagnosticSeverity[result.severity]; | ||
const start = `${result.range.start.line}:${result.range.start.character}`; | ||
const end = `${result.range.end.line}:${result.range.end.character}`; | ||
const escapedSource = markdownEscape(source); | ||
lines.push([codeWithOptionalLink, escapedPath, escapedMessage, severityString, start, end, escapedSource]); | ||
} | ||
} | ||
} | ||
|
||
const headers = ['Code', 'Path', 'Message', 'Severity', 'Start', 'End', 'Source']; | ||
return createMdTable(headers, lines); | ||
}; | ||
|
||
function createMdTable(headers: string[], lines: string[][]): string { | ||
//find lenght of each column | ||
const columnLengths = headers.map((_, i) => Math.max(...lines.map(line => line[i].length), headers[i].length)); | ||
|
||
let string = ''; | ||
//create markdown table header | ||
string += '|'; | ||
for (const header of headers) { | ||
string += ` ${header}`; | ||
string += ' '.repeat(columnLengths[headers.indexOf(header)] - header.length); | ||
string += ' |'; | ||
} | ||
|
||
//create markdown table rows delimiter | ||
string += '\n|'; | ||
for (const _ of headers) { | ||
string += ' '; | ||
string += '-'.repeat(columnLengths[headers.indexOf(_)]); | ||
string += ' |'; | ||
} | ||
|
||
//create markdown table rows | ||
for (const line of lines) { | ||
string += '\n|'; | ||
for (const cell of line) { | ||
string += ` ${cell}`; | ||
string += ' '.repeat(columnLengths[line.indexOf(cell)] - cell.length); | ||
string += ' |'; | ||
} | ||
} | ||
|
||
return string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { FormatterContext } from '../types'; | ||
|
||
/// Returns the documentation URL, either directly from the rule or by combining the ruleset documentation URL with the rule code. | ||
export function getRuleDocumentationUrl(ruleCode: string | number, ctx?: FormatterContext): string | undefined { | ||
if (!ctx?.ruleset) { | ||
return undefined; | ||
} | ||
|
||
const rule = ctx.ruleset.rules[ruleCode.toString()]; | ||
//if rule.documentationUrl is not null and not empty and not undefined, return it | ||
if (rule.documentationUrl != null && rule.documentationUrl) { | ||
return rule.documentationUrl; | ||
} | ||
|
||
//otherwise use the ruleset documentationUrl and append the rulecode as an anchor | ||
const rulesetDocumentationUrl = rule.owner?.definition.documentationUrl; | ||
if (rulesetDocumentationUrl != null && rulesetDocumentationUrl) { | ||
return `${rulesetDocumentationUrl}#${ruleCode}`; | ||
} | ||
|
||
return undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters