Researching Web APIs and Translating SonarQube Rules with Python
This article demonstrates how to query SonarQube web APIs for Java rule data, retrieve quality profile information, and use a Python script that leverages execjs to translate rule descriptions via Google Translate, providing examples of both API responses and translation code.
Researching Web API
The article begins by showing how to call the api/rules/search?languages=java&ps=500&p=1 endpoint to obtain a list of Java rules, including a sample JSON response that contains rule metadata such as key, repository, name, creation date, description, severity, and tags.
It then presents the
api/qualityprofiles/search?language=java&qualityProfile=java1request, displaying the JSON response that describes the Java quality profile named "java1" with its active rule count and other profile attributes.
Next, the
api/rules/search?activation=true&qprofile=AWtqxGzkP99GHtfoWcircall is shown, returning the same rule data as before but filtered for the specific quality profile, again with the full rule description and compliance examples.
Python Test
A short interactive Python session demonstrates how to use requests and json to fetch the rule data, parse the JSON, and inspect the structure of the returned dictionary.
Google Translate Script
The core of the article is a Python script that uses execjs to execute a JavaScript function for generating Google Translate tokens, builds the translation URL, and retrieves translated text. The script includes functions buildUrl, translate, and a SonarRule class that fetches rules from SonarQube, strips HTML tags, counts sentence delimiters, and calls the translation routine for each rule description.
Key parts of the script are presented inside ... blocks, preserving the original indentation and logic, such as the token generation function TL and the request headers used for the translation service.
Translated Rule Content
The final section shows a CSV‑style excerpt of translated rule descriptions, for example: "Boolean expressions should not be gratuitous" translated into Chinese, along with several other rule translations.
DevOps Cloud Academy
Exploring industry DevOps practices and technical expertise.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
