23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
# File 'lib/rail_verdict/check.rb', line 23
def execute(repository_root:, config_path:, runner: ProcessRunner, rubocop_command_resolver: nil, analyzer_timeout_seconds: 30.0, interrupted: nil, baseline_path_override: nil, waiver_path_override: nil, clock: Time.now.utc, changed: false, base: nil)
root = File.realpath(repository_root)
resolved_config = resolve_config_path(root, config_path)
configuration = Configuration.load(resolved_config)
return interrupted_outcome if interrupted&.call
probes = probe_enabled_analyzers(root, configuration, runner: runner, rubocop_command_resolver: rubocop_command_resolver, timeout_seconds: analyzer_timeout_seconds)
analyzer_versions = probes.transform_values(&:version)
git_context = nil
git_error = nil
if changed
begin
git_context = Git::Context.build(repository_root: root, base_override: base, configuration: configuration, runner: runner)
rescue Git::Error => error
git_error = error
end
if git_error
git_incomplete = Verification::Policy.incomplete_result(
analyzer_results: [],
findings: [],
operational_failures: [{ "code" => "failed", "message" => git_error.message }],
code: "git_scope_failed",
message: git_error.message
)
context_for_error = begin
RunContext.build(repository_root: root, configuration: configuration, analyzer_versions: analyzer_versions, revision_resolver: revision_resolver(root, runner))
rescue StandardError
nil
end
git_payload = git_error_payload(git_error, base, configuration)
enriched = GateResult.new(
completion_status: git_incomplete.completion_status,
gate: git_incomplete.gate,
policy_status: git_incomplete.policy_status,
findings: git_incomplete.findings,
analyzer_results: git_incomplete.analyzer_results,
operational_failures: git_incomplete.operational_failures,
decision_reasons: git_incomplete.decision_reasons,
git: git_payload
)
return Outcome.new(result: enriched, context: context_for_error, configuration: configuration, findings: [].freeze)
end
end
context = RunContext.build(
repository_root: root,
configuration: configuration,
analyzer_versions: analyzer_versions,
revision_resolver: revision_resolver(root, runner),
git_context: git_context
)
return interrupted_outcome(context: context, configuration: configuration) if interrupted&.call
analyzer_results = []
findings = []
configuration.analyzers.each do |name, selection|
next unless selection.fetch("enabled")
adapter_class = REGISTRY[name]
unless adapter_class
analyzer_results << AnalyzerResult.new(
analyzer: name,
invocation: { "executable" => name, "argv" => [] },
execution_status: "unavailable",
finding_ids: [],
failure: { "code" => "unavailable", "message" => "analyzer #{name} is not implemented in this build" }
)
next
end
adapter = build_adapter(name, rubocop_command_resolver)
probe = probes[name]
analyzer_result, analyzer_findings = adapter.run(
root,
runner: runner,
probe_result: probe,
timeout_seconds: analyzer_timeout_seconds
)
analyzer_results << analyzer_result
findings.concat(analyzer_findings)
end
return interrupted_outcome(context: context, configuration: configuration, analyzer_results: analyzer_results, findings: findings) if interrupted&.call
baseline = nil
baseline_meta = nil
comparison = nil
classified_findings = findings
waivers = []
waiver_meta = nil
resolved_baseline_path = Baseline.resolve_path(repository_root: root, configuration: configuration, output_override: baseline_path_override)
resolved_waiver_path = WaiverStore.resolve_path(repository_root: root, configuration: configuration, waiver_override: waiver_path_override)
begin
waivers = WaiverStore.read_optional(resolved_waiver_path)
waiver_meta = { "loaded" => !waivers.empty?, "path" => resolved_waiver_path, "count" => waivers.length }
rescue Waiver::IncompatibleError => error
incomplete = Verification::Policy.incomplete_result(
analyzer_results: analyzer_results,
findings: findings,
operational_failures: [{ "code" => "failed", "message" => error.message }],
code: "waiver_incompatible",
message: error.message
)
enriched = GateResult.new(completion_status: incomplete.completion_status, gate: incomplete.gate, policy_status: incomplete.policy_status, findings: incomplete.findings, analyzer_results: incomplete.analyzer_results, operational_failures: incomplete.operational_failures, decision_reasons: incomplete.decision_reasons, baseline: nil, comparison: { "counts" => {}, "waiver_error" => error.message })
return Outcome.new(result: enriched, context: context, configuration: configuration, findings: findings.freeze)
end
rename_map = git_context ? git_context.rename_map : nil
begin
if File.file?(resolved_baseline_path)
loaded = Baseline.read(resolved_baseline_path)
baseline = loaded
baseline_meta = { "loaded" => true, "path" => resolved_baseline_path, "schema_version" => Baseline::SCHEMA_VERSION, "fingerprint_version" => Fingerprint::VERSION, "compatible" => true }
cmp = Comparison.classify(findings: findings, baseline: loaded, waivers: waivers.map(&:to_h), clock: clock, rename_map: rename_map)
comparison = { "counts" => cmp.counts, "introduced" => cmp.introduced.map(&:fingerprint).sort, "existing" => cmp.existing.map(&:fingerprint).sort, "resolved" => cmp.resolved.map { |entry| entry.fetch("fingerprint") }.sort, "changed" => cmp.changed.map(&:fingerprint).sort, "moved" => cmp.moved.map(&:fingerprint).sort, "waived" => cmp.counts.fetch("waived", 0) == 0 ? [] : cmp.classified_findings.select { |item| item.state == "waived" }.map(&:fingerprint).sort, "orphaned_waivers" => cmp.orphaned_waivers.map { |w| w["fingerprint"] || w[:fingerprint] }.compact.sort }
if waiver_meta
comparison["waivers"] = waiver_meta
end
classified_findings = cmp.classified_findings
elsif waiver_meta && waiver_meta["count"] > 0
cmp = Comparison.classify(findings: findings, baseline: nil, waivers: waivers.map(&:to_h), clock: clock, rename_map: rename_map)
comparison = { "counts" => cmp.counts, "introduced" => cmp.introduced.map(&:fingerprint).sort, "existing" => cmp.existing.map(&:fingerprint).sort, "resolved" => cmp.resolved.map { |entry| entry.fetch("fingerprint") }.sort, "changed" => cmp.changed.map(&:fingerprint).sort, "moved" => cmp.moved.map(&:fingerprint).sort, "waived" => cmp.counts.fetch("waived", 0) == 0 ? [] : cmp.classified_findings.select { |item| item.state == "waived" }.map(&:fingerprint).sort, "orphaned_waivers" => cmp.orphaned_waivers.map { |w| w["fingerprint"] || w[:fingerprint] }.compact.sort, "waivers" => waiver_meta }
classified_findings = cmp.classified_findings
elsif waiver_meta
comparison = nil
end
rescue Baseline::IncompatibleError => error
incomplete = Verification::Policy.incomplete_result(
analyzer_results: analyzer_results,
findings: findings,
operational_failures: [{ "code" => "failed", "message" => error.message }],
code: "baseline_incompatible",
message: error.message
)
enriched = GateResult.new(completion_status: incomplete.completion_status, gate: incomplete.gate, policy_status: incomplete.policy_status, findings: incomplete.findings, analyzer_results: incomplete.analyzer_results, operational_failures: incomplete.operational_failures, decision_reasons: incomplete.decision_reasons, baseline: { "loaded" => false, "path" => resolved_baseline_path, "compatible" => false }, comparison: nil)
return Outcome.new(result: enriched, context: context, configuration: configuration, findings: findings.freeze)
end
unless git_context.nil?
analyzer_results = attach_changed_coverage(analyzer_results: analyzer_results, git_context: git_context)
end
filtered_findings_for_policy = classified_findings
changed_filter_active = !git_context.nil?
if changed_filter_active
filtered_findings_for_policy = classified_findings
end
result = Verification::Policy.evaluate(
configuration: configuration,
analyzer_results: analyzer_results,
findings: filtered_findings_for_policy,
comparison: comparison,
baseline_meta: baseline_meta,
git_context: git_context
)
git_payload = nil
if git_context
git_payload = build_git_payload(git_context, root)
end
rails_context_h = nil
begin
rails_ctx = RailsContext::Context.build(repository_root: root, git_context: git_context)
rails_context_h = rails_ctx.to_h
rescue StandardError
rails_context_h = nil
end
if baseline_meta || comparison || git_payload || rails_context_h
result = GateResult.new(
completion_status: result.completion_status,
gate: result.gate,
policy_status: result.policy_status,
findings: result.findings,
analyzer_results: result.analyzer_results,
operational_failures: result.operational_failures,
decision_reasons: result.decision_reasons,
baseline: baseline_meta,
comparison: comparison,
git: git_payload,
rails_context: rails_context_h
)
elsif git_payload && result.complete?
result = GateResult.new(
completion_status: result.completion_status,
gate: result.gate,
policy_status: result.policy_status,
findings: result.findings,
analyzer_results: result.analyzer_results,
operational_failures: result.operational_failures,
decision_reasons: result.decision_reasons,
git: git_payload,
rails_context: rails_context_h
)
elsif git_payload
result = GateResult.new(
completion_status: result.completion_status,
gate: result.gate,
policy_status: result.policy_status,
findings: result.findings,
analyzer_results: result.analyzer_results,
operational_failures: result.operational_failures,
decision_reasons: result.decision_reasons,
git: git_payload,
rails_context: rails_context_h
)
elsif rails_context_h
result = GateResult.new(
completion_status: result.completion_status,
gate: result.gate,
policy_status: result.policy_status,
findings: result.findings,
analyzer_results: result.analyzer_results,
operational_failures: result.operational_failures,
decision_reasons: result.decision_reasons,
rails_context: rails_context_h
)
end
Outcome.new(result: result, context: context, configuration: configuration, findings: classified_findings.freeze)
rescue ConfigurationError => error
Outcome.new(
result: Verification::Policy.incomplete_result(
operational_failures: [{ "code" => "configuration", "message" => canonical_error_message(error, root) }],
code: "configuration",
message: "Configuration is invalid; no analyzer evidence was evaluated."
),
context: nil,
configuration: nil,
findings: [].freeze
)
rescue ProcessRunner::DirectoryError, RailVerdict::Error => error
Outcome.new(
result: Verification::Policy.incomplete_result(
operational_failures: [{ "code" => "failed", "message" => error.message }],
code: "execution_failed",
message: "Verification could not complete."
),
context: nil,
configuration: nil,
findings: [].freeze
)
end
|