Set DiagnosticMessage defaults

Refactor `GetDiagnosticSource` into `MakeDiagnostic`
which sets the defaults.
This commit is contained in:
Michael B. Gale
2023-02-15 12:12:32 +00:00
parent 28b350ee95
commit 802e2319b5
2 changed files with 35 additions and 35 deletions

View File

@@ -134,41 +134,36 @@ namespace Semmle.Autobuild.CSharp
// otherwise, we just report that the one script we found didn't work
if (this.buildCommandAutoRule.CandidatePaths.Count() > 1)
{
var source = GetDiagnosticSource("multiple-build-scripts", "There are multiple potential build scripts");
message = new DiagnosticMessage(source)
{
MarkdownMessage =
"CodeQL found multiple potential build scripts for your project and " +
$"attempted to run `{buildCommandAutoRule.ScriptPath}`, which failed. " +
"This may not be the right build script for your project. " +
"You can specify which build script to use by providing a suitable build command for your project."
};
message = MakeDiagnostic("multiple-build-scripts", "There are multiple potential build scripts");
message.MarkdownMessage =
"CodeQL found multiple potential build scripts for your project and " +
$"attempted to run `{buildCommandAutoRule.ScriptPath}`, which failed. " +
"This may not be the right build script for your project. " +
"You can specify which build script to use by providing a suitable build command for your project.";
}
else
{
var source = GetDiagnosticSource("script-failure", "Unable to build project using build script");
message = new DiagnosticMessage(source)
{
MarkdownMessage =
"CodeQL attempted to build your project using a script located at " +
$"`{buildCommandAutoRule.ScriptPath}`, which failed. " +
"You can manually specify a suitable build command for your project."
};
message = MakeDiagnostic("script-failure", "Unable to build project using build script");
message.MarkdownMessage =
"CodeQL attempted to build your project using a script located at " +
$"`{buildCommandAutoRule.ScriptPath}`, which failed. " +
"You can manually specify a suitable build command for your project.";
}
message.Severity = DiagnosticMessage.TspSeverity.Error;
Diagnostic(message);
}
// both dotnet and msbuild builds require project or solution files; if we haven't found any
// then neither of those rules would've worked
if (this.ProjectsOrSolutionsToBuild.Count == 0)
{
var source = GetDiagnosticSource("no-projects-or-solutions", "No project or solutions files found");
var message = new DiagnosticMessage(source)
{
PlaintextMessage = "CodeQL could not find any project or solution files in your repository.",
Severity = DiagnosticMessage.TspSeverity.Error
};
var message = MakeDiagnostic("no-projects-or-solutions", "No project or solutions files found");
message.PlaintextMessage = "CodeQL could not find any project or solution files in your repository.";
message.Severity = DiagnosticMessage.TspSeverity.Error;
Diagnostic(message);
}
Diagnostic(message);
}

View File

@@ -319,14 +319,21 @@ namespace Semmle.Autobuild.Shared
public abstract BuildScript GetBuildScript();
/// <summary>
/// Constructs a standard <see cref="DiagnosticMessage.TspSource" /> for some message
/// <see cref="id" /> with a human-friendly <see cref="name" />.
/// Constructs a standard <see cref="DiagnosticMessage" /> for some message with
/// <see cref="id" /> and a human-friendly <see cref="name" />.
/// </summary>
/// <param name="id">The last part of the message id.</param>
/// <param name="name">The human-friendly description of the message.</param>
/// <returns>The resulting <see cref="DiagnosticMessage.TspSource" />.</returns>
protected DiagnosticMessage.TspSource GetDiagnosticSource(string id, string name) =>
new($"{this.Options.Language.UpperCaseName.ToLower()}/autobuilder/{id}", name);
/// <returns>The resulting <see cref="DiagnosticMessage" />.</returns>
protected DiagnosticMessage MakeDiagnostic(string id, string name)
{
DiagnosticMessage diag = new(new($"{this.Options.Language.UpperCaseName.ToLower()}/autobuilder/{id}", name));
diag.Source.ExtractorName = Options.Language.UpperCaseName.ToLower();
diag.Visibility.StatusPage = true;
return diag;
}
/// <summary>
/// Produces a diagnostic for the tool status page that we were unable to automatically
@@ -336,13 +343,11 @@ namespace Semmle.Autobuild.Shared
/// </summary>
protected virtual void AutobuildFailureDiagnostic()
{
var message = new DiagnosticMessage(GetDiagnosticSource("autobuild-failure", "Unable to build project"))
{
PlaintextMessage =
"We were unable to automatically build your project. " +
"You can manually specify a suitable build command for your project.",
Severity = DiagnosticMessage.TspSeverity.Error
};
var message = MakeDiagnostic("autobuild-failure", "Unable to build project");
message.PlaintextMessage =
"We were unable to automatically build your project. " +
"You can manually specify a suitable build command for your project.";
message.Severity = DiagnosticMessage.TspSeverity.Error;
Diagnostic(message);
}