gitlab-customer-support-operations_gitlab 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. checksums.yaml +7 -0
  2. data/lib/support_ops_gitlab/gitlab/badges.rb +229 -0
  3. data/lib/support_ops_gitlab/gitlab/base.rb +552 -0
  4. data/lib/support_ops_gitlab/gitlab/client.rb +51 -0
  5. data/lib/support_ops_gitlab/gitlab/commits.rb +198 -0
  6. data/lib/support_ops_gitlab/gitlab/configuration.rb +86 -0
  7. data/lib/support_ops_gitlab/gitlab/epics.rb +325 -0
  8. data/lib/support_ops_gitlab/gitlab/events.rb +167 -0
  9. data/lib/support_ops_gitlab/gitlab/gpg_keys.rb +64 -0
  10. data/lib/support_ops_gitlab/gitlab/group_memberships.rb +33 -0
  11. data/lib/support_ops_gitlab/gitlab/groups.rb +431 -0
  12. data/lib/support_ops_gitlab/gitlab/invitations.rb +72 -0
  13. data/lib/support_ops_gitlab/gitlab/issues.rb +606 -0
  14. data/lib/support_ops_gitlab/gitlab/jobs.rb +61 -0
  15. data/lib/support_ops_gitlab/gitlab/markdown.rb +54 -0
  16. data/lib/support_ops_gitlab/gitlab/merge_requests.rb +411 -0
  17. data/lib/support_ops_gitlab/gitlab/milestones.rb +195 -0
  18. data/lib/support_ops_gitlab/gitlab/namespaces.rb +184 -0
  19. data/lib/support_ops_gitlab/gitlab/notes.rb +182 -0
  20. data/lib/support_ops_gitlab/gitlab/pipelines.rb +258 -0
  21. data/lib/support_ops_gitlab/gitlab/project_access_tokens.rb +245 -0
  22. data/lib/support_ops_gitlab/gitlab/project_memberships.rb +33 -0
  23. data/lib/support_ops_gitlab/gitlab/project_webhook_events.rb +33 -0
  24. data/lib/support_ops_gitlab/gitlab/project_webhooks.rb +218 -0
  25. data/lib/support_ops_gitlab/gitlab/projects.rb +741 -0
  26. data/lib/support_ops_gitlab/gitlab/repository_files.rb +102 -0
  27. data/lib/support_ops_gitlab/gitlab/repository_submodules.rb +78 -0
  28. data/lib/support_ops_gitlab/gitlab/ssh_keys.rb +67 -0
  29. data/lib/support_ops_gitlab/gitlab/user_emails.rb +147 -0
  30. data/lib/support_ops_gitlab/gitlab/user_memberships.rb +21 -0
  31. data/lib/support_ops_gitlab/gitlab/user_tokens.rb +344 -0
  32. data/lib/support_ops_gitlab/gitlab/users.rb +1059 -0
  33. data/lib/support_ops_gitlab/gitlab.rb +45 -0
  34. data/lib/support_ops_gitlab.rb +28 -0
  35. metadata +251 -0
@@ -0,0 +1,344 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Defines the module SupportOps.
4
+ module SupportOps
5
+ # Defines the module GitLab
6
+ module GitLab
7
+ ##
8
+ # Defines the class UserTokens within the module {SupportOps::GitLab}.
9
+ #
10
+ # @author Jason Colyer
11
+ # @since 1.0.0
12
+ # @attr [Boolean] active If the token is active or not
13
+ # @attr [String] created_at Timestamp when the token was created
14
+ # @attr [String] description Description of token
15
+ # @attr [String] expires_at Expiration date of the token in ISO format (YYYY-MM-DD)
16
+ # @attr [Integer] id ID of token
17
+ # @attr [Boolean] impersonation If the token is an impersonation token
18
+ # @attr [String] name Name of token
19
+ # @attr [Boolean] revoked If the token is revoked or not
20
+ # @attr [Array] scopes Array of approved scopes
21
+ # @attr [String] token The token itself
22
+ # @attr [Integer] user_id ID of user account
23
+ # @todo Get details on a personal access token => https://docs.gitlab.com/api/personal_access_tokens/#get-details-on-a-personal-access-token
24
+ # @todo List all token associations => https://docs.gitlab.com/api/personal_access_tokens/#list-all-token-associations
25
+ # @todo Get an impersonation token for a user => https://docs.gitlab.com/api/user_tokens/#get-an-impersonation-token-for-a-user
26
+ class UserTokens < SupportOps::GitLab::Base
27
+ # @!parse
28
+ # # Creates a user token (personal access or impersonation)
29
+ # #
30
+ # # @author Jason Colyer
31
+ # # @since 1.0.0
32
+ # # @return [Object] Instance of {SupportOps::GitLab::UserTokens}
33
+ # # @note This is inherited from {SupportOps::GitLab::Base#save!}
34
+ # # @see
35
+ # # https://docs.gitlab.com/api/user_tokens/#create-a-personal-access-token-for-a-user
36
+ # # GitLab API > User tokens > Create a personal access token for a user
37
+ # # @see
38
+ # # https://docs.gitlab.com/api/user_tokens/#create-a-personal-access-token
39
+ # # GitLab API > User tokenss > Create a personal access token
40
+ # # @see
41
+ # # https://docs.gitlab.com/api/user_tokens/#create-an-impersonation-token
42
+ # # GitLab API > User tokens > Create an impersonation token
43
+ # # @example
44
+ # # require 'support_ops_gitlab'
45
+ # #
46
+ # # SupportOps::GitLab::Configuration.configure do |config|
47
+ # # config.token = ENV.fetch('GL_TOKEN')
48
+ # # config.url = 'https://gitlab.com/api/v4'
49
+ # # end
50
+ # #
51
+ # # user = SupportOps::GitLab::Users.get!(123456)
52
+ # # new_personal_access_token = SupportOps::GitLab::UserTokens.new
53
+ # # new_personal_access_token.name = 'Jason Test'
54
+ # # new_personal_access_token.expires_at = '2025-05-28'
55
+ # # new_personal_access_token.description = 'Jason Test Desc'
56
+ # # new_personal_access_token.scopes = ['api']
57
+ # # new_personal_access_token.user_id = user.id
58
+ # #
59
+ # # new_personal_access_token.save!
60
+ # #
61
+ # # pp new_personal_access_token.token
62
+ # # # => "glpat-abc123"
63
+ # # @example
64
+ # # require 'support_ops_gitlab'
65
+ # #
66
+ # # SupportOps::GitLab::Configuration.configure do |config|
67
+ # # config.token = ENV.fetch('GL_TOKEN')
68
+ # # config.url = 'https://gitlab.com/api/v4'
69
+ # # end
70
+ # #
71
+ # # new_personal_access_token = SupportOps::GitLab::UserTokens.new
72
+ # # new_personal_access_token.name = 'Jason Test'
73
+ # # new_personal_access_token.expires_at = '2025-05-28'
74
+ # # new_personal_access_token.description = 'Jason Test Desc'
75
+ # # new_personal_access_token.scopes = ['k8s_proxy']
76
+ # #
77
+ # # new_personal_access_token.save!
78
+ # #
79
+ # # pp new_personal_access_token.token
80
+ # # # => "glpat-def456"
81
+ # # @example
82
+ # # require 'support_ops_gitlab'
83
+ # #
84
+ # # SupportOps::GitLab::Configuration.configure do |config|
85
+ # # config.token = ENV.fetch('GL_TOKEN')
86
+ # # config.url = 'https://gitlab.com/api/v4'
87
+ # # end
88
+ # #
89
+ # # user = SupportOps::GitLab::Users.get!(123456)
90
+ # # new_impersonation_token = SupportOps::GitLab::UserTokens.new
91
+ # # new_impersonation_token.name = 'Jason Test'
92
+ # # new_impersonation_token.expires_at = '2025-05-28'
93
+ # # new_impersonation_token.description = 'Jason Test Desc'
94
+ # # new_impersonation_token.scopes = ['api']
95
+ # # new_impersonation_token.user_id = user.id
96
+ # # new_impersonation_token.impersonation = true
97
+ # #
98
+ # # new_impersonation_token.save!
99
+ # #
100
+ # # pp new_impersonation_token.token
101
+ # # # => "glpat-ghi789"
102
+ # def save!; end
103
+ # @!parse
104
+ # # Rotates a personal access token (does not work on impersonation tokens)
105
+ # #
106
+ # # @author Jason Colyer
107
+ # # @since 1.0.0
108
+ # # @return [Object] Instance of {SupportOps::GitLab::UserTokens}
109
+ # # @note This is inherited from {SupportOps::GitLab::Base#rotate!}
110
+ # # @see
111
+ # # https://docs.gitlab.com/api/personal_access_tokens/#rotate-a-personal-access-token
112
+ # # GitLab API > Personal access tokens > Rotate a personal access token
113
+ # # @example
114
+ # # require 'support_ops_gitlab'
115
+ # #
116
+ # # SupportOps::GitLab::Configuration.configure do |config|
117
+ # # config.token = ENV.fetch('GL_TOKEN')
118
+ # # config.url = 'https://gitlab.com/api/v4'
119
+ # # end
120
+ # #
121
+ # # user = SupportOps::GitLab::Users.get!(123456)
122
+ # # tokens = users.pats
123
+ # # new_token = tokens.last.rotate!
124
+ # # pp new_token.token
125
+ # # # => "glpat-def456"
126
+ # def rotate!; end
127
+ # @!parse
128
+ # # Revokes a token (personal access or impersonation)
129
+ # #
130
+ # # @author Jason Colyer
131
+ # # @since 1.0.0
132
+ # # @return [Object] Instance of {SupportOps::GitLab::UserTokens}
133
+ # # @note This is inherited from {SupportOps::GitLab::Base#revoke!}
134
+ # # @see
135
+ # # https://docs.gitlab.com/api/personal_access_tokens/#revoke-a-personal-access-token
136
+ # # GitLab API > Personal access tokens > Revoke a personal access token
137
+ # # @see
138
+ # # https://docs.gitlab.com/api/user_tokens/#revoke-an-impersonation-token
139
+ # # GitLab API > User tokens > Revoke an impersonation token
140
+ # # @example
141
+ # # require 'support_ops_gitlab'
142
+ # #
143
+ # # SupportOps::GitLab::Configuration.configure do |config|
144
+ # # config.token = ENV.fetch('GL_TOKEN')
145
+ # # config.url = 'https://gitlab.com/api/v4'
146
+ # # end
147
+ # #
148
+ # # user = SupportOps::GitLab::Users.get!(123456)
149
+ # # tokens = users.pats
150
+ # # tokens.last.revoke!
151
+ # def revoke!; end
152
+ define_attributes :active, :created_at, :description, :expires_at, :id,
153
+ :impersonation, :name, :revoked, :scopes, :token,
154
+ :user_id
155
+ readonly_attributes :active, :created_at, :id, :impersonation, :revoked,
156
+ :token, :user_id
157
+
158
+ ##
159
+ # List all personal access tokens
160
+ #
161
+ # @author Jason Colyer
162
+ # @since 1.0.0
163
+ # @overload list(key: value)
164
+ # @param created_after [String optional] If defined, returns tokens
165
+ # created after the specified time (ISO 8601)
166
+ # @param created_before [String optional] If defined, returns tokens
167
+ # created before the specified time (ISO 8601)
168
+ # @param expires_after [String optional] If defined, returns tokens that
169
+ # expire after the specified time (ISO 8601)
170
+ # @param expires_before [String optional] If defined, returns tokens
171
+ # that expire before the specified time (ISO 8601)
172
+ # @param last_used_after [String optional] If defined, returns tokens
173
+ # last used after the specified time (ISO 8601)
174
+ # @param last_used_before [String optional] If defined, returns tokens
175
+ # last used before the specified time (ISO 8601)
176
+ # @param revoked [Boolean optional] If true, only returns revoked tokens
177
+ # @param search [String optional] If defined, returns tokens that
178
+ # include the specified value in the name
179
+ # @param state [String optional] if defined, returns tokens with the
180
+ # specified statel possible values: active and inactive
181
+ # @param user_id [Integer optional] if defined, returns tokens owned by
182
+ # the specified user
183
+ # @return [Array]
184
+ # @see
185
+ # https://docs.gitlab.com/api/personal_access_tokens/#list-all-personal-access-tokens
186
+ # GitLab API > Personal access tokens > List all personal access tokens
187
+ # @see SupportOps::GitLab::Configuration Setting up a client
188
+ # @example
189
+ # require 'support_ops_gitlab'
190
+ #
191
+ # SupportOps::GitLab::Configuration.configure do |config|
192
+ # config.url = 'https://gitlab.example.com/api/v4'
193
+ # config.token = 'abc123'
194
+ # end
195
+ #
196
+ # pats = SupportOps::GitLab::UserTokens.list_paths(revoked: false)
197
+ # pp pats.count
198
+ # # => 2
199
+ # pp pats.last.expires_at
200
+ # # => "2025-08-15"
201
+ def self.list_pats(**args)
202
+ args[:created_after] = nil unless args[:created_after]
203
+ args[:created_before] = nil unless args[:created_before]
204
+ args[:expires_after] = nil unless args[:expires_after]
205
+ args[:expires_before] = nil unless args[:expires_before]
206
+ args[:last_used_after] = nil unless args[:last_used_after]
207
+ args[:last_used_before] = nil unless args[:last_used_before]
208
+ args[:revoked] = nil unless args[:revoked]
209
+ args[:search] = nil unless args[:search]
210
+ args[:state] = nil unless args[:state]
211
+ args[:user_id] = nil unless args[:user_id]
212
+ params = ''
213
+ params += "created_after=#{args[:created_after]}&" unless args[:created_after].nil?
214
+ params += "created_before=#{args[:created_before]}&" unless args[:created_before].nil?
215
+ params += "expires_after=#{args[:expires_after]}&" unless args[:expires_after].nil?
216
+ params += "expires_before=#{args[:expires_before]}&" unless args[:expires_before].nil?
217
+ params += "last_used_after=#{args[:last_used_after]}&" unless args[:last_used_after].nil?
218
+ params += "last_used_before=#{args[:last_used_before]}&" unless args[:last_used_before].nil?
219
+ params += "revoked=#{args[:revoked]}&" unless args[:revoked].nil?
220
+ params += "search=#{args[:search]}&" unless args[:search].nil?
221
+ params += "state=#{args[:state]}&" unless args[:state].nil?
222
+ params += "user_id=#{args[:user_id]}&" unless args[:user_id].nil?
223
+ tokens = []
224
+ page = 1
225
+ loop do
226
+ response = client.connection.get("personal_access_tokens?#{params}&page=#{page}&per_page=100")
227
+ body = Oj.load(response.body)
228
+ tokens += body.map { |p| UserTokens.new(p) }
229
+ break if body.count < 100
230
+
231
+ page += 1
232
+ end
233
+ tokens
234
+ end
235
+
236
+ ##
237
+ # List all impersonation tokens for a user
238
+ #
239
+ # @author Jason Colyer
240
+ # @since 1.0.0
241
+ # @overload list(key: value)
242
+ # @param state [String optional] if defined, returns tokens with the
243
+ # specified statel possible values: active and inactive
244
+ # @param user_id [Integer required] if defined, returns tokens owned by
245
+ # the specified user
246
+ # @return [Array]
247
+ # @see
248
+ # https://docs.gitlab.com/api/user_tokens/#list-all-impersonation-tokens-for-a-user
249
+ # GitLab API > User tokens > List all impersonation tokens for a user
250
+ # @see SupportOps::GitLab::Configuration Setting up a client
251
+ # @example
252
+ # require 'support_ops_gitlab'
253
+ #
254
+ # SupportOps::GitLab::Configuration.configure do |config|
255
+ # config.url = 'https://gitlab.example.com/api/v4'
256
+ # config.token = 'abc123'
257
+ # end
258
+ #
259
+ # user = Readiness::GitLab::Users.current
260
+ # tokens = SupportOps::GitLab::UserTokens.list_impersonation(state: 'active', user_id: user.id)
261
+ # pp tokens.count
262
+ # # => 1
263
+ # pp tokens.last.expires_at
264
+ # # => "2025-08-15"
265
+ def self.list_impersonation(**args)
266
+ raise 'You must provide a user_id for this' unless args[:user_id]
267
+ args[:state] = nil unless args[:state]
268
+ params = ''
269
+ params += "state=#{args[:state]}&" unless args[:state].nil?
270
+ tokens = []
271
+ page = 1
272
+ loop do
273
+ response = client.connection.get("users/#{args[:user_id]}/impersonation_tokens?#{params}&page=#{page}&per_page=100")
274
+ body = Oj.load(response.body)
275
+ tokens += body.map { |p| UserTokens.new(p) }
276
+ break if body.count < 100
277
+
278
+ page += 1
279
+ end
280
+ tokens
281
+ end
282
+
283
+ private
284
+
285
+ ##
286
+ # @private
287
+ def create_record
288
+ url = if self.impersonation
289
+ "users/#{self.user_id}/impersonation_tokens"
290
+ elsif self.user_id.nil?
291
+ 'user/personal_access_tokens'
292
+ else
293
+ "users/#{self.user_id}/personal_access_tokens"
294
+ end
295
+ response = self.client.connection.post(url, attributes_for_save.to_json)
296
+ body = Oj.load(response.body)
297
+ raise "Error creating token => #{body}" unless response.status == 201
298
+ body
299
+ end
300
+
301
+ ##
302
+ # @private
303
+ def update_record
304
+ raise 'You cannot update a token, you must create a new one'
305
+ end
306
+
307
+ ##
308
+ # @private
309
+ def delete_record
310
+ raise 'You cannot delete a token, you must revoke it'
311
+ end
312
+
313
+ ##
314
+ # @private
315
+ def rotate_record
316
+ raise 'You cannot rotate an impersonation token' if self.impersonation
317
+ data = { expires_at: self.expires_at }.to_json
318
+ response = self.client.connection.post("personal_access_tokens/#{self.id}/rotate", data)
319
+ body = Oj.load(response.body)
320
+ raise "Faield to rotate token #{self.id} => #{body}" unless response.status == 200
321
+ body.each do |key, value|
322
+ self.instance_variable_set("@#{key}", value) if self.respond_to?("#{key}=")
323
+ end
324
+ body
325
+ end
326
+
327
+ ##
328
+ # @private
329
+ def revoke_record
330
+ url = if self.impersonation
331
+ "users/#{self.user_id}/impersonation_tokens/#{self.id}"
332
+ else
333
+ "personal_access_tokens/#{self.id}"
334
+ end
335
+ response = self.client.connection.delete(url)
336
+ body = Oj.load(response.body)
337
+ raise "Error deleting token => #{self.body}" unless response.status == 204
338
+ self.instance_variable_set("@active", false)
339
+ self.instance_variable_set("@revoked", true)
340
+ true
341
+ end
342
+ end
343
+ end
344
+ end