Fixing SonarCloud integration + fixing CodeSmells
This commit is contained in:
3
.github/workflows/build.yml
vendored
3
.github/workflows/build.yml
vendored
@@ -44,5 +44,6 @@ jobs:
|
|||||||
shell: powershell
|
shell: powershell
|
||||||
run: |
|
run: |
|
||||||
.\.sonar\scanner\dotnet-sonarscanner begin /k:"csimonapastore_BasicDotnetTemplate" /o:"csimonapastore-github" /d:sonar.token="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io"
|
.\.sonar\scanner\dotnet-sonarscanner begin /k:"csimonapastore_BasicDotnetTemplate" /o:"csimonapastore-github" /d:sonar.token="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io"
|
||||||
dotnet build
|
dotnet build --no-incremental
|
||||||
|
dotnet test --collect "Code Coverage"
|
||||||
.\.sonar\scanner\dotnet-sonarscanner end /d:sonar.token="${{ secrets.SONAR_TOKEN }}"
|
.\.sonar\scanner\dotnet-sonarscanner end /d:sonar.token="${{ secrets.SONAR_TOKEN }}"
|
||||||
|
|||||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
|||||||
**/obj
|
**/obj
|
||||||
**/bin
|
**/bin
|
||||||
**/appsettings.*.json
|
**/appsettings.*.json
|
||||||
|
**/TestResults
|
||||||
@@ -12,7 +12,7 @@ namespace BasicDotnetTemplate.MainProject.Tests;
|
|||||||
public class Program_Tests
|
public class Program_Tests
|
||||||
{
|
{
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public async Task Program_Configuration_IsValid()
|
public void Program_Configuration_IsValid()
|
||||||
{
|
{
|
||||||
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development");
|
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development");
|
||||||
|
|
||||||
@@ -36,12 +36,21 @@ public class Program_Tests
|
|||||||
|
|
||||||
if (execute != null)
|
if (execute != null)
|
||||||
{
|
{
|
||||||
object initializeObj = execute.Invoke(null, new object[] { });
|
object? initializeObj = execute != null ? execute.Invoke(null, Array.Empty<object>()) : throw new ArgumentNullException("LaunchConfiguration not found");
|
||||||
MethodInfo initialize = (MethodInfo)initializeObj;
|
MethodInfo? initialize = initializeObj != null ? (MethodInfo)initializeObj : throw new ArgumentNullException("Unable to convert object because execute.Invoke is null");
|
||||||
if (initialize != null)
|
if (initialize != null)
|
||||||
{
|
{
|
||||||
initialize.Invoke(null, new object[] { new string[] { } });
|
var success = false;
|
||||||
Assert.IsTrue(true);
|
try
|
||||||
|
{
|
||||||
|
initialize.Invoke(null, new object[] { Array.Empty<string>() });
|
||||||
|
success = true;
|
||||||
|
}
|
||||||
|
catch (Exception innerException)
|
||||||
|
{
|
||||||
|
Assert.Fail($"An exception was thrown during initialize.Invoke: {innerException.Message}");
|
||||||
|
}
|
||||||
|
Assert.IsTrue(success);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -3,56 +3,58 @@ using Microsoft.AspNetCore.Mvc;
|
|||||||
using BasicDotnetTemplate.MainProject.Models.Api.Response;
|
using BasicDotnetTemplate.MainProject.Models.Api.Response;
|
||||||
using BasicDotnetTemplate.MainProject.Models.Settings;
|
using BasicDotnetTemplate.MainProject.Models.Settings;
|
||||||
|
|
||||||
[Controller]
|
namespace BasicDotnetTemplate.MainProject.Controllers
|
||||||
public abstract class BaseController : ControllerBase
|
|
||||||
{
|
{
|
||||||
protected readonly IConfiguration _configuration;
|
public abstract class BaseController : ControllerBase
|
||||||
protected readonly AppSettings _appSettings;
|
|
||||||
|
|
||||||
public BaseController(
|
|
||||||
IConfiguration configuration
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
_configuration = configuration;
|
protected readonly IConfiguration _configuration;
|
||||||
_appSettings = new AppSettings();
|
protected readonly AppSettings _appSettings;
|
||||||
_configuration.GetSection("AppSettings").Bind(_appSettings);
|
|
||||||
}
|
public BaseController(
|
||||||
|
IConfiguration configuration
|
||||||
|
)
|
||||||
|
{
|
||||||
|
_configuration = configuration;
|
||||||
|
_appSettings = new AppSettings();
|
||||||
|
_configuration.GetSection("AppSettings").Bind(_appSettings);
|
||||||
|
}
|
||||||
|
|
||||||
#nullable enable
|
#nullable enable
|
||||||
private BaseResponse CreateResponse(HttpStatusCode status, string message, object? data = null)
|
private BaseResponse CreateResponse(HttpStatusCode status, string message, object? data = null)
|
||||||
{
|
|
||||||
return new BaseResponse()
|
|
||||||
{
|
{
|
||||||
Status = (int)status,
|
return new BaseResponse()
|
||||||
Message = message,
|
{
|
||||||
Data = data
|
Status = (int)status,
|
||||||
};
|
Message = message,
|
||||||
}
|
Data = data
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
protected new IActionResult Created(string message, object? data = null)
|
protected new IActionResult Created(string message, object? data = null)
|
||||||
{
|
{
|
||||||
return StatusCode((int)HttpStatusCode.Created, CreateResponse(HttpStatusCode.Created, message, data));
|
return StatusCode((int)HttpStatusCode.Created, CreateResponse(HttpStatusCode.Created, message, data));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected IActionResult Success(string message, object? data = null)
|
protected IActionResult Success(string message, object? data = null)
|
||||||
{
|
{
|
||||||
return StatusCode((int)HttpStatusCode.OK, CreateResponse(HttpStatusCode.OK, message, data));
|
return StatusCode((int)HttpStatusCode.OK, CreateResponse(HttpStatusCode.OK, message, data));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected IActionResult NotFound(string message, object? data = null)
|
protected IActionResult NotFound(string message, object? data = null)
|
||||||
{
|
{
|
||||||
return StatusCode((int)HttpStatusCode.NotFound, CreateResponse(HttpStatusCode.NotFound, message, data));
|
return StatusCode((int)HttpStatusCode.NotFound, CreateResponse(HttpStatusCode.NotFound, message, data));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected IActionResult BadRequest(string message, object? data = null)
|
protected IActionResult BadRequest(string message, object? data = null)
|
||||||
{
|
{
|
||||||
return StatusCode((int)HttpStatusCode.BadRequest, CreateResponse(HttpStatusCode.BadRequest, message, data));
|
return StatusCode((int)HttpStatusCode.BadRequest, CreateResponse(HttpStatusCode.BadRequest, message, data));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected IActionResult InternalServerError(string message)
|
protected IActionResult InternalServerError(string message)
|
||||||
{
|
{
|
||||||
return StatusCode((int)HttpStatusCode.InternalServerError, CreateResponse(HttpStatusCode.InternalServerError, message));
|
return StatusCode((int)HttpStatusCode.InternalServerError, CreateResponse(HttpStatusCode.InternalServerError, message));
|
||||||
}
|
}
|
||||||
|
|
||||||
#nullable disable
|
#nullable disable
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -3,6 +3,7 @@ using BasicDotnetTemplate.MainProject.Models.Settings;
|
|||||||
|
|
||||||
namespace BasicDotnetTemplate.MainProject.Controllers
|
namespace BasicDotnetTemplate.MainProject.Controllers
|
||||||
{
|
{
|
||||||
|
[Route("")]
|
||||||
public class RootController : BaseController
|
public class RootController : BaseController
|
||||||
{
|
{
|
||||||
public RootController(
|
public RootController(
|
||||||
@@ -12,8 +13,7 @@ namespace BasicDotnetTemplate.MainProject.Controllers
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet("")]
|
||||||
[Route("")]
|
|
||||||
public IActionResult GetVersion()
|
public IActionResult GetVersion()
|
||||||
{
|
{
|
||||||
return Success(String.Empty, "Success");
|
return Success(String.Empty, "Success");
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ using BasicDotnetTemplate.MainProject.Models.Settings;
|
|||||||
|
|
||||||
namespace BasicDotnetTemplate.MainProject.Controllers
|
namespace BasicDotnetTemplate.MainProject.Controllers
|
||||||
{
|
{
|
||||||
|
[Route("[controller]")]
|
||||||
public class VersionController : BaseController
|
public class VersionController : BaseController
|
||||||
{
|
{
|
||||||
public VersionController(
|
public VersionController(
|
||||||
@@ -12,8 +13,7 @@ namespace BasicDotnetTemplate.MainProject.Controllers
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet("get")]
|
||||||
[Route("version")]
|
|
||||||
public IActionResult GetVersion()
|
public IActionResult GetVersion()
|
||||||
{
|
{
|
||||||
return Success(String.Empty, _appSettings?.Settings?.Version);
|
return Success(String.Empty, _appSettings?.Settings?.Version);
|
||||||
|
|||||||
@@ -35,15 +35,11 @@ internal static class Program
|
|||||||
{
|
{
|
||||||
private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
|
private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
|
||||||
|
|
||||||
internal static WebApplication app;
|
public static WebApplication Initialize(string[] args)
|
||||||
|
|
||||||
public static void Initialize(string[] args)
|
|
||||||
{
|
{
|
||||||
Logger.Info("[Program][Main] Start building");
|
Logger.Info("[Program][Main] Start building");
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
Logger.Info("[Program][Main] Creating configuration");
|
|
||||||
|
|
||||||
var _configuration = new ConfigurationBuilder()
|
var _configuration = new ConfigurationBuilder()
|
||||||
.SetBasePath(System.AppDomain.CurrentDomain.BaseDirectory)
|
.SetBasePath(System.AppDomain.CurrentDomain.BaseDirectory)
|
||||||
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
||||||
@@ -113,7 +109,7 @@ internal static class Program
|
|||||||
options.SwaggerDoc("v1", openApiInfo);
|
options.SwaggerDoc("v1", openApiInfo);
|
||||||
});
|
});
|
||||||
|
|
||||||
app = builder.Build();
|
WebApplication app = builder.Build();
|
||||||
|
|
||||||
// REGISTER MIDDLEWARE HERE
|
// REGISTER MIDDLEWARE HERE
|
||||||
app.UseRouting();
|
app.UseRouting();
|
||||||
@@ -136,19 +132,15 @@ internal static class Program
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger.Info("[Program][Main] Launching app");
|
return app;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void Main(string[] args)
|
public static void Main(string[] args)
|
||||||
{
|
{
|
||||||
ReflectionProgram.LaunchConfiguration();
|
ReflectionProgram.LaunchConfiguration();
|
||||||
Initialize(args);
|
WebApplication app = Initialize(args);
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
|
|
||||||
Logger.Info("[Program][Main] Shutting down logger");
|
|
||||||
|
|
||||||
NLog.LogManager.Shutdown(); // Flush and close down internal threads and timers
|
NLog.LogManager.Shutdown(); // Flush and close down internal threads and timers
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user