github/actions: fix annotation title handling

We currently generate invalid workflow commands when we create
annotations with a `title` but no `file`. Let's fix that.

While we're here, let's improve handling of `title`s with `:`s. We
cannot use `title`s with `::` since GitHub Actions uses this as a
separator for different fields between a workflow command.

Let's also make sure `metadata` never ends in a `:` to avoid confusing
the GitHub Actions command parser about where the `::` separator is
meant to be.
This commit is contained in:
Carlo Cabrera 2024-09-08 17:56:04 +08:00
parent 170a7ea56e
commit 54383d1c5f
No known key found for this signature in database
GPG Key ID: C74D447FC549A1D0

View File

@ -81,6 +81,7 @@ module GitHub
} }
def initialize(type, message, file: nil, 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) raise ArgumentError, "Unsupported type: #{type.inspect}" if ANNOTATION_TYPES.exclude?(type)
raise ArgumentError, "`title` must not contain `::`" if title.present? && title.include?("::")
require "utils/tty" require "utils/tty"
@type = type @type = type
@ -110,7 +111,11 @@ module GitHub
end end
end end
metadata << ",title=#{Actions.escape(@title)}" if @title if @title
metadata << (@file ? "," : " ")
metadata << "title=#{Actions.escape(@title)}"
end
metadata << " " if metadata.end_with?(":")
"::#{metadata}::#{Actions.escape(@message)}" "::#{metadata}::#{Actions.escape(@message)}"
end end