Using /rules/create API

sonarqube

(Waldo) #1

Must-share information:

  • Sonarqube 6.7.1, 6.7.4
  • Using /api/rules API
  • I never succeeded in using this API

First of all, it seems that parameters documentation isn’t right, as it’s shown that only the key is required while finally the ̀template_key` parameter is also requried.

Also, after many try I couldn’t successfully realize a good call (curl). I tried many way and when I fix the request using all required parameters in the request it fails to work, e.g. :

curl -v -su admin:admin -X POST 'localhost:9000/api/rules/create?custom_key="cat"&template_key="dog"'
 
curl -su admin:admin \
  --header "Content-Type: application/json" \
  --request POST \
  --data '{"custom_key":"checkstyle:Custom_InnerAssignmentCheck","markdown_description":"Checks for assignments in subexpressions, such as in String s = Integer.toString(i = 2);.\n\nThis custom rule inherits from Inner Assignment template.","name":"Custom Inner Assignement","severity":"MAJOR","status":"READY","template_key":"chekstyle:com.puppycrawl.tools.checkstyle.checks.coding.InnerAssignmentCheck","type":"CODE_SMELL"}' \
  http://localhost:9000/api/rules/create

curl -v -su admin:admin -X POST 'localhost:9000/api/rules/create?custom_key="checkstyle:Custom_InnerAssignmentCheck"&markdown_description="Checks for assignments in subexpressions, such as in String s = Integer.toString(i = 2);.\n\nThis custom rule inherits from Inner Assignment template."&name="Custom Inner Assignment"&severity="MAJOR"&status="READY"&template_key="checkstyle:com.puppycrawl.tools.checkstyle.checks.coding.InnerAssignmentCheck"&type="CODE_SMELL"'

(Grégoire Aubert) #2

Hi Waldo,
Indeed it seems that this web-service has a bad documentation and we have a ticket to improve it: SONAR-9539 (make sure to vote for it).

In the mean time, on my side, I was able to create custom rules with the following curl request:

curl -su admin:admin \
  -X POST \
  -d 'template_key=javascript%3ACommentRegularExpression&custom_key=customRuleTest&name=customRuleTest&markdown_description=My%20custom%20rule&severity=MAJOR' \
  'http://localhost:9000/api/rules/create'

All those 5 parameters are required to be able to create a custom rule, also note that template_key must be the key of an existing template rule.

If you are still not able to perform a successful request, please also send here the answer from the web-service, there is an error message that should help understand the problem.