Update setting via API

We want to set the default value for all created projects via the API.

The following curl command leads to insufficient privileges:
curl -u : --request POST https://sonarcloud.io/api/settings/set?key=sonar.leak.period&value=previous_project&component=

Why? The API key belongs to a user who is org admin.

  1. Is the API approach the best way to set default values for all projects in SonarCloud?
  2. How can I set project settings via API?

Thanks,
Martin

1 Like

Hi Martin,

First, in order to set the default value on settings, you should use api/settings/reset instead of api/settings/set.

Then, about permission, the user needs to administrator of the project.
By default, an administrator of the organization should also be administrator of all proejcts, but if it’s not the case he can grant himself back the permission using “Administration” > “Project Management” > “Edit Permission” on the project.

Regards

Hi Julien

Thanks for your quick reply. Sorry, my first question was a bit misleading

  1. Is the API approach the best way to set default values for all projects in SonarCloud?
    Whenever a new project is created in SonarCloud by submitting the first analysis, I wonder if I can configure the default settings for such a new project created. Not the SonarCloud default settings, but our company internal ones. For example, to force “sonar.cloud.leak” to “previous_version” instead of “30”.

  2. How can I set project settings via API?
    I gave all permissions to the user but still no luck (see screenshot). In the API documentation is stated, that the “Administer System” permission is required. Maybe I do not have that permission on a hosted SonarCloud account?

Hi Martin,

  1. Indeed the best way to customise the leak period on new projects is to use web services (for the moment).

  2. The API documentation is also mentioning the fact that a project administrator is allowed to update a setting value, when a component parameter is set on the project key.
    Could you please check again by checking that the component parameter is used ?

Regards

Hi Julien

I already use the component parameter. I retrieved the value from the Project-Key-Copy Button in the SonarCloud Web UI.

curl -u abc: --request POST https://sonarcloud.io/api/settings/set?key=sonar.leak.period&value=previous_project&component=xyz

I have to add that it is a component of a an organizational account where I am the admin of. Do I need to add org information to the component param? Running out of ideas :slight_smile:

Hi Martin,

The WS query you’re using sounds good. Do you confirm that when executing it you receive a 403 error ?
If yes, can you check that you’re an Administrator of the project ? Because even if you’re the administrator of the organization, you also need explicitly to be administrator of the project.

Regards