Skip to content

Commit

Permalink
Add SARIF report (#332)
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalberger authored Aug 16, 2023
1 parent 99b4a66 commit e1ac282
Show file tree
Hide file tree
Showing 20 changed files with 160 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ public override void PublishIssuesArtifacts(IIssuesContext context)
{
context.AppVeyor().UploadArtifact(context.State.FullIssuesReport);
}

if (context.Parameters.BuildServer.ShouldPublishSarifReport &&
context.State.SarifReport != null &&
context.FileExists(context.State.SarifReport))
{
context.AppVeyor().UploadArtifact(context.State.SarifReport);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ public override void PublishIssuesArtifacts(IIssuesContext context)
{
context.AzurePipelines().Commands.UploadArtifact("Issues", context.State.FullIssuesReport, "Issues");
}

if (context.Parameters.BuildServer.ShouldPublishSarifReport &&
context.State.SarifReport != null &&
context.FileExists(context.State.SarifReport))
{
context.AzurePipelines().Commands.UploadArtifact("Issues", context.State.SarifReport, "CodeAnalysisLogs");
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ For recipe compatible with Cake Script Runners see Cake.Issues.Recipe.</Descript
<PackageReference Include="Cake.Issues.PullRequests.AzureDevOps" Version="3.0.0" />
<PackageReference Include="Cake.Issues.PullRequests.GitHubActions" Version="3.0.0" />
<PackageReference Include="Cake.Issues.Reporting" Version="3.0.0" />
<PackageReference Include="Cake.Issues.Reporting.Sarif" Version="3.0.1" />
<PackageReference Include="Cake.Frosting.Issues.Reporting.Generic" Version="3.0.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ public interface IIssuesParametersBuildServer
/// </summary>
bool ShouldPublishFullIssuesReport { get; set; }

/// <summary>
/// Gets or sets a value indicating whether report in SARIF format should be published as artifact to the build system.
/// Default value is <c>true</c>.
/// </summary>
bool ShouldPublishSarifReport { get; set; }

/// <summary>
/// Gets or sets a value indicating whether summary issues report should be created.
/// Default value is <c>true</c>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,11 @@ public interface IIssuesParametersReporting
/// with <see cref="DevExtremeTheme.MaterialBlueLight"/> theme.
/// </summary>
GenericIssueReportFormatSettings FullIssuesReportSettings { get; set; }

/// <summary>
/// Gets or sets a value indicating whether report in SARIF format should be created.
/// Default value is <c>true</c>.
/// </summary>
bool ShouldCreateSarifReport { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ public class IssuesParametersBuildServer : IIssuesParametersBuildServer
/// <inheritdoc />
public bool ShouldPublishFullIssuesReport { get; set; } = true;

/// <inheritdoc />
public bool ShouldPublishSarifReport { get; set; } = true;

/// <inheritdoc />
public bool ShouldCreateSummaryIssuesReport { get; set; } = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@ public class IssuesParametersReporting : IIssuesParametersReporting
GenericIssueReportFormatSettings
.FromEmbeddedTemplate(GenericIssueReportTemplate.HtmlDxDataGrid)
.WithOption(HtmlDxDataGridOption.Theme, DevExtremeTheme.MaterialBlueLight);

/// <inheritdoc />
public bool ShouldCreateSarifReport { get; set; } = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public interface IIssuesState
/// </summary>
FilePath FullIssuesReport { get; set; }

/// <summary>
/// Gets or sets the path to the SARIF report.
/// </summary>
FilePath SarifReport { get; set; }

/// <summary>
/// Gets or sets the path to the summary issues report.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public class IssuesState : IIssuesState
/// <inheritdoc />
public FilePath FullIssuesReport { get; set; }

/// <inheritdoc />
public FilePath SarifReport { get; set; }

/// <inheritdoc />
public FilePath SummaryIssuesReport { get; set; }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Cake.Common.IO;
using Cake.Issues;
using Cake.Issues.Reporting;
using Cake.Issues.Reporting.Sarif;

namespace Cake.Frosting.Issues.Recipe
{
/// <summary>
/// Creates issue report in SARIF format.
/// </summary>
[TaskName("Create-SarifReport")]
[IsDependentOn(typeof(ReadIssuesTask))]
public sealed class CreateSarifReportTask : FrostingTask<IIssuesContext>
{
/// <inheritdoc/>
public override bool ShouldRun(IIssuesContext context)
{
context.NotNull(nameof(context));

return context.Parameters.Reporting.ShouldCreateSarifReport;
}

/// <inheritdoc/>
public override void Run(IIssuesContext context)
{
context.NotNull(nameof(context));

var reportFileName = "report";
if (!string.IsNullOrWhiteSpace(context.Parameters.BuildIdentifier))
{
reportFileName += $"-{context.Parameters.BuildIdentifier}";
}
reportFileName += ".sarif";

context.State.SarifReport =
context.Parameters.OutputDirectory.CombineWithFilePath(reportFileName);
context.EnsureDirectoryExists(context.Parameters.OutputDirectory);

// Create SARIF report.
context.CreateIssueReport(
context.State.Issues,
context.SarifIssueReportFormat(),
context.State.ProjectRootDirectory,
context.State.SarifReport);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Cake.Frosting.Issues.Recipe
/// </summary>
[TaskName("Publish-IssuesArtifacts")]
[IsDependentOn(typeof(CreateFullIssuesReportTask))]
[IsDependentOn(typeof(CreateSarifReportTask))]
public sealed class PublishIssuesArtifactsTask : FrostingTask<IIssuesContext>
{
/// <inheritdoc/>
Expand Down
5 changes: 5 additions & 0 deletions Cake.Issues.Recipe/Content/IssuesBuildTasksDefinitions.cake
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public class IssuesBuildTaskDefinitions
/// </summary>
public CakeTaskBuilder CreateFullIssuesReportTask { get; set; }

/// <summary>
/// Gets or sets the task for creating a report in SARIF format.
/// </summary>
public CakeTaskBuilder CreateSarifReportTask { get; set; }

/// <summary>
/// Gets or sets the task for publishing issues artifacts.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions Cake.Issues.Recipe/Content/addins.cake
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#addin nuget:?package=Cake.Issues.EsLint&version=3.0.0
#addin nuget:?package=Cake.Issues.Reporting&version=3.0.0
#addin nuget:?package=Cake.Issues.Reporting.Generic&version=3.0.0
#addin nuget:?package=Cake.Issues.Reporting.Sarif&version=3.0.1
#addin nuget:?package=Cake.Issues.PullRequests&version=3.0.0
#addin nuget:?package=Cake.Issues.PullRequests.AppVeyor&version=3.0.0
#addin nuget:?package=Cake.Issues.PullRequests.AzureDevOps&version=3.0.0
Expand Down
26 changes: 26 additions & 0 deletions Cake.Issues.Recipe/Content/build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,36 @@ IssuesBuildTasks.CreateFullIssuesReportTask = Task("Create-FullIssuesReport")
data.FullIssuesReport);
});

IssuesBuildTasks.CreateSarifReportTask = Task("Create-SarifReport")
.Description("Creates report in SARIF format.")
.WithCriteria(() => IssuesParameters.Reporting.ShouldCreateSarifReport, "Creating of report in SARIF format is disabled")
.IsDependentOn("Read-Issues")
.Does<IssuesData>((data) =>
{
var reportFileName = "report";
if (!string.IsNullOrWhiteSpace(IssuesParameters.BuildIdentifier))
{
reportFileName += $"-{IssuesParameters.BuildIdentifier}";
}
reportFileName += ".sarif";
data.SarifReport =
IssuesParameters.OutputDirectory.CombineWithFilePath(reportFileName);
EnsureDirectoryExists(IssuesParameters.OutputDirectory);
// Create SARIF report.
CreateIssueReport(
data.Issues,
SarifIssueReportFormat(),
data.ProjectRootDirectory,
data.SarifReport);
});

IssuesBuildTasks.PublishIssuesArtifactsTask = Task("Publish-IssuesArtifacts")
.Description("Publish issue artifacts to build server.")
.WithCriteria(() => !BuildSystem.IsLocalBuild, "Not running on build server")
.IsDependentOn("Create-FullIssuesReport")
.IsDependentOn("Create-SarifReport")
.Does<IssuesData>((data) =>
{
if (data.BuildServer == null)
Expand Down
5 changes: 5 additions & 0 deletions Cake.Issues.Recipe/Content/data/IssuesData.cake
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public class IssuesData
/// </summary>
public FilePath FullIssuesReport { get; set; }

/// <summary>
/// Gets or sets the path to the SARIF report.
/// </summary>
public FilePath SarifReport { get; set; }

/// <summary>
/// Gets or sets the path to the summary issues report.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ public class IssuesParametersBuildServer
/// </summary>
public bool ShouldPublishFullIssuesReport { get; set; } = true;

/// <summary>
/// Gets or sets a value indicating whether report in SARIF format should be published as artifact to the build system.
/// Default value is <c>true</c>.
/// </summary>
public bool ShouldPublishSarifReport { get; set; } = true;

/// <summary>
/// Gets or sets a value indicating whether summary issues report should be created.
/// Default value is <c>true</c>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,10 @@ public class IssuesParametersReporting
GenericIssueReportFormatSettings
.FromEmbeddedTemplate(GenericIssueReportTemplate.HtmlDxDataGrid)
.WithOption(HtmlDxDataGridOption.Theme, DevExtremeTheme.MaterialBlueLight);

/// <summary>
/// Gets or sets a value indicating whether report in SARIF format should be created.
/// Default value is <c>true</c>.
/// </summary>
public bool ShouldCreateSarifReport { get; set; } = true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,12 @@ public class AppVeyorBuildServer : BaseBuildServer
{
context.AppVeyor().UploadArtifact(data.FullIssuesReport);
}

if (IssuesParameters.BuildServer.ShouldPublishSarifReport &&
data.SarifReport != null &&
context.FileExists(data.SarifReport))
{
context.AppVeyor().UploadArtifact(data.SarifReport);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,12 @@ public class AzureDevOpsBuildServer : BaseBuildServer
{
context.AzurePipelines().Commands.UploadArtifact("Issues", data.FullIssuesReport, "Issues");
}

if (IssuesParameters.BuildServer.ShouldPublishSarifReport &&
data.SarifReport != null &&
context.FileExists(data.SarifReport))
{
context.AzurePipelines().Commands.UploadArtifact("Issues", data.SarifReport, "CodeAnalysisLogs");
}
}
}
12 changes: 7 additions & 5 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,18 @@ By default [Cake.Git addin] will be used.
|-------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------|
| `IssuesParameters.Reporting.`<br/>&nbsp;&nbsp;&nbsp;&nbsp;`ShouldCreateFullIssuesReport` | `IssuesContext.Parameters.Reporting.`<br/>&nbsp;&nbsp;&nbsp;&nbsp;`ShouldCreateFullIssuesReport` | `true` | Indicates whether full issues report should be created. |
| `IssuesParameters.Reporting.`<br/>&nbsp;&nbsp;&nbsp;&nbsp;`FullIssuesReportSettings` | `IssuesContext.Parameters.Reporting.`<br/>&nbsp;&nbsp;&nbsp;&nbsp;`FullIssuesReportSettings` | `GenericIssueReportTemplate.HtmlDxDataGrid` template with `DevExtremeTheme.MaterialBlueLight` theme. | Settings for creating the full issues report. See [Template Gallery] for possible options. |
| `IssuesParameters.Reporting.`<br/>&nbsp;&nbsp;&nbsp;&nbsp;`ShouldCreateSarifReport` | `IssuesContext.Parameters.Reporting.`<br/>&nbsp;&nbsp;&nbsp;&nbsp;`ShouldCreateSarifReport` | `true` | Indicates whether a report in SARIF format should be created. |

[Template Gallery]: /docs/report-formats/generic/templates/

# Build server integration

| Cake.Issues.Recipe Property | Cake.Frosting.Issues.Recipe Property | Default Value | Description |
|-----------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------|---------------|-------------------------------------------------------------------------------------------|
| `IssuesParameters.BuildServer.`<br/>&nbsp;&nbsp;&nbsp;&nbsp;`ShouldReportIssuesToBuildServer` | `IssuesContext.Parameters.BuildServer.`<br/>&nbsp;&nbsp;&nbsp;&nbsp;`ShouldReportIssuesToBuildServer` | `true` | Indicates whether issues should be reported to the build server. |
| `IssuesParameters.BuildServer.`<br/>&nbsp;&nbsp;&nbsp;&nbsp;`ShouldPublishFullIssuesReport` | `IssuesContext.Parameters.BuildServer.`<br/>&nbsp;&nbsp;&nbsp;&nbsp;`ShouldPublishFullIssuesReport` | `true` | Indicates whether full issues report should be published as artifact to the build system. |
| `IssuesParameters.BuildServer.`<br/>&nbsp;&nbsp;&nbsp;&nbsp;`ShouldCreateSummaryIssuesReport` | `IssuesContext.Parameters.BuildServer.`<br/>&nbsp;&nbsp;&nbsp;&nbsp;`ShouldCreateSummaryIssuesReport` | `true` | Indicates whether summary issues report should be created. |
| Cake.Issues.Recipe Property | Cake.Frosting.Issues.Recipe Property | Default Value | Description |
|-----------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------|---------------|------------------------------------------------------------------------------------------------|
| `IssuesParameters.BuildServer.`<br/>&nbsp;&nbsp;&nbsp;&nbsp;`ShouldReportIssuesToBuildServer` | `IssuesContext.Parameters.BuildServer.`<br/>&nbsp;&nbsp;&nbsp;&nbsp;`ShouldReportIssuesToBuildServer` | `true` | Indicates whether issues should be reported to the build server. |
| `IssuesParameters.BuildServer.`<br/>&nbsp;&nbsp;&nbsp;&nbsp;`ShouldPublishFullIssuesReport` | `IssuesContext.Parameters.BuildServer.`<br/>&nbsp;&nbsp;&nbsp;&nbsp;`ShouldPublishFullIssuesReport` | `true` | Indicates whether full issues report should be published as artifact to the build system. |
| `IssuesParameters.BuildServer.`<br/>&nbsp;&nbsp;&nbsp;&nbsp;`ShouldPublishSarifReport` | `IssuesContext.Parameters.BuildServer.`<br/>&nbsp;&nbsp;&nbsp;&nbsp;`ShouldPublishSarifReport` | `true` | Indicates whether report int SARIF format shoudl be published as artifact to the build system. |
| `IssuesParameters.BuildServer.`<br/>&nbsp;&nbsp;&nbsp;&nbsp;`ShouldCreateSummaryIssuesReport` | `IssuesContext.Parameters.BuildServer.`<br/>&nbsp;&nbsp;&nbsp;&nbsp;`ShouldCreateSummaryIssuesReport` | `true` | Indicates whether summary issues report should be created. |

# Pull request integration

Expand Down

0 comments on commit e1ac282

Please sign in to comment.