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

Fix location Uri and number of artifact in work item description #2391

Merged
merged 4 commits into from
Oct 6, 2021
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
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()
Copy link
Collaborator

Choose a reason for hiding this comment

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

SarifWorkItemExtensions_CreateWorkItemDescription_SingleResultWithMultipleArtifacts

Let's also create two more tests:

  1. multiple results and count the number of issues
  2. one result with multiple locations

{
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