-
-
Notifications
You must be signed in to change notification settings - Fork 278
Add support for GitLab output format in AnalyzeCommand #1633
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
Merged
+166
−3
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3f6166c
Add support for GitLab output format in AnalyzeCommand
marais-vzc 67a7da0
Refactor GitLab issue classes to improve readability and structure
marais-vzc bb77a14
Fix namespace usage in DiagnosticGitLabJsonSerializer for consistency
marais-vzc 7f91989
Rename variable for clarity in DiagnosticGitLabJsonSerializer
marais-vzc 402378e
Updated help text for the output-format option
marais-vzc 3e7db2f
Make use of expressions for readability
marais-vzc 3ae3fe7
Validate output format input
marais-vzc 8c0a790
Improve language for description
marais-vzc 72a3ad4
Introduce a private static readonly field `_jsonSerializerSettings` i…
marais-vzc 6493db6
Correctly name validation method
marais-vzc e099dc4
Update ChangeLog for version 4.14.0 and add GitLab analyzer reports s…
marais-vzc c98da1c
Update ChangeLog.md
josefpihrt a96774d
Update src/CommandLine/Json/DiagnosticGitLabJsonSerializer.cs
josefpihrt ea3f7ad
Update src/CommandLine/Json/DiagnosticGitLabJsonSerializer.cs
josefpihrt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| using Newtonsoft.Json; | ||
|
|
||
| namespace Roslynator.CommandLine.GitLab; | ||
|
|
||
| internal sealed class GitLabIssue | ||
| { | ||
| public string Type { get; set; } | ||
| public string Fingerprint { get; set; } | ||
| [JsonProperty("check_name")] | ||
| public string CheckName { get; set; } | ||
| public string Description { get; set; } | ||
| public string Severity { get; set; } | ||
| public GitLabIssueLocation Location { get; set; } | ||
| public string[] Categories { get; set; } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| namespace Roslynator.CommandLine.GitLab; | ||
|
|
||
| internal sealed class GitLabIssueLocation | ||
| { | ||
| public string Path { get; set; } | ||
| public GitLabLocationLines Lines { get; set; } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| namespace Roslynator.CommandLine.GitLab; | ||
|
|
||
| internal sealed class GitLabLocationLines | ||
| { | ||
| public int Begin { get; set; } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Security.Cryptography; | ||
| using System.Text; | ||
| using Microsoft.CodeAnalysis; | ||
| using Newtonsoft.Json; | ||
| using Newtonsoft.Json.Serialization; | ||
| using Roslynator.CommandLine.GitLab; | ||
| using Roslynator.Diagnostics; | ||
|
|
||
| namespace Roslynator.CommandLine.Json; | ||
|
|
||
| internal static class DiagnosticGitLabJsonSerializer | ||
| { | ||
| private static readonly JsonSerializerSettings _jsonSerializerSettings = new() | ||
| { | ||
| Formatting = Newtonsoft.Json.Formatting.Indented, | ||
| NullValueHandling = NullValueHandling.Ignore, | ||
| ContractResolver = new DefaultContractResolver() | ||
| { | ||
| NamingStrategy = new CamelCaseNamingStrategy() | ||
| }, | ||
| }; | ||
|
|
||
| public static void Serialize( | ||
| IEnumerable<ProjectAnalysisResult> results, | ||
| string filePath, | ||
| IFormatProvider formatProvider = null) | ||
| { | ||
| IEnumerable<DiagnosticInfo> diagnostics = results.SelectMany(f => f.CompilerDiagnostics.Concat(f.Diagnostics)); | ||
|
|
||
| var reportItems = new List<GitLabIssue>(); | ||
| foreach (DiagnosticInfo diagnostic in diagnostics) | ||
| { | ||
| GitLabIssueLocation location = null; | ||
| if (diagnostic.LineSpan.IsValid) | ||
| { | ||
| location = new GitLabIssueLocation() | ||
| { | ||
| Path = diagnostic.LineSpan.Path, | ||
| Lines = new GitLabLocationLines() | ||
| { | ||
| Begin = diagnostic.LineSpan.StartLinePosition.Line | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| var severity = "minor"; | ||
| severity = diagnostic.Severity switch | ||
| { | ||
| DiagnosticSeverity.Warning => "major", | ||
| DiagnosticSeverity.Error => "critical", | ||
| _ => "minor", | ||
| }; | ||
|
|
||
| string issueFingerPrint = $"{diagnostic.Descriptor.Id}-{diagnostic.Severity}-{location?.Path}-{location?.Lines.Begin}"; | ||
| byte[] source = Encoding.UTF8.GetBytes(issueFingerPrint); | ||
| byte[] hashBytes; | ||
| #if NETFRAMEWORK | ||
| using (var sha256 = SHA256.Create()) | ||
| hashBytes = sha256.ComputeHash(source); | ||
| #else | ||
| hashBytes = SHA256.HashData(source); | ||
| #endif | ||
| issueFingerPrint = BitConverter.ToString(hashBytes) | ||
| .Replace("-", "") | ||
| .ToLowerInvariant(); | ||
|
|
||
| reportItems.Add(new GitLabIssue() | ||
| { | ||
| Type = "issue", | ||
| Fingerprint = issueFingerPrint, | ||
| CheckName = diagnostic.Descriptor.Id, | ||
| Description = diagnostic.Descriptor.Title.ToString(formatProvider), | ||
| Severity = severity, | ||
| Location = location, | ||
| Categories = new string[] { diagnostic.Descriptor.Category }, | ||
| }); | ||
| } | ||
|
|
||
| string report = JsonConvert.SerializeObject(reportItems, _jsonSerializerSettings); | ||
|
|
||
| File.WriteAllText(filePath, report, Encoding.UTF8); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.