|
| 1 | +package printers |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "io" |
| 6 | + |
| 7 | + "github.com/golangci/golangci-lint/pkg/result" |
| 8 | +) |
| 9 | + |
| 10 | +const ( |
| 11 | + sarifVersion = "2.1.0" |
| 12 | + sarifSchemaURI = "https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0-rtm.6.json" |
| 13 | +) |
| 14 | + |
| 15 | +type SarifOutput struct { |
| 16 | + Version string `json:"version"` |
| 17 | + Schema string `json:"$schema"` |
| 18 | + Runs []sarifRun `json:"runs"` |
| 19 | +} |
| 20 | + |
| 21 | +type sarifRun struct { |
| 22 | + Tool sarifTool `json:"tool"` |
| 23 | + Results []sarifResult `json:"results"` |
| 24 | +} |
| 25 | + |
| 26 | +type sarifTool struct { |
| 27 | + Driver struct { |
| 28 | + Name string `json:"name"` |
| 29 | + } `json:"driver"` |
| 30 | +} |
| 31 | + |
| 32 | +type sarifResult struct { |
| 33 | + RuleID string `json:"ruleId"` |
| 34 | + Level string `json:"level"` |
| 35 | + Message sarifMessage `json:"message"` |
| 36 | + Locations []sarifLocation `json:"locations"` |
| 37 | +} |
| 38 | + |
| 39 | +type sarifMessage struct { |
| 40 | + Text string `json:"text"` |
| 41 | +} |
| 42 | + |
| 43 | +type sarifLocation struct { |
| 44 | + PhysicalLocation sarifPhysicalLocation `json:"physicalLocation"` |
| 45 | +} |
| 46 | + |
| 47 | +type sarifPhysicalLocation struct { |
| 48 | + ArtifactLocation sarifArtifactLocation `json:"artifactLocation"` |
| 49 | + Region sarifRegion `json:"region"` |
| 50 | +} |
| 51 | + |
| 52 | +type sarifArtifactLocation struct { |
| 53 | + URI string `json:"uri"` |
| 54 | + Index int `json:"index"` |
| 55 | +} |
| 56 | + |
| 57 | +type sarifRegion struct { |
| 58 | + StartLine int `json:"startLine"` |
| 59 | + StartColumn int `json:"startColumn"` |
| 60 | +} |
| 61 | + |
| 62 | +type Sarif struct { |
| 63 | + w io.Writer |
| 64 | +} |
| 65 | + |
| 66 | +func NewSarif(w io.Writer) *Sarif { |
| 67 | + return &Sarif{w: w} |
| 68 | +} |
| 69 | + |
| 70 | +func (p Sarif) Print(issues []result.Issue) error { |
| 71 | + run := sarifRun{} |
| 72 | + run.Tool.Driver.Name = "golangci-lint" |
| 73 | + |
| 74 | + for i := range issues { |
| 75 | + issue := issues[i] |
| 76 | + |
| 77 | + severity := issue.Severity |
| 78 | + if severity == "" { |
| 79 | + severity = "error" |
| 80 | + } |
| 81 | + |
| 82 | + sr := sarifResult{ |
| 83 | + RuleID: issue.FromLinter, |
| 84 | + Level: severity, |
| 85 | + Message: sarifMessage{Text: issue.Text}, |
| 86 | + Locations: []sarifLocation{ |
| 87 | + { |
| 88 | + PhysicalLocation: sarifPhysicalLocation{ |
| 89 | + ArtifactLocation: sarifArtifactLocation{URI: issue.FilePath()}, |
| 90 | + Region: sarifRegion{ |
| 91 | + StartLine: issue.Line(), |
| 92 | + StartColumn: issue.Column(), |
| 93 | + }, |
| 94 | + }, |
| 95 | + }, |
| 96 | + }, |
| 97 | + } |
| 98 | + |
| 99 | + run.Results = append(run.Results, sr) |
| 100 | + } |
| 101 | + |
| 102 | + output := SarifOutput{ |
| 103 | + Version: sarifVersion, |
| 104 | + Schema: sarifSchemaURI, |
| 105 | + Runs: []sarifRun{run}, |
| 106 | + } |
| 107 | + |
| 108 | + return json.NewEncoder(p.w).Encode(output) |
| 109 | +} |
0 commit comments