Merge pull request #12439 from carlocab/file-annotations

utils/github/actions: make `file` a mandatory argument
This commit is contained in:
Mike McQuaid 2021-11-17 10:48:57 +00:00 committed by GitHub
commit 5e72aea97c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 26 deletions

View File

@ -9,19 +9,19 @@ describe GitHub::Actions::Annotation do
describe "#new" do describe "#new" do
it "fails when the type is wrong" do it "fails when the type is wrong" do
expect { expect {
described_class.new(:fatal, message) described_class.new(:fatal, message, file: "file.txt")
}.to raise_error(ArgumentError) }.to raise_error(ArgumentError)
end end
end end
describe "#to_s" do describe "#to_s" do
it "escapes newlines" do it "escapes newlines" do
annotation = described_class.new(:warning, <<~EOS) annotation = described_class.new(:warning, <<~EOS, file: "file.txt")
lorem lorem
ipsum ipsum
EOS 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 end
it "allows specifying the file" do 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" expect(annotation.to_s).to eq "::warning file=file.txt::lorem ipsum"
end 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 it "allows specifying the file and line" do
annotation = described_class.new(:error, "lorem ipsum", file: "file.txt", line: 3) annotation = described_class.new(:error, "lorem ipsum", file: "file.txt", line: 3)

View File

@ -37,21 +37,21 @@ module GitHub
params( params(
type: Symbol, type: Symbol,
message: String, message: String,
file: T.any(String, Pathname),
title: T.nilable(String), title: T.nilable(String),
file: T.nilable(T.any(String, Pathname)),
line: T.nilable(Integer), line: T.nilable(Integer),
end_line: T.nilable(Integer), end_line: T.nilable(Integer),
column: T.nilable(Integer), column: T.nilable(Integer),
end_column: T.nilable(Integer), end_column: T.nilable(Integer),
).void ).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) raise ArgumentError, "Unsupported type: #{type.inspect}" if ANNOTATION_TYPES.exclude?(type)
@type = type @type = type
@message = Tty.strip_ansi(message) @message = Tty.strip_ansi(message)
@file = self.class.path_relative_to_workspace(file)
@title = Tty.strip_ansi(title) if title @title = Tty.strip_ansi(title) if title
@file = self.class.path_relative_to_workspace(file) if file
@line = Integer(line) if line @line = Integer(line) if line
@end_line = Integer(end_line) if end_line @end_line = Integer(end_line) if end_line
@column = Integer(column) if column @column = Integer(column) if column
@ -61,8 +61,6 @@ module GitHub
sig { returns(String) } sig { returns(String) }
def to_s def to_s
metadata = @type.to_s metadata = @type.to_s
if @file
metadata << " file=#{Actions.escape(@file.to_s)}" metadata << " file=#{Actions.escape(@file.to_s)}"
if @line if @line
@ -74,16 +72,8 @@ module GitHub
metadata << ",endColumn=#{@end_column}" if @end_column metadata << ",endColumn=#{@end_column}" if @end_column
end end
end end
end
if @title metadata << ",title=#{Actions.escape(@title)}" if @title
metadata << if @file
","
else
" "
end
metadata << "title=#{Actions.escape(@title)}"
end
"::#{metadata}::#{Actions.escape(@message)}" "::#{metadata}::#{Actions.escape(@message)}"
end end
@ -92,8 +82,6 @@ module GitHub
# the `GITHUB_WORKSPACE` directory or if no `file` is specified. # the `GITHUB_WORKSPACE` directory or if no `file` is specified.
sig { returns(T::Boolean) } sig { returns(T::Boolean) }
def relevant? def relevant?
return true if @file.nil?
@file.descend.next.to_s != ".." @file.descend.next.to_s != ".."
end end
end end