Skip to content

Commit

Permalink
Fix clj-kondo#2345: SARIF output fix and some enhancements
Browse files Browse the repository at this point in the history
- 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
nxvipin committed Jun 28, 2024
1 parent c16439a commit fb6f010
Showing 1 changed file with 31 additions and 19 deletions.
50 changes: 31 additions & 19 deletions src/clj_kondo/impl/sarif.clj
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)}]}))

0 comments on commit fb6f010

Please sign in to comment.