Add user_token api support

This commit is contained in:
Mariani Lucas 2020-08-24 21:41:02 -03:00
parent 9da35de5a5
commit 5b08c2647f
10 changed files with 169 additions and 70 deletions

View File

@ -4,6 +4,14 @@
Please see: https://github.com/psyreactor/sonarqube-ruby/releases
### 1.1.0 (24/08/2020)
- New features
* Added support for users_token API
- Fix
* Fix Readme
* Add site documentation
### 1.0.2 (24/08/2020)
- Fix

View File

@ -88,28 +88,6 @@ project.to_hash
# => {"project"=>{"key"=>"new_project", "name"=>"new_project", "qualifier"=>"TRK", "visibility"=>"public"}}
```
#### Delete Project
```ruby
project = Sonarqube.project_delete('test')
# => #<Sonarqube::ObjectifiedHash:46220 {hash: {}}
project.to_hash.empty?
# => true
```
#### Search Project
```ruby
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
@ -122,56 +100,9 @@ user.user.name
# name_user
```
#### Delete User
```ruby
user = Sonarqube.user_delete('test')
# => #<Sonarqube::ObjectifiedHash:46220 {hash: {}}
user.to_hash.empty?
# => true
```
#### Search User
```ruby
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
```ruby
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
```ruby
group = Sonarqube.group_delete('New-Group')
# => #<Sonarqube::ObjectifiedHash:46220 {hash: {}}
group.to_hash.empty?
# => true
```
#### Search Group
```ruby
groups = Sonarqube.search_groups({ q: 'sonar-users' })

View File

@ -0,0 +1,47 @@
---
layout: default
title: Tokens
nav_order: 4
parent: Client
---
<details close markdown="block">
<summary>
Table of contents
</summary>
{: .text-delta }
1. TOC
{:toc}
</details>
# Tokens
## Create Token
```ruby
user = Sonarqube.token_create('token_name', 'login_user')
# => #<Sonarqube::ObjectifiedHash:46100 {hash: {"login"=>"login_name", "name"=>"token_name", "token"=>"c45bc5855603e851904f7ae5357e667b286a9fc1", "createdAt"=>"2020-08-24T23:56:41+0000"}}
user.token
# c45bc5855603e851904f7ae5357e667b286a9fc1
user.name
# token_name
```
## Revoke Token
```ruby
user = Sonarqube.token_revoke('token_name', 'login_user')
# => #<Sonarqube::ObjectifiedHash:46220 {hash: {}}
user.to_hash.empty?
# => true
```
## List Tokens
```ruby
users = Sonarqube.tokens_list('login_user')
# => #<Sonarqube::ObjectifiedHash:46080 {hash: {"login"=>"login_user", "userTokens"=>[{"name"=>"token_service", "createdAt"=>"2020-08-24T23:56:41+0000"}]}}
puts "login: #{user.login}"
# login: login_user
users.userTokens.each do | token |
puts "name: #{token.name}"
end
# name: token_service
```

View File

@ -8,6 +8,7 @@ module Sonarqube
# Please keep in alphabetical order
include Groups
include Projects
include Tokens
include Users
# Text representation of the client, masking private token.

View File

@ -0,0 +1,57 @@
# frozen_string_literal: true
class Sonarqube::Client
# Defines methods related to tokens.
# @see https://SONAR_URL/web_api/api/user_tokens
module Tokens
# Create token for user.
#
# @example
# Sonarqube.create_token('name_token')
# Sonarqube.create_token('name_token', 'login_name')
#
# @param [String] name (required) Token name.
# @param [String] login (optional) Login user name.
# @return [Sonarqube::ObjectifiedHash]
def create_token(name, login = nil)
raise ArgumentError, 'Missing required parameters' if name.nil?
body = { name: name }
body = { login: login }.merge!(body) unless login.nil?
post('/api/user_tokens/generate', body: body)
end
alias token_create create_token
# Revoke a token.
#
# @example
# Sonarqube.revoke_token('new-group')
# Sonarqube.revoke_token('sonarqube', { description: 'New Sonarqube project' })
#
# @param [String] name (required) Token name.
# @param [String] login (optional) Login user name.
# @return [Sonarqube::ObjectifiedHash] Empty hash response.
def revoke_token(name, login = nil)
raise ArgumentError, 'Missing required parameters' if name.nil?
body = { name: name }
body = { login: login }.merge!(body) unless login.nil?
post('/api/user_tokens/revoke', body: body)
end
alias token_revoke revoke_token
# List token for user.
#
# @example
# Sonarqube.list_token('login')
#
# @param [String] login (required) The login name of user
# @return [Sonarqube::ObjectifiedHash].
def list_tokens(login)
raise ArgumentError, 'Missing required parameters' if login.nil?
get('/api/user_tokens/search', query: { login: login })
end
alias tokens_list list_tokens
end
end

View File

@ -1,5 +1,5 @@
# frozen_string_literal: true
module Sonarqube
VERSION = '1.0.2'
VERSION = '1.0.3'
end

1
spec/fixtures/token_create.json vendored Normal file
View File

@ -0,0 +1 @@
{"login":"login_name","name":"token_name","token":"c45bc5855603e851904f7ae5357e667b286a9fc1","createdAt":"2020-08-24T23:56:41+0000"}

1
spec/fixtures/token_revoke.json vendored Normal file
View File

@ -0,0 +1 @@
{}

1
spec/fixtures/tokens_list.json vendored Normal file
View File

@ -0,0 +1 @@
{"login":"login_name","userTokens":[{"name":"token_service","createdAt":"2020-08-24T23:56:41+0000"}]}

View File

@ -0,0 +1,52 @@
# frozen_string_literal: true
require 'spec_helper'
describe Sonarqube::Client do
describe '.token_create' do
before do
stub_post('/api/user_tokens/generate', 'token_create')
@users = Sonarqube.token_create('token_name', 'login_name')
end
it 'gets the correct resource' do
expect(a_post('/api/user_tokens/generate')).to have_been_made
end
it 'returns a token users info' do
expect(@users).to be_a Sonarqube::ObjectifiedHash
expect(@users.name).to eq('token_name')
end
end
describe '.token_revoke' do
before do
stub_post('/api/user_tokens/revoke', 'token_revoke')
@user = Sonarqube.token_revoke('name_token', 'login_name')
end
it 'gets the correct resource' do
expect(a_post('/api/user_tokens/revoke')).to have_been_made
end
it 'returns information about a created token' do
expect(@user.to_hash).to be_empty
end
end
describe '.list_tokens' do
before do
stub_get('/api/user_tokens/search', 'tokens_list').with(query: { login: 'login_name' })
@result = Sonarqube.list_tokens('login_name')
end
it 'gets the correct resource' do
expect(a_get('/api/user_tokens/search').with(query: { login: 'login_name' })).to have_been_made
end
it 'returns array of tokens' do
expect(@result.login).to eq('login_name')
expect(@result.userTokens.first.name).to eq('token_service')
end
end
end