Class: SupportOps::GitLab::ProjectAccessTokens

Inherits:
Base
  • Object
show all
Defined in:
lib/support_ops_gitlab/gitlab/project_access_tokens.rb

Overview

Defines the class ProjectAccessTokens within the module SupportOps::GitLab.

Author:

  • Jason Colyer

Since:

  • 1.0.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#activate!, #approve!, attributes, #badges, #ban!, #block!, client, #client=, #commits, configure, #contributors, #create_support_pin!, #deactivate!, define_attributes, #delete!, #diffs, #disable_2fa!, #discussions, #emails, #encoded_path, #events, #find, #find!, #gpg_keys, #hard_delete!, #impersonations, #initialize, #issues, #jobs, #latest_pipeline, #members, #memberships, #merge_requests, #move!, #notes, #paid?, #pats, #pipeline_variables, #pipelines, #preferences, #projects, readonly_attributes, #reject!, #ssh_keys, #status, #store_original_attributes, #subscribe!, #support_pin, to_hash, #tokens, #unban!, #unblock!, #unsubscribe!, #webhooks

Constructor Details

This class inherits a constructor from SupportOps::GitLab::Base

Instance Attribute Details

#access_levelInteger

Role for the token; possible values: 10 (Guest), 15 (Planner), 20 (Reporter), 30 (Developer), 40 (Maintainer), and 50 (Owner)

Returns:

  • (Integer)

    the current value of access_level



26
27
28
# File 'lib/support_ops_gitlab/gitlab/project_access_tokens.rb', line 26

def access_level
  @access_level
end

#activeBoolean

If the token is active or not

Returns:

  • (Boolean)

    the current value of active



26
27
28
# File 'lib/support_ops_gitlab/gitlab/project_access_tokens.rb', line 26

def active
  @active
end

#created_atString

The timestamp the token was created at

Returns:

  • (String)

    the current value of created_at



26
27
28
# File 'lib/support_ops_gitlab/gitlab/project_access_tokens.rb', line 26

def created_at
  @created_at
end

#descriptionString

The description of the token

Returns:

  • (String)

    the current value of description



26
27
28
# File 'lib/support_ops_gitlab/gitlab/project_access_tokens.rb', line 26

def description
  @description
end

#expires_atString

Expiration date of the token in ISO format (YYYY-MM-DD).

Returns:

  • (String)

    the current value of expires_at



26
27
28
# File 'lib/support_ops_gitlab/gitlab/project_access_tokens.rb', line 26

def expires_at
  @expires_at
end

#idInteger

The ID of the token

Returns:

  • (Integer)

    the current value of id



26
27
28
# File 'lib/support_ops_gitlab/gitlab/project_access_tokens.rb', line 26

def id
  @id
end

#last_used_atString

The timestamp the token was last used

Returns:

  • (String)

    the current value of last_used_at



26
27
28
# File 'lib/support_ops_gitlab/gitlab/project_access_tokens.rb', line 26

def last_used_at
  @last_used_at
end

#nameString

The name of the token

Returns:

  • (String)

    the current value of name



26
27
28
# File 'lib/support_ops_gitlab/gitlab/project_access_tokens.rb', line 26

def name
  @name
end

#project_idInteger

The ID of the project the token is attached to

Returns:

  • (Integer)

    the current value of project_id



26
27
28
# File 'lib/support_ops_gitlab/gitlab/project_access_tokens.rb', line 26

def project_id
  @project_id
end

#revokedString

If the token is revoked or not

Returns:

  • (String)

    the current value of revoked



26
27
28
# File 'lib/support_ops_gitlab/gitlab/project_access_tokens.rb', line 26

def revoked
  @revoked
end

#scopesArray

The scopes of the token

Returns:

  • (Array)

    the current value of scopes



26
27
28
# File 'lib/support_ops_gitlab/gitlab/project_access_tokens.rb', line 26

def scopes
  @scopes
end

#tokenString

The token itself (only shows on create and rotate)

Returns:

  • (String)

    the current value of token



26
27
28
# File 'lib/support_ops_gitlab/gitlab/project_access_tokens.rb', line 26

def token
  @token
end

#user_idInteger

The user who created the token

Returns:

  • (Integer)

    the current value of user_id



26
27
28
# File 'lib/support_ops_gitlab/gitlab/project_access_tokens.rb', line 26

def user_id
  @user_id
end

Class Method Details

.list(**args) ⇒ Array

List all project access tokens

overload list(key: value)

@param created_after [String optional] If defined, returns tokens
  created after the specified time
@param created_before [String optional] If defined, returns tokens
  created before the specified time
@param expires_after [String optional] If defined, returns tokens that
  expire after the specified time
@param expires_before [String optional] If defined, returns tokens
  that expire before the specified time
@param last_used_after [String optional] If defined, returns tokens
  last used after the specified time
@param last_used_before [String optional] If defined, returns tokens
  last used before the specified time
@param project_id [Integer required] The ID of the project to use
@param revoked [Boolean optional] If true, only returns revoked tokens
@param search [String optional] If defined, returns tokens that
  include the specified value in the name
@param sort [String optional] If defined, sorts the results by the
  specified value; possible values: created_asc, created_desc,
  expires_asc, expires_desc, last_used_asc, last_used_desc, name_asc,
  name_desc
@param state [String optional] If defined, returns tokens with the
  specified state; possible values: active and inactive
@param limit [Integer optional] The limit to the number of users
  returned. Default to 0 (i.e. no limit)

#see

https://docs.gitlab.com/api/project_access_tokens/#list-all-project-access-tokens
GitLab API > Projects > Access tokens > List all project access tokens

Examples:

require 'support_ops_gitlab'

SupportOps::GitLab::Configuration.configure do |config|
  config.url = 'https://gitlab.example.com/api/v4'
  config.token = 'abc123'
end

tokens = SupportOps::GitLab::ProjectAccessTokens.list(project_id: 5, limit: 10)
pp tokens.count
# => 10
pp tokens.last.revoked
# => false

Returns:

  • (Array)

Author:

  • Jason Colyer

Since:

  • 1.0.0



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/support_ops_gitlab/gitlab/project_access_tokens.rb', line 154

def self.list(**args)
  raise 'You have to provide a project_id' unless args[:project_id]

  args[:created_after] = nil unless args[:created_after]
  args[:created_before] = nil unless args[:created_before]
  args[:expires_after] = nil unless args[:expires_after]
  args[:expires_before] = nil unless args[:expires_before]
  args[:last_used_after] = nil unless args[:last_used_after]
  args[:last_used_before] = nil unless args[:last_used_before]
  args[:revoked] = nil unless args[:revoked]
  args[:search] = nil unless args[:search]
  args[:sort] = nil unless args[:sort]
  args[:state] = nil unless args[:state]
  args[:limit] = 0 unless args[:limit]
  params = ''
  params += "created_after=#{args[:created_after]}&" unless args[:created_after].nil?
  params += "created_before=#{args[:created_before]}&" unless args[:created_before].nil?
  params += "expires_after=#{args[:expires_after]}&" unless args[:expires_after].nil?
  params += "expires_before=#{args[:expires_before]}&" unless args[:expires_before].nil?
  params += "last_used_after=#{args[:last_used_after]}&" unless args[:last_used_after].nil?
  params += "last_used_before=#{args[:last_used_before]}&" unless args[:last_used_before].nil?
  params += "revoked=#{args[:revoked]}&" unless args[:revoked].nil?
  params += "search=#{args[:search]}&" unless args[:search].nil?
  params += "sort=#{args[:sort]}&" unless args[:sort].nil?
  params += "state=#{args[:state]}&" unless args[:state].nil?
  array = []
  page = 1
  loop do
    response = client.connection.get("projects/#{args[:project_id]}/access_tokens?#{params}&page=#{page}&per_page=100")
    body = Oj.load(response.body)
    array += body.map { |p| Projects.new(p) }
    break if args[:limit].to_i.positive? && array.count >= args[:limit].to_i
    break if body.count < 100

    page += 1
  end
  return array if args[:limit].to_i.zero?

  array.first(args[:limit].to_i)
end

Instance Method Details

#revoke!Object

Note:

This is inherited from Base#revoke!

Revokes a project access token

Examples:

require 'support_ops_gitlab'

SupportOps::GitLab::Configuration.configure do |config|
  config.token = ENV.fetch('GL_TOKEN')
  config.url = 'https://gitlab.com/api/v4'
end

tokens = SupportOps::GitLab::ProjectAccessTokens.list(project_id: 123456)
tokens.last.revoke!

Returns:

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



20
# File 'lib/support_ops_gitlab/gitlab/project_access_tokens.rb', line 20

def revoke!; end

#rotate!Object

Note:

This is inherited from Base#rotate!

Rotates a project access token

Examples:

require 'support_ops_gitlab'

SupportOps::GitLab::Configuration.configure do |config|
  config.token = ENV.fetch('GL_TOKEN')
  config.url = 'https://gitlab.com/api/v4'
end

tokens = SupportOps::GitLab::ProjectAccessTokens.list(project_id: 123456)
new_token = tokens.last.rotate!
pp new_token.token
# => "glpat-def456"

Returns:

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



22
# File 'lib/support_ops_gitlab/gitlab/project_access_tokens.rb', line 22

def rotate!; end

#save!Object

Note:

This is inherited from Base#save!

Create a project access token

Examples:

require 'support_ops_gitlab'

SupportOps::GitLab::Configuration.configure do |config|
  config.token = ENV.fetch('GL_TOKEN')
  config.url = 'https://gitlab.com/api/v4'
end

new_project_access_token = SupportOps::GitLab::ProjectAccessTokens.new
new_project_access_token.name = 'Jason Test'
new_project_access_token.expires_at = '2025-05-28'
new_project_access_token.description = 'Jason Test Desc'
new_project_access_token.scopes = ['api']
new_project_access_token.project_id = 123456

new_project_access_token.save!

pp new_project_access_token.token
# => "glpat-abc123"

Returns:

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



29
# File 'lib/support_ops_gitlab/gitlab/project_access_tokens.rb', line 29

def save!; end