Sha256: 732de6b947b19f972affa53001b0b25ba65b6b2af12dbf3a477ee0baf10d0ca3

Contents?: true

Size: 1.98 KB

Versions: 7

Compression:

Stored size: 1.98 KB

Contents

require "json" unless defined?(JSON)

module Inspec::Reporters
  class JsonAutomate < Json
    def initialize(config)
      super(config)
      @profiles = []
    end

    def render
      output(report.to_json, false)
    end

    def report
      # grab profiles from the json parent class
      @profiles = profiles

      output = {
        platform: platform,
        profiles: merge_profiles,
        statistics: {
          duration: run_data[:statistics][:duration],
        },
        version: run_data[:version],
      }

      # optional json-config passthrough options
      %w{node_name environment roles job_uuid passthrough}.each do |option|
        output[option.to_sym] = @config[option] unless @config[option].nil?
      end
      output
    end

    private

    def merge_profiles
      @profiles.each do |profile|
        next unless profile.key?(:parent_profile)

        parent_profile = find_master_parent(profile)
        merge_controls(parent_profile, profile)
        merge_depends(parent_profile, profile)
      end

      # delete child profiles
      @profiles.delete_if { |p| p.key?(:parent_profile) }

      @profiles
    end

    def find_master_parent(profile)
      return profile unless profile.key?(:parent_profile)

      parent_profile = @profiles.select { |parent| parent[:name] == profile[:parent_profile] }.first
      find_master_parent(parent_profile)
    end

    def merge_controls(parent, child)
      parent[:controls].each do |control|
        child_control = child[:controls].select { |c| c[:id] == control[:id] }.first
        next if child_control.nil?

        control.each do |name, _value|
          child_value = child_control[name]
          next if child_value.nil? || (child_value.respond_to?(:empty?) && child_value.empty?)

          control[name] = child_value
        end
      end
    end

    def merge_depends(parent, child)
      return unless child.key?(:depends)

      child[:depends].each do |d|
        parent[:depends] << d
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
inspec-core-4.28.0 lib/inspec/reporters/json_automate.rb
inspec-core-4.26.13 lib/inspec/reporters/json_automate.rb
inspec-core-4.26.4 lib/inspec/reporters/json_automate.rb
inspec-core-4.25.1 lib/inspec/reporters/json_automate.rb
inspec-core-4.24.32 lib/inspec/reporters/json_automate.rb
inspec-core-4.24.28 lib/inspec/reporters/json_automate.rb
inspec-core-4.24.26 lib/inspec/reporters/json_automate.rb