-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from Black-Cockpit/integrating_static_code_ana…
…lysis 👮 Integrated sonar cloud
- Loading branch information
Showing
9 changed files
with
273 additions
and
46 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
name: Build test and analyze | ||
on: | ||
push: | ||
branches: | ||
- master | ||
pull_request: | ||
branches: | ||
- master | ||
types: | ||
- opened | ||
- reopened | ||
- edited | ||
- synchronize | ||
|
||
jobs: | ||
# Build test and analyze source code | ||
build_test_analyze: | ||
runs-on: ubuntu-22.04 | ||
strategy: | ||
matrix: | ||
java-version: [ 21 ] | ||
steps: | ||
- name: Checkout Repository | ||
uses: actions/checkout@v4 | ||
|
||
# Setup OpenJDK | ||
- name: Setup OpenJDK | ||
uses: actions/setup-java@v3 | ||
with: | ||
distribution: 'adopt' | ||
java-version: ${{ matrix.java-version }} | ||
|
||
# Install all required .NET SDK versions | ||
- name: Install .NET SDKs (6.0, 7.0, 8.0) | ||
uses: actions/setup-dotnet@v1 | ||
with: | ||
dotnet-version: | | ||
6.0.x | ||
7.0.x | ||
8.0.x | ||
# Install dependencies | ||
- name: Install dependencies | ||
run: | | ||
sudo apt install -y make python3-pip python3-rpm python3-psycopg2 | ||
pip install 'python-keycloak==3.3.0' --user | ||
dotnet tool install --global dotnet-sonarscanner | ||
dotnet tool install --global Cake.Tool | ||
dotnet tool install --global JetBrains.dotCover.GlobalTool | ||
# Build test and analyze the project | ||
- name: Build test and analyze the project | ||
if: success() | ||
env: | ||
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} | ||
run: | | ||
# Copy Licence | ||
cp LICENSE NETCore.Keycloak.Client/ | ||
# Build, test and analyze project with keycloak version 20 | ||
cd NETCore.Keycloak.Client.Tests | ||
dotnet cake build_test_analyse.cake --kc_major_version=20 --sonar_token=${SONAR_TOKEN} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/// <summary> | ||
/// Main Cake build script to manage the build, restore, test, and setup of the Keycloak testing environment. | ||
/// Includes and calls tasks from external scripts. | ||
/// </summary> | ||
|
||
// Load external task scripts | ||
#load "cakeScripts/check_tools.cake"; | ||
#load "cakeScripts/setup_keycloak_test_environment.cake"; | ||
#load "cakeScripts/sonar_analysis.cake"; | ||
#load "../build.cake"; | ||
|
||
// Update the solution context | ||
slnContext = ".."; | ||
|
||
/// <summary> | ||
/// Executes unit tests using JetBrains dotCover for code coverage analysis. | ||
/// Runs the tests without rebuilding the project. | ||
/// </summary> | ||
Task("Test") | ||
.IsDependentOn("Build") | ||
.Does(() => | ||
{ | ||
Information("Running tests with dotCover..."); | ||
|
||
// Ensure dotnet is installed | ||
var dotnetPath = Context.Tools.Resolve("dotnet"); | ||
if (dotnetPath == null) | ||
{ | ||
Error("dotnet is not installed or cannot be found."); | ||
Environment.Exit(255); | ||
} | ||
|
||
// Define the test command | ||
var testCommand = $"dotcover test {slnContext}/NETCore.Keycloak.sln --configuration {configuration} -l:\"console;verbosity=normal\" --no-restore --no-build --dcReportType=HTML"; | ||
|
||
// Configure dotCover settings | ||
var processSettings = new ProcessSettings | ||
{ | ||
Arguments = new ProcessArgumentBuilder() | ||
.Append(testCommand), | ||
RedirectStandardOutput = true, | ||
RedirectStandardError = true | ||
}; | ||
|
||
// Run tests with dotCover | ||
var result = StartProcess(dotnetPath, processSettings, out var output, out var error); | ||
|
||
// Evaluate the result of the process execution | ||
if (result != 0) | ||
{ | ||
Error("Unit tests failed with dotCover. Error:\n{0}", string.Join(Environment.NewLine, error)); | ||
Environment.Exit(255); | ||
} | ||
|
||
Information("Unit tests executed successfully with dotCover."); | ||
}); | ||
|
||
|
||
/// <summary> | ||
/// The BuildTestAnalyse task orchestrates the setup, testing, and analysis of the Keycloak client. | ||
/// It ensures that the environment is correctly configured, executes end-to-end tests, | ||
/// and performs SonarQube analysis while validating required parameters. | ||
/// </summary> | ||
Task("BuildTestAnalyse") | ||
.Does(() => | ||
{ | ||
Information("Executing Keycloak client build, test, and analysis..."); | ||
|
||
// Generate a list of supported Keycloak versions from 20 to 26 | ||
var versions = Enumerable.Range(20, 26 - 20 + 1).ToList(); | ||
|
||
// Get the major version argument if provided | ||
var kcMajorVersion = Argument<int?>("kc_major_version", null); | ||
|
||
// Validate the provided version | ||
if (!kcMajorVersion.HasValue || !versions.Contains(kcMajorVersion.Value)) | ||
{ | ||
Error($"Invalid Keycloak version: {kcMajorVersion}. Supported versions: {string.Join(", ", versions)}"); | ||
Environment.Exit(255); | ||
} | ||
|
||
// Retrieve the Sonar token from the arguments | ||
var sonarToken = Argument<string>("sonar_token", null); | ||
|
||
if (string.IsNullOrWhiteSpace(sonarToken)) | ||
{ | ||
// Ensure the Sonar token is provided before proceeding | ||
Error("Sonar token is required."); | ||
Environment.Exit(255); | ||
} | ||
|
||
// Log the processing of the specific Keycloak version | ||
Information($"Processing Keycloak Version: {kcMajorVersion.Value}"); | ||
|
||
// Set environment variables for Keycloak and SonarQube | ||
Environment.SetEnvironmentVariable("KC_TEST_VERSION", $"prepare_keycloak_{kcMajorVersion.Value}_environment"); | ||
Environment.SetEnvironmentVariable("KC_SONAR_TOKEN", sonarToken); | ||
|
||
// Execute the required setup, testing, and analysis tasks | ||
RunTarget("Setup-Testing-Environment"); | ||
RunTarget("SonarBegin"); | ||
RunTarget("Test"); | ||
RunTarget("SonarEnd"); | ||
}); | ||
|
||
// Execute the BuildTestAnalyse task | ||
RunTarget("BuildTestAnalyse"); |
85 changes: 85 additions & 0 deletions
85
NETCore.Keycloak.Client.Tests/cakeScripts/sonar_analysis.cake
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/// <summary> | ||
/// Initiates SonarCloud analysis by executing the `dotnet sonarscanner begin` command. | ||
/// This task resolves the `dotnet` executable, ensures the required Sonar token is provided, | ||
/// and configures the necessary SonarCloud parameters for project analysis. | ||
/// </summary> | ||
Task("SonarBegin") | ||
.Does(() => | ||
{ | ||
// Retrieve the Sonar token from the environment variable | ||
var sonarToken = Environment.GetEnvironmentVariable("KC_SONAR_TOKEN") ?? ""; | ||
|
||
if (string.IsNullOrWhiteSpace(sonarToken)) | ||
{ | ||
// Ensure the token is provided before proceeding | ||
Error("Sonar token is required."); | ||
Environment.Exit(255); | ||
} | ||
|
||
// Resolve the path to the `dotnet` executable | ||
FilePath dotnetPath = Context.Tools.Resolve("dotnet"); | ||
|
||
// Configure process settings for executing the SonarScanner command | ||
var processSettings = new ProcessSettings | ||
{ | ||
Arguments = new ProcessArgumentBuilder() | ||
.Append("sonarscanner begin") | ||
.Append($"/d:sonar.token={sonarToken}") | ||
.Append("/k:Black-Cockpit_NETCore.Keycloak") | ||
.Append("/o:black-cockpit") | ||
.Append("/d:sonar.host.url=\"https://sonarcloud.io\"") | ||
.Append("/d:sonar.coverage.exclusions=\"**/NETCore.Keycloak.Client.Tests/**/*.*\"") | ||
.Append("/d:sonar.test.exclusions=\"**/NETCore.Keycloak.Client.Tests/**/*.*\"") | ||
.Append("/d:sonar.exclusions=\"**/NETCore.Keycloak.Client.Tests/**/*.*\"") | ||
.Append("/d:sonar.cs.dotcover.reportsPaths=dotCover.Output.html"), | ||
RedirectStandardOutput = true, | ||
RedirectStandardError = true | ||
}; | ||
|
||
// Execute the SonarScanner command | ||
StartProcess(dotnetPath, processSettings); | ||
}); | ||
|
||
/// <summary> | ||
/// Finalizes the SonarCloud analysis by executing the `dotnet sonarscanner end` command. | ||
/// This task ensures the required Sonar token is provided, resolves the `dotnet` executable, | ||
/// and captures the process output and error streams for evaluation. | ||
/// </summary> | ||
Task("SonarEnd") | ||
.IsDependentOn("Build") | ||
.Does(() => | ||
{ | ||
// Retrieve the Sonar token from the environment variable | ||
var sonarToken = Environment.GetEnvironmentVariable("KC_SONAR_TOKEN") ?? ""; | ||
|
||
if (string.IsNullOrWhiteSpace(sonarToken)) | ||
{ | ||
// Ensure the token is provided before proceeding | ||
Error("Sonar token is required."); | ||
Environment.Exit(255); | ||
} | ||
|
||
// Resolve the path to the `dotnet` executable | ||
FilePath dotnetPath = Context.Tools.Resolve("dotnet"); | ||
|
||
// Configure process settings for executing the SonarScanner command | ||
var processSettings = new ProcessSettings | ||
{ | ||
Arguments = new ProcessArgumentBuilder() | ||
.Append("sonarscanner end") | ||
.Append($"/d:sonar.token={sonarToken}"), | ||
RedirectStandardOutput = true, | ||
RedirectStandardError = true | ||
}; | ||
|
||
// Execute the SonarScanner command and capture output/error streams | ||
var result = StartProcess(dotnetPath, processSettings, out var output, out var error); | ||
|
||
// Evaluate the result of the process execution | ||
if (result != 0) | ||
{ | ||
// Log the error message and exit if the command failed | ||
Error("Sonar analysis finalization failed. Error:\n{0}", string.Join(Environment.NewLine, error)); | ||
Environment.Exit(255); | ||
} | ||
}); |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters