Skip to content

Commit

Permalink
Fix location Uri and number of artifact in work item description (#2391)
Browse files Browse the repository at this point in the history
* Fix location Uri and number of artifact in work item description

* Fix LGTM issues

* Add more test cases

Co-authored-by: Eddy Nakamura <eddynaka@gmail.com>
  • Loading branch information
yongyan-gh and eddynaka authored Oct 6, 2021
1 parent 7d78667 commit d31bc91
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Sarif.WorkItems/SarifWorkItemModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public class SarifWorkItemModel : WorkItemModel<SarifWorkItemContext>

this.BodyOrDescription =
Environment.NewLine +
sarifLog.CreateWorkItemDescription(this.Context, LocationUris) +
sarifLog.CreateWorkItemDescription(this.Context) +
descriptionFooter;

// These properties are Azure DevOps-specific. All ADO work item board
Expand Down
18 changes: 14 additions & 4 deletions src/Sarif.WorkItems/SarifWorkItemsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,23 +141,33 @@ public static int GetAggregateFilableResultsCount(this SarifLog log)
.Sum() ?? 0;
}

public static string CreateWorkItemDescription(this SarifLog log, SarifWorkItemContext context, IList<Uri> locationUris)
public static string CreateWorkItemDescription(this SarifLog log, SarifWorkItemContext context)
{
int totalResults = log.GetAggregateFilableResultsCount();
List<string> toolNames = log.GetToolNames();
string phrasedToolNames = toolNames.ToAndPhrase();
string multipleToolsFooter = toolNames.Count > 1 ? WorkItemsResources.MultipleToolsFooter : string.Empty;

IEnumerable<Result> results = log?.Runs?[0]?.Results.Where(r => r.ShouldBeFiled());
Uri runRepositoryUri = log?.Runs.FirstOrDefault()?.VersionControlProvenance?.FirstOrDefault().RepositoryUri;
Uri detectionLocationUri = !string.IsNullOrEmpty(runRepositoryUri?.OriginalString) ? runRepositoryUri : locationUris?[0];
Uri detectionLocationUri = !string.IsNullOrEmpty(runRepositoryUri?.OriginalString) ?
runRepositoryUri :
results?.FirstOrDefault().Locations?[0].PhysicalLocation?.ArtifactLocation?.Uri;

string detectionLocation = (detectionLocationUri?.IsAbsoluteUri == true && detectionLocationUri?.Scheme == "https")
? context.CreateLinkText(detectionLocationUri.OriginalString, detectionLocationUri?.OriginalString)
: detectionLocationUri?.OriginalString;

if (locationUris?.Count > 1)
int locCount = results == null ? 0 :
results
.Where(r => r.Locations != null)
.SelectMany(r => r.Locations)
.Where(l => l.PhysicalLocation != null && l.PhysicalLocation.ArtifactLocation != null && l.PhysicalLocation.ArtifactLocation.Uri != null)
.Count();

if (locCount > 1)
{
int additionalLocations = locationUris.Count - 1;
int additionalLocations = locCount - 1;
detectionLocation = $"{detectionLocation} (+{additionalLocations} locations)";
}

Expand Down
159 changes: 159 additions & 0 deletions src/Test.UnitTests.Sarif.WorkItems/SarifWorkItemExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using FluentAssertions;

using Microsoft.CodeAnalysis.Test.Utilities.Sarif;
using Microsoft.VisualStudio.Services.Common;

using Xunit;

Expand Down Expand Up @@ -225,6 +226,164 @@ public void SarifWorkItemExtensions_GetRunToolNames_FetchesAllRunToolNames()
toolNames.Count.Should().Be(0);
}

[Fact]
public void SarifWorkItemExtensions_CreateWorkItemDescription_SingleResultMultipleLocations()
{
string toolName = "TestToolName";
string firstLocation = @"C:\Test\Data\File1.sarif";
string additionLocationCount = "2";

SarifLog sarifLog = TestData.CreateOneIdThreeLocations();
sarifLog.Runs[0].VersionControlProvenance = null;

string description = sarifLog.CreateWorkItemDescription(new SarifWorkItemContext() { CurrentProvider = Microsoft.WorkItems.FilingClient.SourceControlProvider.AzureDevOps });

description.Should().BeEquivalentTo($"This work item contains 1 '{toolName}' issue(s) detected in {firstLocation} (+{additionLocationCount} locations). Click the 'Scans' tab to review results.");
}

[Fact]
public void SarifWorkItemExtensions_CreateWorkItemDescription_MultipleResults()
{
string toolName = "TestToolName";
string firstLocation = @"C:\Test\Data\File{0}sarif";
int numOfResult = 15;
string additionLocationCount = "14";

int index = 1;
SarifLog sarifLog = TestData.CreateSimpleLogWithRules(0, numOfResult);
sarifLog.Runs[0].Results.ForEach(r => r.Locations
= new[]
{
new Location
{
PhysicalLocation = new PhysicalLocation
{
ArtifactLocation = new ArtifactLocation
{
Uri = new Uri(string.Format(firstLocation, index++))
}
}
}
});

string description = sarifLog.CreateWorkItemDescription(new SarifWorkItemContext() { CurrentProvider = Microsoft.WorkItems.FilingClient.SourceControlProvider.AzureDevOps });

description.Should().BeEquivalentTo($"This work item contains {numOfResult} '{toolName}' issue(s) detected in {string.Format(firstLocation, 1)} (+{additionLocationCount} locations). Click the 'Scans' tab to review results.");
}

[Fact]
public void SarifWorkItemExtensions_CreateWorkItemDescription_MultipleResultsMultipleLocations()
{
string toolName = "TestToolName";
string firstLocation = @"C:\Test\Data\File{0}sarif";
int numOfResult = 15;
string additionLocationCount = "29"; // 15 results each results have 2 locations

int index = 1;
SarifLog sarifLog = TestData.CreateSimpleLogWithRules(0, numOfResult);
sarifLog.Runs[0].Results.ForEach(r => r.Locations
= new[]
{
new Location
{
PhysicalLocation = new PhysicalLocation
{
ArtifactLocation = new ArtifactLocation
{
Uri = new Uri(string.Format(firstLocation, index++))
}
}
},
new Location
{
PhysicalLocation = new PhysicalLocation
{
ArtifactLocation = new ArtifactLocation
{
Uri = new Uri(string.Format(firstLocation, index++))
}
}
}
});

string description = sarifLog.CreateWorkItemDescription(new SarifWorkItemContext() { CurrentProvider = Microsoft.WorkItems.FilingClient.SourceControlProvider.AzureDevOps });

description.Should().BeEquivalentTo($"This work item contains {numOfResult} '{toolName}' issue(s) detected in {string.Format(firstLocation, 1)} (+{additionLocationCount} locations). Click the 'Scans' tab to review results.");
}

[Fact]
public void SarifWorkItemExtensions_CreateWorkItemDescription_SingleResultWithMultipleArtifacts()
{
string toolName = "TestToolName";
string firstLocation = @"C:\Test\Data\File1.sarif";
string secondLocation = @"C:\Test\Data\File2.sarif";
string thirdLocation = @"C:\Test\Data\File3.sarif";

SarifLog sarifLog = TestData.CreateSimpleLogWithRules(0, 1);
sarifLog.Runs[0].Results[0].Locations = new[]
{
new Location
{
PhysicalLocation = new PhysicalLocation
{
ArtifactLocation = new ArtifactLocation
{
Uri = new Uri(firstLocation),
}
}
}
};

sarifLog.Runs[0].Results[0].RelatedLocations = new[]
{
new Location
{
PhysicalLocation = new PhysicalLocation
{
ArtifactLocation = new ArtifactLocation
{
Uri = new Uri(secondLocation),
}
}
}
};

sarifLog.Runs[0].Results[0].CodeFlows = new[]
{
new CodeFlow
{
ThreadFlows = new[]
{
new ThreadFlow
{
Locations = new[]
{
new ThreadFlowLocation
{
Location = new Location
{
PhysicalLocation = new PhysicalLocation
{
ArtifactLocation = new ArtifactLocation
{
Uri = new Uri(thirdLocation),
}
}
}
}
}
}
}
}
};

sarifLog.Runs[0].VersionControlProvenance = null;

string description = sarifLog.CreateWorkItemDescription(new SarifWorkItemContext() { CurrentProvider = Microsoft.WorkItems.FilingClient.SourceControlProvider.AzureDevOps });

description.Should().BeEquivalentTo($"This work item contains 1 '{toolName}' issue(s) detected in {firstLocation}. Click the 'Scans' tab to review results.");
}

private static readonly string ToolName = Guid.NewGuid().ToString();

public Tuple<string, Result>[] ResultsWithVariousRuleExpressions = new[]
Expand Down

0 comments on commit d31bc91

Please sign in to comment.