File: sarif.rb

package info (click to toggle)
ruby-mdl 0.13.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 196 kB
  • sloc: ruby: 1,485; python: 40; makefile: 4; sh: 3
file content (89 lines) | stat: -rw-r--r-- 2,722 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
require 'json'

module MarkdownLint
  # SARIF formatter
  #
  # @see https://docs.oasis-open.org/sarif/sarif/v2.1.0/sarif-v2.1.0.html
  class SarifFormatter
    class << self
      def generate(rules, results)
        matched_rules_id = results.map { |result| result['rule'] }.uniq
        matched_rules = rules.select { |id, _| matched_rules_id.include?(id) }
        JSON.generate(generate_sarif(matched_rules, results))
      end

      def generate_sarif(rules, results)
        {
          :'$schema' => 'https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json',
          :version => '2.1.0',
          :runs => [
            {
              :tool => {
                :driver => {
                  :name => 'Markdown lint',
                  :version => MarkdownLint::VERSION,
                  :informationUri => 'https://github.com/markdownlint/markdownlint',
                  :rules => generate_sarif_rules(rules),
                },
              },
              :results => generate_sarif_results(rules, results),
            }
          ],
        }
      end

      def generate_sarif_rules(rules)
        rules.map do |id, rule|
          {
            :id => id,
            :name => rule.aliases.first.split('-').map(&:capitalize).join,
            :defaultConfiguration => {
              :level => 'note',
            },
            :properties => {
              :description => rule.description,
              :tags => rule.tags,
              :queryURI => rule.docs_url,
            },
            :shortDescription => {
              :text => rule.description,
            },
            :fullDescription => {
              :text => rule.description,
            },
            :helpUri => rule.docs_url,
            :help => {
              :text => "More info: #{rule.docs_url}",
              :markdown => "[More info](#{rule.docs_url})",
            },
          }
        end
      end

      def generate_sarif_results(rules, results)
        results.map do |result|
          {
            :ruleId => result['rule'],
            :ruleIndex => rules.find_index { |id, _| id == result['rule'] },
            :message => {
              :text => "#{result['rule']} - #{result['description']}",
            },
            :locations => [
              {
                :physicalLocation => {
                  :artifactLocation => {
                    :uri => result['filename'],
                    :uriBaseId => '%SRCROOT%',
                  },
                  :region => {
                    :startLine => result['line'],
                  },
                },
              }
            ],
          }
        end
      end
    end
  end
end