Output annotations for brew cask audit.

This commit is contained in:
Markus Reiter 2020-09-09 20:41:29 +02:00
parent 1654de3279
commit 769fa066e2
3 changed files with 41 additions and 24 deletions

View File

@ -1,5 +1,7 @@
# frozen_string_literal: true
require "utils/github/actions"
module Cask
class Cmd
# Implementation of the `brew cask audit` command.
@ -57,7 +59,19 @@ module Cask
odebug "Auditing Cask #{cask}"
result = Auditor.audit(cask, **options)
result[:warnings].empty? && result[:errors].empty?
next true if result[:warnings].empty? && result[:errors].empty?
if ENV["GITHUB_ACTIONS"]
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
false
end
return if failed_casks.empty?

View File

@ -15,8 +15,15 @@ module Homebrew
success = check_style_impl(files, :print, **options)
if ENV["GITHUB_ACTIONS"] && !success
offenses = check_style_json(files, **options)
puts offenses.to_github_annotations
check_style_json(files, **options).each do |path, offenses|
offenses.each do |o|
line = o.location.line
column = o.location.line
annotation = GitHub::Actions::Annotation.new(:error, o.message, file: path, line: line, column: column)
puts annotation if annotation.relevant?
end
end
end
success
@ -236,26 +243,6 @@ module Homebrew
def each(*args, &block)
@offenses.each(*args, &block)
end
def to_github_annotations
workspace = ENV["GITHUB_WORKSPACE"]
return [] if workspace.blank?
workspace = Pathname(workspace).realpath
@offenses.flat_map do |path, offenses|
relative_path = path.relative_path_from(workspace)
# Only generate annotations for paths relative to the `GITHUB_WORKSPACE` directory.
next [] if relative_path.descend.next.to_s == ".."
offenses.map do |o|
line = o.location.line
column = o.location.line
GitHub::Actions::Annotation.new(:error, o.message, file: relative_path, line: line, column: column)
end
end
end
end
# A style offense.

View File

@ -14,12 +14,20 @@ module GitHub
# Helper class for formatting annotations on GitHub Actions.
class Annotation
def self.path_relative_to_workspace(path)
workspace = Pathname(ENV.fetch("GITHUB_WORKSPACE", Dir.pwd)).realpath
path = Pathname(path)
return path unless path.exist?
path.realpath.relative_path_from(workspace)
end
def initialize(type, message, file: nil, line: nil, column: nil)
raise ArgumentError, "Unsupported type: #{type.inspect}" unless [:warning, :error].include?(type)
@type = type
@message = String(message)
@file = Pathname(file) if file
@file = self.class.path_relative_to_workspace(file) if file
@line = Integer(line) if line
@column = Integer(column) if column
end
@ -33,6 +41,14 @@ module GitHub
"::#{@type}#{metadata}::#{Actions.escape(@message)}"
end
# An annotation is only relevant if the corresponding `file` is relative to
# the `GITHUB_WORKSPACE` directory or if no `file` is specified.
def relevant?
return true if @file.nil?
@file.descend.next.to_s != ".."
end
end
end
end