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:
Resharper Code Quality Analysis - Visual Studio Marketplace
And you can simply add it to your pipeline.
YAML:
steps:
- task: alanwales.resharper-code-analysis.custom-build-task.ResharperCli@2
displayName: 'Automated code quality checks'
inputs:
solutionOrProjectPath: AiurVersionControl.sln
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:
name: InspectMaster
on:
push:
branches: [ master ]
jobs:
self-test:
runs-on: ubuntu-latest
name: Self Test
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Inspect
uses: nbadal/inspectcode-action
with:
solution: './MyProject.sln'
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.
Run this:
dotnet tool install -g JetBrains.ReSharper.GlobalTools
Read more: ReSharper Command Line Tools | ReSharper (jetbrains.com)
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.
Read more: InspectCode Command-Line Tool | ReSharper (jetbrains.com)
Suppress warnings
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.
resharper_unknown_css_class_highlighting=none
# Some JS Resharper doesn't know how to load.
resharper_undeclared_global_variable_using_highlighting=none
# Some HTML reference Resharper doesn't know how to load.
resharper_html_path_error_highlighting=none
# Allow JS global var.
resharper_use_of_implicit_global_in_function_scope_highlighting=none
# Stop suggesting IE compatibility.
resharper_css_browser_compatibility_highlighting=none
# Localization might not be finished.
resharper_not_overridden_in_specific_culture_highlighting=suggestion
# Allow names like `IPAddress`.
resharper_inconsistent_naming_highlighting=suggestion
Read more: InspectCode Command-Line Tool | ReSharper (jetbrains.com)
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!
I just finished reading your blog post about using JetBrains code quality analyzer to prevent checking-in bad C# code, and I must say it was quite informative and well-structured. The core idea of automating code quality checks using JetBrains grammar checker and integrating it with Azure DevOps and GitHub actions is a great approach to maintain code quality in a project. I appreciate how you provided clear instructions and relevant images to help readers understand the process.
The highlight of your post is how you've presented a free solution for maintaining code quality without having to purchase ReSharper. This is valuable information for developers who want to improve their code quality without incurring additional costs.
However, I noticed a few minor improvements that could be made to the post:
In the section "How to check the code locally?", you mentioned that the best way is to buy ReSharper directly, but then you provided a free solution. It would be better to rephrase this part to avoid confusion. You could say something like, "While purchasing ReSharper is a great option, there is also a free solution available."
In the same section, after running the command
jb inspectcode --output=o.xml --build ./YourSolution.sln
, it would be helpful to mention that the generated 'o.xml' file contains the inspection results and can be reviewed using any text editor.In the "Suppress warnings" section, you provided an example of an .editorconfig file with some rules. It would be helpful to give a brief explanation of each rule and why they might be useful for the readers.
Finally, it would be great to include a conclusion section summarizing the benefits of using JetBrains code quality analyzer and how it can help developers maintain better code quality in their projects.
Overall, your blog post is insightful and offers valuable information for developers looking to improve their code quality. With a few minor improvements, it could become an even more comprehensive resource. Keep up the great work!
Can you please tell me how to publish this results to our azure pipeline