Skip to content

Commit c2049c2

Browse files
committed
Refactor to avoid public setters
1 parent 4f0a932 commit c2049c2

File tree

10 files changed

+166
-138
lines changed

10 files changed

+166
-138
lines changed

csharp/autobuilder/Semmle.Autobuild.CSharp/CSharpAutobuilder.cs

Lines changed: 62 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -105,23 +105,30 @@ protected override void AutobuildFailureDiagnostic()
105105
// otherwise, we just report that the one script we found didn't work
106106
if (this.autoBuildRule.BuildCommandAutoRule.CandidatePaths.Count() > 1)
107107
{
108-
message = MakeDiagnostic("multiple-build-scripts", "There are multiple potential build scripts");
109-
message.MarkdownMessage =
110-
"CodeQL found multiple potential build scripts for your project and " +
111-
$"attempted to run `{relScriptPath}`, which failed. " +
112-
"This may not be the right build script for your project. " +
113-
$"Set up a [manual build command]({buildCommandDocsUrl}).";
108+
message = new(
109+
this.Options.Language,
110+
"multiple-build-scripts",
111+
"There are multiple potential build scripts",
112+
markdownMessage:
113+
"CodeQL found multiple potential build scripts for your project and " +
114+
$"attempted to run `{relScriptPath}`, which failed. " +
115+
"This may not be the right build script for your project. " +
116+
$"Set up a [manual build command]({buildCommandDocsUrl})."
117+
);
114118
}
115119
else
116120
{
117-
message = MakeDiagnostic("script-failure", "Unable to build project using build script");
118-
message.MarkdownMessage =
119-
"CodeQL attempted to build your project using a script located at " +
120-
$"`{relScriptPath}`, which failed. " +
121-
$"Set up a [manual build command]({buildCommandDocsUrl}).";
121+
message = new(
122+
this.Options.Language,
123+
"script-failure",
124+
"Unable to build project using build script",
125+
markdownMessage:
126+
"CodeQL attempted to build your project using a script located at " +
127+
$"`{relScriptPath}`, which failed. " +
128+
$"Set up a [manual build command]({buildCommandDocsUrl})."
129+
);
122130
}
123131

124-
message.Severity = DiagnosticMessage.TspSeverity.Error;
125132
AddDiagnostic(message);
126133
}
127134

@@ -135,50 +142,63 @@ protected override void AutobuildFailureDiagnostic()
135142
// then neither of those rules would've worked
136143
if (this.ProjectsOrSolutionsToBuild.Count == 0)
137144
{
138-
var message = MakeDiagnostic("no-projects-or-solutions", "No project or solutions files found");
139-
message.PlaintextMessage =
140-
"CodeQL could not find any project or solution files in your repository. " +
141-
$"Set up a [manual build command]({buildCommandDocsUrl}).";
142-
message.Severity = DiagnosticMessage.TspSeverity.Error;
143-
144-
AddDiagnostic(message);
145+
this.AddDiagnostic(new(
146+
this.Options.Language,
147+
"no-projects-or-solutions",
148+
"No project or solutions files found",
149+
markdownMessage:
150+
"CodeQL could not find any project or solution files in your repository. " +
151+
$"Set up a [manual build command]({buildCommandDocsUrl})."
152+
));
145153
}
146154
// show a warning if there are projects which are not compatible with .NET Core, in case that is unintentional
147155
else if (foundNotDotNetProjects.Any())
148156
{
149-
var message = MakeDiagnostic("dotnet-incompatible-projects", "Some projects are incompatible with .NET Core");
150-
message.MarkdownMessage =
151-
"CodeQL found some projects which cannot be built with .NET Core:\n" +
152-
autoBuildRule.DotNetRule.NotDotNetProjects.Select(p => this.MakeRelative(p.FullPath)).ToMarkdownList(MarkdownUtil.CodeFormatter, 5);
153-
message.Severity = DiagnosticMessage.TspSeverity.Warning;
154-
155-
AddDiagnostic(message);
157+
this.AddDiagnostic(new(
158+
this.Options.Language,
159+
"dotnet-incompatible-projects",
160+
"Some projects are incompatible with .NET Core",
161+
severity: DiagnosticMessage.TspSeverity.Warning,
162+
markdownMessage: $"""
163+
CodeQL found some projects which cannot be built with .NET Core:
164+
165+
{autoBuildRule.DotNetRule.NotDotNetProjects.Select(p => this.MakeRelative(p.FullPath)).ToMarkdownList(MarkdownUtil.CodeFormatter, 5)}
166+
"""
167+
));
156168
}
157169

158170
// report any projects that failed to build with .NET Core
159171
if (autoBuildRule.DotNetRule.FailedProjectsOrSolutions.Any())
160172
{
161-
var message = MakeDiagnostic("dotnet-build-failure", "Some projects or solutions failed to build using .NET Core");
162-
message.MarkdownMessage =
163-
"CodeQL was unable to build the following projects using .NET Core:\n" +
164-
autoBuildRule.DotNetRule.FailedProjectsOrSolutions.Select(p => this.MakeRelative(p.FullPath)).ToMarkdownList(MarkdownUtil.CodeFormatter, 10) +
165-
$"\nSet up a [manual build command]({buildCommandDocsUrl}).";
166-
message.Severity = DiagnosticMessage.TspSeverity.Error;
167-
168-
AddDiagnostic(message);
173+
this.AddDiagnostic(new(
174+
this.Options.Language,
175+
"dotnet-build-failure",
176+
"Some projects or solutions failed to build using .NET Core",
177+
markdownMessage: $"""
178+
CodeQL was unable to build the following projects using .NET Core:
179+
180+
{autoBuildRule.DotNetRule.FailedProjectsOrSolutions.Select(p => this.MakeRelative(p.FullPath)).ToMarkdownList(MarkdownUtil.CodeFormatter, 10)}
181+
182+
Set up a [manual build command]({buildCommandDocsUrl}).
183+
"""
184+
));
169185
}
170186

171187
// report any projects that failed to build with MSBuild
172188
if (autoBuildRule.MsBuildRule.FailedProjectsOrSolutions.Any())
173189
{
174-
var message = MakeDiagnostic("msbuild-build-failure", "Some projects or solutions failed to build using MSBuild");
175-
message.MarkdownMessage =
176-
"CodeQL was unable to build the following projects using MSBuild:\n" +
177-
autoBuildRule.MsBuildRule.FailedProjectsOrSolutions.Select(p => this.MakeRelative(p.FullPath)).ToMarkdownList(MarkdownUtil.CodeFormatter, 10) +
178-
$"\nSet up a [manual build command]({buildCommandDocsUrl}).";
179-
message.Severity = DiagnosticMessage.TspSeverity.Error;
180-
181-
AddDiagnostic(message);
190+
this.AddDiagnostic(new(
191+
this.Options.Language,
192+
"msbuild-build-failure",
193+
"Some projects or solutions failed to build using MSBuild",
194+
markdownMessage: $"""
195+
CodeQL was unable to build the following projects using MSBuild:
196+
197+
{autoBuildRule.MsBuildRule.FailedProjectsOrSolutions.Select(p => this.MakeRelative(p.FullPath)).ToMarkdownList(MarkdownUtil.CodeFormatter, 10)}
198+
199+
Set up a [manual build command]({buildCommandDocsUrl}).
200+
"""
201+
));
182202
}
183203
}
184204

csharp/autobuilder/Semmle.Autobuild.CSharp/CSharpDiagnosticClassifier.cs

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,13 @@ public Result(string sdkName)
2626
this.SDKName = sdkName;
2727
}
2828

29-
public DiagnosticMessage ToDiagnosticMessage<T>(Autobuilder<T> builder) where T : AutobuildOptionsShared
30-
{
31-
var diag = builder.MakeDiagnostic(
32-
$"missing-xamarin-{this.SDKName.ToLower()}-sdk",
33-
$"Missing Xamarin SDK for {this.SDKName}"
34-
);
35-
diag.MarkdownMessage = $"[Configure your workflow]({docsUrl}) for this SDK before running CodeQL.";
36-
37-
return diag;
38-
}
29+
public DiagnosticMessage ToDiagnosticMessage<T>(Autobuilder<T> builder, DiagnosticMessage.TspSeverity? severity = null) where T : AutobuildOptionsShared => new(
30+
builder.Options.Language,
31+
$"missing-xamarin-{this.SDKName.ToLower()}-sdk",
32+
$"Missing Xamarin SDK for {this.SDKName}",
33+
severity: severity ?? DiagnosticMessage.TspSeverity.Error,
34+
markdownMessage: $"[Configure your workflow]({docsUrl}) for this SDK before running CodeQL."
35+
);
3936
}
4037

4138
public MissingXamarinSdkRule() :
@@ -74,24 +71,23 @@ public Result()
7471
this.MissingProjectFiles = new HashSet<string>();
7572
}
7673

77-
public DiagnosticMessage ToDiagnosticMessage<T>(Autobuilder<T> builder) where T : AutobuildOptionsShared
78-
{
79-
var diag = builder.MakeDiagnostic(
80-
$"missing-project-files",
81-
$"Missing project files"
82-
);
83-
diag.MarkdownMessage =
84-
"Some project files were not found when CodeQL built your project:\n\n" +
85-
this.MissingProjectFiles.AsEnumerable().Select(p => builder.MakeRelative(p)).ToMarkdownList(MarkdownUtil.CodeFormatter, 5) +
86-
"\n\nThis may lead to subsequent failures. " +
87-
"You can check for common causes for missing project files:\n\n" +
88-
$"- Ensure that the project is built using the {runsOnDocsUrl.ToMarkdownLink("intended operating system")} and that filenames on case-sensitive platforms are correctly specified.\n" +
89-
$"- If your repository uses Git submodules, ensure that those are {checkoutDocsUrl.ToMarkdownLink("checked out")} before the CodeQL action is run.\n" +
90-
"- If you auto-generate some project files as part of your build process, ensure that these are generated before the CodeQL action is run.";
91-
diag.Severity = DiagnosticMessage.TspSeverity.Warning;
92-
93-
return diag;
94-
}
74+
public DiagnosticMessage ToDiagnosticMessage<T>(Autobuilder<T> builder, DiagnosticMessage.TspSeverity? severity = null) where T : AutobuildOptionsShared => new(
75+
builder.Options.Language,
76+
"missing-project-files",
77+
"Missing project files",
78+
severity: severity ?? DiagnosticMessage.TspSeverity.Warning,
79+
markdownMessage: $"""
80+
Some project files were not found when CodeQL built your project:
81+
82+
{this.MissingProjectFiles.AsEnumerable().Select(p => builder.MakeRelative(p)).ToMarkdownList(MarkdownUtil.CodeFormatter, 5)}
83+
84+
This may lead to subsequent failures. You can check for common causes for missing project files:
85+
86+
- Ensure that the project is built using the {runsOnDocsUrl.ToMarkdownLink("intended operating system")} and that filenames on case-sensitive platforms are correctly specified.
87+
- If your repository uses Git submodules, ensure that those are {checkoutDocsUrl.ToMarkdownLink("checked out")} before the CodeQL action is run.
88+
- If you auto-generate some project files as part of your build process, ensure that these are generated before the CodeQL action is run.
89+
"""
90+
);
9591
}
9692

9793
public MissingProjectFileRule() :

csharp/autobuilder/Semmle.Autobuild.Shared/Autobuilder.cs

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -330,11 +330,9 @@ void exitCallback(int ret, string msg, bool silent)
330330
// if the build succeeded, all diagnostics we captured from the build output should be warnings;
331331
// otherwise they should all be errors
332332
var diagSeverity = buildResult == 0 ? DiagnosticMessage.TspSeverity.Warning : DiagnosticMessage.TspSeverity.Error;
333-
this.DiagnosticClassifier.Results.Select(result => result.ToDiagnosticMessage(this)).ForEach(result =>
334-
{
335-
result.Severity = diagSeverity;
336-
AddDiagnostic(result);
337-
});
333+
this.DiagnosticClassifier.Results
334+
.Select(result => result.ToDiagnosticMessage(this, diagSeverity))
335+
.ForEach(AddDiagnostic);
338336

339337
return buildResult;
340338
}
@@ -344,42 +342,23 @@ void exitCallback(int ret, string msg, bool silent)
344342
/// </summary>
345343
public abstract BuildScript GetBuildScript();
346344

347-
/// <summary>
348-
/// Constructs a standard <see cref="DiagnosticMessage" /> for some message with
349-
/// <see cref="id" /> and a human-friendly <see cref="name" />.
350-
/// </summary>
351-
/// <param name="id">The last part of the message id.</param>
352-
/// <param name="name">The human-friendly description of the message.</param>
353-
/// <returns>The resulting <see cref="DiagnosticMessage" />.</returns>
354-
public DiagnosticMessage MakeDiagnostic(string id, string name)
355-
{
356-
DiagnosticMessage diag = new(new(
357-
$"{this.Options.Language.UpperCaseName.ToLower()}/autobuilder/{id}",
358-
name,
359-
Options.Language.UpperCaseName.ToLower()
360-
));
361-
diag.Visibility.StatusPage = true;
362-
363-
return diag;
364-
}
365-
366345

367346
/// <summary>
368347
/// Produces a diagnostic for the tool status page that we were unable to automatically
369348
/// build the user's project and that they can manually specify a build command. This
370349
/// can be overriden to implement more specific messages depending on the origin of
371350
/// the failure.
372351
/// </summary>
373-
protected virtual void AutobuildFailureDiagnostic()
374-
{
375-
var message = MakeDiagnostic("autobuild-failure", "Unable to build project");
376-
message.PlaintextMessage =
377-
"We were unable to automatically build your project. " +
378-
"You can manually specify a suitable build command for your project.";
379-
message.Severity = DiagnosticMessage.TspSeverity.Error;
380-
381-
AddDiagnostic(message);
382-
}
352+
protected virtual void AutobuildFailureDiagnostic() => AddDiagnostic(new DiagnosticMessage(
353+
this.Options.Language,
354+
"autobuild-failure",
355+
"Unable to build project",
356+
visibility: new DiagnosticMessage.TspVisibility(statusPage: true),
357+
plaintextMessage: """
358+
We were unable to automatically build your project.
359+
Set up a manual build command.
360+
"""
361+
));
383362

384363
/// <summary>
385364
/// Returns a build script that can be run upon autobuild failure.

csharp/autobuilder/Semmle.Autobuild.Shared/DiagnosticClassifier.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ public interface IDiagnosticsResult
1616
/// <param name="builder">
1717
/// The autobuilder to use for constructing the base <see cref="DiagnosticMessage" />.
1818
/// </param>
19+
/// <param name="severity">
20+
/// An optional severity value which overrides the default severity of the diagnostic.
21+
/// </param>
1922
/// <returns>The <see cref="DiagnosticMessage" /> corresponding to this result.</returns>
20-
DiagnosticMessage ToDiagnosticMessage<T>(Autobuilder<T> builder) where T : AutobuildOptionsShared;
23+
DiagnosticMessage ToDiagnosticMessage<T>(Autobuilder<T> builder, DiagnosticMessage.TspSeverity? severity = null) where T : AutobuildOptionsShared;
2124
}
2225

2326
public class DiagnosticRule

0 commit comments

Comments
 (0)