class LicenseScout::Reporter

Attributes

all_dependencies[R]
dependency_license_manifest[R]
results[R]

Public Class Methods

new(all_dependencies) click to toggle source
# File lib/license_scout/reporter.rb, line 82
def initialize(all_dependencies)
  @all_dependencies = all_dependencies.sort
  @results = {}
  @did_fail = false
  @needs_fallback = false
  @needs_exception = false
end

Public Instance Methods

report() click to toggle source
# File lib/license_scout/reporter.rb, line 90
def report
  generate_dependency_license_manifest
  save_manifest_file
  detect_problems
  evaluate_results
end

Private Instance Methods

detect_problems() click to toggle source
# File lib/license_scout/reporter.rb, line 106
def detect_problems
  LicenseScout::Log.info("[reporter] Analyzing dependency's license information against requirements")

  LicenseScout::Log.info("[reporter] Allowed licenses: #{LicenseScout::Config.allowed_licenses.join(", ")}") unless LicenseScout::Config.allowed_licenses.empty?
  LicenseScout::Log.info("[reporter] Flagged licenses: #{LicenseScout::Config.flagged_licenses.join(", ")}") unless LicenseScout::Config.flagged_licenses.empty?

  all_dependencies.each do |dependency|
    @results[dependency.type] ||= []

    if dependency.license.records.empty?
      @results[dependency.type] << Result.failure(dependency, :missing)
      @did_fail = true
      @needs_fallback = true
    elsif dependency.license.undetermined?
      @results[dependency.type] << Result.failure(dependency, :undetermined)
      @did_fail = true
      @needs_fallback = true
    elsif !LicenseScout::Config.allowed_licenses.empty? && !dependency.license.is_allowed?
      unless dependency.has_exception?
        @results[dependency.type] << Result.failure(dependency, :not_allowed)
        @did_fail = true
        @needs_exception = true
      else
        @results[dependency.type] << Result.success(dependency)
      end
    elsif dependency.license.is_flagged?
      unless dependency.has_exception?
        @results[dependency.type] << Result.failure(dependency, :flagged)
        @did_fail = true
        @needs_exception = true
      else
        @results[dependency.type] << Result.success(dependency)
      end
    else
      @results[dependency.type] << Result.success(dependency)
    end
  end
end
evaluate_results() click to toggle source
# File lib/license_scout/reporter.rb, line 145
def evaluate_results
  table = Terminal::Table.new
  table.headings = ["Type", "Dependency", "License(s)", "Results"]
  table.style = { border_bottom: false } # the extra :separator will add this

  results.each do |type, results_for_type|
    type_in_table = false

    results_for_type.each do |result|
      next if LicenseScout::Config.only_show_failures && result.succeeded?

      modified_row = []
      modified_row << (type_in_table ? "" : type)
      modified_row << result.dependency_string
      modified_row << result.license_string
      modified_row << result.reason_string

      type_in_table = true
      table.add_row(modified_row)
    end

    table.add_separator if type_in_table
  end

  puts table unless LicenseScout::Config.only_show_failures && !@did_fail

  if @did_fail
    puts
    puts "Additional steps are required in order to pass Open Source license compliance:"
    puts "  * Please add fallback licenses for the 'Missing' or 'Undetermined' dependencies"   if @needs_fallback
    puts "         https://github.com/chef/license_scout#fallback-licenses"                    if @needs_fallback
    puts "  * Please add exceptions for the 'Flagged' or 'Not Allowed' dependencies"           if @needs_exception
    puts "         https://github.com/chef/license_scout#dependency-exceptions"                if @needs_exception

    raise Exceptions::FailExit.new("missing or not allowed licenses detected")
  end
end
generate_dependency_license_manifest() click to toggle source
# File lib/license_scout/reporter.rb, line 183
def generate_dependency_license_manifest
  @dependency_license_manifest = {
    license_manifest_version: 2,
    generated_on: DateTime.now.to_s,
    name: LicenseScout::Config.name,
    dependencies: [],
  }

  all_dependencies.each do |dep|
    dependency_license_manifest[:dependencies] << {
      type: dep.type,
      name: dep.name,
      version: dep.version,
      has_exception: dep.has_exception?,
      exception_reason: dep.exception_reason,
      licenses: dep.license.records.map(&:to_h),
    }
  end
end
license_manifest_path() click to toggle source
# File lib/license_scout/reporter.rb, line 203
def license_manifest_path
  File.join(LicenseScout::Config.output_directory, "#{LicenseScout::Config.name}-dependency-licenses.json")
end
save_manifest_file() click to toggle source
# File lib/license_scout/reporter.rb, line 99
def save_manifest_file
  LicenseScout::Log.info("[reporter] Writing dependency license manifest written to #{license_manifest_path}")
  File.open(license_manifest_path, "w+") do |file|
    file.print(FFI_Yajl::Encoder.encode(dependency_license_manifest, pretty: true))
  end
end