Hi, I’m currently trying to push results of clazy-standalone to sonarqube instance. The community cxx-plugin (SonarOpenCommunity/sonar-cxx/wiki/sonar.cxx.other.reportPaths) provides no direct support, but there is a general way to register ‘unknown’ rules by providing a xml file descriptions.
Therefore I wanted to know, if it is possible to provide such an xml-file as part of the clazy project in the future, or if there is an easy way to generate this file from the existing documentation (Readme files here: (KDE/clazy/tree/master/docs/checks)) automatically.
What do you think? Is there no need for integration in a quality dashboard?
For a proof-of-concept ChatGPT was used to create a python script that creates a correspondig rules xml file for all README-* files of the current directory - see below. Maybe this is helpful also for others, or a starting point for providing a rule description file for sonarqube as part of clazy itself.
The format of the resulting xml file is documented here: SonarOpenCommunity/sonar-cxx/wiki/sonar.cxx.other.rules (I’m not allowed to post links)
create_sonarqube_rules.py:
import os
from xml.dom.minidom import Document
def process_markdown_to_rule(file_path):
"""
Reads a markdown file and converts it to a dictionary representing the rule.
"""
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Extract title from the first line
name = content.splitlines()[0].strip("# ").strip() # First line as title
description = "\n".join(content.splitlines()[1:]).strip() # Remaining lines as description
# Construct the key
key = f"clazy.{name.lower().replace(' ', '-')}"
return {
"key": key,
"name": name,
"description": description
}
def create_rules_xml(directory, output_file):
"""
Takes all markdown files starting with "Readme" in a given directory and creates an XML file.
"""
# Create an XML document
doc = Document()
root = doc.createElement("rules")
doc.appendChild(root)
for filename in os.listdir(directory):
if filename.lower().startswith("readme") and filename.endswith(".md"):
file_path = os.path.join(directory, filename)
rule = process_markdown_to_rule(file_path)
# Create a rule element
rule_element = doc.createElement("rule")
# Add key
key_element = doc.createElement("key")
key_element.appendChild(doc.createTextNode(rule["key"]))
rule_element.appendChild(key_element)
# Add name
name_element = doc.createElement("name")
name_element.appendChild(doc.createTextNode(rule["name"]))
rule_element.appendChild(name_element)
# Add severity
severity_element = doc.createElement("severity")
severity_element.appendChild(doc.createTextNode("MINOR"))
rule_element.appendChild(severity_element)
# Add descriptionFormat
description_format_element = doc.createElement("descriptionFormat")
description_format_element.appendChild(doc.createTextNode("MARKDOWN"))
rule_element.appendChild(description_format_element)
# Add description with CDATA
description_element = doc.createElement("description")
cdata = doc.createCDATASection(f"\n{rule['description']}\n")
description_element.appendChild(cdata)
rule_element.appendChild(description_element)
# Add tag
tag_element = doc.createElement("tag")
tag_element.appendChild(doc.createTextNode("clazy"))
rule_element.appendChild(tag_element)
# Append the rule to the root
root.appendChild(rule_element)
# Write to the output file
with open(output_file, 'w', encoding='utf-8') as f:
f.write(doc.toprettyxml(indent=" "))
# Directory containing the markdown files
input_directory = "./"
# Output XML file
output_file = "rules.xml"
# Create the XML file
create_rules_xml(input_directory, output_file)