Search code examples
c#sonarqubecodacy

Disable rule for Sonar C#


I’d like to disable certain rules for Sonar C# for my Codacy project. According to the SonarQube docs, I have to create an .editorconfig file, so I did:

[*.cs]
dotnet_diagnostic.S121.severity = none
dotnet_diagnostic.S3216.severity = none

However, Codacy does not seem to pick up the .editorconfig file - at least the warnings that should be disabled are still there.

What do I have to do to disable certain rules for Sonar C#?

Thanks!


Solution

  • I also asked this question in Codacy's forum (see here). Here is the answer I got:

    Basically, the SonarLint.xml file should contain all and only the rules that you want to use in your repository. One of our engineers developed a script that’s going to the public repository codacy-sonar-csharp, fetches all the patterns used by default in Codacy’s Sonar tool, and creates a SonarLint.xml file with them.

    And here the mentioned script:

    #!/bin/env bash
    
    container_id=$(docker create codacy-csharp:latest)
    codacy_rules_file=/tmp/codacy-rules.json
    
    # get all the rules defined for the tool from inside the docker container
    docker cp $container_id:/docs/patterns.json $codacy_rules_file
    docker rm $container_id
    
    # obtain all ids of the rules we have enabled by default
    rules_ids=$(cat $codacy_rules_file | jq -r '.patterns[] | select (.enabled == true) | .patternId')
    
    cat << EOF > SonarLint.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <AnalysisInput>
    <Rules>
    EOF
    
    for rule_id in $rules_ids
    do
        echo -e "\t<Rule>\n\t\t<Key>$rule_id</Key>\n\t</Rule>\n" >> SonarLint.xml
    done
    
    cat << EOF >> SonarLint.xml
    </Rules>
    </AnalysisInput>
    EOF