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!