Go to file
KingChan 80f07d4273 Update sonarqube.gemspec 2024-05-29 15:08:51 +08:00
.github/workflows Add coverall.io support 2020-08-25 10:14:59 -03:00
docs Merge pull request #9 from psyreactor/develop 2020-09-08 20:03:24 -03:00
lib Bump version 1.3.0 2021-11-11 22:37:21 -03:00
spec Added quality gate operations 2021-11-11 12:02:05 -08: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 Add permissions support 2020-09-03 11:02:03 -03:00
.rubocop_todo.yml First release 2020-08-22 21:54:24 -03:00
CHANGELOG.md Bump fix version 2020-09-08 20:01:03 -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 Add permissions support 2020-09-03 11:02:03 -03:00
Guardfile Add permissions support 2020-09-03 11:02:03 -03:00
LICENSE.txt First release 2020-08-22 21:54:24 -03:00
README.md Bump fix version 2020-09-08 20:01:03 -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 Update sonarqube.gemspec 2024-05-29 15:08:51 +08:00

README.md

Sonarqube

Build Status Inline docs Coverage Status Gem version License

website | documentation

Sonarqube is a Ruby wrapper and CLI for the Sonarqube API As of version 1.2.1 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"}}

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

Groups

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.