Skip to content
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

Enable export rules in multitool #2052

Merged
9 commits merged into from
Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
506 changes: 1 addition & 505 deletions docs/Producing effective SARIF.md

Large diffs are not rendered by default.

642 changes: 642 additions & 0 deletions docs/ValidationRules.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Sarif.Driver/Sdk/CompositionUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Microsoft.CodeAnalysis.Sarif.Driver
{
internal static class CompositionUtilities
public static class CompositionUtilities
{
public static ImmutableArray<T> GetExports<T>(IEnumerable<Assembly> assemblies)
{
Expand Down
66 changes: 66 additions & 0 deletions src/Sarif.Multitool/ExportRuleDocumentationCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 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.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

using Microsoft.CodeAnalysis.Sarif.Driver;
using Microsoft.CodeAnalysis.Sarif.Multitool.Rules;

namespace Microsoft.CodeAnalysis.Sarif.Multitool
{
public class ExportRuleDocumentationCommand : CommandBase
{
private readonly IFileSystem _fileSystem;

public ExportRuleDocumentationCommand(IFileSystem fileSystem = null)
{
_fileSystem = fileSystem ?? new FileSystem();
}

public int Run(ExportRuleDocumentationOptions options)
{
try
{
var rules = CompositionUtilities.GetExports<SarifValidationSkimmerBase>(
new Assembly[] { Assembly.GetExecutingAssembly() }).ToList();

var sb = new StringBuilder();
sb.AppendLine($"# Rules{Environment.NewLine}");

foreach (SarifValidationSkimmerBase rule in rules)
{
BuildRule(rule, sb);
}

_fileSystem.WriteAllText(options.OutputFilePath, sb.ToString());
}
catch (Exception ex)
{
Console.WriteLine(ex);
return FAILURE;
}

return SUCCESS;
}

internal void BuildRule(SarifValidationSkimmerBase rule, StringBuilder sb)
{
sb.AppendLine($"## Rule `{rule.Moniker}`{Environment.NewLine}");
sb.AppendLine($"### Description{Environment.NewLine}");
sb.AppendLine($"{rule.FullDescription.Text}{Environment.NewLine}");
sb.AppendLine($"### Messages{Environment.NewLine}");

foreach (KeyValuePair<string, MultiformatMessageString> message in rule.MessageStrings)
{
sb.AppendLine($"#### `{message.Key.Split('_').Last()}`: {rule.DefaultLevel}{Environment.NewLine}");
sb.AppendLine($"{message.Value.Text}{Environment.NewLine}");
}

sb.AppendLine($"---{Environment.NewLine}");
}
}
}
17 changes: 17 additions & 0 deletions src/Sarif.Multitool/ExportRuleDocumentationOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using CommandLine;

namespace Microsoft.CodeAnalysis.Sarif.Multitool
{
[Verb("export-rule-documentation", HelpText = "Export the documentation for the validation rules to a Markdown file.")]
public class ExportRuleDocumentationOptions
{
[Option(
'o',
"output-file-path",
HelpText = "Path to the generated Markdown file. Default: ValidationRules.md in the current directory.")]
public string OutputFilePath { get; internal set; }
}
}
2 changes: 2 additions & 0 deletions src/Sarif.Multitool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ internal static class Program
public static int Main(string[] args)
{
return Parser.Default.ParseArguments<
ExportRuleDocumentationOptions,
ValidateOptions,
ConvertOptions,
RewriteOptions,
Expand All @@ -26,6 +27,7 @@ public static int Main(string[] args)
ResultMatchSetOptions,
FileWorkItemsOptions>(args)
.MapResult(
(ExportRuleDocumentationOptions options) => new ExportRuleDocumentationCommand().Run(options),
(ValidateOptions validateOptions) => new ValidateCommand().Run(validateOptions),
(ConvertOptions convertOptions) => new ConvertCommand().Run(convertOptions),
(RewriteOptions rewriteOptions) => new RewriteCommand().Run(rewriteOptions),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Generic;
using System.Text;
using FluentAssertions;
using Microsoft.CodeAnalysis.Sarif.Multitool.Rules;
using Xunit;

namespace Microsoft.CodeAnalysis.Sarif.Multitool
{
public class ExportRuleDocumentationCommandTests
{
[Fact]
public void BuildRule_GeneratesExpectedMarkdown()
{
var resourceExtractor = new ResourceExtractor(this.GetType());
var sb = new StringBuilder();
var testRule = new TestRule();
var command = new ExportRuleDocumentationCommand();
command.BuildRule(testRule, sb);

string expectedMarkdown = resourceExtractor.GetResourceText("Test.md");

sb.ToString().Should().Be(expectedMarkdown);
}

private class TestRule : SarifValidationSkimmerBase
{
public override string Id => "TEST0001";
public override string Name => "TEST";
public override FailureLevel DefaultLevel => FailureLevel.Note;
public override MultiformatMessageString FullDescription => new MultiformatMessageString { Text = "full description text" };
public override IDictionary<string, MultiformatMessageString> MessageStrings => new Dictionary<string, MultiformatMessageString>
{
{ "TEST0001_TEST_Note_Default", new MultiformatMessageString{ Text="default text"} }
};
}
}
}
Copy link

@ghost ghost Sep 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beautiful! #ByDesign

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

<ItemGroup>
<None Remove="TestData\ConvertCommand\SemmleQlSample.csv" />
<None Remove="TestData\ExportRuleDocumentationCommand\ExpectedOutputs\Test.md" />
<None Remove="TestData\MergeCommand\ExpectedOutputs\NoInputFiles.sarif" />
<None Remove="TestData\PageCommand\elfie-arriba - Copy.sarif" />
<None Remove="TestData\PageCommand\elfie-arriba.sarif" />
Expand All @@ -24,6 +25,7 @@

<ItemGroup>
<EmbeddedResource Include="TestData\ConvertCommand\SemmleQlSample.csv" />
<EmbeddedResource Include="TestData\ExportRuleDocumentationCommand\ExpectedOutputs\Test.md" />
<EmbeddedResource Include="TestData\MergeCommand\ExpectedOutputs\NoInputFiles.sarif" />
<EmbeddedResource Include="TestData\QueryCommand\elfie-arriba.CSCAN0020.sarif" />
<EmbeddedResource Include="TestData\PageCommand\elfie-arriba.sarif" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## Rule `TEST0001.TEST`

### Description

full description text

### Messages

#### `Default`: Note

default text

---