Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OSOE-758: Dummy PR to test CodeRabbit again #114

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using Lombiq.Hosting.Tenants.EnvironmentRobots.Extensions;
using Lombiq.Hosting.Tenants.EnvironmentRobots.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Lombiq.Hosting.Tenants.EnvironmentRobots.Middlewares;

public class EnvironmentRobotsMiddleware
{
private readonly RequestDelegate _next;
private readonly IHostEnvironment _hostEnvironment;
private readonly IOptions<EnvironmentRobotsOptions> _options;

public EnvironmentRobotsMiddleware(
RequestDelegate next,
IHostEnvironment hostEnvironment,
IOptions<EnvironmentRobotsOptions> options)
{
_next = next;
_hostEnvironment = hostEnvironment;
_options = options;
}

public Task InvokeAsync(HttpContext context)
{
if (!_hostEnvironment.IsProductionWithConfiguration(_options))
{
var headerValue = context.Response.Headers["X-Robots-Tag"].FirstOrDefault() ?? string.Empty;

var directives = new List<string>();

if (!string.IsNullOrEmpty(headerValue))
{
directives.Add(headerValue);
}

// False warning, since headerValue is initialized to string.Empty if it would be null.
#pragma warning disable S2259 // Null pointers should not be dereferenced
if (!headerValue.Contains("noindex"))
{
directives.Add("noindex");
}
#pragma warning restore S2259 // Null pointers should not be dereferenced

if (!headerValue.Contains("nofollow"))
{
directives.Add("nofollow");
}

if (directives.Count > 1)
{
context.Response.Headers["X-Robots-Tag"] = $"{string.Join(", ", directives)}";
}
}

return _next(context);
}
Comment on lines +28 to +61
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The InvokeAsync method correctly checks if the current environment is not production using a custom extension method IsProductionWithConfiguration. This is a good practice to conditionally apply logic based on the environment. However, there are a few areas for improvement:

  1. The check for existing directives (noindex, nofollow) in the X-Robots-Tag header is done sequentially. Consider refactoring this into a more concise approach, such as using a loop or LINQ to avoid repetitive code.
  2. The comment regarding the false warning (#pragma warning disable S2259) could be avoided if the code structure is improved to ensure headerValue is always non-null, which it already is due to the null-coalescing operator. This directive might hide other potential null reference issues in the future.
  3. The condition if (directives.Count > 1) before setting the X-Robots-Tag header seems incorrect. It should likely be if (directives.Any()) to ensure the header is set if there are any directives, not just more than one.
- if (directives.Count > 1)
+ if (directives.Any())

Consider refactoring the checks for noindex and nofollow to reduce code duplication and remove the need for disabling the null pointer dereference warning.


Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
public Task InvokeAsync(HttpContext context)
{
if (!_hostEnvironment.IsProductionWithConfiguration(_options))
{
var headerValue = context.Response.Headers["X-Robots-Tag"].FirstOrDefault() ?? string.Empty;
var directives = new List<string>();
if (!string.IsNullOrEmpty(headerValue))
{
directives.Add(headerValue);
}
// False warning, since headerValue is initialized to string.Empty if it would be null.
#pragma warning disable S2259 // Null pointers should not be dereferenced
if (!headerValue.Contains("noindex"))
{
directives.Add("noindex");
}
#pragma warning restore S2259 // Null pointers should not be dereferenced
if (!headerValue.Contains("nofollow"))
{
directives.Add("nofollow");
}
if (directives.Count > 1)
{
context.Response.Headers["X-Robots-Tag"] = $"{string.Join(", ", directives)}";
}
}
return _next(context);
}
public Task InvokeAsync(HttpContext context)
{
if (!_hostEnvironment.IsProductionWithConfiguration(_options))
{
var headerValue = context.Response.Headers["X-Robots-Tag"].FirstOrDefault() ?? string.Empty;
var directives = new List<string>();
if (!string.IsNullOrEmpty(headerValue))
{
directives.Add(headerValue);
}
// False warning, since headerValue is initialized to string.Empty if it would be null.
#pragma warning disable S2259 // Null pointers should not be dereferenced
if (!headerValue.Contains("noindex"))
{
directives.Add("noindex");
}
#pragma warning restore S2259 // Null pointers should not be dereferenced
if (!headerValue.Contains("nofollow"))
{
directives.Add("nofollow");
}
if (directives.Any())
{
context.Response.Headers["X-Robots-Tag"] = $"{string.Join(", ", directives)}";
}
}
return _next(context);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using Lombiq.Hosting.Tenants.EnvironmentRobots.Extensions;
using Lombiq.Hosting.Tenants.EnvironmentRobots.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Lombiq.Hosting.Tenants.EnvironmentRobots.Middlewares;

public class EnvironmentRobotsMiddleware
{
private readonly RequestDelegate _next;
private readonly IHostEnvironment _hostEnvironment;
private readonly IOptions<EnvironmentRobotsOptions> _options;

public EnvironmentRobotsMiddleware(
RequestDelegate next,
IHostEnvironment hostEnvironment,
IOptions<EnvironmentRobotsOptions> options)
{
_next = next;
_hostEnvironment = hostEnvironment;
_options = options;
}

public Task InvokeAsync(HttpContext context)
{
if (!_hostEnvironment.IsProductionWithConfiguration(_options))
{
var headerValue = context.Response.Headers["X-Robots-Tag"].FirstOrDefault() ?? string.Empty;

var directives = new List<string>();

if (!string.IsNullOrEmpty(headerValue))
{
directives.Add(headerValue);
}

// False warning, since headerValue is initialized to string.Empty if it would be null.
#pragma warning disable S2259 // Null pointers should not be dereferenced
if (!headerValue.Contains("noindex"))
{
directives.Add("noindex");
}
#pragma warning restore S2259 // Null pointers should not be dereferenced

if (!headerValue.Contains("nofollow"))
{
directives.Add("nofollow");
}

if (directives.Count > 1)
{
context.Response.Headers["X-Robots-Tag"] = $"{string.Join(", ", directives)}";
}
}

return _next(context);
}
Comment on lines +28 to +61
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The InvokeAsync method correctly checks if the current environment is not production using a custom extension method IsProductionWithConfiguration. This is a good practice to conditionally apply logic based on the environment. However, there are a few areas for improvement:

  1. The check for existing directives (noindex, nofollow) in the X-Robots-Tag header is done sequentially. Consider refactoring this into a more concise approach, such as using a loop or LINQ to avoid repetitive code.
  2. The comment regarding the false warning (#pragma warning disable S2259) could be avoided if the code structure is improved to ensure headerValue is always non-null, which it already is due to the null-coalescing operator. This directive might hide other potential null reference issues in the future.
  3. The condition if (directives.Count > 1) before setting the X-Robots-Tag header seems incorrect. It should likely be if (directives.Any()) to ensure the header is set if there are any directives, not just more than one.
- if (directives.Count > 1)
+ if (directives.Any())

Consider refactoring the checks for noindex and nofollow to reduce code duplication and remove the need for disabling the null pointer dereference warning.


Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
public Task InvokeAsync(HttpContext context)
{
if (!_hostEnvironment.IsProductionWithConfiguration(_options))
{
var headerValue = context.Response.Headers["X-Robots-Tag"].FirstOrDefault() ?? string.Empty;
var directives = new List<string>();
if (!string.IsNullOrEmpty(headerValue))
{
directives.Add(headerValue);
}
// False warning, since headerValue is initialized to string.Empty if it would be null.
#pragma warning disable S2259 // Null pointers should not be dereferenced
if (!headerValue.Contains("noindex"))
{
directives.Add("noindex");
}
#pragma warning restore S2259 // Null pointers should not be dereferenced
if (!headerValue.Contains("nofollow"))
{
directives.Add("nofollow");
}
if (directives.Count > 1)
{
context.Response.Headers["X-Robots-Tag"] = $"{string.Join(", ", directives)}";
}
}
return _next(context);
}
public Task InvokeAsync(HttpContext context)
{
if (!_hostEnvironment.IsProductionWithConfiguration(_options))
{
var headerValue = context.Response.Headers["X-Robots-Tag"].FirstOrDefault() ?? string.Empty;
var directives = new List<string>();
if (!string.IsNullOrEmpty(headerValue))
{
directives.Add(headerValue);
}
// False warning, since headerValue is initialized to string.Empty if it would be null.
#pragma warning disable S2259 // Null pointers should not be dereferenced
if (!headerValue.Contains("noindex"))
{
directives.Add("noindex");
}
#pragma warning restore S2259 // Null pointers should not be dereferenced
if (!headerValue.Contains("nofollow"))
{
directives.Add("nofollow");
}
if (directives.Any())
{
context.Response.Headers["X-Robots-Tag"] = $"{string.Join(", ", directives)}";
}
}
return _next(context);
}

}
Empty file added New Text Document.txt
Empty file.
Loading