Skip to content

Commit cfcd26c

Browse files
committed
Swift: support markdown TSP diagnostics
1 parent d8c0054 commit cfcd26c

File tree

7 files changed

+42
-22
lines changed

7 files changed

+42
-22
lines changed

swift/integration-tests/linux-only/autobuilder/unsupported-os/diagnostics.expected

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
{
22
"helpLinks": [
3-
"https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idruns-on",
4-
"https://docs.github.com/en/enterprise-server/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/customizing-code-scanning",
5-
"https://docs.github.com/en/enterprise-server/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-the-codeql-workflow-for-compiled-languages#adding-build-steps-for-a-compiled-language"
3+
""
64
],
7-
"plaintextMessage": "CodeQL Swift analysis is currently only officially supported on macOS.\n\nChange the action runner to a macOS one. Analysis on Linux might work, but requires setting up a custom build command.",
5+
"markdownMessage": "Currently, `autobuild` for Swift analysis is only supported on macOS.\n\n[Change the Actions runner][1] to run on macOS.\n\nYou may be able to run analysis on Linux by setting up a [manual build command][2].\n\n[1]: https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idruns-on\n[2]: https://docs.github.com/en/enterprise-server/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-the-codeql-workflow-for-compiled-languages#adding-build-steps-for-a-compiled-language",
86
"severity": "error",
97
"source": {
108
"extractorName": "swift",
119
"id": "swift/autobuilder/incompatible-os",
12-
"name": "Incompatible operating system for autobuild (expected macOS)"
10+
"name": "Incompatible operating system (expected macOS)"
1311
},
1412
"visibility": {
1513
"cliSummaryTable": true,

swift/logging/SwiftDiagnostics.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ nlohmann::json SwiftDiagnostic::json(const std::chrono::system_clock::time_point
2525
}},
2626
{"severity", "error"},
2727
{"helpLinks", std::vector<std::string_view>(absl::StrSplit(helpLinks, ' '))},
28-
{"plaintextMessage", absl::StrCat(message, ".\n\n", action, ".")},
28+
{format == Format::markdown ? "markdownMessage" : "plaintextMessage",
29+
absl::StrCat(message, ".\n\n", action)},
2930
{"timestamp", fmt::format("{:%FT%T%z}", timestamp)},
3031
};
3132
if (location) {

swift/logging/SwiftDiagnostics.h

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ struct SwiftDiagnosticsLocation {
3333
// These are internally stored into a map on id's. A specific error log can use binlog's category
3434
// as id, which will then be used to recover the diagnostic source while dumping.
3535
struct SwiftDiagnostic {
36+
enum class Format {
37+
plaintext,
38+
markdown,
39+
};
40+
3641
enum class Visibility : unsigned char {
3742
none = 0b000,
3843
statusPage = 0b001,
@@ -44,6 +49,7 @@ struct SwiftDiagnostic {
4449
std::string_view id;
4550
std::string_view name;
4651
static constexpr std::string_view extractorName = "swift";
52+
Format format;
4753
std::string_view action;
4854
// space separated if more than 1. Not a vector to allow constexpr
4955
// TODO(C++20) with vector going constexpr this can be turned to `std::vector<std::string_view>`
@@ -54,15 +60,20 @@ struct SwiftDiagnostic {
5460

5561
std::optional<SwiftDiagnosticsLocation> location{};
5662

63+
// notice help links are really required only for plaintext messages, otherwise they should be
64+
// directly embedded in the markdown message
5765
constexpr SwiftDiagnostic(std::string_view id,
5866
std::string_view name,
67+
Format format,
5968
std::string_view action = "",
6069
std::string_view helpLinks = "",
6170
Visibility visibility = Visibility::all)
62-
: id{id}, name{name}, action{action}, helpLinks{helpLinks}, visibility{visibility} {}
63-
64-
constexpr SwiftDiagnostic(std::string_view id, std::string_view name, Visibility visibility)
65-
: SwiftDiagnostic(id, name, "", "", visibility) {}
71+
: id{id},
72+
name{name},
73+
format{format},
74+
action{action},
75+
helpLinks{helpLinks},
76+
visibility{visibility} {}
6677

6778
// create a JSON diagnostics for this source with the given timestamp and message to out
6879
// 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
103114
constexpr SwiftDiagnostic internalError{
104115
"internal-error",
105116
"Internal error",
117+
SwiftDiagnostic::Format::plaintext,
118+
/* action=*/"",
119+
/* helpLinks=*/"",
106120
SwiftDiagnostic::Visibility::telemetry,
107121
};
108122
} // namespace codeql

swift/tools/autobuilder-diagnostics/IncompatibleOs.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@
99
const std::string_view codeql::programName = "autobuilder";
1010

1111
constexpr codeql::SwiftDiagnostic incompatibleOs{
12-
"incompatible-os", "Incompatible operating system for autobuild (expected macOS)",
13-
"Change the action runner to a macOS one. Analysis on Linux might work, but requires setting "
14-
"up a custom build command",
12+
"incompatible-os", "Incompatible operating system (expected macOS)",
13+
codeql::SwiftDiagnostic::Format::markdown,
14+
"[Change the Actions runner][1] to run on macOS.\n"
15+
"\n"
16+
"You may be able to run analysis on Linux by setting up a [manual build command][2].\n"
17+
"\n"
18+
"[1]: "
1519
"https://docs.github.com/en/actions/using-workflows/"
16-
"workflow-syntax-for-github-actions#jobsjob_idruns-on "
17-
"https://docs.github.com/en/enterprise-server/code-security/code-scanning/"
18-
"automatically-scanning-your-code-for-vulnerabilities-and-errors/customizing-code-scanning "
20+
"workflow-syntax-for-github-actions#jobsjob_idruns-on\n"
21+
"[2]: "
1922
"https://docs.github.com/en/enterprise-server/code-security/code-scanning/"
2023
"automatically-scanning-your-code-for-vulnerabilities-and-errors/"
2124
"configuring-the-codeql-workflow-for-compiled-languages#adding-build-steps-for-a-compiled-"
@@ -28,6 +31,6 @@ static codeql::Logger& logger() {
2831

2932
int main() {
3033
DIAGNOSE_ERROR(incompatibleOs,
31-
"CodeQL Swift analysis is currently only officially supported on macOS");
34+
"Currently, `autobuild` for Swift analysis is only supported on macOS");
3235
return 1;
3336
}

swift/xcode-autobuilder/CustomizingBuildDiagnostics.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <string_view>
22

33
namespace codeql {
4-
constexpr std::string_view customizingBuildAction = "Set up a manual build command";
4+
constexpr std::string_view customizingBuildAction = "Set up a manual build command.";
55
constexpr std::string_view customizingBuildHelpLinks =
66
"https://docs.github.com/en/enterprise-server/code-security/code-scanning/"
77
"automatically-scanning-your-code-for-vulnerabilities-and-errors/customizing-code-scanning "

swift/xcode-autobuilder/XcodeBuildRunner.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
#include "swift/xcode-autobuilder/CustomizingBuildDiagnostics.h"
1010

1111
constexpr codeql::SwiftDiagnostic buildCommandFailed{
12-
"build-command-failed", "Detected build command failed", codeql::customizingBuildAction,
12+
"build-command-failed", "Detected build command failed",
13+
codeql::SwiftDiagnostic::Format::plaintext, codeql::customizingBuildAction,
1314
codeql::customizingBuildHelpLinks};
1415

1516
static codeql::Logger& logger() {

swift/xcode-autobuilder/xcode-autobuilder.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,19 @@ static const char* unitTest = "com.apple.product-type.bundle.unit-test";
1313
const std::string_view codeql::programName = "autobuilder";
1414

1515
constexpr codeql::SwiftDiagnostic noProjectFound{
16-
"no-project-found", "No Xcode project or workspace detected", codeql::customizingBuildAction,
16+
"no-project-found", "No Xcode project or workspace detected",
17+
codeql::SwiftDiagnostic::Format::plaintext, codeql::customizingBuildAction,
1718
codeql::customizingBuildHelpLinks};
1819

1920
constexpr codeql::SwiftDiagnostic noSwiftTarget{
20-
"no-swift-target", "No Swift compilation target found", codeql::customizingBuildAction,
21+
"no-swift-target", "No Swift compilation target found",
22+
codeql::SwiftDiagnostic::Format::plaintext, codeql::customizingBuildAction,
2123
codeql::customizingBuildHelpLinks};
2224

2325
constexpr codeql::SwiftDiagnostic spmNotSupported{
2426
"spm-not-supported", "Swift Package Manager build unsupported by autobuild",
25-
codeql::customizingBuildAction, codeql::customizingBuildHelpLinks};
27+
codeql::SwiftDiagnostic::Format::plaintext, codeql::customizingBuildAction,
28+
codeql::customizingBuildHelpLinks};
2629

2730
static codeql::Logger& logger() {
2831
static codeql::Logger ret{"main"};

0 commit comments

Comments
 (0)