Skip to content

Commit

Permalink
remove newtonsoft json dependency in favour of system.text.json
Browse files Browse the repository at this point in the history
  • Loading branch information
moly committed Feb 5, 2021
1 parent ecca158 commit 87d691a
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 61 deletions.
80 changes: 65 additions & 15 deletions src/ReSharperToCodeClimate/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using Newtonsoft.Json.Linq;
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using System.Xml.Linq;

namespace ReSharperToCodeclimate
Expand All @@ -11,28 +12,54 @@ class Program
{
static void Main(string[] args)
{
JArray codeClimateReport = new JArray();
List<CodeClimateIssue> codeClimateReport = new List<CodeClimateIssue>();

XElement resharperReport = XElement.Load(args[0]);
SeverityParser.Parse(resharperReport);
Dictionary<string, string> severityByIssueType = CreateSeverityByIssueTypeDictionary(resharperReport.Descendants("IssueType"));

foreach (XElement issue in resharperReport.Descendants("Issue"))
{
codeClimateReport.Add(
new JObject(
new JProperty("description", issue.Attribute("Message").Value),
new JProperty("severity", SeverityParser.GetSeverity(issue.Attribute("TypeId").Value)),
new JProperty("fingerprint", CalculateFingerprint(issue)),
new JProperty("location", new JObject(
new JProperty("path", issue.Attribute("File").Value.Replace("\\", "/")),
new JProperty("lines", new JObject(
new JProperty("begin", issue.Attribute("Line")?.Value ?? "0")))
))
)
new CodeClimateIssue()
{
Description = issue.Attribute("Message").Value,
Severity = severityByIssueType[issue.Attribute("TypeId").Value],
Fingerprint = CalculateFingerprint(issue),
Location = new IssueLocation()
{
Path = issue.Attribute("File").Value.Replace("\\", "/"),
Lines = new LineRange()
{
Begin = int.Parse(issue.Attribute("Line")?.Value ?? "0")
}
}
}
);
}

File.WriteAllText(args[1], codeClimateReport.ToString());
JsonSerializerOptions options = new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
File.WriteAllText(args[1], JsonSerializer.Serialize(codeClimateReport, options));
}

private static Dictionary<string, string> CreateSeverityByIssueTypeDictionary(IEnumerable<XElement> issueTypes)
{
Dictionary<string, string> codeClimateSeverityByResharperSeverity = new Dictionary<string, string>()
{
{"ERROR", "critical"},
{"WARNING", "major"},
{"SUGGESTION", "minor"},
{"HINT", "info"}
};

Dictionary<string, string> serverityByIssueType = new Dictionary<string, string>();

foreach (var issueType in issueTypes)
{
string severity = codeClimateSeverityByResharperSeverity[issueType.Attribute("Severity").Value];
serverityByIssueType.Add(issueType.Attribute("Id").Value, severity);
}

return serverityByIssueType;
}

private static string CalculateFingerprint(XElement issue)
Expand All @@ -46,4 +73,27 @@ private static string CalculateFingerprint(XElement issue)
}
}
}

class LineRange
{
public int Begin { get; set; }
}

class IssueLocation
{
public string Path { get; set; }

public LineRange Lines { get; set; }
}

class CodeClimateIssue
{
public string Description { get; set; }

public string Fingerprint { get; set; }

public string Severity { get; set; }

public IssueLocation Location { get; set; }
}
}
6 changes: 1 addition & 5 deletions src/ReSharperToCodeClimate/ReSharperToCodeClimate.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp3.1;net5.0</TargetFrameworks>
<TargetFramework>netcoreapp3.1</TargetFramework>
<LangVersion>latest</LangVersion>
<Version>1.0.2</Version>
<RootNamespace>ReSharperToCodeClimate</RootNamespace>
Expand All @@ -20,8 +20,4 @@
<PackageOutputPath>./nupkg</PackageOutputPath>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>

</Project>
41 changes: 0 additions & 41 deletions src/ReSharperToCodeClimate/SeverityParser.cs

This file was deleted.

0 comments on commit 87d691a

Please sign in to comment.