Skip to content

Commit

Permalink
Merge pull request #362 from coinbase/report_filter
Browse files Browse the repository at this point in the history
add report filter
  • Loading branch information
ghbren authored May 12, 2021
2 parents 80ea880 + afc7e35 commit 6c9a6a5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
19 changes: 18 additions & 1 deletion lib/salus/report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class ExportReportError < StandardError; end

attr_reader :builds

@@filters = []

def initialize(report_uris: [], builds: {}, project_name: nil, custom_info: nil, config: nil,
repo_path: nil, filter_sarif: nil, ignore_config_id: nil)
@report_uris = report_uris # where we will send this report
Expand All @@ -40,6 +42,19 @@ def initialize(report_uris: [], builds: {}, project_name: nil, custom_info: nil,
@ignore_config_id = ignore_config_id # ignore id in salus config
end

def apply_report_hash_filters(report_hash)
@@filters.each do |filter|
if filter.respond_to?(:filter_report_hash)
report_hash = filter.filter_report_hash(report_hash)
end
end
report_hash
end

def self.register_filter(filter)
@@filters << filter
end

def record
started_at = monotime
yield
Expand All @@ -62,7 +77,7 @@ def error(hsh)
def to_h
scans = @scan_reports.map { |report, _required| [report.scanner_name, report.to_h] }.to_h

{
report_hash = {
version: VERSION,
project_name: @project_name,
passed: passed?,
Expand All @@ -72,6 +87,8 @@ def to_h
custom_info: @custom_info,
config: @config
}.compact

apply_report_hash_filters(report_hash)
end

# Generates the text report.
Expand Down
11 changes: 11 additions & 0 deletions spec/fixtures/blank_repository2/test_plugins/report_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Filter
module TestReportFilter
def self.filter_report_hash(report_hash)
report_hash[:config][:builds] ||= {}
report_hash[:config][:builds][:mykey] = 'myval'
report_hash
end
end

Salus::Report.register_filter(Filter::TestReportFilter)
end
3 changes: 2 additions & 1 deletion spec/lib/salus_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@
expected_builds = { "abc" => "xyz",
"abcd" => "xyzw",
"service_name" => "circle_CI",
"url" => "my_url" }
"url" => "my_url",
"mykey" => "myval" }
expect(builds).to eq(expected_builds)
end
end
Expand Down

0 comments on commit 6c9a6a5

Please sign in to comment.