Web API to automatically create users and groups

sonarqube

(Kevin Hsu) #1

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


(Emre Dundar) #2

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"


(Julien Lancelot) #3

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


(Kevin Hsu) #4

Thank you, Emre and Julien, for your help!


(Cid Da Costa) #5

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…


(Colin Mueller) #6

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.


(Cid Da Costa) #7

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!!!