Skip to content
Lukasz Rajchel edited this page Feb 2, 2021 · 7 revisions

Extracting rule IDs and names

SonarCloud

cls
$res = Invoke-RestMethod 'https://sonarcloud.io/api/rules/search?languages=cs&organization=sonarsource&ps=500&s=key'
$res.rules | ? { $_.key -notlike 'common-cs*' } | % {
  $key = (($_.key -replace 'csharpsquid:', '') -replace 'roslyn.sonaranalyzer.security.cs:', '')
  $name = '{0}:{1}' -f $key, (($_.name -replace '"', '') -replace '  ', ' ')
  'public const string {0} = "{1}";' -f $key, $name
}

StyleCop.Analyzers

  1. Go to StyleCopAnalyzers Status page
  2. Run the following script and copy the extracted rules from the console (there may be some trash at the begining of each line - nothing that simple text replace couldn't handle).
jQuery('#renderedDiagnostics tr').each(function() {
  var id = $(this).children('td:eq(1)').text().trim();
  var name = $(this).children('td:eq(2)').text().trim();
  console.log('public const string ' + id + ' = "' + id + ':' + name + '";');
});

FxCop

  1. Install local SonarQube
  2. Install CodeCracker for C# and FxCop plugins.
  3. Go to Rules section, mark C# as Language and specific analyzer as Repository, expand the list until all rules are visible. Restart SonarQube if needed.
  4. jQueryfy the page.
  5. Run the following script and copy the extracted rules from the console (there may be some trash at the begining of each line - nothing that simple text replace couldn't handle).
jQuery('.coding-rule').each(function() {
  var text = jQuery(this).find('a').text().replace(/"/g, '').replace(/  /g, ' ')
    .replace(/  {...}/g, '{...}').trim();
  var regex = /(CA\d+): (.*)/
  if (regex.test(text)) {
    var textMatch = text.match(regex);
    var id = textMatch[1];
    var name = textMatch[2];
    console.log('public const string ' + id + ' = "' + id + ':' + name + '";');
  }
});

CodeCracker

This project has not been updated in couple of years. It's probably dead.

Clone this wiki locally