Skip to content

Commit

Permalink
add v1 support and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
dicko2 committed Nov 17, 2024
1 parent d0783ac commit ad1a750
Show file tree
Hide file tree
Showing 25 changed files with 293 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<ItemGroup>
<None Remove="TestData\invalid.sarif" />
<None Remove="TestData\sample.sarif" />
<None Remove="TestData\v1.sarif" />
</ItemGroup>

<ItemGroup>
Expand All @@ -18,11 +19,15 @@
<Content Include="TestData\sample.sarif">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestData\v1.sarif">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="17.8.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="NSubstitute" Version="5.3.0" />
<PackageReference Include="NUnit" Version="4.2.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
<PackageReference Include="Shouldly" Version="4.2.1" />
Expand Down
44 changes: 38 additions & 6 deletions src/Agoda.CodeCompass.MSBuild.Tests/SarifConversionTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System.Text.Json;
using Agoda.CodeCompass.MSBuild.Sarif;
using Microsoft.Build.Framework;
using Microsoft.VisualStudio.TestPlatform.Utilities;
using NSubstitute;
using NUnit.Framework;
using NUnit.Framework.Internal;
using Shouldly;
Expand All @@ -10,16 +14,18 @@ public class SarifConversionTests
{
private readonly string _writeSarifPath = "TestData/write.sarif";
private readonly string _sampleSarifPath = "TestData/sample.sarif";
private readonly IBuildEngine _buildEngine = Substitute.For<IBuildEngine>();

[Test]
public async Task ConvertSarif_WithValidInput_ShouldAddTechDebtProperties()
{
var outfile = "TestData/" + Guid.NewGuid().ToString();
var outfile = "TestData/" + Guid.NewGuid();
// Arrange
var task = new TechDebtSarifTask
{
InputPath = _sampleSarifPath,
OutputPath = outfile
OutputPath = outfile,
BuildEngine = _buildEngine
};

// Act
Expand Down Expand Up @@ -49,14 +55,39 @@ public void ConvertSarif_WithInvalidPath_ShouldReturnFalse()
var task = new TechDebtSarifTask
{
InputPath = "TestData/invalid.sarif",
OutputPath = Guid.NewGuid().ToString()
OutputPath = Guid.NewGuid().ToString(),
BuildEngine = _buildEngine
};

var result = task.Execute();

result.ShouldBeFalse();
}

[Test]
public async Task ConvertSarif_WithV1FromTestData_ShouldHave1Violation()
{
var outfile = "TestData/" + Guid.NewGuid();
var task = new TechDebtSarifTask
{
InputPath = "TestData/v1.sarif",
OutputPath = outfile,
BuildEngine = _buildEngine
};

var result = task.Execute();

result.ShouldBeTrue();

var outputJson = await File.ReadAllTextAsync(outfile);
var output = JsonSerializer.Deserialize<SarifReport>(outputJson, new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });

output.Runs[0].Results.Count.ShouldBe(1);

Check warning on line 85 in src/Agoda.CodeCompass.MSBuild.Tests/SarifConversionTests.cs

View workflow job for this annotation

GitHub Actions / Build Package

Dereference of a possibly null reference.

Check warning on line 85 in src/Agoda.CodeCompass.MSBuild.Tests/SarifConversionTests.cs

View workflow job for this annotation

GitHub Actions / Build Package

Dereference of a possibly null reference.

var results = output.Runs[0].Results;
results[0].RuleId.ShouldBe("CA1707");

}
[Test]
public async Task ConvertSarif_WithMultipleRules_ShouldPreserveRuleMetadata()
{
Expand All @@ -78,11 +109,12 @@ public async Task ConvertSarif_WithMultipleRules_ShouldPreserveRuleMetadata()

await File.WriteAllTextAsync(_writeSarifPath,
JsonSerializer.Serialize(sarif, new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }));
var outfile = "TestData/" + Guid.NewGuid().ToString();
var outfile = "TestData/" + Guid.NewGuid();
var task = new TechDebtSarifTask
{
InputPath = _writeSarifPath,
OutputPath = outfile
OutputPath = outfile,
BuildEngine = _buildEngine
};

// Act
Expand Down
56 changes: 56 additions & 0 deletions src/Agoda.CodeCompass.MSBuild.Tests/TestData/v1.sarif
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"$schema": "http://json.schemastore.org/sarif-1.0.0",
"version": "1.0.0",
"runs": [
{
"tool": {
"name": "Microsoft (R) Visual C# Compiler",
"version": "4.11.0.0",
"fileVersion": "4.11.0-3.24468.6 (b4e5d1dd)",
"semanticVersion": "4.11.0",
"language": "en-US"
},
"results": [
{
"ruleId": "CA1707",
"level": "warning",
"message": "Remove the underscores from member name Agoda.SupplyExtranetTemplate.UnitTests.ExampleTestFixture.ExampleTest_WhenProvide1_ValueShouldBe1()",
"locations": [
{
"resultFile": {
"uri": "file:///C:/source/gitlab.agoda.com/full-stack/templates/supply-extranet-template/src/AspnetWebApi/src/Agoda.SupplyExtranetTemplate.UnitTests/ExampleTestFixture.cs",
"region": {
"startLine": 10,
"startColumn": 17,
"endLine": 10,
"endColumn": 56
}
}
}
],
"properties": {
"warningLevel": 1
}
}
],
"rules": {
"CA1707": {
"id": "CA1707",
"shortDescription": "Identifiers should not contain underscores",
"fullDescription": "By convention, identifier names do not contain the underscore (_) character. This rule checks namespaces, types, members, and parameters.",
"defaultLevel": "note",
"helpUri": "https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1707",
"properties": {
"category": "Naming",
"isEnabledByDefault": true,
"tags": [
"PortedFromFxCop",
"Telemetry",
"EnabledRuleInAggressiveMode"
]
}
}
}
}
]
}
59 changes: 36 additions & 23 deletions src/Agoda.CodeCompass.MSBuild/Agoda.CodeCompass.MSBuild.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<BuildOutputTargetFolder>tools</BuildOutputTargetFolder>
<DevelopmentDependency>true</DevelopmentDependency>
<NoPackageAnalysis>true</NoPackageAnalysis>
</PropertyGroup>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<BuildOutputTargetFolder>tools</BuildOutputTargetFolder>
<DevelopmentDependency>true</DevelopmentDependency>
<NoPackageAnalysis>true</NoPackageAnalysis>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<IncludeReferencedProjects>true</IncludeReferencedProjects>
</PropertyGroup>

<PropertyGroup>
<Authors>Joel Dickson</Authors>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
Expand All @@ -16,19 +19,29 @@
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<Description>CodeCompass is a .NET analyzer that helps you navigate the treacherous waters of technical debt. It analyzes your code and produces standardized SARIF reports that quantify technical debt in terms of estimated remediation time, categorization, and priority.</Description>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Build.Framework" Version="17.8.3" PrivateAssets="all" />
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="17.8.3" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
<None Include="build\*.props" Pack="true" PackagePath="build" />
<None Include="build\*.targets" Pack="true" PackagePath="build" />
</ItemGroup>
<ItemGroup>
<Content Include="AgodaAnalyzersAgoji.png" Pack="true" PackagePath="">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Build.Framework" Version="17.8.3" PrivateAssets="all" />
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="17.8.3" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
<None Include="build\*.props" Pack="true" PackagePath="build" />
<None Include="build\*.targets" Pack="true" PackagePath="build" />
</ItemGroup>

<Target Name="CollectRuntimeOutputs" BeforeTargets="GenerateNuspec">
<ItemGroup>
<_PackageFiles Include="$(OutputPath)\*.dll">
<BuildAction>None</BuildAction>
<PackagePath>tools\net8.0\</PackagePath>
</_PackageFiles>
</ItemGroup>
</Target>
<ItemGroup>
<Content Include="AgodaAnalyzersAgoji.png" Pack="true" PackagePath="">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Agoda.CodeCompass.MSBuild;
namespace Agoda.CodeCompass.MSBuild.Sarif;

public class ArtifactLocation
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Agoda.CodeCompass.MSBuild;
namespace Agoda.CodeCompass.MSBuild.Sarif;

public class Location
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Agoda.CodeCompass.MSBuild;
namespace Agoda.CodeCompass.MSBuild.Sarif;

public class Message
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Agoda.CodeCompass.MSBuild;
namespace Agoda.CodeCompass.MSBuild.Sarif;

public class PhysicalLocation
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Agoda.CodeCompass.MSBuild;
namespace Agoda.CodeCompass.MSBuild.Sarif;

public class Region
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Agoda.CodeCompass.MSBuild;
namespace Agoda.CodeCompass.MSBuild.Sarif;

public class Result
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Agoda.CodeCompass.MSBuild;
using Agoda.CodeCompass.MSBuild.Sarif;

public class Rule
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Agoda.CodeCompass.MSBuild;
using Agoda.CodeCompass.MSBuild.Sarif;

public class Run
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Agoda.CodeCompass.MSBuild;
namespace Agoda.CodeCompass.MSBuild.Sarif;

public class SarifReport
{
Expand Down
8 changes: 8 additions & 0 deletions src/Agoda.CodeCompass.MSBuild/Sarif/SarifV1Report.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Agoda.CodeCompass.MSBuild.Sarif;

public class SarifV1Report
{
public string Schema { get; set; } = "http://json.schemastore.org/sarif-1.0.0";
public string Version { get; set; } = "1.0.0";
public V1Run[] Runs { get; set; } = Array.Empty<V1Run>();
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Agoda.CodeCompass.MSBuild;
namespace Agoda.CodeCompass.MSBuild.Sarif;

public class Tool
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Agoda.CodeCompass.MSBuild;
namespace Agoda.CodeCompass.MSBuild.Sarif;

public class ToolDriver
{
Expand Down
6 changes: 6 additions & 0 deletions src/Agoda.CodeCompass.MSBuild/Sarif/V1Location.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Agoda.CodeCompass.MSBuild.Sarif;

public class V1Location
{
public V1ResultFile ResultFile { get; set; } = new();
}
9 changes: 9 additions & 0 deletions src/Agoda.CodeCompass.MSBuild/Sarif/V1Region.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Agoda.CodeCompass.MSBuild.Sarif;

public class V1Region
{
public int StartLine { get; set; }
public int StartColumn { get; set; }
public int EndLine { get; set; }
public int EndColumn { get; set; }
}
10 changes: 10 additions & 0 deletions src/Agoda.CodeCompass.MSBuild/Sarif/V1Result.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Agoda.CodeCompass.MSBuild.Sarif;

public class V1Result
{
public string RuleId { get; set; } = string.Empty;
public string Level { get; set; } = string.Empty;
public string Message { get; set; } = string.Empty;
public V1Location[] Locations { get; set; } = Array.Empty<V1Location>();
public TechDebtProperties Properties { get; set; } = new();
}
7 changes: 7 additions & 0 deletions src/Agoda.CodeCompass.MSBuild/Sarif/V1ResultFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Agoda.CodeCompass.MSBuild.Sarif;

public class V1ResultFile
{
public string Uri { get; set; } = string.Empty;
public V1Region Region { get; set; } = new();
}
6 changes: 6 additions & 0 deletions src/Agoda.CodeCompass.MSBuild/Sarif/V1Run.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Agoda.CodeCompass.MSBuild.Sarif;

public class V1Run
{
public V1Result[] Results { get; set; } = Array.Empty<V1Result>();
}
2 changes: 2 additions & 0 deletions src/Agoda.CodeCompass.MSBuild/SarifReporter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Text.Json;
using Agoda.CodeCompass.Data;
using Agoda.CodeCompass.MSBuild.Sarif;
using Microsoft.CodeAnalysis;
using Location = Agoda.CodeCompass.MSBuild.Sarif.Location;

namespace Agoda.CodeCompass.MSBuild;

Expand Down
Loading

0 comments on commit ad1a750

Please sign in to comment.