When we are using Azure DevOps to review PR, it's simple and easy to use. Usually, we only run tests and build tasks, like this:
In my previous blog, I mentioned how to run test and generate code coverage for CI pipelines: Display code coverage information for .NET Core project using Azure DevOps. - Anduin Xue (aiursoft.com)
JetBrains ReSharper and Rider are impressive C# grammar checking tools which prevents you write bad code. But as a project maintainer, you can't ensure that everybody contributes to your repository can follow their code quality regulations. How can Azure DevOps automatically check the code quality using JetBrains grammar checker and reject Pull Requests which contains bad code?
First, install the JetBrains Code Quality checker here:
And you can simply add it to your pipeline.
- task: alanwales.resharper-code-analysis.custom-build-task.ResharperCli@2
displayName: 'Automated code quality checks'
I strongly suggest insert this job after the build job. Since the grammar checker might cause some issue without building the project first.
And I also suggest insert this job before the test job. Since you shouldn't run tests on invalid code. But running grammar checker doesn't matter.
Select suitable severity level. And check the checkbox if you want the pipeline to be failed with invalid code detected.
Kickoff a new build and see the results:
For GitHub actions?
It's also easy to use that in GitHub actions.
Consider the following yaml:
branches: [ master ]
name: Self Test
- name: Checkout
- name: Inspect
How to check the code locally?
Since you know how to check the quality on the cloud. But you can't use the cloud environment as a debugging tool.
You need a way to check the code quality and generate reports locally. Understand that the best way is to buy ReSharper directly. But what if you want a solution for free?
Yes. You can run the code quality check without buying ReSharper.
dotnet tool install -g JetBrains.ReSharper.GlobalTools
And after installing the grammar check tool, run this with your solution:
jb inspectcode --output=o.xml --build ./YourSolution.sln
And simply open the o.xml using your favorite editor to review the results.
Since there might be grammar checks that not suitable for your project, and with warnings you are not able to check in. How can you adjust the check level globally or suppress warnings?
Simply create an .editorconfig file alongside your solution. Add it with rules like this:
root = true
# Anduin suggested
# Some CSS Resharper doesn't know how to load.
# Some JS Resharper doesn't know how to load.
# Some HTML reference Resharper doesn't know how to load.
# Allow JS global var.
# Stop suggesting IE compatibility.
# Localization might not be finished.
# Allow names like `IPAddress`.
Then all the level of those rules are adjusted.
To query all the level properties of editorconfig, please reference: Index of EditorConfig properties | ReSharper (jetbrains.com)
Now you have a complete automatically code quality check solution for C#. And it's free! Happy coding!