diff --git a/Library/Homebrew/test/utils/github/actions_spec.rb b/Library/Homebrew/test/utils/github/actions_spec.rb index 290e0f8550..3bd0f3f255 100644 --- a/Library/Homebrew/test/utils/github/actions_spec.rb +++ b/Library/Homebrew/test/utils/github/actions_spec.rb @@ -9,19 +9,19 @@ describe GitHub::Actions::Annotation do describe "#new" do it "fails when the type is wrong" do expect { - described_class.new(:fatal, message) + described_class.new(:fatal, message, file: "file.txt") }.to raise_error(ArgumentError) end end describe "#to_s" do it "escapes newlines" do - annotation = described_class.new(:warning, <<~EOS) + annotation = described_class.new(:warning, <<~EOS, file: "file.txt") lorem ipsum EOS - expect(annotation.to_s).to eq "::warning::lorem%0Aipsum%0A" + expect(annotation.to_s).to eq "::warning file=file.txt::lorem%0Aipsum%0A" end it "allows specifying the file" do @@ -30,6 +30,12 @@ describe GitHub::Actions::Annotation do expect(annotation.to_s).to eq "::warning file=file.txt::lorem ipsum" end + it "allows specifying the title" do + annotation = described_class.new(:warning, "lorem ipsum", file: "file.txt", title: "foo") + + expect(annotation.to_s).to eq "::warning file=file.txt,title=foo::lorem ipsum" + end + it "allows specifying the file and line" do annotation = described_class.new(:error, "lorem ipsum", file: "file.txt", line: 3) diff --git a/Library/Homebrew/utils/github/actions.rb b/Library/Homebrew/utils/github/actions.rb index a248c65e23..c255dec4a0 100644 --- a/Library/Homebrew/utils/github/actions.rb +++ b/Library/Homebrew/utils/github/actions.rb @@ -37,21 +37,21 @@ module GitHub params( type: Symbol, message: String, + file: T.any(String, Pathname), title: T.nilable(String), - file: T.nilable(T.any(String, Pathname)), line: T.nilable(Integer), end_line: T.nilable(Integer), column: T.nilable(Integer), end_column: T.nilable(Integer), ).void } - def initialize(type, message, title: nil, file: nil, line: nil, end_line: nil, column: nil, end_column: nil) + def initialize(type, message, file:, 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) @title = Tty.strip_ansi(title) if title - @file = self.class.path_relative_to_workspace(file) if file @line = Integer(line) if line @end_line = Integer(end_line) if end_line @column = Integer(column) if column @@ -61,29 +61,19 @@ module GitHub 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 - end + if @column + metadata << ",col=#{@column}" + metadata << ",endColumn=#{@end_column}" if @end_column end end - if @title - metadata << if @file - "," - else - " " - end - metadata << "title=#{Actions.escape(@title)}" - end + metadata << ",title=#{Actions.escape(@title)}" if @title "::#{metadata}::#{Actions.escape(@message)}" end @@ -92,8 +82,6 @@ module GitHub # the `GITHUB_WORKSPACE` directory or if no `file` is specified. sig { returns(T::Boolean) } def relevant? - return true if @file.nil? - @file.descend.next.to_s != ".." end end