Web API to automatically create users and groups

Our SonarQube version is 6.7.4.
I tried to use Web API to create/add users and groups, just like what we can do via GUI.
I am using a shell script to automate that so everytime when we install a new instance of SonarQube in an environment (such as Dev, QA, etc.) I can automatically create/add some standard users/groups. I know we can do that through GUI. However, we want to automate that through Web API.

Is the following the way to create a user?
https://docs.sonarqube.org/pages/viewpage.action?pageId=2752787#WebService/api/users-CreateaUser

curl -X POST -v -u admin:admin ā€˜http://localhost:9000/api/users/create?login=myuser&password=mypassword&password_confirmation=mypassword&name=My%20Name&email=myname@email.comā€™

I see thatā€™s an archive. Could you verify if the above is still working? Or please guide me the exact curl I need to do in order to create a user thru Web API?

Thank you,
Kevin Hsu

Hi Kevin,

You can check this page on your Sonar instance for web API guide: http://localhost:9000/web_api/api/users

I tried to run this command and it works:

curl -X POST -v -u admin:admin "http://localhost:9000/api/users/create?login=myuser&password=mypassword&name=myname&email=myname@email.com"

2 Likes

Hi Kevin,

All web services are listed in your SonarQube instance in http://localhost:9000/web_api

For instance on SonarSource instance, the web service to create a user is https://next.sonarqube.com/sonarqube/web_api/api/users/create

Regards,

Julien Lancelot

1 Like

Thank you, Emre and Julien, for your help!

1 Like

Hi, I was reading this post above, about creating users and groups, but i cant figure out how to create groups. I was wondering this command only works to create a user.

I am a system administrator on my installation of sonarqube and I need to create a bunch of groups and apply some permissions on that.
What is the command to create a group, please.

curl -X POST -v -u admin:admin ā€œhttp://localhost:9000/api/users/create?login=myuser&password=mypassword&name=myname&email=myname@email.comā€

Sorry about my poor english languageā€¦

Greetings Cid,

Please donā€™t apologize for your English! Itā€™s both fine, and the only requirement to post here is that we understand you. :slight_smile:

Youā€™ll find Web API documentation linked in the footer of your SonarQube instance, and an api/groups domain similar to api/users. You can find details about the Web API as it applies to groups there.

Hi Colin,

Iā€™ve created a group using this command below. Now, Iā€™m trying to understand, how to add this group in to a Permission Templateā€¦ and then create a script to make all this things work automatically.

curl -u MyToken: ā€˜https://ServerAddress/api/user_groups/createā€™ -d ā€œname=GroupNameā€

To grant permissions to a group or a project

In my case Iā€™ve set ā€œprovisioningā€ permission.

It possible to set:

  • Possible values for global permissions: admin, profileadmin, gateadmin, scan, provisioning
  • Possible values for project permissions: user, admin, issueadmin, codeviewer, scan,

curl -u MyToken: ā€˜https://ServerAddress/api/permissions/add_groupā€™ -d ā€œpermission=provisioning&groupName=NameOfTheGroupā€

Depending on a kind of permission you will grant global parmission or project permission

To apply a Permission Template to a group

curl -u MyToken: ā€˜https://ServerAddress/api/permissions/add_group_to_templateā€™ -d ā€œpermission=user, admin, issueadmin, codeviewer, scan&groupName=nameof_the_group&templateName=templatetameā€

I have a doubt, its possible to set more than one permiission at a time?

Thank you all!!!

Did you every find a way to add more than one permission at a time? Iā€™m having trouble doing so.

I tried these cur commands none of these working for me. example

curl -u MyToken: ā€˜https://server.com/api/permissions/add_groupā€™ -d ā€œpermission=provisioning&groupName=TestGroupā€

curl -u MyToken: ā€˜https://server.com/api/user_groups/createā€™ -d ā€œname=TestGroupNameā€

please suggest what i am missing here.