// TODO: Make the core baselining algorithm support BaselineFilteringMode.ToIncludedArtifacts private static SarifLog Baseline(SarifLog baselineLog, SarifLog currentLog) { // Baseline the complete log ISarifLogMatcher matcher = ResultMatchingBaselinerFactory.GetDefaultResultMatchingBaseliner(); SarifLog outputLog = matcher.Match(new[] { baselineLog }, new[] { currentLog }).First(); // Is this an incremental scan? BaselineFilteringMode filteringMode = currentLog.GetBaselineFilteringMode(); // Mark all Results which are NOT in the new run as 'Unchanged' if (filteringMode == BaselineFilteringMode.ToIncludedArtifacts) { HashSet <string> includedArtifacts = new HashSet <string>(currentLog.AllResultArtifactUris().Select(uri => uri.OriginalString)); foreach (Result result in outputLog.EnumerateResults()) { if (!ContainsUriInSet(result, includedArtifacts)) { result.BaselineState = BaselineState.Unchanged; } } } // Ensure the Baseline is sorted for fast future baselining SortForBaselining(outputLog); return(outputLog); }
private static Run CreateMatchedRun(SarifLog previousLog, SarifLog currentLog) { ISarifLogMatcher baseliner = ResultMatchingBaselinerFactory.GetDefaultResultMatchingBaseliner(); SarifLog matchedLog = baseliner.Match(new[] { previousLog }, new[] { currentLog }).Single(); return(matchedLog.Runs[0]); }
public int Run(ResultMatchSetOptions options) { options.OutputFolderPath = options.OutputFolderPath ?? Path.Combine(options.FolderPath, "Out"); ISarifLogMatcher matcher = ResultMatchingBaselinerFactory.GetDefaultResultMatchingBaseliner(); Formatting formatting = options.PrettyPrint ? Formatting.Indented : Formatting.None; // Remove previous results. if (_fileSystem.DirectoryExists(options.OutputFolderPath)) { _fileSystem.DeleteDirectory(options.OutputFolderPath, true); } // Create output folder. _fileSystem.CreateDirectory(options.OutputFolderPath); string previousFileName = ""; string previousGroup = ""; SarifLog previousLog = null, currentLog = null; foreach (string filePath in Directory.GetFiles(options.FolderPath, "*.sarif")) { string fileName = Path.GetFileName(filePath); string currentGroup = GetGroupName(fileName); try { currentLog = ReadSarifFile <SarifLog>(_fileSystem, filePath); // Compare each log with the previous one in the same group. if (currentGroup.Equals(previousGroup) && currentLog?.Runs?[0]?.Results.Count != 0 && previousLog?.Runs?[0]?.Results.Count != 0) { Console.WriteLine(); Console.WriteLine($"{previousFileName} -> {fileName}:"); SarifLog mergedLog = matcher.Match(new[] { previousLog }, new[] { currentLog }).First(); // Write the same and different count and different IDs. WriteDifferences(mergedLog); // Write the log, if there were any changed results if (mergedLog.Runs[0].Results.Any(r => r.BaselineState != BaselineState.Unchanged)) { string outputFilePath = Path.Combine(options.OutputFolderPath, fileName); WriteSarifFile(_fileSystem, mergedLog, outputFilePath, formatting); } } } catch (Exception ex) when(!Debugger.IsAttached) { Console.WriteLine(ex.ToString()); } previousFileName = fileName; previousGroup = currentGroup; previousLog = currentLog; } return(0); }
public void Overall_CheckingAbsentUnchangedAndNew() { SarifLog baselineSarif = TestData.CreateSimpleLogWithRules(ruleIdStartIndex: 0, resultCount: 2); SarifLog currentSarif = TestData.CreateSimpleLogWithRules(ruleIdStartIndex: 1, resultCount: 2); ISarifLogMatcher matcher = ResultMatchingBaselinerFactory.GetDefaultResultMatchingBaseliner(); SarifLog output = matcher.Match(new SarifLog[] { baselineSarif }, new SarifLog[] { currentSarif }).First(); output.Runs[0].Results[0].BaselineState.Should().Be(BaselineState.Absent); output.Runs[0].Results[1].BaselineState.Should().Be(BaselineState.Unchanged); output.Runs[0].Results[2].BaselineState.Should().Be(BaselineState.New); }
public int Run(ResultMatchingOptions matchingOptions) { try { SarifLog baselineFile = null; if (!string.IsNullOrEmpty(matchingOptions.PreviousFilePath)) { baselineFile = ReadSarifFile <SarifLog>(_fileSystem, matchingOptions.PreviousFilePath); } string outputFilePath = matchingOptions.OutputFilePath; if (string.IsNullOrEmpty(outputFilePath)) { outputFilePath = Path.GetFileNameWithoutExtension(matchingOptions.PreviousFilePath) + "-annotated.sarif"; } if (!DriverUtilities.ReportWhetherOutputFileCanBeCreated(outputFilePath, matchingOptions.Force, _fileSystem)) { return(FAILURE); } var currentSarifLogs = new List <SarifLog>(); foreach (string currentFilePath in matchingOptions.CurrentFilePaths) { currentSarifLogs.Add(ReadSarifFile <SarifLog>(_fileSystem, currentFilePath)); } ISarifLogMatcher matcher = ResultMatchingBaselinerFactory.GetDefaultResultMatchingBaseliner(); SarifLog output = matcher.Match(new SarifLog[] { baselineFile }, currentSarifLogs).First(); Newtonsoft.Json.Formatting formatting = matchingOptions.PrettyPrint ? Newtonsoft.Json.Formatting.Indented : Newtonsoft.Json.Formatting.None; WriteSarifFile(_fileSystem, output, outputFilePath, formatting); } catch (Exception ex) { Console.WriteLine(ex.ToString()); return(FAILURE); } return(SUCCESS); }
public static int Run(ResultMatchingOptions matchingOptions) { try { SarifLog baselineFile = null; if (!string.IsNullOrEmpty(matchingOptions.PreviousFilePath)) { baselineFile = FileHelpers.ReadSarifFile <SarifLog>(matchingOptions.PreviousFilePath); } string outputFilePath = matchingOptions.OutputFilePath; if (string.IsNullOrEmpty(outputFilePath)) { outputFilePath = Path.GetFileNameWithoutExtension(matchingOptions.PreviousFilePath) + "-annotated.sarif"; } SarifLog currentFile = FileHelpers.ReadSarifFile <SarifLog>(matchingOptions.CurrentFilePath); ISarifLogMatcher matcher = ResultMatchingBaselinerFactory.GetDefaultResultMatchingBaseliner(); SarifLog output = matcher.Match(new SarifLog[] { baselineFile }, new SarifLog[] { currentFile }).First(); var formatting = matchingOptions.PrettyPrint ? Newtonsoft.Json.Formatting.Indented : Newtonsoft.Json.Formatting.None; FileHelpers.WriteSarifFile(output, outputFilePath, formatting); } catch (Exception ex) { Console.WriteLine(ex.ToString()); return(1); } return(0); }
private static SarifLog Baseline(SarifLog baseline, SarifLog current) { return(matcher.Match(new[] { baseline }, new[] { current }).FirstOrDefault()); }