Class: Readiness::GitLab::Users

Inherits:
Client
  • Object
show all
Defined in:
lib/support_readiness/gitlab/users.rb

Overview

TODO:

Anything listed on docs.gitlab.com/ee/api/users.html not currently here

Defines the class Projects within the module Readiness::GitLab.

Author:

  • Jason Colyer

Since:

  • 1.0.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Client

auth_error, bad_request_error, convert_actions, convert_conditions, convert_standard_names_to_ids, convert_ticket_form_agent_conditions, convert_ticket_form_brands, convert_ticket_form_end_user_conditions, convert_ticket_form_names_to_ids, convert_view_names_to_ids, convert_view_restrictions, covert_ticket_form_field_ids, create_package!, erb_renderer, handle_request_error, not_found_error, not_processible_error, put_into_archive, recursively_deflate_directory, timestamp_filename, to_clean_json, to_clean_json_with_key, to_hash, to_nearly_clean_json, to_nearly_clean_json_with_key, to_param_string, write_entries

Constructor Details

#initialize(object = {}) ⇒ Users

Creates a new Readiness::GitLab::Users instance

Examples:

require 'support_readiness'
Readiness::GitLab::Users.new

Parameters:

Author:

  • Jason Colyer

Since:

  • 1.0.0



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/support_readiness/gitlab/users.rb', line 25

def initialize(object = {})
  @avatar_url = object['avatar_url']
  @bio = object['bio']
  @bot = object['bot']
  @can_create_group = object['can_create_group']
  @can_create_project = object['can_create_project']
  @color_scheme_id = object['color_scheme_id']
  @commit_email = object['commit_email']
  @confirmed_at = object['confirmed_at']
  @created_at = object['created_at']
  @created_by = object['created_by']
  @current_sign_in_at = object['current_sign_in_at']
  @current_sign_in_ip = object['current_sign_in_ip']
  @discord = object['discord']
  @email = object['email']
  @email_reset_offered_at = object['email_reset_offered_at']
  @enterprise_group_associated_at = object['enterprise_group_associated_at']
  @enterprise_group_id = object['enterprise_group_id']
  @external = object['external']
  @extra_shared_runners_minutes_limit = object['extra_shared_runners_minutes_limit']
  @followers = object['followers']
  @following = object['following']
  @highest_role = object['highest_role']
  @id = object['id']
  @identities = object['identities']
  @is_admin = object['is_admin']
  @is_auditor = object['is_auditor']
  @is_followed = object['is_followed']
  @job_title = object['job_title']
  @last_activity_on = object['last_activity_on']
  @last_sign_in_at = object['last_sign_in_at']
  @last_sign_in_ip = object['last_sign_in_ip']
  @linkedin = object['linkedin']
  @local_time = object['local_time']
  @location = object['location']
  @locked = object['locked']
  @name = object['name']
  @namespace_id = object['namespace_id']
  @note = object['note']
  @organization = object['organization']
  @plan = object['plan']
  @private_profile = object['private_profile']
  @projects_limit = object['projects_limit']
  @pronouns = object['pronouns']
  @provisioned_by_group_id = object['provisioned_by_group_id']
  @public_email = object['public_email']
  @scim_identities = object['scim_identities']
  @shared_runners_minutes_limit = object['shared_runners_minutes_limit']
  @sign_in_count = object['sign_in_count']
  @skype = object['skype']
  @state = object['state']
  @theme_id = object['theme_id']
  @trial = object['trial']
  @twitter = object['twitter']
  @two_factor_enabled = object['two_factor_enabled']
  @username = object['username']
  @using_license_seat = object['using_license_seat']
  @website_url = object['website_url']
  @web_url = object['web_url']
  @work_information = object['work_information']
end

Instance Attribute Details

#avatar_urlObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def avatar_url
  @avatar_url
end

#bioObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def bio
  @bio
end

#botObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def bot
  @bot
end

#can_create_groupObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def can_create_group
  @can_create_group
end

#can_create_projectObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def can_create_project
  @can_create_project
end

#color_scheme_idObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def color_scheme_id
  @color_scheme_id
end

#commit_emailObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def commit_email
  @commit_email
end

#confirmed_atObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def confirmed_at
  @confirmed_at
end

#created_atObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def created_at
  @created_at
end

#created_byObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def created_by
  @created_by
end

#current_sign_in_atObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def 
  @current_sign_in_at
end

#current_sign_in_ipObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def 
  @current_sign_in_ip
end

#discordObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def discord
  @discord
end

#emailObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def email
  @email
end

#email_reset_offered_atObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def email_reset_offered_at
  @email_reset_offered_at
end

#enterprise_group_associated_atObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def enterprise_group_associated_at
  @enterprise_group_associated_at
end

#enterprise_group_idObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def enterprise_group_id
  @enterprise_group_id
end

#externalObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def external
  @external
end

#extra_shared_runners_minutes_limitObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def extra_shared_runners_minutes_limit
  @extra_shared_runners_minutes_limit
end

#followersObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def followers
  @followers
end

#followingObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def following
  @following
end

#highest_roleObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def highest_role
  @highest_role
end

#idObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def id
  @id
end

#identitiesObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def identities
  @identities
end

#is_adminObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def is_admin
  @is_admin
end

#is_auditorObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def is_auditor
  @is_auditor
end

#is_followedObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def is_followed
  @is_followed
end

#job_titleObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def job_title
  @job_title
end

#last_activity_onObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def last_activity_on
  @last_activity_on
end

#last_sign_in_atObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def 
  @last_sign_in_at
end

#last_sign_in_ipObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def 
  @last_sign_in_ip
end

#linkedinObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def linkedin
  @linkedin
end

#local_timeObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def local_time
  @local_time
end

#locationObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def location
  @location
end

#lockedObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def locked
  @locked
end

#nameObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def name
  @name
end

#namespace_idObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def namespace_id
  @namespace_id
end

#noteObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def note
  @note
end

#organizationObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def organization
  @organization
end

#planObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def plan
  @plan
end

#private_profileObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def private_profile
  @private_profile
end

#projects_limitObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def projects_limit
  @projects_limit
end

#pronounsObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def pronouns
  @pronouns
end

#provisioned_by_group_idObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def provisioned_by_group_id
  @provisioned_by_group_id
end

#public_emailObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def public_email
  @public_email
end

#scim_identitiesObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def scim_identities
  @scim_identities
end

#shared_runners_minutes_limitObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def shared_runners_minutes_limit
  @shared_runners_minutes_limit
end

#sign_in_countObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def 
  @sign_in_count
end

#skypeObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def skype
  @skype
end

#stateObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def state
  @state
end

#theme_idObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def theme_id
  @theme_id
end

#trialObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def trial
  @trial
end

#twitterObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def twitter
  @twitter
end

#two_factor_enabledObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def two_factor_enabled
  @two_factor_enabled
end

#usernameObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def username
  @username
end

#using_license_seatObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def using_license_seat
  @using_license_seat
end

#web_urlObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def web_url
  @web_url
end

#website_urlObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def website_url
  @website_url
end

#work_informationObject

Since:

  • 1.0.0



14
15
16
# File 'lib/support_readiness/gitlab/users.rb', line 14

def work_information
  @work_information
end

Class Method Details

.block!(client, user) ⇒ Boolean

Blocks a user in GitLab. This will exit on error

Examples:

require 'support_readiness'
config = Readiness::GitLab::Configuration.new
config.token = 'test123abc'
client = Readiness::GitLab::Client.new(config)
user = Readiness::GitLab::Users.find!(client, 2672834)
block = Readiness::GitLab::Users.block!(client, user)
pp block
# => true

Parameters:

Returns:

  • (Boolean)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



457
458
459
460
461
# File 'lib/support_readiness/gitlab/users.rb', line 457

def self.block!(client, user)
  response = client.connection.post "users/#{user.id}/block"
  handle_request_error(1, 'GitLab', response.status, { action: 'Block a user', id: user.id }) unless response.status == 201
  true
end

.create_token!(client, user, params) ⇒ Object

Creates a personal access token for a user within GitLab. This will exit on error

Examples:

require 'support_readiness'
config = Readiness::GitLab::Configuration.new
config.token = 'test123abc'
client = Readiness::GitLab::Client.new(config)
user = Readiness::GitLab::Users.find!(client, 2672834)
token_params = {
  name: 'Token for updating submodule of test_user123456/awesome_project',
  expires_at: '2024-10-13',
  scopes: [
    'api'
  ]
}
token = Readiness::GitLab::Users.create_token!(client, user, token_params)
pp token['token']
# => "s3cr3t1"

Parameters:

  • client (Object)

    An instance of Client

  • user (Object)

    An instance of Readiness::GitLab::Users

  • params (Hash)

    The parameters to create the token with. At a minimum needs { name: ‘xxx’ }.

Returns:

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



385
386
387
388
389
# File 'lib/support_readiness/gitlab/users.rb', line 385

def self.create_token!(client, user, params)
  response = client.connection.post "users/#{user.id}/personal_access_tokens", params.to_json
  handle_request_error(1, 'GitLab', response.status, { action: 'Create PAT for a user', id: "#{user.id}" }) unless response.status == 200
  Oj.load(response.body)
end

.emails(client, user) ⇒ Array

Fetches the email addresses for a user

Examples:

require 'support_readiness'
config = Readiness::GitLab::Configuration.new
config.token = 'test123abc'
client = Readiness::GitLab::Client.new(config)
user = Readiness::GitLab::Users.find!(client, 2672834)
emails = Readiness::GitLab::Users.emails(client, user)
pp emaiks.first['email']
# => 'alice+test@example.com'

Parameters:

Returns:

  • (Array)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.3



505
506
507
508
509
# File 'lib/support_readiness/gitlab/users.rb', line 505

def self.emails(client, user)
  response = client.connection.get "users/#{user.id}/emails"
  handle_request_error(0, 'GitLab', response.status) unless response.status == 200
  Oj.load(response.body)
end

.find(client, uid) ⇒ Hash

Locates a user within GitLab. This will not exit on error (except Authentication errors)

Examples:

require 'support_readiness'
config = Readiness::GitLab::Configuration.new
config.token = 'test123abc'
client = Readiness::GitLab::Client.new(config)
user = Readiness::GitLab::Users.find(client, 123456)
pp user.username
# => 'test_user123456'

Parameters:

  • client (Object)

    An instance of Client

  • uid (Integer)

    The user ID to find

Returns:

  • (Hash)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



104
105
106
107
108
109
110
# File 'lib/support_readiness/gitlab/users.rb', line 104

def self.find(client, uid)
  response = client.connection.get "users/#{uid}"
  handle_request_error(0, 'GitLab', response.status,  { action: 'get', id: uid }) unless response.status == 200
  return Users.new(Oj.load(response.body)) if response.status == 200

  Oj.load(response.body)
end

.find!(client, uid, attributes = []) ⇒ Object

Locates a user within GitLab. This will exit on error

Examples:

require 'support_readiness'
config = Readiness::GitLab::Configuration.new
config.token = 'test123abc'
client = Readiness::GitLab::Client.new(config)
user = Readiness::GitLab::Users.find!(client, 123456)
pp user.username
# => 'test_user123456'

Parameters:

  • client (Object)

    An instance of Client

  • uid (Integer)

    The user ID to find

Returns:

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



129
130
131
132
133
# File 'lib/support_readiness/gitlab/users.rb', line 129

def self.find!(client, uid, attributes = [])
  response = client.connection.get "users/#{uid}"
  handle_request_error(1, 'GitLab', response.status, { action: 'Find user', id: uid }) unless response.status == 200
  Users.new(Oj.load(response.body))
end

.gpg_keys(client, user) ⇒ Array

Lists SSH keys for a user.

Examples:

require 'support_readiness'
config = Readiness::GitLab::Configuration.new
config.token = 'test123abc'
client = Readiness::GitLab::Client.new(config)
user = Readiness::GitLab::Users.find!(client, 2672834)
keys = Readiness::GitLab::Users.gpg_keys(client, user)
pp keys.count
# => 1

Parameters:

Returns:

  • (Array)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



318
319
320
321
322
# File 'lib/support_readiness/gitlab/users.rb', line 318

def self.gpg_keys(client, user)
  response = client.connection.get "users/#{user.id}/gpg_keys"
  handle_request_error(0, 'GitLab', response.status) unless response.status == 200
  Oj.load(response.body)
end

.memberships(client, user, params = []) ⇒ Array

Lists all memberships for a user. This method can take a long time to run depending on the parameters used.

Examples:

require 'support_readiness'
config = Readiness::GitLab::Configuration.new
config.token = 'test123abc'
client = Readiness::GitLab::Client.new(config)
user = Readiness::GitLab::Users.find!(client, 123456)
memberships = Readiness::GitLab::Users.memberships(client, user)
pp memberships.select { |m| m['access_level'] == 50 }.count
# => 28

Parameters:

  • client (Object)

    An instance of Client

  • user (Object)

    An instance of Readiness::GitLab::Users

  • params (Array) (defaults to: [])

    An array of filter Strings to use. Should be in the format of key=value

Returns:

  • (Array)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/support_readiness/gitlab/users.rb', line 226

def self.memberships(client, user, params = [])
  array = []
  page = 1
  loop do
    response = client.connection.get "users/#{user.id}/memberships?per_page=100&page=#{page}&#{to_param_string(params)}"
    handle_request_error(0, 'GitLab', response.status) unless response.status == 200
    body = Oj.load(response.body)
    array += body
    break if body.count < 100

    page += 1
  end
  array
end

.only_maintainer?(client, user) ⇒ Boolean

Determines if a user is the only maintainer on projects they are within

Examples:

require 'support_readiness'
config = Readiness::GitLab::Configuration.new
config.token = 'test123abc'
client = Readiness::GitLab::Client.new(config)
user = Readiness::GitLab::Users.find!(client, 5881867)
pp Readiness::GitLab::Users.only_maintainer?(client, user)
# => true

Parameters:

Returns:

  • (Boolean)

Author:

  • Jason Colyer

Since:

  • 1.0.3



527
528
529
530
531
532
533
# File 'lib/support_readiness/gitlab/users.rb', line 527

def self.only_maintainer?(client, user)
  Users.projects(client, user).each do |p|
    members = Projects.all_members(client, p)
    return true if members = members.select { |m| m['access_level'] >= 40 && m['username'] != user.username }.count.zero?
  end
  false
end

.projects(client, user, filters = []) ⇒ Array

Lists all user projects. This method can take a long time to run depending on the parameters used.

Examples:

require 'support_readiness'
config = Readiness::GitLab::Configuration.new
config.token = 'test123abc'
client = Readiness::GitLab::Client.new(config)
user = Readiness::GitLab::Users.find!(client, 123456)
memberships = Readiness::GitLab::Users.projects(client, user)
pp projects.first.web_url
# => "https://gitlab.com/test_user123456/awesome_project"

Parameters:

  • client (Object)

    An instance of Client

  • user (Object)

    An instance of Readiness::GitLab::Users

  • filters (Array) (defaults to: [])

    An array of filter Strings to use. Should be in the format of key=value

Returns:

  • (Array)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/support_readiness/gitlab/users.rb', line 261

def self.projects(client, user, filters = [])
  array = []
  page = 1
  loop do
    response = client.connection.get "users/#{user.id}/projects?per_page=100&page=#{page}&#{to_param_string(filters)}"
    handle_request_error(0, 'GitLab', response.status) unless response.status == 200
    body = Oj.load(response.body)
    array += body.map { |p| Projects.new(p) }
    break if body.count < 100

    page += 1
  end
  array
end

.revoke_token!(client, tid) ⇒ Boolean

Revokes a personal access token within GitLab. This will exit on error

Examples:

require 'support_readiness'
config = Readiness::GitLab::Configuration.new
config.token = 'test123abc'
client = Readiness::GitLab::Client.new(config)
user = Readiness::GitLab::Users.find!(client, 2672834)
token = Readiness::GitLab::Users.revoke_token!(client, 123456)
pp token
# => true

Parameters:

  • client (Object)

    An instance of Client

  • tid (Integer)

    The personal access token ID

Returns:

  • (Boolean)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



409
410
411
412
413
# File 'lib/support_readiness/gitlab/users.rb', line 409

def self.revoke_token!(client, tid)
  response = client.connection.delete "personal_access_tokens/#{tid}"
  handle_request_error(1, 'GitLab', response.status, { action: 'Revoke PAT', id: tid }) unless response.status == 204
  true
end

.rotate_token!(client, tid) ⇒ Hash

Rotates a personal access token (revokes it and makes a new one with the same user). This will exit on error

Examples:

require 'support_readiness'
config = Readiness::GitLab::Configuration.new
config.token = 'test123abc'
client = Readiness::GitLab::Client.new(config)
user = Readiness::GitLab::Users.find!(client, 2672834)
token = Readiness::GitLab::Users.rotate_token!(client, 123456)
pp token['token']
# => "s3cr3t2"

Parameters:

  • client (Object)

    An instance of Client

  • tid (Integer)

    The personal access token ID

Returns:

  • (Hash)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



433
434
435
436
437
# File 'lib/support_readiness/gitlab/users.rb', line 433

def self.rotate_token!(client, tid)
  response = client.connection.post "personal_access_tokens/#{tid}/rotate"
  handle_request_error(1, 'GitLab', response.status, { action: 'Rotate PAT', id: tid }) unless response.status == 200
  Oj.load(response.body)
end

.search_by_email(client, email) ⇒ Array

Locates the first 20 users matching a email within GitLab.

Examples:

require 'support_readiness'
config = Readiness::GitLab::Configuration.new
config.token = 'test123abc'
client = Readiness::GitLab::Client.new(config)
users = Readiness::GitLab::Users.search_by_email(client, 'test_user123456@example.com')
pp users.first.name
# => "test_user_123456"

Parameters:

  • client (Object)

    An instance of Client

  • email (String)

    The email to look for

Returns:

  • (Array)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



175
176
177
178
179
# File 'lib/support_readiness/gitlab/users.rb', line 175

def self.search_by_email(client, email)
  response = client.connection.get "users?search=#{CGI.escape(email)}"
  handle_request_error(0, 'GitLab', response.status, { action: 'Find user by email', id: email }) unless response.status == 200
  Oj.load(response.body).map { |u| Users.new(u) }
end

.search_by_username(client, username) ⇒ Array

Locates the first 20 users matching a username within GitLab.

Examples:

require 'support_readiness'
config = Readiness::GitLab::Configuration.new
config.token = 'test123abc'
client = Readiness::GitLab::Client.new(config)
users = Readiness::GitLab::Users.search_by_username(client, 'test_user123456')
pp users.first.id
# => 123456

Parameters:

  • client (Object)

    An instance of Client

  • username (String)

    The username to look for

Returns:

  • (Array)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



152
153
154
155
156
# File 'lib/support_readiness/gitlab/users.rb', line 152

def self.search_by_username(client, username)
  response = client.connection.get "users?username=#{username}"
  handle_request_error(0, 'GitLab', response.status, { action: 'Find user by username', id: username }) unless response.status == 200
  Oj.load(response.body).map { |u| Users.new(u) }
end

.ssh_keys(client, user) ⇒ Array

Lists SSH keys for a user.

Examples:

require 'support_readiness'
config = Readiness::GitLab::Configuration.new
config.token = 'test123abc'
client = Readiness::GitLab::Client.new(config)
user = Readiness::GitLab::Users.find!(client, 2672834)
keys = Readiness::GitLab::Users.ssh_keys(client, user)
pp keys.last['title']
# => "Linux Laptop"

Parameters:

Returns:

  • (Array)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



294
295
296
297
298
# File 'lib/support_readiness/gitlab/users.rb', line 294

def self.ssh_keys(client, user)
  response = client.connection.get "users/#{user.id}/keys"
  handle_request_error(0, 'GitLab', response.status) unless response.status == 200
  Oj.load(response.body)
end

.tokens(client, user, filters = []) ⇒ Array

Lists all personal access tokens for a user. This method can take a long time to run depending on the parameters used.

Examples:

require 'support_readiness'
config = Readiness::GitLab::Configuration.new
config.token = 'test123abc'
client = Readiness::GitLab::Client.new(config)
user = Readiness::GitLab::Users.find!(client, 2672834)
tokens = Readiness::GitLab::Users.tokens(client, user)
pp tokens.select { |t| Date.parse(t['expires_at']) <= (Date.today + 7.days) }.count
# => 3

Parameters:

  • client (Object)

    An instance of Client

  • user (Object)

    An instance of Readiness::GitLab::Users

  • filters (Array) (defaults to: [])

    An array of filter Strings to use. Should be in the format of key=value

Returns:

  • (Array)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/support_readiness/gitlab/users.rb', line 344

def self.tokens(client, user, filters = [])
  array = []
  page = 1
  loop do
    response = client.connection.get "personal_access_tokens?user_id=#{user.id}&per_page=100&page=#{page}&#{to_param_string(filters)}"
    handle_request_error(0, 'GitLab', response.status) unless response.status == 200
    body = Oj.load(response.body)
    array += body
    break if body.count < 100

    page += 1
  end
  array
end

.unblock!(client, user) ⇒ Boolean

Unblocks a user in GitLab. This will exit on error

Examples:

require 'support_readiness'
config = Readiness::GitLab::Configuration.new
config.token = 'test123abc'
client = Readiness::GitLab::Client.new(config)
user = Readiness::GitLab::Users.find!(client, 2672834)
block = Readiness::GitLab::Users.unblock!(client, user)
pp block
# => true

Parameters:

Returns:

  • (Boolean)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



481
482
483
484
485
# File 'lib/support_readiness/gitlab/users.rb', line 481

def self.unblock!(client, user)
  response = client.connection.post "users/#{user.id}/unblock"
  handle_request_error(1, 'GitLab', response.status, { action: 'Unblock a user', id: user.id }) unless response.status == 201
  true
end

.update!(client, user) ⇒ Object

Updates a user within GitLab. This will exit on error

Examples:

require 'support_readiness'
config = Readiness::GitLab::Configuration.new
config.token = 'test123abc'
client = Readiness::GitLab::Client.new(config)
user = Readiness::GitLab::Users.find!(client, 123456)
user.name = 'Test User 123456'
update = Readiness::GitLab::Users.update!(client, user)
pp update.name
# => "Test User 123456"

Parameters:

Returns:

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



200
201
202
203
204
# File 'lib/support_readiness/gitlab/users.rb', line 200

def self.update!(client, user)
  response = client.connection.put "users/#{user.id}", to_clean_json(user)
  handle_request_error(1, 'GitLab', response.status, { action: 'Update an user', id: "#{user.id}" }) unless response.status == 200
  Users.new(Oj.load(response.body))
end