forked from tomferreira/action-bundler-audit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrdjson_formatter.rb
executable file
·63 lines (51 loc) · 1.32 KB
/
rdjson_formatter.rb
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
#!/usr/bin/env ruby
require 'json'
GEMFILE_LOCK_PATH = "Gemfile.lock"
CRITICALITY_RANK = {
none: 0,
low: 1,
medium: 2,
high: 3,
critical: 3,
nil => 1,
}
SEVERITY = ["UNKNOWN_SEVERITY", "INFO", "WARNING", "ERROR"]
input_json = JSON.parse(ARGF.read)
max_criticality_rank = 0
results = input_json["results"]
diagnostics = results.map do |result|
gem_name = result.dig("gem", "name")
message = <<~EOS
Title: #{result.dig("advisory", "title")}
Solution: upgrade to #{result.dig("advisory", "patched_versions").map{|v| "'#{v}'"}.join(', ')}
EOS
criticality_rank = CRITICALITY_RANK[result.dig("advisory", "criticality")&.to_sym]
max_criticality_rank = [max_criticality_rank, criticality_rank].max
line = `grep -n -E '^\s{4}#{gem_name}' #{GEMFILE_LOCK_PATH} | cut -d : -f 1`.to_i
diagnostic = {
message: message,
location: {
path: GEMFILE_LOCK_PATH,
range: {
start: {
line: line,
column: 0
}
}
},
severity: SEVERITY[criticality_rank],
code: {
value: result.dig("advisory", "id"),
url: result.dig("advisory", "url")
}
}
end
result = {
source: {
name: "bundler-audit",
url: "https://github.com/rubysec/bundler-audit"
},
severity: SEVERITY[max_criticality_rank],
diagnostics: diagnostics
}
puts result.to_json