gpt4 book ai didi

c# - 在 MSBuild 中解析 "Static Analysis Results Interchange Format (SARIF)"

转载 作者:行者123 更新时间:2023-12-01 20:16:12 24 4
gpt4 key购买 nike

当使用 MSBuild 针对项目运行各种分析器时,所有失败都将以“静态分析结果交换格式 (SARIF)”格式输出(参见例如 https://github.com/sarif-standard/sarif-spec )。例如,构建可能会产生以下结果

{
"version": "0.1",
"toolInfo": {
"toolName": "Microsoft (R) Visual C# Compiler",
"productVersion": "1.1.0",
"fileVersion": "1.1.0"
},
"issues": [
{
"ruleId": "SA1401",
"locations": [
{
"analysisTarget": [
{
"uri": "C:\\SomeFile.cs",
"region": {
"startLine": 708,
"startColumn": 30,
"endLine": 708,
"endColumn": 36
}
}
]
}
],
"shortMessage": "Field must be private",
"fullMessage": "A field within a C# class has an access modifier other than private.",
"properties": {
"severity": "Warning",
"warningLevel": "1",
"defaultSeverity": "Warning",
"title": "Fields must be private",
"category": "StyleCop.CSharp.MaintainabilityRules",
"helpLink": "https:\/\/github.com\/DotNetAnalyzers\/StyleCopAnalyzers\/blob\/master\/documentation\/SA1401.md",
"isEnabledByDefault": "True",
"isSuppressedInSource": "True"
}
}
]
}

现在我希望能够以最简单的方式解析上面的数据(如果遇到任何非抑制问题,则中断构建)。如何着手做到这一点?

PS。最好我还想避免实现我自己的 MSBuild 任务和安装特定软件(例如 PowerShell 3.0 - ConvertFrom-Json)。

最佳答案

有一个 SARIF SDK 可用于处理 SARIF 文件。它可以作为 NuGet package Sarif.Sdk 提供。 ,源代码位于 GitHub 上的 Microsoft/sarif-sdk 项目中,带有 How-To document显示如何从磁盘读取 SARIF 文件并将其反序列化为 SarifLog 对象;然后您可以浏览 SARIF 对象模型来检查各个结果。

就您而言,您对结果的“属性包”中的 isSuppressedInSource 属性感兴趣。操作方法文档解释了如何检索该属性:

Result result = …;

string isSuppressedInSource = result.GetProperty("isSuppressedInSource");

SARIF 规范为 available online ,并且有一个 SARIF home page包含更多信息的链接。

最后:请注意,SARIF 格式在 Visual Studio 2015 Update 2 和 Update 3 之间发生了显着变化。该格式现在处于稳定的 1.0.0 版本。

关于c# - 在 MSBuild 中解析 "Static Analysis Results Interchange Format (SARIF)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36045924/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com