Skip to content

Commit 802e231

Browse files
committed
Set DiagnosticMessage defaults
Refactor `GetDiagnosticSource` into `MakeDiagnostic` which sets the defaults.
1 parent 28b350e commit 802e231

2 files changed

Lines changed: 35 additions & 35 deletions

File tree

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

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -134,41 +134,36 @@ protected override void AutobuildFailureDiagnostic()
134134
// otherwise, we just report that the one script we found didn't work
135135
if (this.buildCommandAutoRule.CandidatePaths.Count() > 1)
136136
{
137-
var source = GetDiagnosticSource("multiple-build-scripts", "There are multiple potential build scripts");
138-
message = new DiagnosticMessage(source)
139-
{
140-
MarkdownMessage =
141-
"CodeQL found multiple potential build scripts for your project and " +
142-
$"attempted to run `{buildCommandAutoRule.ScriptPath}`, which failed. " +
143-
"This may not be the right build script for your project. " +
144-
"You can specify which build script to use by providing a suitable build command for your project."
145-
};
137+
message = MakeDiagnostic("multiple-build-scripts", "There are multiple potential build scripts");
138+
message.MarkdownMessage =
139+
"CodeQL found multiple potential build scripts for your project and " +
140+
$"attempted to run `{buildCommandAutoRule.ScriptPath}`, which failed. " +
141+
"This may not be the right build script for your project. " +
142+
"You can specify which build script to use by providing a suitable build command for your project.";
146143
}
147144
else
148145
{
149-
var source = GetDiagnosticSource("script-failure", "Unable to build project using build script");
150-
message = new DiagnosticMessage(source)
151-
{
152-
MarkdownMessage =
153-
"CodeQL attempted to build your project using a script located at " +
154-
$"`{buildCommandAutoRule.ScriptPath}`, which failed. " +
155-
"You can manually specify a suitable build command for your project."
156-
};
146+
message = MakeDiagnostic("script-failure", "Unable to build project using build script");
147+
message.MarkdownMessage =
148+
"CodeQL attempted to build your project using a script located at " +
149+
$"`{buildCommandAutoRule.ScriptPath}`, which failed. " +
150+
"You can manually specify a suitable build command for your project.";
157151
}
158152

159153
message.Severity = DiagnosticMessage.TspSeverity.Error;
160154
Diagnostic(message);
161155
}
156+
162157
// both dotnet and msbuild builds require project or solution files; if we haven't found any
163158
// then neither of those rules would've worked
164159
if (this.ProjectsOrSolutionsToBuild.Count == 0)
165160
{
166-
var source = GetDiagnosticSource("no-projects-or-solutions", "No project or solutions files found");
167-
var message = new DiagnosticMessage(source)
168-
{
169-
PlaintextMessage = "CodeQL could not find any project or solution files in your repository.",
170-
Severity = DiagnosticMessage.TspSeverity.Error
171-
};
161+
var message = MakeDiagnostic("no-projects-or-solutions", "No project or solutions files found");
162+
message.PlaintextMessage = "CodeQL could not find any project or solution files in your repository.";
163+
message.Severity = DiagnosticMessage.TspSeverity.Error;
164+
165+
Diagnostic(message);
166+
}
172167

173168
Diagnostic(message);
174169
}

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -319,14 +319,21 @@ void exitCallback(int ret, string msg, bool silent)
319319
public abstract BuildScript GetBuildScript();
320320

321321
/// <summary>
322-
/// Constructs a standard <see cref="DiagnosticMessage.TspSource" /> for some message
323-
/// <see cref="id" /> with a human-friendly <see cref="name" />.
322+
/// Constructs a standard <see cref="DiagnosticMessage" /> for some message with
323+
/// <see cref="id" /> and a human-friendly <see cref="name" />.
324324
/// </summary>
325325
/// <param name="id">The last part of the message id.</param>
326326
/// <param name="name">The human-friendly description of the message.</param>
327-
/// <returns>The resulting <see cref="DiagnosticMessage.TspSource" />.</returns>
328-
protected DiagnosticMessage.TspSource GetDiagnosticSource(string id, string name) =>
329-
new($"{this.Options.Language.UpperCaseName.ToLower()}/autobuilder/{id}", name);
327+
/// <returns>The resulting <see cref="DiagnosticMessage" />.</returns>
328+
protected DiagnosticMessage MakeDiagnostic(string id, string name)
329+
{
330+
DiagnosticMessage diag = new(new($"{this.Options.Language.UpperCaseName.ToLower()}/autobuilder/{id}", name));
331+
diag.Source.ExtractorName = Options.Language.UpperCaseName.ToLower();
332+
diag.Visibility.StatusPage = true;
333+
334+
return diag;
335+
}
336+
330337

331338
/// <summary>
332339
/// Produces a diagnostic for the tool status page that we were unable to automatically
@@ -336,13 +343,11 @@ protected DiagnosticMessage.TspSource GetDiagnosticSource(string id, string name
336343
/// </summary>
337344
protected virtual void AutobuildFailureDiagnostic()
338345
{
339-
var message = new DiagnosticMessage(GetDiagnosticSource("autobuild-failure", "Unable to build project"))
340-
{
341-
PlaintextMessage =
342-
"We were unable to automatically build your project. " +
343-
"You can manually specify a suitable build command for your project.",
344-
Severity = DiagnosticMessage.TspSeverity.Error
345-
};
346+
var message = MakeDiagnostic("autobuild-failure", "Unable to build project");
347+
message.PlaintextMessage =
348+
"We were unable to automatically build your project. " +
349+
"You can manually specify a suitable build command for your project.";
350+
message.Severity = DiagnosticMessage.TspSeverity.Error;
346351

347352
Diagnostic(message);
348353
}

0 commit comments

Comments
 (0)