Class: JenkinsApi::Client::User

Inherits:
Object
  • Object
show all
Includes:
UriHelper
Defined in:
lib/improved_jenkins_client/user.rb

Overview

This class is used to communicate with Jenkins and performing some user level operations - currently limited to fetching user info, but can be extended to support updating user fields

Since:

  • 0.14.0

Instance Method Summary collapse

Methods included from UriHelper

#form_encode, #path_encode

Constructor Details

#initialize(client) ⇒ User

Initializes a new User object.

Parameters:

  • client (Client)

    the client object

Since:

  • 0.14.0



43
44
45
46
47
# File 'lib/improved_jenkins_client/user.rb', line 43

def initialize(client)
  @client = client
  @logger = @client.logger
  @timeout = @client.timeout
end

Instance Method Details

#get(user_id) ⇒ Hash

Get a single user

Examples:

Example JSON for user info

{
  "absoluteUrl" : "https://myjenkins.example.com/jenkins/user/fred",
  "description" : "",
  "fullName" : "Fred Flintstone",
  "id" : "fred",
  "property" : [
    {
    },
    {
    },
    {
      "address" : "[email protected]"
    },
    {
    },
    {
    },
    {
      "insensitiveSearch" : false
    }
  ]
}

Parameters:

  • user_id (String)

    User ID or Full Name

Returns:

  • (Hash)
    • id Jenkins user id

    • fullName Full name of user (or user id if not set)

    • other fields populated by Jenkins - this may vary based on version/plugins

Since:

  • 0.14.0



115
116
117
# File 'lib/improved_jenkins_client/user.rb', line 115

def get(user_id)
  @client.api_get_request("/user/#{path_encode user_id}")
end

#get_api_token(user_id) ⇒ Object

Since:

  • 0.14.0



119
120
121
122
123
124
125
126
127
# File 'lib/improved_jenkins_client/user.rb', line 119

def get_api_token(user_id)
  response = @client.api_get_request("/user/#{path_encode user_id}/configure", nil, '')
  html = Nokogiri::HTML(response)
  attr = html.xpath('//input[@id="apiToken"]/@value').first
  if attr.nil?
    raise Exceptions::ApiException.new(@logger, "Failed to get api token")
  end
  attr.value
end

#listHash

Get a list of users Response will include same as is available from jenkins/user/#username userid, display name, and email-address

Returns:

  • (Hash)

    of [Hash], keyed by Jenkins user id

    • fullName The jenkins user idoutput+ Console output of the job

    • properties Size of the text. This ca

Since:

  • 0.14.0



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/improved_jenkins_client/user.rb', line 64

def list
  @logger.info "Obtaining the list of users from jenkins"
  # First we need to get the list of users.
  # This is the same as "System.list_users", but since I didn't want to
  # depend on that class I reproduced the request here.
  userlist = @client.api_get_request("/asynchPeople")
  users = {}

  userlist['users'].each { |user|
    # Jenkins seems ok to fetch by full-name, as long as perfect match
    # since the name *came* from Jenkins this seems reasonably safe
    user = get(user['user']['fullName'])
    users[user['id']] = user if user
  } unless userlist.nil?

  return users
end

#to_sObject

Returns a string representation of User class.

Since:

  • 0.14.0



51
52
53
# File 'lib/improved_jenkins_client/user.rb', line 51

def to_s
  "#<JenkinsApi::Client::User>"
end