Skip to content

Commit

Permalink
extend/kernel: make opoo/odie/etc. print GitHub Actions notes.
Browse files Browse the repository at this point in the history
We already do this for deprecations but these may make warnings
and errors from Homebrew easier to spot in GitHub Actions logs.
  • Loading branch information
MikeMcQuaid committed May 9, 2024
1 parent fbb73ba commit 1c0b23f
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 22 deletions.
3 changes: 1 addition & 2 deletions Library/Homebrew/cask/installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,9 @@ def check_deprecate_disable

case deprecate_disable_type
when :deprecated
puts "::warning::#{message_full}" if ENV["GITHUB_ACTIONS"]
opoo message_full
when :disabled
puts "::error::#{message_full}" if ENV["GITHUB_ACTIONS"]
puts GitHub::Actions::Annotation.new(:error, message) if GitHub::Actions.env_set?
raise CaskCannotBeInstalledError.new(@cask, message)
end
end
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/dev-cmd/audit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ def run
ofail "#{errors_summary}."
end

return unless ENV["GITHUB_ACTIONS"]
return unless GitHub::Actions.env_set?

annotations = formula_problems.merge(cask_problems).flat_map do |(_, path), problems|
problems.map do |problem|
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/dev-cmd/pr-pull.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def run
safe_system HOMEBREW_BREW_FILE, *upload_args
end
ensure
if args.retain_bottle_dir? && ENV["GITHUB_ACTIONS"]
if args.retain_bottle_dir? && GitHub::Actions.env_set?
ohai "Bottle files retained at:", dir
File.open(ENV.fetch("GITHUB_OUTPUT"), "a") do |f|
f.puts "bottle_path=#{dir}"
Expand Down
3 changes: 3 additions & 0 deletions Library/Homebrew/dev-cmd/tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ def setup_environment!
ENV["HOMEBREW_TEST_ONLINE"] = "1" if args.online?
ENV["HOMEBREW_SORBET_RUNTIME"] = "1"

# We don't want tests to change behaviour based on where they are run.
ENV.delete("GITHUB_ACTIONS")

# TODO: remove this and fix tests when possible.
ENV["HOMEBREW_NO_INSTALL_FROM_API"] = "1"
ENV.delete("HOMEBREW_INTERNAL_JSON_V3")
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/diagnostic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@ def check_cask_install_location

def check_cask_staging_location
# Skip this check when running CI since the staging path is not writable for security reasons
return if ENV["GITHUB_ACTIONS"]
return if GitHub::Actions.env_set?

path = Cask::Caskroom.path

Expand Down
7 changes: 5 additions & 2 deletions Library/Homebrew/extend/kernel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def oh1(title, truncate: :auto)
def opoo(message)
Tty.with($stderr) do |stderr|
stderr.puts Formatter.warning(message, label: "Warning")
puts GitHub::Actions::Annotation.new(:warning, message) if GitHub::Actions.env_set?
end
end

Expand All @@ -73,6 +74,7 @@ def opoo(message)
def onoe(message)
Tty.with($stderr) do |stderr|
stderr.puts Formatter.error(message, label: "Error")
puts GitHub::Actions::Annotation.new(:error, message) if GitHub::Actions.env_set?
end
end

Expand Down Expand Up @@ -151,19 +153,20 @@ def odeprecated(method, replacement = nil,
tap_message << ":\n #{line.sub(/^(.*:\d+):.*$/, '\1')}\n\n"
break
end
file, line, = backtrace.first.split(":")

message = +"Calling #{method} is #{verb}! #{replacement_message}"
message << tap_message if tap_message
message.freeze

disable = true if disable_for_developers && Homebrew::EnvConfig.developer?
if disable || Homebrew.raise_deprecation_exceptions?
puts "::error::#{message}" if ENV["GITHUB_ACTIONS"]
puts GitHub::Actions::Annotation.new(:error, message, file:, line:) if GitHub::Actions.env_set?
exception = MethodDeprecatedError.new(message)
exception.set_backtrace(backtrace)
raise exception
elsif !Homebrew.auditing?
puts "::warning::#{message}" if ENV["GITHUB_ACTIONS"]
puts GitHub::Actions::Annotation.new(:warning, message, file:, line:) if GitHub::Actions.env_set?
opoo message
end
end
Expand Down
4 changes: 2 additions & 2 deletions Library/Homebrew/extend/os/mac/diagnostic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def check_xcode_up_to_date
# `brew test-bot` runs `brew doctor` in the CI for the Homebrew/brew
# repository. This only needs to support whatever CI providers
# Homebrew/brew is currently using.
return if ENV["GITHUB_ACTIONS"]
return if GitHub::Actions.env_set?

# With fake El Capitan for Portable Ruby, we are intentionally not using Xcode 8.
# This is because we are not using the CLT and Xcode 8 has the 10.12 SDK.
Expand Down Expand Up @@ -165,7 +165,7 @@ def check_clt_up_to_date
# `brew test-bot` runs `brew doctor` in the CI for the Homebrew/brew
# repository. This only needs to support whatever CI providers
# Homebrew/brew is currently using.
return if ENV["GITHUB_ACTIONS"]
return if GitHub::Actions.env_set?

<<~EOS
A newer Command Line Tools release is available.
Expand Down
3 changes: 1 addition & 2 deletions Library/Homebrew/formula_installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,9 @@ def prelude

case deprecate_disable_type
when :deprecated
puts "::warning::#{message}" if ENV["GITHUB_ACTIONS"]
opoo message
when :disabled
puts "::error::#{message}" if ENV["GITHUB_ACTIONS"]
puts GitHub::Actions::Annotation.new(:error, message) if GitHub::Actions.env_set?
raise CannotInstallFormulaError, message
end
end
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/style.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module Style
def self.check_style_and_print(files, **options)
success = check_style_impl(files, :print, **options)

if ENV["GITHUB_ACTIONS"] && !success
if GitHub::Actions.env_set? && !success
check_style_json(files, **options).each do |path, offenses|
offenses.each do |o|
line = o.location.line
Expand Down
29 changes: 19 additions & 10 deletions Library/Homebrew/utils/github/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ def self.format_multiline_string(name, value)
EOS
end

sig { returns(T::Boolean) }
def self.env_set?
ENV.fetch("GITHUB_ACTIONS", false).present?
end

# Helper class for formatting annotations on GitHub Actions.
class Annotation
ANNOTATION_TYPES = [:notice, :warning, :error].freeze
Expand All @@ -52,20 +57,20 @@ def self.path_relative_to_workspace(path)
params(
type: Symbol,
message: String,
file: T.any(String, Pathname),
file: T.nilable(T.any(String, Pathname)),
title: T.nilable(String),
line: T.nilable(Integer),
end_line: T.nilable(Integer),
column: T.nilable(Integer),
end_column: T.nilable(Integer),
).void
}
def initialize(type, message, file:, title: nil, line: nil, end_line: nil, column: nil, end_column: nil)
def initialize(type, message, file: nil, title: nil, line: nil, end_line: nil, column: nil, end_column: nil)
raise ArgumentError, "Unsupported type: #{type.inspect}" if ANNOTATION_TYPES.exclude?(type)

@type = type
@message = Tty.strip_ansi(message)
@file = self.class.path_relative_to_workspace(file)
@file = self.class.path_relative_to_workspace(file) if file.present?
@title = Tty.strip_ansi(title) if title
@line = Integer(line) if line
@end_line = Integer(end_line) if end_line
Expand All @@ -76,15 +81,17 @@ def initialize(type, message, file:, title: nil, line: nil, end_line: nil, colum
sig { returns(String) }
def to_s
metadata = @type.to_s
metadata << " file=#{Actions.escape(@file.to_s)}"
if @file
metadata << " file=#{Actions.escape(@file.to_s)}"

if @line
metadata << ",line=#{@line}"
metadata << ",endLine=#{@end_line}" if @end_line
if @line
metadata << ",line=#{@line}"
metadata << ",endLine=#{@end_line}" if @end_line

if @column
metadata << ",col=#{@column}"
metadata << ",endColumn=#{@end_column}" if @end_column
if @column
metadata << ",col=#{@column}"
metadata << ",endColumn=#{@end_column}" if @end_column
end
end
end

Expand All @@ -97,6 +104,8 @@ def to_s
# the `GITHUB_WORKSPACE` directory or if no `file` is specified.
sig { returns(T::Boolean) }
def relevant?
return true if file.blank?

@file.descend.next.to_s != ".."
end
end
Expand Down

0 comments on commit 1c0b23f

Please sign in to comment.