Go to file
Mariani Lucas 26053b7bbe First release 2020-08-22 21:54:24 -03:00
lib First release 2020-08-22 21:54:24 -03:00
spec First release 2020-08-22 21:54:24 -03:00
.dockerignore First release 2020-08-22 21:54:24 -03:00
.gitignore First release 2020-08-22 21:54:24 -03:00
.rubocop.yml First release 2020-08-22 21:54:24 -03:00
.rubocop_todo.yml First release 2020-08-22 21:54:24 -03:00
CHANGELOG.md First release 2020-08-22 21:54:24 -03:00
CONTRIBUTING.md First release 2020-08-22 21:54:24 -03:00
Dockerfile First release 2020-08-22 21:54:24 -03:00
Gemfile First release 2020-08-22 21:54:24 -03:00
LICENSE.txt First release 2020-08-22 21:54:24 -03:00
README.md First release 2020-08-22 21:54:24 -03:00
Rakefile First release 2020-08-22 21:54:24 -03:00
docker-compose.yml First release 2020-08-22 21:54:24 -03:00
docker.env First release 2020-08-22 21:54:24 -03:00
sonarqube.gemspec First release 2020-08-22 21:54:24 -03:00

README.md

Sonarqube

Build Status Inline docs Gem version License

website | documentation

Sonarqube is a Ruby wrapper and CLI for the Sonarqube API As of version 1.0.0 this gem only supports Sonarqube 7.9.

Installation

Install it from rubygems:

gem install sonarqube

Or add to a Gemfile:

gem 'sonarqube'
# gem 'sonarqube', github: 'psyreactor/sonarqube'

Mac OS users can install using Homebrew (may not be the latest version):

brew install sonarqube-gem

Usage

Configuration example

Sonarqube.configure do |config|
  config.endpoint       = 'https://example.net:9000' # API endpoint URL, default: ENV['SONARQUBE_API_ENDPOINT']
  config.private_token  = 'Dfrt938dSgAOWd4'       # user's private token, default: ENV['SONARQUBE_API_PRIVATE_TOKEN']
  # Optional
  # config.user_agent   = 'Custom User Agent'          # user agent, default: 'Sonarqube Ruby Gem [version]'
end

Usage examples

# set an API endpoint
Sonarqube.endpoint = 'https://example.net:9000'
# => "https://example.net:9000"

# set a user private token
Sonarqube.private_token = 'Dfrt938dSgAOWd4'
# => "Dfrt938dSgAOWd4"

# configure a proxy server
Sonarqube.http_proxy('proxyhost', 8888)
# proxy server with basic auth
Sonarqube.http_proxy('proxyhost', 8888, 'proxyuser', 'strongpasswordhere')
# set timeout for responses
ENV['SONARQUBE_API_HTTPARTY_OPTIONS'] = '{read_timeout: 60}'

initialize a new client with custom headers

g = Sonarqube.client(
  endpoint: 'https://example.com:9000',
  private_token: 'Dfrt938dSgAOWd4',
  httparty: {
    headers: { 'Cookie' => 'sonarqube_canary=true' }
  }
)
# => #<Sonarqube::Client:0x00000001e62408 @endpoint="https://api.example.com", @private_token="qEsq1pt6HJPaNciie3MG", @user_agent="Sonarqube Ruby Gem 2.0.0">

Projects

Create Project

project = Sonarqube.project_create('new_project')
# => <Sonarqube::ObjectifiedHash:46200 {hash: {"project"=>{"key"=>"new_project", "name"=>"new_project", "qualifier"=>"TRK", "visibility"=>"public"}}}
project.project.key
# => "new_project"
project.to_hash
# => {"project"=>{"key"=>"new_project", "name"=>"new_project", "qualifier"=>"TRK", "visibility"=>"public"}}

Delete Project

project = Sonarqube.project_delete('test')
# => #<Sonarqube::ObjectifiedHash:46220 {hash: {}}
project.to_hash.empty?
# => true

Search Project

projects = Sonarqube.project_search()
# => #<Sonarqube::ObjectifiedHash:46240 {hash: {"paging"=>{"pageIndex"=>1, "pageSize"=>100, "total"=>2}, "components"=>[{"organization"=>"default-organization", "key"=>"old_key", "name"=>"new_proyect", "qualifier"=>"TRK", "visibility"=>"private"}, {"organization"=>"default-organization", "key"=>"test", "name"=>"test", "qualifier"=>"TRK", "visibility"=>"public"}]}}
projects.components.each do | project |
  puts "name: #{project.name}"
  puts "key: #{project.key}"
end
# name: new_proyect
# key: old_key
# name: test
# key: test

Users

Create User

user = Sonarqube.user_create('new_user', 'key_new_user' ,'secretpassword')
# => #<Sonarqube::ObjectifiedHash:46320 {hash: {"user"=>{"login"=>"login_name", "name"=>"name_user", "scmAccounts"=>[], "active"=>true, "local"=>true}}}
user.user.login
# login_name
user.user.name
# name_user

Delete User

user = Sonarqube.user_delete('test')
# => #<Sonarqube::ObjectifiedHash:46220 {hash: {}}
user.to_hash.empty?
# => true

Search User

users = Sonarqube.users_search()
# => #<Sonarqube::ObjectifiedHash:46340 {hash: {"paging"=>{"pageIndex"=>1, "pageSize"=>50, "total"=>5}, "users"=>[{"login"=>"admin", "name"=>"Administrator", "active"=>true, "groups"=>["sonar-administrators", "sonar-users"], "tokensCount"=>1, "local"=>true, "externalIdentity"=>"admin", "externalProvider"=>"sonarqube", "lastConnectionDate"=>"2020-08-22T23:09:14+0000"}, {"login"=>"new_user", "name"=>"key_new_user", "active"=>true, "groups"=>["sonar-users"], "tokensCount"=>0, "local"=>true, "externalIdentity"=>"new_user", "externalProvider"=>"sonarqube"}, {"login"=>"login_name", "name"=>"name_user", "active"=>true, "groups"=>["sonar-users"], "tokensCount"=>0, "local"=>true, "externalIdentity"=>"login_name", "externalProvider"=>"sonarqube"}, {"login"=>"test3", "name"=>"test QA", "active"=>true, "groups"=>["sonar-users"], "tokensCount"=>0, "local"=>true, "externalIdentity"=>"test3", "externalProvider"=>"sonarqube"}, {"login"=>"newlogin", "name"=>"test QA", "active"=>true, "groups"=>["sonar-users"], "tokensCount"=>0, "local"=>true, "externalIdentity"=>"newlogin", "externalProvider"=>"sonarqube"}]}}
users.users.each do | user |
  puts "name: #{user.name}"
  puts "login: #{user.login}"
  puts "lastConection: #{user.lastConnectionDate}" if defined? user.lastConnectionDate
end
# name: Administrator
# login: admin
# lastConection: 2020-08-22T23:09:14+0000
# name: key_new_user
# login: new_user
# name: name_user
# login: login_name
# name: test QA
# login: test3
# name: test QA
# login: newlogin

Groups

Create Group

group = Sonarqube.create_group('New-Group', {description: 'Sonarqube group users'})
# => #<Sonarqube::ObjectifiedHash:46500 {hash: {"group"=>{"uuid"=>"AXQYrrgCsrvdoo0YodNM", "organization"=>"default-organization", "name"=>"New-Group", "description"=>"Sonarqube group users", "membersCount"=>0, "default"=>false}}}
group.group.uuid
# AXQYrrgCsrvdoo0YodNM
group.group.description
# Sonarqube group users

Delete Group

group = Sonarqube.group_delete('New-Group')
# => #<Sonarqube::ObjectifiedHash:46220 {hash: {}}
group.to_hash.empty?
# => true

Search Group

groups = Sonarqube.search_groups({ q: 'sonar-users' })
# => #<Sonarqube::ObjectifiedHash:46520 {hash: {"paging"=>{"pageIndex"=>1, "pageSize"=>100, "total"=>1}, "groups"=>[{"uuid"=>"AXOt93S3gMZPhbn-E_O7", "name"=>"sonar-users", "description"=>"Any new users created will automatically join this group", "membersCount"=>5, "default"=>true}]}}
groups.groups.each do | group |
  puts "name: #{group.name}"
  puts "login: #{group.description}"
  puts "membersCount: #{group.membersCount}"
end
# name: sonar-users
# description: Any new users created will automatically join this group
# MembersCount: 5

For more information, refer to documentation.

Development

With a dockerized Sonarqube instance

docker-compose up -d sonarqube # Will start the Sonarqube instance in the background (approx. 3 minutes)

After a while, your Sonarqube instance will be accessible on http://localhost:9000.

You can login with the admin/admin user/password.

You can now setup a personal access token here: http://localhost:9000

Once you have your token, set the variables to the correct values in the docker.env file.

Then, launch the tool:

docker-compose run app
Sonarqube.users
=> [#<Sonarqube::ObjectifiedHash:47231290771040 {hash: {"id"=>1, "name"=>"Administrator", "username"=>"root", ...]

To launch the specs:

docker-compose run app rake spec

With an external Sonarqube instance

First, set the variables to the correct values in the docker.env file.

Then, launch the tool:

docker-compose run app
Sonarqube.users
=> [#<Sonarqube::ObjectifiedHash:47231290771040 {hash: {"id"=>1, "name"=>"Administrator", "username"=>"root", ...]

To launch the specs,

docker-compose run app rake spec

For more information see CONTRIBUTING.md.

License

Released under the BSD 2-clause license. See LICENSE.txt for details.