-
Notifications
You must be signed in to change notification settings - Fork 93
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
Adding command policy to multitool #2118
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using Microsoft.CodeAnalysis.Sarif.Driver; | ||
using Newtonsoft.Json; | ||
|
||
namespace Microsoft.CodeAnalysis.Sarif.Multitool | ||
{ | ||
public class ApplyPolicyCommand : CommandBase | ||
{ | ||
private readonly IFileSystem _fileSystem; | ||
|
||
public ApplyPolicyCommand(IFileSystem fileSystem = null) | ||
{ | ||
_fileSystem = fileSystem ?? FileSystem.Instance; | ||
} | ||
|
||
public int Run(ApplyPolicyOptions applyPolicyOptions) | ||
{ | ||
try | ||
{ | ||
Console.WriteLine($"Applying policy '{applyPolicyOptions.InputFilePath}' => '{applyPolicyOptions.OutputFilePath}'..."); | ||
Stopwatch w = Stopwatch.StartNew(); | ||
|
||
bool valid = ValidateOptions(applyPolicyOptions); | ||
if (!valid) { return FAILURE; } | ||
|
||
SarifLog actualLog = ReadSarifFile<SarifLog>(_fileSystem, applyPolicyOptions.InputFilePath); | ||
|
||
actualLog.ApplyPolicies(); | ||
|
||
string fileName = CommandUtilities.GetTransformedOutputFileName(applyPolicyOptions); | ||
|
||
Formatting formatting = applyPolicyOptions.PrettyPrint | ||
? Formatting.Indented | ||
: Formatting.None; | ||
|
||
WriteSarifFile(_fileSystem, actualLog, fileName, formatting); | ||
|
||
w.Stop(); | ||
Console.WriteLine($"Rewrite completed in {w.Elapsed}."); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine(ex); | ||
return FAILURE; | ||
} | ||
|
||
return SUCCESS; | ||
} | ||
|
||
private bool ValidateOptions(ApplyPolicyOptions applyPolicyOptions) | ||
{ | ||
bool valid = true; | ||
|
||
valid &= applyPolicyOptions.Validate(); | ||
|
||
valid &= DriverUtilities.ReportWhetherOutputFileCanBeCreated(applyPolicyOptions.OutputFilePath, applyPolicyOptions.Force, _fileSystem); | ||
|
||
return valid; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using CommandLine; | ||
using Microsoft.CodeAnalysis.Sarif.Driver; | ||
|
||
namespace Microsoft.CodeAnalysis.Sarif.Multitool | ||
{ | ||
[Verb("apply-policy", HelpText = "Apply policies from SARIF log.")] | ||
public class ApplyPolicyOptions : SingleFileOptionsBase | ||
{ | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Will there in future be an option to specify one out of multiple policies present in the file? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to rename. But... ok, this is a "requirements" question that you should discuss with @michaelcfanning. As the code stands, you apply policies in order, and if two policies refer to the same rule, the last one wins (as your test verifies). Is Michael sure that's what we want? In reply to: 506565036 [](ancestors = 506565036,506563829) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Definitely what we specified for now. In reply to: 506571247 [](ancestors = 506571247,506565036,506563829) |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.IO; | ||
eddynaka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace Microsoft.CodeAnalysis.Sarif.Multitool | ||
{ | ||
public class ApplyPolicyCommandTests | ||
{ | ||
private static readonly ResourceExtractor Extractor = new ResourceExtractor(typeof(ApplyPolicyCommandTests)); | ||
|
||
[Fact] | ||
public void WhenInputContainsOnePolicy_ShouldSucceed() | ||
{ | ||
string path = "WithPolicy.sarif"; | ||
File.WriteAllText(path, Extractor.GetResourceText($"ApplyPolicyCommand.{path}")); | ||
|
||
// Verify log loads, has correct Result count, and spot check a Result | ||
SarifLog log = ExecuteTest(path); | ||
log.Runs[0].Results.Count.Should().Be(1); | ||
log.Runs[0].Results[0].Level.Should().Be(FailureLevel.Error); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to delete the files. They are written to the test binaries directory and overwritten on each run (and deleted on a clean build). #Closed |
||
|
||
[Fact] | ||
public void WhenInputContainsMultiplePolicies_ShouldApplyPoliciesInOrder() | ||
{ | ||
string path = "WithPolicy2.sarif"; | ||
File.WriteAllText(path, Extractor.GetResourceText($"ApplyPolicyCommand.{path}")); | ||
|
||
// Verify log loads, has correct Result count, and spot check a Result | ||
SarifLog log = ExecuteTest(path); | ||
log.Runs[0].Results.Count.Should().Be(1); | ||
log.Runs[0].Results[0].Level.Should().Be(FailureLevel.Note); | ||
} | ||
|
||
private SarifLog ExecuteTest(string path) | ||
{ | ||
var options = new ApplyPolicyOptions | ||
{ | ||
InputFilePath = path, | ||
OutputFilePath = path, | ||
Force = true | ||
}; | ||
|
||
// Verify command returned success | ||
int returnCode = new ApplyPolicyCommand().Run(options); | ||
returnCode.Should().Be(0); | ||
|
||
// Verify SARIF output log exists | ||
File.Exists(path).Should().BeTrue(); | ||
|
||
return SarifLog.Load(path); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/sarif-2.1.0-rtm.1", | ||
"version": "2.1.0", | ||
"runs": [ | ||
{ | ||
"tool": { | ||
"driver": { | ||
"name": "Test", | ||
"version": "1.0.0", | ||
"rules": [ | ||
{ | ||
"id": "TEST0001", | ||
"name": "Test", | ||
"shortDescription": { | ||
"text": "Test description." | ||
}, | ||
"messageStrings": { | ||
"default": { | ||
"text": "Test description." | ||
} | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
"results": [ | ||
{ | ||
"ruleId": "TEST0001", | ||
"ruleIndex": 0, | ||
"message": { | ||
"text": "Test text." | ||
} | ||
} | ||
], | ||
"columnKind": "utf16CodeUnits", | ||
"policies": [ | ||
{ | ||
"name": "TEST Policy", | ||
"version": "1.0.0", | ||
"rules": [ | ||
{ | ||
"id": "TEST0001", | ||
"defaultConfiguration": { | ||
"level": "error" | ||
} | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/sarif-2.1.0-rtm.1", | ||
"version": "2.1.0", | ||
"runs": [ | ||
{ | ||
"tool": { | ||
"driver": { | ||
"name": "Test", | ||
"version": "1.0.0", | ||
"rules": [ | ||
{ | ||
"id": "TEST0001", | ||
"name": "Test", | ||
"shortDescription": { | ||
"text": "Test description." | ||
}, | ||
"messageStrings": { | ||
"default": { | ||
"text": "Test description." | ||
} | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
"results": [ | ||
{ | ||
"ruleId": "TEST0001", | ||
"ruleIndex": 0, | ||
"message": { | ||
"text": "Test text." | ||
} | ||
} | ||
], | ||
"columnKind": "utf16CodeUnits", | ||
"policies": [ | ||
{ | ||
"name": "TEST Policy", | ||
"version": "1.0.0", | ||
"rules": [ | ||
{ | ||
"id": "TEST0001", | ||
"defaultConfiguration": { | ||
"level": "error" | ||
} | ||
} | ||
] | ||
}, | ||
{ | ||
"name": "TEST Policy 2", | ||
"version": "1.0.0", | ||
"rules": [ | ||
{ | ||
"id": "TEST0001", | ||
"defaultConfiguration": { | ||
"level": "note" | ||
} | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] | ||
} |
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.
You know, we have this transformation from "common options" to Newtonsoft Formatting in many places. This could be an extension method on CommonOptions (it would be called like this:
applyPolicyOptions.GetFormatting()
. Ask @michaelcfanning if this is a good time to do that. #ResolvedThere 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.
I think we can do a separate pr for that. So i can tackle all places, what do you think?
In reply to: 506574244 [](ancestors = 506574244)
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.
Sure, that's fine.
In reply to: 506659476 [](ancestors = 506659476,506574244)
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.
The new pr with this: #2121 #Resolved