-
Notifications
You must be signed in to change notification settings - Fork 339
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Added] Add support for JSON reports [#161595251]
Signed-off-by: Parv Mital <pmital@pivotal.io>
- Loading branch information
1 parent
2b14299
commit 5a1f735
Showing
4 changed files
with
60 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require 'csv' | ||
|
||
module LicenseFinder | ||
class JsonReport < CsvReport | ||
def initialize(dependencies, options) | ||
super(dependencies, options) | ||
end | ||
|
||
def to_s | ||
{dependencies: build_deps}.to_json | ||
end | ||
|
||
private | ||
|
||
def build_deps | ||
sorted_dependencies.map do |dep| | ||
@columns.inject({}) do |memo, column| | ||
memo[column] = send("format_#{column}", dep) | ||
memo | ||
end | ||
end | ||
end | ||
|
||
def format_licenses(dep) | ||
dep.missing? ? [] : dep.licenses.map(&:name) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'json' | ||
require 'spec_helper' | ||
|
||
module LicenseFinder | ||
describe JsonReport do | ||
it 'understands many columns' do | ||
dep = Package.new('gem_a', '1.0', authors: 'the authors', | ||
description: 'A description', summary: 'A summary', | ||
homepage: 'http://homepage.example.com') | ||
dep.decide_on_license(License.find_by_name('MIT')) | ||
dep.decide_on_license(License.find_by_name('GPL')) | ||
dep.whitelisted! | ||
subject = described_class.new([dep], columns: %w[name version authors licenses approved summary description homepage]) | ||
expected = { | ||
dependencies: | ||
[ | ||
{ | ||
name: 'gem_a', version: '1.0', authors: 'the authors', licenses: ['MIT','GPL'], | ||
approved: 'Approved', summary: 'A summary', description: 'A description', homepage: 'http://homepage.example.com' | ||
} | ||
] | ||
}.to_json | ||
|
||
expect(subject.to_s).to eq(expected) | ||
end | ||
end | ||
end |