Merge pull request #11016 from reitermarkus/cask-audit-annotations

Fix audit annotations for casks.
This commit is contained in:
Markus Reiter 2021-04-03 18:33:46 +02:00 committed by GitHub
commit 1ce8ea659c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 62 additions and 62 deletions

View File

@ -96,15 +96,15 @@ module Cask
@warnings ||= [] @warnings ||= []
end end
def add_error(message) def add_error(message, location: nil)
errors << message errors << ({ message: message, location: location })
end end
def add_warning(message) def add_warning(message, location: nil)
if strict? if strict?
add_error message add_error message, location: location
else else
warnings << message warnings << ({ message: message, location: location })
end end
end end
@ -134,12 +134,12 @@ module Cask
summary = ["audit for #{cask}: #{result}"] summary = ["audit for #{cask}: #{result}"]
errors.each do |error| errors.each do |error|
summary << " #{Formatter.error("-")} #{error}" summary << " #{Formatter.error("-")} #{error[:message]}"
end end
if include_warnings if include_warnings
warnings.each do |warning| warnings.each do |warning|
summary << " #{Formatter.warning("-")} #{warning}" summary << " #{Formatter.warning("-")} #{warning[:message]}"
end end
end end

View File

@ -59,8 +59,6 @@ module Cask
display_failures_only: args.display_failures_only?, display_failures_only: args.display_failures_only?,
) )
self.class.print_annotations(results)
failed_casks = results.reject { |_, result| result[:errors].empty? }.map(&:first) failed_casks = results.reject { |_, result| result[:errors].empty? }.map(&:first)
return if failed_casks.empty? return if failed_casks.empty?
@ -103,23 +101,9 @@ module Cask
casks.map do |cask| casks.map do |cask|
odebug "Auditing Cask #{cask}" odebug "Auditing Cask #{cask}"
[cask, Auditor.audit(cask, **options)] [cask.sourcefile_path, Auditor.audit(cask, **options)]
end.to_h end.to_h
end end
def self.print_annotations(results)
return unless ENV["GITHUB_ACTIONS"]
results.each do |cask, result|
cask_path = cask.sourcefile_path
annotations = (result[:warnings].map { |w| [:warning, w] } + result[:errors].map { |e| [:error, e] })
.map { |type, message| GitHub::Actions::Annotation.new(type, message, file: cask_path) }
annotations.each do |annotation|
puts annotation if annotation.relevant?
end
end
end
end end
end end
end end

View File

@ -166,7 +166,7 @@ module Homebrew
spdx_license_data = SPDX.license_data spdx_license_data = SPDX.license_data
spdx_exception_data = SPDX.exception_data spdx_exception_data = SPDX.exception_data
new_formula_problem_lines = [] new_formula_problem_lines = []
audit_formulae.sort.each do |f| formula_results = audit_formulae.sort.map do |f|
only = only_cops ? ["style"] : args.only only = only_cops ? ["style"] : args.only
options = { options = {
new_formula: new_formula, new_formula: new_formula,
@ -184,8 +184,8 @@ module Homebrew
fa = FormulaAuditor.new(f, **options) fa = FormulaAuditor.new(f, **options)
fa.audit fa.audit
next if fa.problems.empty? && fa.new_formula_problems.empty?
if fa.problems.any? || fa.new_formula_problems.any?
formula_count += 1 formula_count += 1
problem_count += fa.problems.size problem_count += fa.problems.size
problem_lines = format_problem_lines(fa.problems) problem_lines = format_problem_lines(fa.problems)
@ -196,19 +196,13 @@ module Homebrew
else else
puts "#{f.full_name}:", problem_lines.map { |s| " #{s}" } puts "#{f.full_name}:", problem_lines.map { |s| " #{s}" }
end end
next unless ENV["GITHUB_ACTIONS"]
(fa.problems + fa.new_formula_problems).each do |message:, location:|
annotation = GitHub::Actions::Annotation.new(
:error, message, file: f.path, line: location&.line, column: location&.column
)
puts annotation if annotation.relevant?
end
end end
casks_results = if audit_casks.empty? [f.path, { errors: fa.problems + fa.new_formula_problems, warnings: [] }]
[] end.to_h
cask_results = if audit_casks.empty?
{}
else else
require "cask/cmd/audit" require "cask/cmd/audit"
@ -228,15 +222,15 @@ module Homebrew
) )
end end
failed_casks = casks_results.reject { |_, result| result[:errors].empty? } failed_casks = cask_results.reject { |_, result| result[:errors].empty? }
cask_count = failed_casks.count cask_count = failed_casks.count
cask_problem_count = failed_casks.sum { |_, result| result[:warnings].count + result[:errors].count } cask_problem_count = failed_casks.sum { |_, result| result[:warnings].count + result[:errors].count }
new_formula_problem_count += new_formula_problem_lines.count new_formula_problem_count += new_formula_problem_lines.count
total_problems_count = problem_count + new_formula_problem_count + cask_problem_count + tap_problem_count total_problems_count = problem_count + new_formula_problem_count + cask_problem_count + tap_problem_count
return unless total_problems_count.positive?
if total_problems_count.positive?
puts new_formula_problem_lines.map { |s| " #{s}" } puts new_formula_problem_lines.map { |s| " #{s}" }
errors_summary = "#{total_problems_count} #{"problem".pluralize(total_problems_count)}" errors_summary = "#{total_problems_count} #{"problem".pluralize(total_problems_count)}"
@ -257,6 +251,28 @@ module Homebrew
ofail errors_summary ofail errors_summary
end end
return unless ENV["GITHUB_ACTIONS"]
annotations = formula_results.merge(cask_results).flat_map do |path, result|
(
result[:warnings].map { |w| [:warning, w] } +
result[:errors].map { |e| [:error, e] }
).map do |type, problem|
GitHub::Actions::Annotation.new(
type,
problem[:message],
file: path,
line: problem[:location]&.line,
column: problem[:location]&.column,
)
end
end
annotations.each do |annotation|
puts annotation if annotation.relevant?
end
end
def format_problem_lines(problems) def format_problem_lines(problems)
problems.uniq problems.uniq
.map { |message:, location:| format_problem(message, location) } .map { |message:, location:| format_problem(message, location) }

View File

@ -4,11 +4,11 @@
require "cask/audit" require "cask/audit"
describe Cask::Audit, :cask do describe Cask::Audit, :cask do
def include_msg?(messages, msg) def include_msg?(problems, msg)
if msg.is_a?(Regexp) if msg.is_a?(Regexp)
Array(messages).any? { |m| m =~ msg } Array(problems).any? { |problem| problem[:message] =~ msg }
else else
Array(messages).include?(msg) Array(problems).any? { |problem| problem[:message] == msg }
end end
end end