Skip to content

Commit

Permalink
Merge branch 'master' into analyze-test
Browse files Browse the repository at this point in the history
  • Loading branch information
eddynaka authored Nov 5, 2020
2 parents 1c8c629 + 173eaa0 commit f40a308
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/Sarif/Core/Run.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,15 @@ internal static Dictionary<string, FailureLevel> ComputePolicies(IEnumerable<Too
/// When multiple policies remap the same rule, the last policy in the policies
/// collection has precedence.
/// </summary>
public void ApplyPolicies()
public void ApplyPolicies(Dictionary<string, FailureLevel> policiesCache = null)
{
// Use policies cache if exist
if (policiesCache != null)
{
PoliciesCache = policiesCache;
}

// Compute policies
if (PoliciesCache == null || PoliciesCache.Count == 0)
{
PoliciesCache = ComputePolicies(this.Policies);
Expand Down
43 changes: 41 additions & 2 deletions src/Sarif/Core/SarifLog.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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.IO;

using Microsoft.CodeAnalysis.Sarif.Readers;
Expand Down Expand Up @@ -106,7 +107,7 @@ public void Save(StreamWriter streamWriter)
/// <summary>
/// Applies the policies contained in each run
/// </summary>
public void ApplyPolicies()
public void ApplyPolicies(Dictionary<string, FailureLevel> policiesCache = null)
{
if (this.Runs == null)
{
Expand All @@ -115,7 +116,45 @@ public void ApplyPolicies()

foreach (Run run in this.Runs)
{
run.ApplyPolicies();
run.ApplyPolicies(policiesCache);
}
}

/// <summary>
/// Applies the policies contained from a sarif file
/// </summary>
public void ApplyPolicies(string sarifLogPath)
{
SarifLog sarifLog = Load(sarifLogPath);
if (sarifLog == null || sarifLog.Runs == null)
{
return;
}

Dictionary<string, FailureLevel> localCache = new Dictionary<string, FailureLevel>();

foreach (Run run in sarifLog.Runs)
{
ComputePolicies(localCache, run);
}

ApplyPolicies(localCache);
}

internal static void ComputePolicies(Dictionary<string, FailureLevel> localCache, Run run)
{
// checking if we have policies
if (run.Policies == null || run.Policies.Count == 0)
{
return;
}

foreach (ToolComponent policy in run.Policies)
{
foreach (ReportingDescriptor rule in policy.Rules)
{
localCache[rule.Id] = rule.DefaultConfiguration.Level;
}
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/Test.UnitTests.Sarif/Core/RunTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,20 @@ public void Run_ApplyPolicies_WhenWeHavePolicies()
run.Results[0].Level.Should().Be(FailureLevel.Warning);
}

[Fact]
public void Run_ApplyPoliies_WhenWeHaveCacheAvailable()
{
var cachedPolicies = new Dictionary<string, FailureLevel>() { { "TEST0", FailureLevel.None } };
Run run = CreateRun(resultCount: 1);
run.Policies = new ToolComponent[]
{
CreateToolComponent("test1", rulesCount: 1, FailureLevel.Error)
};
run.ApplyPolicies(cachedPolicies);

run.Results[0].Level.Should().Be(FailureLevel.None);
}

private void RoundTripColumnKind(ColumnKind persistedValue, ColumnKind expectedRoundTrippedValue)
{
var sarifLog = new SarifLog
Expand Down

0 comments on commit f40a308

Please sign in to comment.