-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to output SARIF Signed-off-by: Mathieu Rul <mathroule@gmail.com>
- Loading branch information
Showing
8 changed files
with
127 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
require 'json' | ||
|
||
module MarkdownLint | ||
# SARIF formatter | ||
# | ||
# @see https://docs.oasis-open.org/sarif/sarif/v2.1.0/sarif-v2.1.0.html | ||
class SarifFormatter | ||
class << self | ||
def generate(rules, results) | ||
matched_rules_id = results.map { |result| result['rule'] }.uniq | ||
matched_rules = rules.select { |id, _| matched_rules_id.include?(id) } | ||
JSON.generate(generate_sarif(matched_rules, results)) | ||
end | ||
|
||
def generate_sarif(rules, results) | ||
{ | ||
:'$schema' => 'https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json', | ||
:version => '2.1.0', | ||
:runs => [ | ||
{ | ||
:tool => { | ||
:driver => { | ||
:name => 'Markdown lint', | ||
:version => MarkdownLint::VERSION, | ||
:informationUri => 'https://github.com/markdownlint/markdownlint', | ||
:rules => generate_sarif_rules(rules), | ||
}, | ||
}, | ||
:results => generate_sarif_results(rules, results), | ||
} | ||
], | ||
} | ||
end | ||
|
||
def generate_sarif_rules(rules) | ||
rules.map do |id, rule| | ||
{ | ||
:id => id, | ||
:name => rule.aliases.first.split('-').map(&:capitalize).join, | ||
:defaultConfiguration => { | ||
:level => 'note', | ||
}, | ||
:properties => { | ||
:description => rule.description, | ||
:tags => rule.tags, | ||
:queryURI => rule.docs_url, | ||
}, | ||
:shortDescription => { | ||
:text => rule.description, | ||
}, | ||
:fullDescription => { | ||
:text => rule.description, | ||
}, | ||
:helpUri => rule.docs_url, | ||
:help => { | ||
:text => "More info: #{rule.docs_url}", | ||
:markdown => "[More info](#{rule.docs_url})", | ||
}, | ||
} | ||
end | ||
end | ||
|
||
def generate_sarif_results(rules, results) | ||
results.map do |result| | ||
{ | ||
:ruleId => result['rule'], | ||
:ruleIndex => rules.find_index { |id, _| id == result['rule'] }, | ||
:message => { | ||
:text => "#{result['rule']} - #{result['description']}", | ||
}, | ||
:locations => [ | ||
{ | ||
:physicalLocation => { | ||
:artifactLocation => { | ||
:uri => result['filename'], | ||
:uriBaseId => '%SRCROOT%', | ||
}, | ||
:region => { | ||
:startLine => result['line'], | ||
}, | ||
}, | ||
} | ||
], | ||
} | ||
end | ||
end | ||
end | ||
end | ||
end |
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 @@ | ||
[{"filename":"(stdin)","line":1,"rule":"MD002","aliases":["first-header-h1"],"description":"First header should be a top level header","docs":"https://github.com/markdownlint/markdownlint/blob/master/docs/RULES.md#md002---first-header-should-be-a-top-level-header"}] |
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 @@ | ||
[] |
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 @@ | ||
{"$schema":"https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json","version":"2.1.0","runs":[{"tool":{"driver":{"name":"Markdown lint","version":"0.12.0","informationUri":"https://github.com/markdownlint/markdownlint","rules":[{"id":"MD002","name":"FirstHeaderH1","defaultConfiguration":{"level":"note"},"properties":{"description":"First header should be a top level header","tags":["headers"],"queryURI":"https://github.com/markdownlint/markdownlint/blob/master/docs/RULES.md#md002---first-header-should-be-a-top-level-header"},"shortDescription":{"text":"First header should be a top level header"},"fullDescription":{"text":"First header should be a top level header"},"helpUri":"https://github.com/markdownlint/markdownlint/blob/master/docs/RULES.md#md002---first-header-should-be-a-top-level-header","help":{"text":"More info: https://github.com/markdownlint/markdownlint/blob/master/docs/RULES.md#md002---first-header-should-be-a-top-level-header","markdown":"[More info](https://github.com/markdownlint/markdownlint/blob/master/docs/RULES.md#md002---first-header-should-be-a-top-level-header)"}}]}},"results":[{"ruleId":"MD002","ruleIndex":0,"message":{"text":"MD002 - First header should be a top level header"},"locations":[{"physicalLocation":{"artifactLocation":{"uri":"(stdin)","uriBaseId":"%SRCROOT%"},"region":{"startLine":1}}}]}]}]} |
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 @@ | ||
{"$schema":"https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json","version":"2.1.0","runs":[{"tool":{"driver":{"name":"Markdown lint","version":"0.12.0","informationUri":"https://github.com/markdownlint/markdownlint","rules":[]}},"results":[]}]} |
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