Prompt:
In an Azure based project written in C# and using DevOps Repos for code and DevOps Pipelines for releases, is there a way to make sure pull requests pass standards tests in an automated fashion, including specifically “if a method’s signature has parameters added to it, those parameters are used?”
Response:
Yes, in an Azure-based C# project using Azure DevOps Repos for version control and Azure DevOps Pipelines for CI/CD, you can enforce standards, including verifying that newly added parameters in method signatures are actually used. Here’s how you can do it:
1. Use Roslyn Analyzers to Enforce Code Standards
Since your project is in C#, Roslyn analyzers can be used to enforce static code analysis. You can:
- Write a custom Roslyn analyzer that detects method signatures with new parameters that are not used.
- Use built-in .NET analyzers or third-party tools like SonarQube or ReSharper Command Line Tools.
✅ Implementation Steps:
- Install
Microsoft.CodeAnalysis.CSharp
NuGet package in a separate analyzer project. - Write an analyzer that checks for:
- Methods where parameters are introduced in a pull request.
- Parameters that are not referenced within the method body.
- Deploy the analyzer as a Roslyn-based NuGet package and integrate it into your CI/CD pipeline.
Example Custom Analyzer (Simplified):
public override void AnalyzeNode(SyntaxNodeAnalysisContext context)
{
var methodDeclaration = (MethodDeclarationSyntax)context.Node;
foreach (var parameter in methodDeclaration.ParameterList.Parameters)
{
var parameterName = parameter.Identifier.Text;
var references = methodDeclaration.DescendantNodes()
.OfType<IdentifierNameSyntax>()
.Count(id => id.Identifier.Text == parameterName);
if (references == 0)
{
context.ReportDiagnostic(Diagnostic.Create(Rule, parameter.GetLocation(), parameterName));
}
}
}
- Enforce it in
Directory.Build.props
oreditorconfig
.
2. Enforce Static Code Analysis in DevOps Pipeline
You can configure the Azure DevOps build pipeline to enforce static code analysis during PR validation.
✅ Options:
- Enable Code Analysis in Pipeline
- Use
.NET Build
with the/warnaserror
flag to enforce Roslyn analyzers. - Example YAML:
- task: DotNetCoreCLI@2 inputs: command: 'build' projects: '**/*.csproj' arguments: '--configuration Release /warnaserror'
- Use
- Use SonarQube or Roslynator
- SonarQube can detect unused parameters.
- Integrate SonarQube scan in your pipeline:
- task: SonarQubePrepare@4 inputs: SonarQube: 'SonarQubeServiceConnection' scannerMode: 'MSBuild' - task: SonarQubeAnalyze@4
3. Configure Pull Request Policies
Set up a branch policy to enforce automated checks before merging:
- Require PR build validation that runs static analysis.
- Reject PRs if violations are found.
✅ Steps in Azure DevOps:
- Navigate to Repos → Branches → Select Branch → Branch Policies.
- Under Build Validation, add a policy requiring:
dotnet build
with analyzers.dotnet test
with unit test coverage checks.- SonarQube or other static analysis.
4. Require Unit Test Coverage in PRs
Ensure new parameters are tested in unit tests before merging:
- Enforce code coverage requirements (e.g., > 80% coverage).
- Use
coverlet
to check:- task: DotNetCoreCLI@2 inputs: command: 'test' projects: '**/*.Tests.csproj' arguments: '--configuration Release --collect:"XPlat Code Coverage"'
Conclusion
By combining Roslyn Analyzers, Azure DevOps PR policies, Static Analysis, and Unit Testing, you can automate pull request validation to ensure new method parameters are actually used before merging. Would you like help with setting up a Roslyn analyzer or DevOps YAML pipeline?