metz-scan 0.5.3 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/metz_scan/analyzers/inheritance_descendants.rb +1 -1
- data/lib/metz_scan/analyzers/namespace_leak_pressure.rb +1 -1
- data/lib/metz_scan/analyzers/package_dependency_pressure.rb +1 -1
- data/lib/metz_scan/analyzers/package_map.rb +2 -2
- data/lib/metz_scan/analyzers/test_calls_private_method/call_site_collector.rb +79 -0
- data/lib/metz_scan/analyzers/test_calls_private_method/finding.rb +23 -0
- data/lib/metz_scan/analyzers/test_calls_private_method/metadata.rb +27 -0
- data/lib/metz_scan/analyzers/test_calls_private_method/node_predicates.rb +57 -0
- data/lib/metz_scan/analyzers/test_calls_private_method/receiver_resolution.rb +80 -0
- data/lib/metz_scan/analyzers/test_calls_private_method/scope_facts.rb +76 -0
- data/lib/metz_scan/analyzers/test_calls_private_method/scope_resolution.rb +56 -0
- data/lib/metz_scan/analyzers/test_calls_private_method/triage.rb +23 -0
- data/lib/metz_scan/analyzers/test_calls_private_method.rb +125 -0
- data/lib/metz_scan/cli.rb +7 -13
- data/lib/metz_scan/commands/explain.rb +11 -2
- data/lib/metz_scan/commands/project_analyzers.rb +3 -2
- data/lib/metz_scan/commands/report.rb +22 -8
- data/lib/metz_scan/commands/rules.rb +16 -4
- data/lib/metz_scan/commands/scan/auto_fix.rb +54 -54
- data/lib/metz_scan/commands/scan/check_failure.rb +42 -0
- data/lib/metz_scan/commands/scan/config_disables.rb +83 -0
- data/lib/metz_scan/commands/scan/config_exclusions.rb +112 -0
- data/lib/metz_scan/commands/scan/exclude_entries.rb +64 -0
- data/lib/metz_scan/commands/scan/exclude_entry_locator.rb +84 -0
- data/lib/metz_scan/commands/scan/exclude_provenance.rb +53 -0
- data/lib/metz_scan/commands/scan/execution_config_store.rb +142 -0
- data/lib/metz_scan/commands/scan/ignored_cli_options_note.rb +35 -0
- data/lib/metz_scan/commands/scan/inherited_include_rewriter.rb +43 -0
- data/lib/metz_scan/commands/scan/inline_directive_locator.rb +80 -0
- data/lib/metz_scan/commands/scan/json_guidance.rb +81 -0
- data/lib/metz_scan/commands/scan/offense_extractor.rb +29 -7
- data/lib/metz_scan/commands/scan/path_scoped_include_note.rb +73 -0
- data/lib/metz_scan/commands/scan/project_analyzer_offenses.rb +21 -5
- data/lib/metz_scan/commands/scan/project_analyzer_runner.rb +25 -24
- data/lib/metz_scan/commands/scan/project_config_scope.rb +105 -26
- data/lib/metz_scan/commands/scan/rubocop_failure.rb +38 -0
- data/lib/metz_scan/commands/scan/rubocop_invocation.rb +141 -0
- data/lib/metz_scan/commands/scan/runner.rb +94 -75
- data/lib/metz_scan/commands/scan/sarif_rule_descriptors.rb +6 -24
- data/lib/metz_scan/commands/scan/suppression_ledger.rb +113 -0
- data/lib/metz_scan/commands/scan/suppression_ledger_formatter.rb +54 -0
- data/lib/metz_scan/commands/scan/target_ruby_version.rb +3 -2
- data/lib/metz_scan/commands/scan/text_renderer.rb +5 -4
- data/lib/metz_scan/commands/scan.rb +48 -26
- data/lib/metz_scan/project_index/rubydex_backend/method_declarations.rb +33 -12
- data/lib/metz_scan/project_index/rubydex_backend/method_visibility.rb +155 -0
- data/lib/metz_scan/project_index/rubydex_backend.rb +2 -1
- data/lib/metz_scan/project_index.rb +1 -1
- data/lib/metz_scan/version.rb +1 -1
- data/metz-scan.gemspec +20 -12
- metadata +33 -14
- data/lib/metz_scan/calibration/project_analyzer_evidence_runner/artifact_writer.rb +0 -51
- data/lib/metz_scan/calibration/project_analyzer_evidence_runner/baseline_delta.rb +0 -231
- data/lib/metz_scan/calibration/project_analyzer_evidence_runner/collaborators.rb +0 -76
- data/lib/metz_scan/calibration/project_analyzer_evidence_runner/markdown_renderer.rb +0 -353
- data/lib/metz_scan/calibration/project_analyzer_evidence_runner/readiness_catalog.rb +0 -123
- data/lib/metz_scan/calibration/project_analyzer_evidence_runner/summary.rb +0 -333
- data/lib/metz_scan/calibration/project_analyzer_evidence_runner/target_set.rb +0 -139
- data/lib/metz_scan/calibration/project_analyzer_evidence_runner.rb +0 -64
- data/lib/metz_scan/calibration/rubydex_drift_formatter.rb +0 -91
|
@@ -5,7 +5,12 @@ require "psych"
|
|
|
5
5
|
require "rubocop"
|
|
6
6
|
require "stringio"
|
|
7
7
|
|
|
8
|
+
require "metz/file_classifier"
|
|
9
|
+
require "metz_scan/commands/scan/execution_config_store"
|
|
8
10
|
require "metz_scan/commands/scan/project_config_scope"
|
|
11
|
+
require "metz_scan/commands/scan/rubocop_failure"
|
|
12
|
+
require "metz_scan/commands/scan/rubocop_invocation"
|
|
13
|
+
require "metz_scan/commands/scan/suppression_ledger"
|
|
9
14
|
require "metz_scan/commands/scan/target_ruby_version"
|
|
10
15
|
|
|
11
16
|
module MetzScan
|
|
@@ -18,56 +23,44 @@ module MetzScan
|
|
|
18
23
|
|
|
19
24
|
Result = Struct.new(:stdout, :stderr, :status, keyword_init: true)
|
|
20
25
|
|
|
21
|
-
def self.invoke(paths, all_cops: false, stderr: $stderr)
|
|
22
|
-
with_errors { perform(paths, all_cops: all_cops, stderr: stderr) }
|
|
26
|
+
def self.invoke(paths, all_cops: false, stderr: $stderr, **scope)
|
|
27
|
+
with_errors { perform(paths, all_cops: all_cops, stderr: stderr, scope: scope) }
|
|
23
28
|
end
|
|
24
29
|
|
|
25
|
-
def self.perform(paths, all_cops:, stderr:)
|
|
26
|
-
ProjectConfigScope.
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
report.
|
|
30
|
+
def self.perform(paths, all_cops:, stderr:, scope: {})
|
|
31
|
+
store = scope[:store] || (ProjectConfigScope.store unless all_cops)
|
|
32
|
+
exec_store = ExecutionConfigStore.new(store) unless all_cops
|
|
33
|
+
files = inspection_paths(paths, all_cops: all_cops, store: store, target_files: scope[:target_files])
|
|
34
|
+
report = files.empty? ? empty_report : scan(files, all_cops: all_cops, store: store)
|
|
35
|
+
report.tap { ProjectConfigScope.warn_scope_notes(files, stderr, exec_store: exec_store) unless all_cops }
|
|
30
36
|
end
|
|
31
37
|
|
|
32
|
-
def self.scan(paths, all_cops:)
|
|
33
|
-
report = TargetRubyVersion.with_project_config(paths, all_cops: all_cops) do
|
|
34
|
-
parse_output(
|
|
38
|
+
def self.scan(paths, all_cops:, store: nil)
|
|
39
|
+
report = TargetRubyVersion.with_project_config(paths, all_cops: all_cops, store: store) do
|
|
40
|
+
parse_output(RuboCopInvocation.scan(paths, all_cops: all_cops, store: store))
|
|
35
41
|
end
|
|
36
|
-
|
|
42
|
+
return report if all_cops
|
|
43
|
+
|
|
44
|
+
SuppressionLedger.with_store(store) { SuppressionLedger.apply(report) }
|
|
37
45
|
end
|
|
38
46
|
|
|
47
|
+
# Only what RuboCop and config loading raise; a metz-scan bug keeps its class and backtrace.
|
|
39
48
|
def self.with_errors
|
|
40
49
|
yield
|
|
41
|
-
rescue LoadError,
|
|
50
|
+
rescue LoadError, RuboCop::Error, Psych::Exception, SystemCallError => e
|
|
42
51
|
raise Error, concise_message(e.message)
|
|
43
52
|
end
|
|
44
53
|
|
|
45
|
-
def self.rubocop_argv(paths, all_cops:)
|
|
46
|
-
require_metz_plugin
|
|
47
|
-
["--plugin", "rubocop-metz", *cop_selection_argv(all_cops), "--format", FORMATTER, *paths]
|
|
48
|
-
end
|
|
49
|
-
|
|
50
54
|
def self.require_metz_plugin
|
|
51
55
|
require "rubocop-metz"
|
|
52
56
|
rescue LoadError => e
|
|
53
57
|
raise Error, "could not load rubocop-metz: #{e.message}"
|
|
54
58
|
end
|
|
55
59
|
|
|
56
|
-
def self.inspection_paths(paths, all_cops:)
|
|
60
|
+
def self.inspection_paths(paths, all_cops:, store: nil, target_files: nil)
|
|
57
61
|
return paths if all_cops
|
|
58
62
|
|
|
59
|
-
TargetFileDiscovery.
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
def self.cop_selection_argv(all_cops)
|
|
63
|
-
all_cops ? [] : ["--force-default-config", "--enable-all-cops", "--only", "Metz"]
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
def self.capture_output(argv)
|
|
67
|
-
out = StringIO.new
|
|
68
|
-
err = StringIO.new
|
|
69
|
-
status = with_redirected_io(out, err) { RuboCop::CLI.new.run(argv) }
|
|
70
|
-
Result.new(stdout: out.string, stderr: err.string, status: status)
|
|
63
|
+
(target_files || TargetFileDiscovery.discover(paths, store: store)).map { |path| display_path(path) }
|
|
71
64
|
end
|
|
72
65
|
|
|
73
66
|
def self.with_redirected_io(out, err)
|
|
@@ -90,17 +83,11 @@ module MetzScan
|
|
|
90
83
|
end
|
|
91
84
|
|
|
92
85
|
def self.parse_output(result)
|
|
86
|
+
raise Error, RubocopFailure.message(result) if RubocopFailure.failed?(result)
|
|
87
|
+
|
|
93
88
|
JSON.parse(result.stdout)
|
|
94
89
|
rescue JSON::ParserError
|
|
95
|
-
raise Error,
|
|
96
|
-
end
|
|
97
|
-
|
|
98
|
-
def self.failure_message(result)
|
|
99
|
-
detail = concise_message(result.stderr)
|
|
100
|
-
detail = concise_message(result.stdout) if detail.empty?
|
|
101
|
-
return detail unless detail.empty?
|
|
102
|
-
|
|
103
|
-
"RuboCop exited with status #{result.status} without JSON output"
|
|
90
|
+
raise Error, RubocopFailure.message(result)
|
|
104
91
|
end
|
|
105
92
|
|
|
106
93
|
def self.concise_message(text)
|
|
@@ -120,7 +107,7 @@ module MetzScan
|
|
|
120
107
|
end
|
|
121
108
|
|
|
122
109
|
def self.empty_report
|
|
123
|
-
{ "metadata" => metadata, "files" => [],
|
|
110
|
+
{ "metadata" => metadata, "files" => [], "suppressions" => [],
|
|
124
111
|
"summary" => { "offense_count" => 0, "target_file_count" => 0, "inspected_file_count" => 0 } }
|
|
125
112
|
end
|
|
126
113
|
|
|
@@ -139,59 +126,91 @@ module MetzScan
|
|
|
139
126
|
end
|
|
140
127
|
end
|
|
141
128
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
# resolves the project config's scope per cop; offenses on files a cop is
|
|
146
|
-
# scoped off are dropped here. Invalid project config -> nothing to honor
|
|
147
|
-
# (matches the forced-default target-discovery fallback). See #37.
|
|
148
|
-
module ProjectCopScope
|
|
129
|
+
module TargetFileDiscovery
|
|
130
|
+
STORE_KEY = :metz_scan_target_file_discovery_store
|
|
131
|
+
|
|
149
132
|
module_function
|
|
150
133
|
|
|
151
|
-
def
|
|
134
|
+
def scan_scope(paths)
|
|
152
135
|
store = ProjectConfigScope.store
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
136
|
+
{ store: store, target_files: Runner.with_errors { discover(paths, store: store) } }
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def analyzer_scope(options)
|
|
140
|
+
if options[:all_cops]
|
|
141
|
+
return { rubocop_store: options[:rubocop_store] || Runner.with_errors { all_cops_store } }
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
{ store: options[:store] || ProjectConfigScope.store }
|
|
157
145
|
end
|
|
158
146
|
|
|
159
|
-
def
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
147
|
+
def for_analyzer(paths, all_cops: false, store: nil, rubocop_store: nil)
|
|
148
|
+
return for_all_cops(paths, rubocop_store: rubocop_store || all_cops_store) if all_cops
|
|
149
|
+
|
|
150
|
+
discover(paths, store: store)
|
|
163
151
|
end
|
|
164
152
|
|
|
165
|
-
def
|
|
166
|
-
|
|
167
|
-
|
|
153
|
+
def discover(paths, store: nil)
|
|
154
|
+
Runner.require_metz_plugin
|
|
155
|
+
ProjectConfigScope.reset_unresolved_inherit_gems!
|
|
156
|
+
with_store(store || ProjectConfigScope.store) { for_project_config(paths) }
|
|
157
|
+
end
|
|
168
158
|
|
|
169
|
-
|
|
159
|
+
def for_project_config(paths, store: active_store)
|
|
160
|
+
finder = RuboCop::TargetFinder.new(store, {})
|
|
161
|
+
files = (finder.find(paths, :all_file_types) + view_templates(paths, finder, store)).uniq
|
|
162
|
+
load_governing_configs(files, store)
|
|
170
163
|
end
|
|
171
164
|
|
|
172
|
-
def
|
|
173
|
-
|
|
165
|
+
def for_all_cops(paths, rubocop_store: all_cops_store)
|
|
166
|
+
ProjectConfigScope.ensure_metz_defaults_loaded
|
|
167
|
+
RuboCop::TargetFinder.new(rubocop_store, {}).find(paths, :all_file_types)
|
|
168
|
+
end
|
|
174
169
|
|
|
175
|
-
|
|
176
|
-
|
|
170
|
+
def all_cops_store
|
|
171
|
+
ProjectConfigScope.ensure_metz_defaults_loaded
|
|
172
|
+
RuboCop::ConfigStore.new
|
|
177
173
|
end
|
|
178
|
-
end
|
|
179
174
|
|
|
180
|
-
|
|
181
|
-
|
|
175
|
+
def with_store(store)
|
|
176
|
+
previous = Thread.current[STORE_KEY]
|
|
177
|
+
Thread.current[STORE_KEY] = store
|
|
178
|
+
yield
|
|
179
|
+
ensure
|
|
180
|
+
Thread.current[STORE_KEY] = previous
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
def active_store
|
|
184
|
+
Thread.current[STORE_KEY] || ProjectConfigScope.store
|
|
185
|
+
end
|
|
182
186
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
+
# RuboCop's directory discovery finds only Ruby files; the Metz view cop also
|
|
188
|
+
# checks template files in its effective Include scope. Hidden directories and
|
|
189
|
+
# the project's AllCops: Exclude apply as they do to Ruby.
|
|
190
|
+
def view_templates(paths, finder, store)
|
|
191
|
+
exec_store = ExecutionConfigStore.new(store)
|
|
192
|
+
cops = {}.compare_by_identity
|
|
193
|
+
paths.select { |path| File.directory?(path) }.flat_map do |dir|
|
|
194
|
+
templates_in_dir(File.expand_path(dir), finder, exec_store, cops)
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
def templates_in_dir(base_dir, finder, exec_store, cops)
|
|
199
|
+
finder.find_files(base_dir, 0).select { |file| included_template?(file, exec_store, cops) }
|
|
187
200
|
end
|
|
188
201
|
|
|
189
|
-
def
|
|
190
|
-
|
|
202
|
+
def included_template?(file, exec_store, cops)
|
|
203
|
+
return false unless Metz::FileClassifier.template?(file)
|
|
204
|
+
|
|
205
|
+
exec_config = exec_store.for_file(file)
|
|
206
|
+
views_cop = cops[exec_config] ||= RuboCop::Cop::Metz::ViewsDeepNavigation.new(exec_config)
|
|
207
|
+
!exec_config.file_to_exclude?(file) && views_cop.relevant_file?(file)
|
|
191
208
|
end
|
|
192
209
|
|
|
193
|
-
|
|
194
|
-
|
|
210
|
+
# ExecutionConfigStore builds execution configs per unique loaded_path only, so
|
|
211
|
+
# loading every file's governing config here makes a broken nested config fail the scan.
|
|
212
|
+
def load_governing_configs(files, store)
|
|
213
|
+
files.each { |file| store.for_file(file) }
|
|
195
214
|
end
|
|
196
215
|
end
|
|
197
216
|
end
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require_relative "offense_extractor"
|
|
3
4
|
require_relative "sarif_severity"
|
|
4
5
|
|
|
5
6
|
module MetzScan
|
|
@@ -31,8 +32,8 @@ module MetzScan
|
|
|
31
32
|
end
|
|
32
33
|
|
|
33
34
|
def add_optional_texts(descriptor, cop_name)
|
|
34
|
-
|
|
35
|
-
add_text(descriptor, "fullDescription",
|
|
35
|
+
text = rule_why_text(cop_name)
|
|
36
|
+
add_text(add_text(descriptor, "help", text), "fullDescription", text)
|
|
36
37
|
end
|
|
37
38
|
|
|
38
39
|
def add_text(descriptor, key, text)
|
|
@@ -45,28 +46,9 @@ module MetzScan
|
|
|
45
46
|
SarifSeverity.highest_level(offenses.map { |offense| offense[:severity] })
|
|
46
47
|
end
|
|
47
48
|
|
|
48
|
-
def
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
def full_description_for(cop_name)
|
|
53
|
-
offense_for(cop_name)&.dig(:why_it_matters) || registry_metadata(cop_name).fetch("why_it_matters", nil)
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
def registry_metadata(cop_name)
|
|
57
|
-
return {} unless defined?(RuboCop::Cop::Registry)
|
|
58
|
-
|
|
59
|
-
metadata_for(cop_for(cop_name))
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
def metadata_for(cop)
|
|
63
|
-
return {} unless cop.respond_to?(:why_it_matters)
|
|
64
|
-
|
|
65
|
-
{ "why_it_matters" => cop.why_it_matters }
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
def cop_for(cop_name)
|
|
69
|
-
RuboCop::Cop::Registry.global.cops.find { |candidate| candidate.cop_name == cop_name }
|
|
49
|
+
def rule_why_text(cop_name)
|
|
50
|
+
first = offense_for(cop_name)
|
|
51
|
+
first&.fetch(:why_it_matters) { OffenseExtractor.registry_why_it_matters(cop_name) }
|
|
70
52
|
end
|
|
71
53
|
|
|
72
54
|
def offense_for(cop_name)
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "psych"
|
|
4
|
+
require "rubocop"
|
|
5
|
+
|
|
6
|
+
require "metz_scan/commands/scan/config_disables"
|
|
7
|
+
require "metz_scan/commands/scan/config_exclusions"
|
|
8
|
+
require "metz_scan/commands/scan/inline_directive_locator"
|
|
9
|
+
require "metz_scan/commands/scan/project_config_scope"
|
|
10
|
+
|
|
11
|
+
module MetzScan
|
|
12
|
+
module Commands
|
|
13
|
+
class Scan
|
|
14
|
+
# Default mode asks RuboCop to --display-suppressed, then partitions every
|
|
15
|
+
# offense: a project `Enabled: false` or per-cop Exclude hides it
|
|
16
|
+
# (credited even when an inline disable also covers it), else an inline
|
|
17
|
+
# directive hides it, else it is live. Hidden offenses leave `files` and
|
|
18
|
+
# the counts; the ones a project wrote a suppression for are listed under
|
|
19
|
+
# `suppressions`. Project analyzer offenses go through the same partition
|
|
20
|
+
# (see partition!), with `# metz-scan:disable` as their inline directive.
|
|
21
|
+
class SuppressionLedger
|
|
22
|
+
SORT_KEYS = %w[path line column cop_name].freeze
|
|
23
|
+
STORE_KEY = :metz_scan_suppression_ledger_store
|
|
24
|
+
|
|
25
|
+
def self.with_store(store)
|
|
26
|
+
previous = Thread.current[STORE_KEY]
|
|
27
|
+
Thread.current[STORE_KEY] = store
|
|
28
|
+
yield
|
|
29
|
+
ensure
|
|
30
|
+
Thread.current[STORE_KEY] = previous
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def self.apply(report, store: nil, rubocop_store: nil)
|
|
34
|
+
new(report, config_sources(store, rubocop_store)).apply
|
|
35
|
+
rescue RuboCop::Error, Psych::Exception
|
|
36
|
+
# Invalid project config leaves no project config to honor, matching
|
|
37
|
+
# the forced-default target-discovery fallback.
|
|
38
|
+
new(report, []).apply
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def self.config_sources(store, rubocop_store)
|
|
42
|
+
scope_store = store || Thread.current[STORE_KEY] || ProjectConfigScope.store
|
|
43
|
+
[ConfigDisables.new(scope_store, rubocop_store: rubocop_store),
|
|
44
|
+
ConfigExclusions.new(scope_store, rubocop_store: rubocop_store)]
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Project analyzer offenses join the report after RuboCop's (in every
|
|
48
|
+
# scan mode): partitions `offenses_by_path`, adds its records to the
|
|
49
|
+
# report's ledger (created under --all-cops only when a record exists),
|
|
50
|
+
# and returns the live offenses by display path.
|
|
51
|
+
def self.partition!(report, offenses_by_path, store: nil, rubocop_store: nil)
|
|
52
|
+
files = offenses_by_path.map { |path, live| { "path" => Runner.display_path(path), "offenses" => live } }
|
|
53
|
+
partitioned = apply({ "files" => files, "summary" => {} }, store: store, rubocop_store: rubocop_store)
|
|
54
|
+
add_records(report, partitioned["suppressions"])
|
|
55
|
+
partitioned["files"].to_h { |file| file.values_at("path", "offenses") }.reject { |_path, live| live.empty? }
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def self.add_records(report, new_records)
|
|
59
|
+
records = sort(Array(report["suppressions"]) + new_records)
|
|
60
|
+
report["suppressions"] = records if report.key?("suppressions") || records.any?
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def self.sort(records) = records.sort_by { |record| record.values_at(*SORT_KEYS) }
|
|
64
|
+
|
|
65
|
+
def initialize(report, config_sources)
|
|
66
|
+
@report = report
|
|
67
|
+
@config_sources = config_sources
|
|
68
|
+
@directives = InlineDirectiveLocator.new
|
|
69
|
+
@records = []
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def apply
|
|
73
|
+
files = Array(report["files"]).map { |file| file.merge("offenses" => live_offenses(file)) }
|
|
74
|
+
recount(report.merge("files" => files, "suppressions" => self.class.sort(records)))
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
private
|
|
78
|
+
|
|
79
|
+
attr_reader :report, :config_sources, :directives, :records
|
|
80
|
+
|
|
81
|
+
def live_offenses(file)
|
|
82
|
+
path = file.fetch("path")
|
|
83
|
+
sourced = Array(file["offenses"]).map { |offense| [offense, suppression_source(path, offense)] }
|
|
84
|
+
live, hidden = sourced.partition { |_offense, source| source.nil? }
|
|
85
|
+
hidden.each { |offense, source| record(path, offense, source.record_fields(path, offense)) }
|
|
86
|
+
live.map(&:first)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def suppression_source(path, offense)
|
|
90
|
+
config_source = config_sources.find { |source| source.scoped_off?(path, offense) }
|
|
91
|
+
config_source || (directives if offense["suppressed"] || directives.analyzer_disabled?(path, offense))
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# Hidden offenses with no project-written suppression (stock-default
|
|
95
|
+
# scope, synthetic opt-in coverage) produce no record.
|
|
96
|
+
def record(path, offense, fields)
|
|
97
|
+
records << finding(path, offense).merge(fields) if fields
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def finding(path, offense)
|
|
101
|
+
location = offense.fetch("location")
|
|
102
|
+
{ "cop_name" => offense.fetch("cop_name"), "path" => path, "line" => location.fetch("start_line"),
|
|
103
|
+
"column" => location.fetch("start_column"), "message" => offense.fetch("message") }
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def recount(report)
|
|
107
|
+
count = report["files"].sum { |file| file["offenses"].size }
|
|
108
|
+
report.merge("summary" => report.fetch("summary").merge("offense_count" => count))
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module MetzScan
|
|
4
|
+
module Commands
|
|
5
|
+
class Scan
|
|
6
|
+
class SuppressionLedgerFormatter
|
|
7
|
+
def initialize(parsed)
|
|
8
|
+
@records = Array(parsed["suppressions"])
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def lines
|
|
12
|
+
return [] if records.empty?
|
|
13
|
+
|
|
14
|
+
[heading, *records.flat_map { |record| record_lines(record) }, ""]
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
private
|
|
18
|
+
|
|
19
|
+
attr_reader :records
|
|
20
|
+
|
|
21
|
+
def heading
|
|
22
|
+
missing = records.count { |record| record.fetch("reason_status") == "missing" }
|
|
23
|
+
"Suppressed findings: #{records.size}, #{missing} without a reason"
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def record_lines(record)
|
|
27
|
+
[" #{record.fetch('path')}:#{record.fetch('line')}:#{record.fetch('column')} #{record.fetch('message')}",
|
|
28
|
+
" #{source(record)}, #{reason(record)}"]
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def source(record)
|
|
32
|
+
return "inline disable at line #{record.dig('directive', 'line')}" unless record.fetch("config")
|
|
33
|
+
|
|
34
|
+
config = record.fetch("config")
|
|
35
|
+
"#{[config.fetch('path'), config.fetch('line')].compact.join(':')} #{config_setting(record)}"
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def config_setting(record)
|
|
39
|
+
return "Enabled: false" if record.fetch("suppressed_by") == "config_disabled"
|
|
40
|
+
|
|
41
|
+
"Exclude \"#{record.dig('config', 'pattern')}\""
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def reason(record)
|
|
45
|
+
case record.fetch("reason_status")
|
|
46
|
+
when "present" then "reason: #{record.fetch('reason')}"
|
|
47
|
+
when "missing" then "no reason"
|
|
48
|
+
else "reason not checked"
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -10,10 +10,11 @@ module MetzScan
|
|
|
10
10
|
|
|
11
11
|
module_function
|
|
12
12
|
|
|
13
|
-
def with_project_config(paths, all_cops:, &)
|
|
13
|
+
def with_project_config(paths, all_cops:, store: nil, &)
|
|
14
14
|
return yield if all_cops
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
version = ProjectConfigScope::TargetRubyVersion.new(paths, store: store || ProjectConfigScope.store).version
|
|
17
|
+
EnvOverride.new(version).with_env(&)
|
|
17
18
|
end
|
|
18
19
|
|
|
19
20
|
class EnvOverride
|
|
@@ -6,6 +6,7 @@ require_relative "project_analyzer_summary_aggregate_formatter"
|
|
|
6
6
|
require_relative "project_analyzer_summary_breakdown_formatter"
|
|
7
7
|
require_relative "project_analyzer_triage_formatter"
|
|
8
8
|
require_relative "project_analyzer_triage_priority"
|
|
9
|
+
require_relative "suppression_ledger_formatter"
|
|
9
10
|
|
|
10
11
|
module MetzScan
|
|
11
12
|
module Commands
|
|
@@ -22,16 +23,16 @@ module MetzScan
|
|
|
22
23
|
|
|
23
24
|
def render
|
|
24
25
|
emit_project_analyzer_summary
|
|
25
|
-
sorted_offense_blocks.each
|
|
26
|
-
|
|
27
|
-
end
|
|
28
|
-
ComplianceScorecard.new(parsed).lines.each { |line| stdout.puts line }
|
|
26
|
+
sorted_offense_blocks.each { |cop_name, list| render_block(cop_name, list) }
|
|
27
|
+
closing_lines.each { |line| stdout.puts line }
|
|
29
28
|
end
|
|
30
29
|
|
|
31
30
|
private
|
|
32
31
|
|
|
33
32
|
attr_reader :stdout, :parsed
|
|
34
33
|
|
|
34
|
+
def closing_lines = SuppressionLedgerFormatter.new(parsed).lines + ComplianceScorecard.new(parsed).lines
|
|
35
|
+
|
|
35
36
|
def render_block(cop_name, list)
|
|
36
37
|
stdout.puts heading(cop_name)
|
|
37
38
|
emit_block_metadata(cop_name, representative_offense(list))
|
|
@@ -2,8 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
require "json"
|
|
4
4
|
require "optparse"
|
|
5
|
+
require "stringio"
|
|
5
6
|
|
|
6
7
|
require_relative "../version"
|
|
8
|
+
require_relative "scan/check_failure"
|
|
7
9
|
require_relative "scan/compliance_scorecard"
|
|
8
10
|
require_relative "scan/runner"
|
|
9
11
|
require_relative "scan/project_analyzer_runner"
|
|
@@ -11,6 +13,9 @@ require_relative "scan/text_renderer"
|
|
|
11
13
|
require_relative "scan/sarif_renderer"
|
|
12
14
|
require_relative "scan/github_annotations_renderer"
|
|
13
15
|
require_relative "scan/auto_fix"
|
|
16
|
+
require_relative "scan/ignored_cli_options_note"
|
|
17
|
+
require_relative "scan/json_guidance"
|
|
18
|
+
require_relative "../cli"
|
|
14
19
|
|
|
15
20
|
module MetzScan
|
|
16
21
|
module Commands
|
|
@@ -92,7 +97,6 @@ module MetzScan
|
|
|
92
97
|
class Scan
|
|
93
98
|
USAGE = ScanOptions::USAGE
|
|
94
99
|
VALID_FORMATS = ScanOptions::VALID_FORMATS
|
|
95
|
-
RENDERERS = { "sarif" => SarifRenderer, "gh-annotations" => GithubAnnotationsRenderer }.freeze
|
|
96
100
|
|
|
97
101
|
def self.run(argv, stdout:, stderr:)
|
|
98
102
|
new(stdout: stdout, stderr: stderr).run(argv)
|
|
@@ -107,8 +111,8 @@ module MetzScan
|
|
|
107
111
|
handle_options(parse_options(argv))
|
|
108
112
|
rescue OptionParser::ParseError => e
|
|
109
113
|
parser_error(e)
|
|
110
|
-
rescue
|
|
111
|
-
|
|
114
|
+
rescue *CheckFailure::INTERNAL_ERRORS => e
|
|
115
|
+
CheckFailure.new(stderr).report(e)
|
|
112
116
|
end
|
|
113
117
|
|
|
114
118
|
private
|
|
@@ -128,47 +132,70 @@ module MetzScan
|
|
|
128
132
|
def validate(options)
|
|
129
133
|
return missing_path_arg if options.paths.empty?
|
|
130
134
|
return missing_paths(options.paths) unless options.paths.all? { |p| File.exist?(p) }
|
|
131
|
-
return nil if options.auto_fix
|
|
132
135
|
|
|
133
136
|
invalid_format(options.format) unless VALID_FORMATS.include?(options.format)
|
|
134
137
|
end
|
|
135
138
|
|
|
136
139
|
def dispatch(options)
|
|
137
|
-
return
|
|
140
|
+
return auto_fix(options) if options.auto_fix
|
|
138
141
|
|
|
139
142
|
scan(options)
|
|
140
143
|
end
|
|
141
144
|
|
|
145
|
+
def auto_fix(options)
|
|
146
|
+
AutoFix.new(stdout: stdout, stderr: stderr).run(options)
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
# The report reaches stdout only after every step succeeded; see CheckFailure.
|
|
142
150
|
def scan(options)
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
151
|
+
run_scan(options, options.all_cops ? {} : TargetFileDiscovery.scan_scope(options.paths))
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def run_scan(options, scope)
|
|
155
|
+
parsed = build_report(options, scope)
|
|
156
|
+
stdout.write(rendered(parsed, options))
|
|
157
|
+
IgnoredCliOptionsNote.print_note(stderr)
|
|
147
158
|
Runner.exit_code_for(parsed)
|
|
148
159
|
end
|
|
149
160
|
|
|
150
|
-
def
|
|
161
|
+
def build_report(options, scope)
|
|
162
|
+
parsed = checked_report(options, scope)
|
|
163
|
+
merge_project_analyzers(parsed, options, scope)
|
|
151
164
|
ComplianceScorecard.add_to_summary!(parsed)
|
|
165
|
+
parsed
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def checked_report(options, scope)
|
|
169
|
+
parsed = Runner.invoke(options.paths, all_cops: options.all_cops, stderr: stderr, **scope)
|
|
170
|
+
raise CheckFailure::NoFilesScanned, options.paths if parsed.dig("summary", "inspected_file_count").zero?
|
|
171
|
+
|
|
172
|
+
parsed
|
|
152
173
|
end
|
|
153
174
|
|
|
154
|
-
def merge_project_analyzers(parsed, options)
|
|
155
|
-
|
|
175
|
+
def merge_project_analyzers(parsed, options, scope)
|
|
176
|
+
opts = { default_output: !options.project_analyzers, all_cops: options.all_cops, **scope }
|
|
177
|
+
ProjectAnalyzerRunner.merge!(parsed, options.paths, **opts)
|
|
156
178
|
end
|
|
157
179
|
|
|
158
|
-
def
|
|
159
|
-
{
|
|
180
|
+
def rendered(parsed, options)
|
|
181
|
+
StringIO.new.tap { |out| render(out, parsed, options) }.string
|
|
160
182
|
end
|
|
161
183
|
|
|
162
|
-
|
|
163
|
-
|
|
184
|
+
# Text and SARIF render the compacted report so live output matches `report` on the saved JSON.
|
|
185
|
+
def render(out, parsed, options)
|
|
186
|
+
format = options.format
|
|
187
|
+
return GithubAnnotationsRenderer.new(out, parsed).render if format == "gh-annotations"
|
|
188
|
+
|
|
189
|
+
JsonGuidance.compact!(parsed, all_cops: options.all_cops)
|
|
190
|
+
return out.puts JSON.generate(parsed) if format == "json"
|
|
164
191
|
|
|
165
|
-
|
|
192
|
+
(format == "sarif" ? SarifRenderer : TextRenderer).new(out, parsed).render
|
|
166
193
|
end
|
|
167
194
|
|
|
168
195
|
def parser_error(err)
|
|
169
196
|
stderr.puts "metz-scan scan: #{err.message}"
|
|
170
197
|
stderr.puts USAGE
|
|
171
|
-
|
|
198
|
+
CLI::USAGE_ERROR
|
|
172
199
|
end
|
|
173
200
|
|
|
174
201
|
def print_help
|
|
@@ -179,22 +206,17 @@ module MetzScan
|
|
|
179
206
|
def missing_path_arg
|
|
180
207
|
stderr.puts "metz-scan scan: missing PATH argument."
|
|
181
208
|
stderr.puts USAGE
|
|
182
|
-
|
|
209
|
+
CLI::USAGE_ERROR
|
|
183
210
|
end
|
|
184
211
|
|
|
185
212
|
def missing_paths(paths)
|
|
186
213
|
paths.reject { |p| File.exist?(p) }.each { |p| stderr.puts "metz-scan scan: no such file or directory: #{p}" }
|
|
187
|
-
|
|
214
|
+
CLI::USAGE_ERROR
|
|
188
215
|
end
|
|
189
216
|
|
|
190
217
|
def invalid_format(fmt)
|
|
191
218
|
stderr.puts "metz-scan scan: invalid --format '#{fmt}'. Valid formats: text, json, sarif, gh-annotations."
|
|
192
|
-
|
|
193
|
-
end
|
|
194
|
-
|
|
195
|
-
def runner_error(err)
|
|
196
|
-
stderr.puts "metz-scan scan: RuboCop failed: #{err.message}"
|
|
197
|
-
2
|
|
219
|
+
CLI::USAGE_ERROR
|
|
198
220
|
end
|
|
199
221
|
end
|
|
200
222
|
end
|