Skip to content

Commit

Permalink
[Added] Add support for JSON reports [#161595251]
Browse files Browse the repository at this point in the history
Signed-off-by: Parv Mital <pmital@pivotal.io>
  • Loading branch information
Vikram Yadav authored and pivotal-pmital committed Nov 12, 2018
1 parent 2b14299 commit 5a1f735
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/license_finder/cli/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class Main < Base
'html' => HtmlReport,
'markdown' => MarkdownReport,
'csv' => CsvReport,
'xml' => XmlReport
'xml' => XmlReport,
'json' => JsonReport
}.freeze

class_option :go_full_version, desc: 'Whether dependency version should include full version. Only meaningful if used with a Go project. Defaults to false.'
Expand Down
1 change: 1 addition & 0 deletions lib/license_finder/report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ def sorted_dependencies
require 'license_finder/reports/html_report'
require 'license_finder/reports/markdown_report'
require 'license_finder/reports/xml_report'
require 'license_finder/reports/json_report'
28 changes: 28 additions & 0 deletions lib/license_finder/reports/json_report.rb
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
29 changes: 29 additions & 0 deletions spec/lib/license_finder/reports/json_report_spec.rb
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

0 comments on commit 5a1f735

Please sign in to comment.