Class: Danger::DangerSarif

Inherits:
Plugin
  • Object
show all
Defined in:
lib/sarif/plugin.rb

Overview

Danger plugin for reporting SARIF file.

Examples:

report from SARIF file


sarif.report 'app/build/reports/lint-results-debug.sarif'

report from multiple SARIF files


Dir['**/build/reports/lint-results-*.sarif'].each do |file|
  sarif.report file
end

See Also:

  • irgaly/danger-sarif

Defined Under Namespace

Classes: Warning

Instance Method Summary collapse

Instance Method Details

#parse(file, base_dir: nil) ⇒ DangerSarif::Warning

Parse SARIF file, then return Warnings



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/sarif/plugin.rb', line 37

def parse(file, base_dir: nil)
  raise "SARIF file was not found: #{file}" unless File.exist? file
  base_dir_path = Pathname.new(base_dir || Dir.pwd)
  json = JSON.parse(File.read(file))
  json["runs"].flat_map do |run|
    base_uris = run["originalUriBaseIds"] || {}
    run["results"].flat_map do |result|
      message = result["message"]["markdown"] || result["message"]["text"]
      result["locations"].map do |location|
        physicalLocation = location["physicalLocation"]
        artifactLocation = physicalLocation["artifactLocation"]
        base_uri = base_uris[artifactLocation["uriBaseId"]]
        uri = artifactLocation["uri"]
        target_uri = if base_uri&.key?("uri") then
          File.join(base_uri["uri"], uri)
        else
          uri
        end
        file = begin
          target_path = Pathname.new(URI.parse(target_uri).path)
          target_path.relative_path_from(base_dir_path).to_s
          rescue ArgumentError
            target_path.to_s
        end
        line = physicalLocation["region"]["startLine"].to_i
        Warning.new(message: message, file: file, line: line)
      end
    end
  end
end

#report(file, base_dir: nil) ⇒ void

This method returns an undefined value.

Report errors from SARIF file



28
29
30
31
32
# File 'lib/sarif/plugin.rb', line 28

def report(file, base_dir: nil)
  parse(file, base_dir: base_dir).each do |warning|
    warn(warning.message, file: warning.file, line: warning.line)
  end
end