Skip to content
This repository has been archived by the owner on Jul 24, 2021. It is now read-only.

Commit

Permalink
Fix json exit code
Browse files Browse the repository at this point in the history
  • Loading branch information
rtfpessoa committed Oct 28, 2018
1 parent b9c0597 commit 58fe0d0
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 40 deletions.
14 changes: 7 additions & 7 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
dependency_spy (0.2.1)
dependency_spy (0.2.2)
bibliothecary (~> 6.3)
semantic_range (~> 2.1)
thor (~> 0.20)
Expand All @@ -23,15 +23,15 @@ GEM
citrus (3.0.2)
codacy-coverage (2.1.0)
simplecov
commander (4.4.6)
highline (~> 1.7.2)
commander (4.4.7)
highline (~> 2.0.0)
deb_control (0.0.1)
diff-lcs (1.3)
docile (1.3.1)
ethon (0.11.0)
ffi (>= 1.3.0)
ffi (1.9.25)
highline (1.7.10)
highline (2.0.0)
jaro_winkler (1.5.1)
json (2.1.0)
kramdown (1.17.0)
Expand Down Expand Up @@ -61,14 +61,14 @@ GEM
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.8.0)
rspec-support (3.8.0)
rubocop (0.59.2)
rubocop (0.60.0)
jaro_winkler (~> 1.5.1)
parallel (~> 1.10)
parser (>= 2.5, != 2.5.1.1)
powerpack (~> 0.1)
rainbow (>= 2.2.2, < 4.0)
ruby-progressbar (~> 1.7)
unicode-display_width (~> 1.0, >= 1.0.1)
unicode-display_width (~> 1.4.0)
rubocop-rspec (1.30.0)
rubocop (>= 0.58.0)
ruby-ll (2.1.2)
Expand All @@ -89,7 +89,7 @@ GEM
typhoeus (1.3.0)
ethon (>= 0.9.0)
unicode-display_width (1.4.0)
yavdb (0.4.0)
yavdb (0.4.1)
json (~> 2.1)
kramdown (~> 1.17)
oga (~> 2.15)
Expand Down
2 changes: 1 addition & 1 deletion lib/dependency_spy/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def check
end

has_vulnerabilities =
manifests.any? { |manifest| manifest.dependencies.any? { |dependency| dependency.vulnerabilities.any? } }
manifests.any? { |manifest| manifest[:dependencies]&.any? { |dependency| dependency[:vulnerabilities]&.any? } }

exit(1) if has_vulnerabilities
end
Expand Down
36 changes: 22 additions & 14 deletions lib/dependency_spy/dtos/dependency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,25 @@ class Manifest < Struct.new(
def to_map
map = {}
members.each do |m|
next unless self[m] && (
(self[m].is_a?(String) && !self[m].empty?) ||
(self[m].is_a?(Array) && self[m].any?))

map[m.to_s] = self[m] if self[m]
if !self[m] ||
(self[m].is_a?(String) && self[m].empty?) ||
(self[m].is_a?(Array) && self[m].none?)
next
elsif self[m].is_a?(Struct)
map[m.to_s] = self[m].to_map
else
map[m.to_s] = self[m]
end
end
map
end

def to_json(*attrs)
to_map.to_json(*attrs)
def to_json(*args)
to_map.to_json(*args)
end

def to_yaml(*attrs)
to_map.to_yaml(*attrs)
def to_yaml(*args)
to_map.to_yaml(*args)
end

end
Expand All @@ -55,11 +59,15 @@ class Dependency < Struct.new(
def to_map
map = {}
members.each do |m|
next unless self[m] && (
(self[m].is_a?(String) && !self[m].empty?) ||
(self[m].is_a?(Array) && self[m].any?))

map[m.to_s] = self[m] if self[m]
if !self[m] ||
(self[m].is_a?(String) && self[m].empty?) ||
(self[m].is_a?(Array) && self[m].none?)
next
elsif self[m].is_a?(Struct)
map[m.to_s] = self[m].to_map
else
map[m.to_s] = self[m]
end
end
map
end
Expand Down
13 changes: 5 additions & 8 deletions lib/dependency_spy/formatters/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,15 @@ class Json

def self.format(manifests)
filtered_manifests = manifests.map do |manifest|
manifest[:dependencies] = manifest[:dependencies].map do |dependency|
next unless dependency[:vulnerabilities].any?

dependency[:vulnerabilities] = dependency[:vulnerabilities].map(&:to_map)
dependency
end.reject(&:nil?).map(&:to_map)
manifest
vulnerable_dependencies = manifest[:dependencies]
.select { |dependency| dependency[:vulnerabilities].any? }
manifest_copy = Marshal.load(Marshal.dump(manifest))
manifest_copy[:dependencies] = vulnerable_dependencies
manifest_copy
end

filtered_manifests
.reject { |m| m[:dependencies].nil? }
.map(&:to_map)
.map(&:to_json)
end

Expand Down
15 changes: 6 additions & 9 deletions lib/dependency_spy/formatters/yaml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,16 @@ class Yaml

def self.format(manifests)
filtered_manifests = manifests.map do |manifest|
manifest[:dependencies] = manifest[:dependencies].map do |dependency|
next unless dependency[:vulnerabilities].any?

dependency[:vulnerabilities] = dependency[:vulnerabilities].map(&:to_map)
dependency
end.reject(&:nil?).map(&:to_map)
manifest
vulnerable_dependencies = manifest[:dependencies]
.select { |dependency| dependency[:vulnerabilities].any? }
manifest_copy = Marshal.load(Marshal.dump(manifest))
manifest_copy[:dependencies] = vulnerable_dependencies
manifest_copy
end

filtered_manifests
.reject { |m| m[:dependencies].nil? }
.map(&:to_map)
.map(&:to_yaml)
.map(&:to_json)
end

end
Expand Down
2 changes: 1 addition & 1 deletion lib/dependency_spy/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@

module DependencySpy

VERSION = '0.2.1'
VERSION = '0.2.2'

end

0 comments on commit 58fe0d0

Please sign in to comment.