Swift: support markdown TSP diagnostics

This commit is contained in:
Paolo Tranquilli
2023-05-15 12:41:30 +02:00
parent d8c0054ea9
commit cfcd26cf0d
7 changed files with 42 additions and 22 deletions

View File

@@ -25,7 +25,8 @@ nlohmann::json SwiftDiagnostic::json(const std::chrono::system_clock::time_point
}},
{"severity", "error"},
{"helpLinks", std::vector<std::string_view>(absl::StrSplit(helpLinks, ' '))},
{"plaintextMessage", absl::StrCat(message, ".\n\n", action, ".")},
{format == Format::markdown ? "markdownMessage" : "plaintextMessage",
absl::StrCat(message, ".\n\n", action)},
{"timestamp", fmt::format("{:%FT%T%z}", timestamp)},
};
if (location) {

View File

@@ -33,6 +33,11 @@ struct SwiftDiagnosticsLocation {
// These are internally stored into a map on id's. A specific error log can use binlog's category
// as id, which will then be used to recover the diagnostic source while dumping.
struct SwiftDiagnostic {
enum class Format {
plaintext,
markdown,
};
enum class Visibility : unsigned char {
none = 0b000,
statusPage = 0b001,
@@ -44,6 +49,7 @@ struct SwiftDiagnostic {
std::string_view id;
std::string_view name;
static constexpr std::string_view extractorName = "swift";
Format format;
std::string_view action;
// space separated if more than 1. Not a vector to allow constexpr
// TODO(C++20) with vector going constexpr this can be turned to `std::vector<std::string_view>`
@@ -54,15 +60,20 @@ struct SwiftDiagnostic {
std::optional<SwiftDiagnosticsLocation> location{};
// notice help links are really required only for plaintext messages, otherwise they should be
// directly embedded in the markdown message
constexpr SwiftDiagnostic(std::string_view id,
std::string_view name,
Format format,
std::string_view action = "",
std::string_view helpLinks = "",
Visibility visibility = Visibility::all)
: id{id}, name{name}, action{action}, helpLinks{helpLinks}, visibility{visibility} {}
constexpr SwiftDiagnostic(std::string_view id, std::string_view name, Visibility visibility)
: SwiftDiagnostic(id, name, "", "", visibility) {}
: id{id},
name{name},
format{format},
action{action},
helpLinks{helpLinks},
visibility{visibility} {}
// create a JSON diagnostics for this source with the given timestamp and message to out
// A plaintextMessage is used that includes both the message and the action to take. Dots are
@@ -103,6 +114,9 @@ inline constexpr SwiftDiagnostic::Visibility operator&(SwiftDiagnostic::Visibili
constexpr SwiftDiagnostic internalError{
"internal-error",
"Internal error",
SwiftDiagnostic::Format::plaintext,
/* action=*/"",
/* helpLinks=*/"",
SwiftDiagnostic::Visibility::telemetry,
};
} // namespace codeql