Skip to content

Commit

Permalink
Fail the build whenever SonarQube fails
Browse files Browse the repository at this point in the history
  • Loading branch information
GeertvanHorrik committed Jul 10, 2018
1 parent b5e6a8b commit 5483341
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -133,3 +133,4 @@ TestResults
.github/
BundleArtifacts/

.vscode
3 changes: 2 additions & 1 deletion deployment/cake/apps-uwp-tasks.cake
Expand Up @@ -123,8 +123,9 @@ private void BuildUwpApps()

Information("Building project for platform {0}, artifacts directory is '{1}'", platform.Key, artifactsDirectory);

var projectFileName = GetProjectFileName(uwpApp);

// Note: if csproj doesn't work, use SolutionFileName instead
var projectFileName = string.Format("./src/{0}/{0}.csproj", uwpApp);
//var projectFileName = SolutionFileName;
MSBuild(projectFileName, msBuildSettings);

Expand Down
2 changes: 1 addition & 1 deletion deployment/cake/apps-wpf-tasks.cake
Expand Up @@ -37,7 +37,7 @@ private void BuildWpfApps()
{
Information("Building WPF app '{0}'", wpfApp);

var projectFileName = string.Format("./src/{0}/{0}.csproj", wpfApp);
var projectFileName = GetProjectFileName(wpfApp);

var msBuildSettings = new MSBuildSettings {
Verbosity = Verbosity.Quiet, // Verbosity.Diagnostic
Expand Down
4 changes: 2 additions & 2 deletions deployment/cake/components-tasks.cake
Expand Up @@ -22,7 +22,7 @@ private void UpdateInfoForComponents()
{
Information("Updating version for component '{0}'", component);

var projectFileName = string.Format("./src/{0}/{0}.csproj", component);
var projectFileName = GetProjectFileName(component);

TransformConfig(projectFileName, new TransformationCollection
{
Expand All @@ -44,7 +44,7 @@ private void BuildComponents()
{
Information("Building component '{0}'", component);

var projectFileName = string.Format("./src/{0}/{0}.csproj", component);
var projectFileName = GetProjectFileName(component);

var msBuildSettings = new MSBuildSettings {
Verbosity = Verbosity.Quiet, // Verbosity.Diagnostic
Expand Down
16 changes: 16 additions & 0 deletions deployment/cake/generic-tasks.cake
Expand Up @@ -25,6 +25,22 @@ private void UpdateSolutionAssemblyInfo()

//-------------------------------------------------------------

private string GetProjectDirectory(string projectName)
{
var projectDirectory = string.Format("./src/{0}/", projectName);
return projectDirectory;
}

//-------------------------------------------------------------

private string GetProjectFileName(string projectName)
{
var fileName = string.Format("{0}{1}.csproj", GetProjectDirectory(projectName), projectName);
return fileName;
}

//-------------------------------------------------------------

Task("UpdateNuGet")
.ContinueOnError()
.Does(() =>
Expand Down
71 changes: 68 additions & 3 deletions deployment/cake/tasks.cake
Expand Up @@ -3,6 +3,8 @@
#l "apps-wpf-tasks.cake"
#l "components-tasks.cake"

#addin "nuget:?package=System.Net.Http&version=4.3.3"
#addin "nuget:?package=Newtonsoft.Json&version=11.0.2"
#addin "nuget:?package=Cake.Sonar&version=1.1.0"

#tool "nuget:?package=MSBuild.SonarQube.Runner.Tool&version=4.3.0"
Expand All @@ -20,7 +22,7 @@ private void BuildTestProjects()
{
Information("Building test project '{0}'", testProject);

var projectFileName = string.Format("./src/{0}/{0}.csproj", testProject);
var projectFileName = GetProjectFileName(testProject);

var msBuildSettings = new MSBuildSettings
{
Expand Down Expand Up @@ -63,7 +65,7 @@ Task("UpdateInfo")
Task("Build")
.IsDependentOn("Clean")
.IsDependentOn("UpdateInfo")
.Does(() =>
.Does(async () =>
{
var enableSonar = !string.IsNullOrWhiteSpace(SonarUrl);
if (enableSonar)
Expand Down Expand Up @@ -102,6 +104,69 @@ Task("Build")
Login = SonarUsername,
Password = SonarPassword,
});
Information("Checking whether the project passed the SonarQube gateway...");
var status = "none";
// We need to use /api/qualitygates/project_status
var client = new System.Net.Http.HttpClient();
using (client)
{
var queryUri = string.Format("{0}/api/qualitygates/project_status?projectKey={1}", SonarUrl, SonarProject);
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12 | System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls;
var byteArray = Encoding.ASCII.GetBytes(string.Format("{0}:{1}", SonarUsername, SonarPassword));
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
Debug("Invoking GET request: '{0}'", queryUri);
var response = await client.GetAsync(new Uri(queryUri));
Debug("Parsing request contents");
var content = response.Content;
var jsonContent = await content.ReadAsStringAsync();
Debug(jsonContent);
dynamic result = Newtonsoft.Json.Linq.JObject.Parse(jsonContent);
status = result.projectStatus.status;
}
Information("SonarQube gateway status returned from request: '{0}'", status);
if (string.IsNullOrWhiteSpace(status))
{
status = "none";
}
status = status.ToLower();
switch (status)
{
case "error":
Error("The SonarQube gateway for '{0}' returned ERROR, please check the error(s) at {1}/dashboard?id={0}", SonarProject, SonarUrl);
break;
case "warn":
Warning("The SonarQube gateway for '{0}' returned WARNING, please check the warning(s) at {1}/dashboard?id={0}", SonarProject, SonarUrl);
break;
case "none":
Warning("The SonarQube gateway for '{0}' returned NONE, please check why no gateway status is available at {1}/dashboard?id={0}", SonarProject, SonarUrl);
break;
case "ok":
Information("The SonarQube gateway for '{0}' returned OK, well done! If you want to show off the results, check out {1}/dashboard?id={0}", SonarProject, SonarUrl);
break;
default:
Error("Unexpected SonarQube gateway status '{0}' for project '{1}'", status, SonarProject);
break;
}
}
BuildTestProjects();
Expand Down Expand Up @@ -135,7 +200,7 @@ Task("BuildAndPackage")
//-------------------------------------------------------------

Task("Default")
.IsDependentOn("Build");
.IsDependentOn("BuildAndPackage");

//-------------------------------------------------------------

Expand Down
3 changes: 2 additions & 1 deletion tools/packages.config
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Cake" version="0.26.1" />
<package id="Cake" version="0.29.0" />
<package id="Cake.CoreCLR" version="0.29.0" />
</packages>

0 comments on commit 5483341

Please sign in to comment.