From c6e2cd9037da7e6ab12ba4b02c4bd2b38deb9bea Mon Sep 17 00:00:00 2001 From: Carlo Cabrera <30379873+carlocab@users.noreply.github.com> Date: Sun, 15 Sep 2024 11:31:54 +0800 Subject: [PATCH] Limit usage of GitHub Actions Annotations - only use annotations for `opoo` and `onoe` if `HOMEBREW_GITHUB_ACTIONS` is set. This will make using `brew` less noisy in GitHub Actions for third parties. See Homebrew/discussions#5602. - if we've already called `puts_annotation_if_env_set`, then we no longer need to print the message to `$stderr`. The message from the annotation already show up in the GitHub Actions log, so printing to `$stderr` just leads to duplicate messages in the log. While we're here, let's make sure to forward the `file:` and `line:` kwargs of `puts_annotation_if_env_set` to the `Annotation` constructor. --- Library/Homebrew/extend/kernel.rb | 14 ++++++++++---- Library/Homebrew/utils/github/actions.rb | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/Library/Homebrew/extend/kernel.rb b/Library/Homebrew/extend/kernel.rb index 90481e28bc..465e76e895 100644 --- a/Library/Homebrew/extend/kernel.rb +++ b/Library/Homebrew/extend/kernel.rb @@ -65,8 +65,11 @@ module Kernel sig { params(message: T.any(String, Exception)).void } def opoo(message) Tty.with($stderr) do |stderr| - stderr.puts Formatter.warning(message, label: "Warning") - GitHub::Actions.puts_annotation_if_env_set(:warning, message.to_s) + if ENV["HOMEBREW_GITHUB_ACTIONS"].present? + GitHub::Actions.puts_annotation_if_env_set(:warning, message.to_s) + else + stderr.puts Formatter.warning(message, label: "Warning") + end end end @@ -79,8 +82,11 @@ module Kernel require "utils/github/actions" Tty.with($stderr) do |stderr| - stderr.puts Formatter.error(message, label: "Error") - GitHub::Actions.puts_annotation_if_env_set(:error, message.to_s) + if ENV["HOMEBREW_GITHUB_ACTIONS"].present? + GitHub::Actions.puts_annotation_if_env_set(:error, message.to_s) + else + stderr.puts Formatter.error(message, label: "Error") + end end end diff --git a/Library/Homebrew/utils/github/actions.rb b/Library/Homebrew/utils/github/actions.rb index c2026dfdc3..116795279d 100644 --- a/Library/Homebrew/utils/github/actions.rb +++ b/Library/Homebrew/utils/github/actions.rb @@ -51,7 +51,7 @@ module GitHub return unless env_set? std = (type == :notice) ? $stdout : $stderr - std.puts Annotation.new(type, message) + std.puts Annotation.new(type, message, file:, line:) end # Helper class for formatting annotations on GitHub Actions.