-
Notifications
You must be signed in to change notification settings - Fork 73
WIP: Add Sarif output support to FSharpLint.Console #702
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Numpsy
wants to merge
1
commit into
fsprojects:master
Choose a base branch
from
Numpsy:users/rw/sarif
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,103 @@ | ||
module internal Sarif | ||
|
||
open FSharpLint.Framework | ||
open System.IO | ||
open System | ||
open Microsoft.CodeAnalysis.Sarif | ||
open Microsoft.CodeAnalysis.Sarif.Writers | ||
open FSharpLint.Console.Output | ||
|
||
let writeReport (results: Suggestion.LintWarning list) (codeRoot: string option) (report: string) (logger: IOutput) = | ||
try | ||
let codeRoot = | ||
match codeRoot with | ||
| None -> Directory.GetCurrentDirectory() |> Uri | ||
| Some root -> Path.GetFullPath root |> Uri | ||
|
||
// Construct full path to ensure path separators are normalized. | ||
let report = Path.GetFullPath report | ||
// Ensure the parent directory exists | ||
let reportFile = FileInfo(report) | ||
reportFile.Directory.Create() | ||
|
||
let driver = ToolComponent() | ||
driver.Name <- "FSharpLint.Console" | ||
driver.InformationUri <- Uri("https://fsprojects.github.io/FSharpLint/") | ||
driver.Version <- string<Version> (System.Reflection.Assembly.GetExecutingAssembly().GetName().Version) | ||
let tool = Tool() | ||
tool.Driver <- driver | ||
let run = Run() | ||
run.Tool <- tool | ||
|
||
use sarifLogger = | ||
new SarifLogger( | ||
report, | ||
logFilePersistenceOptions = | ||
(FilePersistenceOptions.PrettyPrint ||| FilePersistenceOptions.ForceOverwrite), | ||
run = run, | ||
levels = BaseLogger.ErrorWarningNote, | ||
kinds = BaseLogger.Fail, | ||
closeWriterOnDispose = true | ||
) | ||
|
||
sarifLogger.AnalysisStarted() | ||
|
||
for analyzerResult in results do | ||
let reportDescriptor = ReportingDescriptor() | ||
reportDescriptor.Id <- analyzerResult.RuleIdentifier | ||
reportDescriptor.Name <- analyzerResult.RuleName | ||
|
||
(* | ||
analyzerResult.ShortDescription | ||
|> Option.iter (fun shortDescription -> | ||
reportDescriptor.ShortDescription <- | ||
MultiformatMessageString(shortDescription, shortDescription, dict []) | ||
) | ||
*) | ||
|
||
let helpUri = $"https://fsprojects.github.io/FSharpLint/how-tos/rules/%s{analyzerResult.RuleIdentifier}.html" | ||
reportDescriptor.HelpUri <- Uri(helpUri) | ||
|
||
let result = Result() | ||
result.RuleId <- reportDescriptor.Id | ||
|
||
(* | ||
result.Level <- | ||
match analyzerResult.Message.Severity with | ||
| Severity.Info -> FailureLevel.Note | ||
| Severity.Hint -> FailureLevel.Note | ||
| Severity.Warning -> FailureLevel.Warning | ||
| Severity.Error -> FailureLevel.Error | ||
*) | ||
result.Level <- FailureLevel.Warning | ||
|
||
let msg = Message() | ||
msg.Text <- analyzerResult.Details.Message | ||
result.Message <- msg | ||
|
||
let physicalLocation = PhysicalLocation() | ||
|
||
physicalLocation.ArtifactLocation <- | ||
let al = ArtifactLocation() | ||
al.Uri <- codeRoot.MakeRelativeUri(Uri(analyzerResult.Details.Range.FileName)) | ||
al | ||
|
||
physicalLocation.Region <- | ||
let r = Region() | ||
r.StartLine <- analyzerResult.Details.Range.StartLine | ||
r.StartColumn <- analyzerResult.Details.Range.StartColumn + 1 | ||
r.EndLine <- analyzerResult.Details.Range.EndLine | ||
r.EndColumn <- analyzerResult.Details.Range.EndColumn + 1 | ||
r | ||
|
||
let location: Location = Location() | ||
location.PhysicalLocation <- physicalLocation | ||
result.Locations <- [| location |] | ||
|
||
sarifLogger.Log(reportDescriptor, result, System.Nullable()) | ||
|
||
sarifLogger.AnalysisStopped(RuntimeConditions.None) | ||
|
||
sarifLogger.Dispose() | ||
with ex -> | ||
logger.WriteError($"Could not write sarif to %s{report}: %s{ex.Message}") |
This file contains hidden or 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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
Argu | ||
FSharp.Compiler.Service | ||
FSharp.Core | ||
Microsoft.SourceLink.GitHub | ||
Microsoft.SourceLink.GitHub | ||
Sarif.Sdk import_targets: false | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This
import_targets: false
is because I was initially getting this error from builds after adding the nuget package reference:And I'm not sure why (I haven't used any of that eventregister stuff myself for years, and don't recall the defaults of it)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add this info in the commit msg