forked from clj-kondo/clj-kondo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix clj-kondo#2345: SARIF output fix and some enhancements
- Fix the nesting level of `region` under the `results` property - Enhance SARIF output - Add Clj-kondo `version` to the SARIF output - Add `endLine` and `endColumn` to the linting results - Add `helpUri`, `level` and `enabled` to the linting rules
- Loading branch information
Showing
1 changed file
with
31 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,36 +1,48 @@ | ||
(ns clj-kondo.impl.sarif | ||
(:require [clj-kondo.impl.config :as config])) | ||
(:require | ||
[clj-kondo.impl.config :as config] | ||
[clj-kondo.impl.version :as version])) | ||
|
||
;; https://github.com/microsoft/sarif-tutorials/blob/main/docs/1-Introduction.md#simple-example | ||
|
||
(set! *warn-on-reflection* true) | ||
|
||
(defn finding->sarif [rules files finding] | ||
{:level (:level finding) | ||
(defn- linter-help-uri [linter] | ||
(format "https://github.com/clj-kondo/clj-kondo/blob/master/doc/linters.md#%s" (name linter))) | ||
|
||
(defn- sarif-level [level] | ||
(case level | ||
:error "error" | ||
:warning "warning" | ||
:off "none")) | ||
|
||
(defn- finding->sarif [finding] | ||
{:level (-> finding :level sarif-level) | ||
:message {:text (:message finding)} | ||
:locations [{:physicalLocation | ||
{:artifactLocation | ||
{:uri (:filename finding) | ||
:index (.indexOf ^java.util.List files (:filename finding)) | ||
:region {:startLine (:row finding) | ||
:startColumn (:col finding)}}}}] | ||
:ruleId (:type finding) | ||
:ruleIndex (:index (get rules (:type finding)))}) | ||
{:artifactLocation {:uri (:filename finding)} | ||
:region {:startLine (:row finding) | ||
:startColumn (:col finding) | ||
:endLine (:end-row finding) | ||
:endColumn (:end-col finding)}}}] | ||
:ruleId (:type finding)}) | ||
|
||
(defn generate-sarif [{:keys [findings]}] | ||
(let [linters (:linters config/default-config) | ||
rules (zipmap (keys linters) | ||
(mapv (fn [[k _] i] | ||
{:id k :index i}) | ||
(mapv (fn [[linter {:keys [level]}] i] | ||
{:id linter | ||
:helpUri (linter-help-uri linter) | ||
:defaultConfiguration | ||
{:enabled (if (= :off level) false true) | ||
:level (sarif-level level)} | ||
:index i}) | ||
linters | ||
(range))) | ||
files (vec (distinct (map :filename findings)))] | ||
(range)))] | ||
{:version "2.1.0" | ||
"$schema" "http://json.schemastore.org/sarif-2.1.0-rtm.4" | ||
:$schema "https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/schemas/sarif-schema-2.1.0.json" | ||
:runs [{:tool {:driver {:name "Clj-kondo" | ||
:version version/version | ||
:informationUri "https://github.com/clj-kondo/clj-kondo" | ||
:rules (mapv #(dissoc % :index) (vals rules))}} | ||
:artifacts (mapv (fn [file] | ||
{:location {:uri file}}) | ||
files) | ||
:results (mapv #(finding->sarif rules files %) findings)}]})) | ||
:results (mapv #(finding->sarif %) findings)}]})) |