Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

utils/github/actions: make file a mandatory argument #12439

Merged
merged 1 commit into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions Library/Homebrew/test/utils/github/actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@
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
Expand All @@ -30,6 +30,12 @@
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)

Expand Down
34 changes: 11 additions & 23 deletions Library/Homebrew/utils/github/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,21 @@ def self.path_relative_to_workspace(path)
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
Expand All @@ -61,29 +61,19 @@ def initialize(type, message, title: nil, file: nil, line: nil, end_line: nil, c
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
Expand All @@ -92,8 +82,6 @@ def to_s
# 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
Expand Down