|
| 1 | +import * as sarif from 'sarif'; |
| 2 | + |
| 3 | +import { AnalysisAlert, ResultSeverity } from './shared/analysis-result'; |
| 4 | + |
| 5 | +const defaultSeverity = 'Warning'; |
| 6 | + |
| 7 | +export function extractAnalysisAlerts( |
| 8 | + sarifLog: sarif.Log |
| 9 | +): { |
| 10 | + alerts: AnalysisAlert[], |
| 11 | + errors: string[] |
| 12 | +} { |
| 13 | + if (!sarifLog) { |
| 14 | + return { alerts: [], errors: ['No SARIF log was found'] }; |
| 15 | + } |
| 16 | + |
| 17 | + if (!sarifLog.runs) { |
| 18 | + return { alerts: [], errors: ['No runs found in the SARIF file'] }; |
| 19 | + } |
| 20 | + |
| 21 | + const errors: string[] = []; |
| 22 | + const alerts: AnalysisAlert[] = []; |
| 23 | + |
| 24 | + for (const run of sarifLog.runs) { |
| 25 | + if (!run.results) { |
| 26 | + errors.push('No results found in the SARIF run'); |
| 27 | + continue; |
| 28 | + } |
| 29 | + |
| 30 | + for (const result of run.results) { |
| 31 | + const message = result.message?.text; |
| 32 | + if (!message) { |
| 33 | + errors.push('No message found in the SARIF result'); |
| 34 | + continue; |
| 35 | + } |
| 36 | + |
| 37 | + const severity = tryGetSeverity(run, result) || defaultSeverity; |
| 38 | + |
| 39 | + if (!result.locations) { |
| 40 | + errors.push('No locations found in the SARIF result'); |
| 41 | + continue; |
| 42 | + } |
| 43 | + |
| 44 | + for (const location of result.locations) { |
| 45 | + const contextRegion = location.physicalLocation?.contextRegion; |
| 46 | + if (!contextRegion) { |
| 47 | + errors.push('No context region found in the SARIF result location'); |
| 48 | + continue; |
| 49 | + } |
| 50 | + if (contextRegion.startLine === undefined) { |
| 51 | + errors.push('No start line set for a result context region'); |
| 52 | + continue; |
| 53 | + } |
| 54 | + if (contextRegion.endLine === undefined) { |
| 55 | + errors.push('No end line set for a result context region'); |
| 56 | + continue; |
| 57 | + } |
| 58 | + if (!contextRegion.snippet?.text) { |
| 59 | + errors.push('No text set for a result context region'); |
| 60 | + continue; |
| 61 | + } |
| 62 | + |
| 63 | + const region = location.physicalLocation?.region; |
| 64 | + if (!region) { |
| 65 | + errors.push('No region found in the SARIF result location'); |
| 66 | + continue; |
| 67 | + } |
| 68 | + if (region.startLine === undefined) { |
| 69 | + errors.push('No start line set for a result region'); |
| 70 | + continue; |
| 71 | + } |
| 72 | + if (region.startColumn === undefined) { |
| 73 | + errors.push('No start column set for a result region'); |
| 74 | + continue; |
| 75 | + } |
| 76 | + if (region.endColumn === undefined) { |
| 77 | + errors.push('No end column set for a result region'); |
| 78 | + continue; |
| 79 | + } |
| 80 | + |
| 81 | + const filePath = location.physicalLocation?.artifactLocation?.uri; |
| 82 | + if (!filePath) { |
| 83 | + errors.push('No file path found in the SARIF result location'); |
| 84 | + continue; |
| 85 | + } |
| 86 | + |
| 87 | + const analysisAlert = { |
| 88 | + message, |
| 89 | + filePath, |
| 90 | + severity, |
| 91 | + codeSnippet: { |
| 92 | + startLine: contextRegion.startLine, |
| 93 | + endLine: contextRegion.endLine, |
| 94 | + text: contextRegion.snippet.text |
| 95 | + }, |
| 96 | + highlightedRegion: { |
| 97 | + startLine: region.startLine, |
| 98 | + startColumn: region.startColumn, |
| 99 | + endLine: region.endLine, |
| 100 | + endColumn: region.endColumn |
| 101 | + } |
| 102 | + }; |
| 103 | + |
| 104 | + const validationErrors = getAlertValidationErrors(analysisAlert); |
| 105 | + if (validationErrors.length > 0) { |
| 106 | + errors.push(...validationErrors); |
| 107 | + continue; |
| 108 | + } |
| 109 | + |
| 110 | + alerts.push(analysisAlert); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + return { alerts, errors }; |
| 116 | +} |
| 117 | + |
| 118 | +export function tryGetSeverity( |
| 119 | + sarifRun: sarif.Run, |
| 120 | + result: sarif.Result |
| 121 | +): ResultSeverity | undefined { |
| 122 | + if (!sarifRun || !result) { |
| 123 | + return undefined; |
| 124 | + } |
| 125 | + |
| 126 | + const rule = tryGetRule(sarifRun, result); |
| 127 | + if (!rule) { |
| 128 | + return undefined; |
| 129 | + } |
| 130 | + |
| 131 | + const severity = rule.properties?.['problem.severity']; |
| 132 | + if (!severity) { |
| 133 | + return undefined; |
| 134 | + } |
| 135 | + |
| 136 | + switch (severity.toLowerCase()) { |
| 137 | + case 'recommendation': |
| 138 | + return 'Recommendation'; |
| 139 | + case 'warning': |
| 140 | + return 'Warning'; |
| 141 | + case 'error': |
| 142 | + return 'Error'; |
| 143 | + } |
| 144 | + |
| 145 | + return undefined; |
| 146 | +} |
| 147 | + |
| 148 | +export function tryGetRule( |
| 149 | + sarifRun: sarif.Run, |
| 150 | + result: sarif.Result |
| 151 | +): sarif.ReportingDescriptor | undefined { |
| 152 | + if (!sarifRun || !result) { |
| 153 | + return undefined; |
| 154 | + } |
| 155 | + |
| 156 | + const resultRule = result.rule; |
| 157 | + if (!resultRule) { |
| 158 | + return undefined; |
| 159 | + } |
| 160 | + |
| 161 | + // The rule can found in two places: |
| 162 | + // - Either in the run's tool driver tool component |
| 163 | + // - Or in the run's tool extensions tool component |
| 164 | + |
| 165 | + const ruleId = resultRule.id; |
| 166 | + if (ruleId) { |
| 167 | + const rule = sarifRun.tool.driver.rules?.find(r => r.id === ruleId); |
| 168 | + if (rule) { |
| 169 | + return rule; |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + const ruleIndex = resultRule.index; |
| 174 | + if (ruleIndex != undefined) { |
| 175 | + const toolComponentIndex = result.rule?.toolComponent?.index; |
| 176 | + const toolExtensions = sarifRun.tool.extensions; |
| 177 | + if (toolComponentIndex !== undefined && toolExtensions !== undefined) { |
| 178 | + const toolComponent = toolExtensions[toolComponentIndex]; |
| 179 | + if (toolComponent?.rules !== undefined) { |
| 180 | + return toolComponent.rules[ruleIndex]; |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + // Couldn't find the rule. |
| 186 | + return undefined; |
| 187 | +} |
| 188 | + |
| 189 | +function getAlertValidationErrors(alert: AnalysisAlert): string[] { |
| 190 | + const errors = []; |
| 191 | + |
| 192 | + if (alert.codeSnippet.startLine > alert.codeSnippet.endLine) { |
| 193 | + errors.push('The code snippet start line is greater than the end line'); |
| 194 | + } |
| 195 | + |
| 196 | + const highlightedRegion = alert.highlightedRegion; |
| 197 | + if (highlightedRegion.endLine === highlightedRegion.startLine && |
| 198 | + highlightedRegion.endColumn < highlightedRegion.startColumn) { |
| 199 | + errors.push('The highlighted region end column is greater than the start column'); |
| 200 | + } |
| 201 | + |
| 202 | + return errors; |
| 203 | +} |
0 commit comments