|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Security.Cryptography; |
| 6 | +using System.Text; |
| 7 | +using Microsoft.CodeAnalysis; |
| 8 | +using Newtonsoft.Json; |
| 9 | +using Newtonsoft.Json.Serialization; |
| 10 | +using Roslynator.CommandLine.GitLab; |
| 11 | +using Roslynator.Diagnostics; |
| 12 | + |
| 13 | +namespace Roslynator.CommandLine.Json; |
| 14 | + |
| 15 | +internal static class DiagnosticGitLabJsonSerializer |
| 16 | +{ |
| 17 | + private static readonly JsonSerializerSettings _jsonSerializerSettings = new() |
| 18 | + { |
| 19 | + Formatting = Newtonsoft.Json.Formatting.Indented, |
| 20 | + NullValueHandling = NullValueHandling.Ignore, |
| 21 | + ContractResolver = new DefaultContractResolver() |
| 22 | + { |
| 23 | + NamingStrategy = new CamelCaseNamingStrategy() |
| 24 | + }, |
| 25 | + }; |
| 26 | + |
| 27 | + public static void Serialize( |
| 28 | + IEnumerable<ProjectAnalysisResult> results, |
| 29 | + string filePath, |
| 30 | + IFormatProvider formatProvider = null) |
| 31 | + { |
| 32 | + IEnumerable<DiagnosticInfo> diagnostics = results.SelectMany(f => f.CompilerDiagnostics.Concat(f.Diagnostics)); |
| 33 | + |
| 34 | + var reportItems = new List<GitLabIssue>(); |
| 35 | + foreach (DiagnosticInfo diagnostic in diagnostics) |
| 36 | + { |
| 37 | + GitLabIssueLocation location = null; |
| 38 | + if (diagnostic.LineSpan.IsValid) |
| 39 | + { |
| 40 | + location = new GitLabIssueLocation() |
| 41 | + { |
| 42 | + Path = diagnostic.LineSpan.Path, |
| 43 | + Lines = new GitLabLocationLines() |
| 44 | + { |
| 45 | + Begin = diagnostic.LineSpan.StartLinePosition.Line |
| 46 | + }, |
| 47 | + }; |
| 48 | + } |
| 49 | + |
| 50 | + var severity = "minor"; |
| 51 | + severity = diagnostic.Severity switch |
| 52 | + { |
| 53 | + DiagnosticSeverity.Warning => "major", |
| 54 | + DiagnosticSeverity.Error => "critical", |
| 55 | + _ => "minor", |
| 56 | + }; |
| 57 | + |
| 58 | + string issueFingerPrint = $"{diagnostic.Descriptor.Id}-{diagnostic.Severity}-{location?.Path}-{location?.Lines.Begin}"; |
| 59 | + byte[] source = Encoding.UTF8.GetBytes(issueFingerPrint); |
| 60 | + byte[] hashBytes; |
| 61 | +#if NETFRAMEWORK |
| 62 | + using (var sha256 = SHA256.Create()) |
| 63 | + hashBytes = sha256.ComputeHash(source); |
| 64 | +#else |
| 65 | + hashBytes = SHA256.HashData(source); |
| 66 | +#endif |
| 67 | + issueFingerPrint = BitConverter.ToString(hashBytes) |
| 68 | + .Replace("-", "") |
| 69 | + .ToLowerInvariant(); |
| 70 | + |
| 71 | + reportItems.Add(new GitLabIssue() |
| 72 | + { |
| 73 | + Type = "issue", |
| 74 | + Fingerprint = issueFingerPrint, |
| 75 | + CheckName = diagnostic.Descriptor.Id, |
| 76 | + Description = diagnostic.Descriptor.Title.ToString(formatProvider), |
| 77 | + Severity = severity, |
| 78 | + Location = location, |
| 79 | + Categories = new string[] { diagnostic.Descriptor.Category }, |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + string report = JsonConvert.SerializeObject(reportItems, _jsonSerializerSettings); |
| 84 | + |
| 85 | + File.WriteAllText(filePath, report, Encoding.UTF8); |
| 86 | + } |
| 87 | +} |
0 commit comments