Class: Readiness::GitLab::Repositories

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

Overview

TODO:

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

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

Author:

  • Jason Colyer

Since:

  • 1.0.0

Class 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

Class Method Details

.commits(client, project, filters) ⇒ Array

Lists a repository’s commits. 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)
project = Readiness::GitLab::Projects.find!(client, 1083469)
commits = Readiness::GitLab::Repositories.commits(client, project)
pp commits.first['title']
# => "Replace sanitize with escape once"

Parameters:

  • client (Object)

    An instance of Client

  • project (Object)

    An instance of Projects

  • filters (Array)

    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.12



286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/support_readiness/gitlab/repositories.rb', line 286

def self.commits(client, project, filters)
  array = []
  page = 1
  loop do
    response = client.connection.get "projects/#{project.id}/repository/commits?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

.create_commit!(client, project, params) ⇒ Hash

Create a commit on a repository 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)
project = Readiness::GitLab::Projects.find!(client, 1083469)
commit_params = {
  branch: 'master',
  commit_message: 'Notify via slack for failed pipelines',
  actions: [
    {
      execute_filemode: true,
      file_path: 'bin/post_to_slack.sh',
      content: 'curl -ss -X POST $SLACK_URL -H "Content-Type=application/json" --data...',
      action: 'create'
    }
  ]
}
commit = Readiness::GitLab::Repositories.create_commit!(client, project, commit_params)
pp commit['title']
# => "Notify via slack for failed pipelines"

Parameters:

  • client (Object)

    An instance of Client

  • project (Object)

    An instance of Projects

  • params (Hash)

    The parameters to use for the request

Returns:

  • (Hash)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



229
230
231
232
233
# File 'lib/support_readiness/gitlab/repositories.rb', line 229

def self.create_commit!(client, project, params)
  response = client.connection.post "projects/#{project.id}/repository/commits", params.to_json
  handle_request_error(1, 'GitLab', response.status, { action: 'Create commit', id: "#{project.id}" }) unless response.status == 200
  Oj.load(response.body)
end

.create_file!(client, project, path, params) ⇒ Array

Create a single file in a repository. This will exit on error. You might want to instead use create_commit!

Examples:

require 'support_readiness'
config = Readiness::GitLab::Configuration.new
config.token = 'test123abc'
client = Readiness::GitLab::Client.new(config)
project = Readiness::GitLab::Projects.find!(client, 1083469)
file_params = {
  branch: 'master',
  commit_message: 'Created test.file',
  content: 'This is a test fi',
  execute_filemode: false
}
file = Readiness::GitLab::Repositories.create_file!(client, project, "spec/test.file", file_params)
pp file['path']
# => "spec/test.file"

Parameters:

  • client (Object)

    An instance of Client

  • project (Object)

    An instance of Projects

  • path (String)

    The full file path. Ex: lib/support_readiness/gitlab/repositories.rb

  • params (Hash)

    The parameters to use for the request

Returns:

  • (Array)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



128
129
130
131
132
# File 'lib/support_readiness/gitlab/repositories.rb', line 128

def self.create_file!(client, project, path, params)
  response = client.connection.post "projects/#{project.id}/repository/files/#{ERB::Util.url_encode(path)}", params.to_json
  handle_request_error(1, 'GitLab', response.status, { action: 'Create file in repo', id: "#{project.id}" }) unless response.status == 200
  Oj.load(response.body)
end

.create_tag!(client, project, tag) ⇒ Hash

Creates a repository tag. This will exit on error

Examples:

require 'support_readiness'
config = Readiness::GitLab::Configuration.new
config.token = 'test123abc'
client = Readiness::GitLab::Client.new(config)
project = Readiness::GitLab::Projects.find!(client, 5)
tag = {
  tag_name: 'test',
  ref: 'main'
}
create = Readiness::GitLab::Repositories.create_tag!(client, project, tag)
pp create['target']
# => "2695effb5807a22ff3d138d593fd856244e155e7"

Parameters:

  • client (Object)

    An instance of Client

  • project (Object)

    An instance of Projects

  • tag (Hash)

    The Hash to use for the tag creation

Returns:

  • (Hash)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.12



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

def self.create_tag!(client, project, tag)
  response = client.connection.post "projects/#{project.id}/repository/tags", tag.to_json
  handle_request_error(1, 'GitLab', response.status, { action: 'Create tag', id: "projects/#{project.id}/repository/tags" }) unless response.status == 200
  Oj.load(response.body)
end

.delete_file!(client, project, path, params) ⇒ Boolean

Deletes a single file in a repository. This will exit on error. You might want to instead use create_commit!

Examples:

require 'support_readiness'
config = Readiness::GitLab::Configuration.new
config.token = 'test123abc'
client = Readiness::GitLab::Client.new(config)
project = Readiness::GitLab::Projects.find!(client, 1083469)
file_params = {
  branch: 'master',
  commit_message: 'Deleting test.file'
}
file = Readiness::GitLab::Repositories.delete_file!(client, project, "spec/test.file", file_params)
pp file
# => true

Parameters:

  • client (Object)

    An instance of Client

  • project (Object)

    An instance of Projects

  • path (String)

    The full file path. Ex: lib/support_readiness/gitlab/repositories.rb

  • params (Hash)

    The parameters to use for the request

Returns:

  • (Boolean)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



192
193
194
195
196
# File 'lib/support_readiness/gitlab/repositories.rb', line 192

def self.delete_file!(client, project, path, params)
  response = client.connection.delete "projects/#{project.id}/repository/files/#{ERB::Util.url_encode(path)}", params
  handle_request_error(1, 'GitLab', response.status, { action: 'Delete file in repo', id: "#{project.id}" }) unless response.status == 204
  true
end

.delete_tag!(client, project, tag_name) ⇒ Boolean

Deletes a repository tag. This will exit on error

Examples:

require 'support_readiness'
config = Readiness::GitLab::Configuration.new
config.token = 'test123abc'
client = Readiness::GitLab::Client.new(config)
project = Readiness::GitLab::Projects.find!(client, 5)
delete = Readiness::GitLab::Repositories.create_tag!(client, project, 'test')
pp delete
# => true

Parameters:

  • client (Object)

    An instance of Client

  • project (Object)

    An instance of Projects

  • tag_name (String)

    The name of the tag to delete

Returns:

  • (Boolean)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.12



434
435
436
437
438
# File 'lib/support_readiness/gitlab/repositories.rb', line 434

def self.delete_tag!(client, project, tag_name)
  response = client.connection.delete "projects/#{project.id}/repository/tags/#{tag_name}"
  handle_request_error(1, 'GitLab', response.status, { action: 'Delete tag', id: "projects/#{project.id}/repository/tags/#{tag_name}" }) unless response.status == 200
  true
end

.file(client, project, path, ref) ⇒ Array

Get information about a file in a repository.

Examples:

require 'support_readiness'
config = Readiness::GitLab::Configuration.new
config.token = 'test123abc'
client = Readiness::GitLab::Client.new(config)
project = Readiness::GitLab::Projects.find!(client, 1083469)
file = Readiness::GitLab::Repositories.file(client, project, ".gitlab/CODEOWNERS", "main")
pp file['size']
# => 88

Parameters:

  • client (Object)

    An instance of Client

  • project (Object)

    An instance of Projects

  • path (String)

    The full file path. Ex: lib/support_readiness/gitlab/repositories.rb

  • ref (String)

    The name of branch, tag or commit

Returns:

  • (Array)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



69
70
71
72
73
# File 'lib/support_readiness/gitlab/repositories.rb', line 69

def self.file(client, project, path, ref)
  response = client.connection.get "projects/#{project.id}/repository/files/#{ERB::Util.url_encode(path)}?ref=#{ref}"
  handle_request_error(0, 'GitLab', response.status) unless response.status == 200
  Oj.load(response.body)
end

.find_tag(client, project, tag_name) ⇒ Hash

Locates a repository tag. 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)
project = Readiness::GitLab::Projects.find!(client, 5)
tag = Readiness::GitLab::Repositories.find_tag(client, project, 'v5.0.0')
pp tag.['target']
# => "60a8ff033665e1207714d6670fcd7b65304ec02f"

Parameters:

  • client (Object)

    An instance of Client

  • project (Object)

    An instance of Projects

  • tag_name (String)

    The tag to find

Returns:

  • (Hash)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.12



355
356
357
358
359
# File 'lib/support_readiness/gitlab/repositories.rb', line 355

def self.find_tag(client, project, tag_name)
  response = client.connection.get "projects/#{project.id}/repository/tags/#{tag_name}"
  handle_request_error(0, 'GitLab', response.status,  { action: 'get', id: "projects/#{project.id}/repository/tags/#{tag_name}" }) unless response.status == 200
  Oj.load(response.body)
end

.find_tag!(client, project, tag_name) ⇒ Hash

Locates a repository tag. This will exit on error

Examples:

require 'support_readiness'
config = Readiness::GitLab::Configuration.new
config.token = 'test123abc'
client = Readiness::GitLab::Client.new(config)
project = Readiness::GitLab::Projects.find!(client, 5)
tag = Readiness::GitLab::Repositories.find_tag!(client, project, 'v5.0.0')
pp tag.['target']
# => "60a8ff033665e1207714d6670fcd7b65304ec02f"

Parameters:

  • client (Object)

    An instance of Client

  • project (Object)

    An instance of Projects

  • tag_name (String)

    The tag to find

Returns:

  • (Hash)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.12



380
381
382
383
384
# File 'lib/support_readiness/gitlab/repositories.rb', line 380

def self.find_tag!(client, project, tag_name)
  response = client.connection.get("projects/#{project.id}/repository/tags/#{tag_name}")
  handle_request_error(1, 'GitLab', response.status, { action: 'Find tag', id: "projects/#{project.id}/repository/tags/#{tag_name}" }) unless response.status == 200
  Oj.load(response.body)
end

.raw_file(client, project, path, ref) ⇒ String

Get raw information about a file in a repository.

Examples:

require 'support_readiness'
config = Readiness::GitLab::Configuration.new
config.token = 'test123abc'
client = Readiness::GitLab::Client.new(config)
project = Readiness::GitLab::Projects.find!(client, 1083469)
file = Readiness::GitLab::Repositories.raw_file(client, project, ".gitlab/CODEOWNERS", "main")
pp file
# => "Hello World!"

Parameters:

  • client (Object)

    An instance of Client

  • project (Object)

    An instance of Projects

  • path (String)

    The full file path. Ex: lib/support_readiness/gitlab/repositories.rb

  • ref (String)

    The name of branch, tag or commit

Returns:

  • (String)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



95
96
97
98
99
# File 'lib/support_readiness/gitlab/repositories.rb', line 95

def self.raw_file(client, project, path, ref)
  response = client.connection.get "projects/#{project.id}/repository/files/#{ERB::Util.url_encode(path)}/raw?ref=#{ref}"
  handle_request_error(0, 'GitLab', response.status) unless response.status == 200
  response.body
end

.tags(client, project, filters = []) ⇒ Array

Lists a repository’s tags. 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)
project = Readiness::GitLab::Projects.find!(client, 5)
tags = Readiness::GitLab::Repositories.tags(client, project)
pp tags.first['commit']['id']
# => "2695effb5807a22ff3d138d593fd856244e155e7"

Parameters:

  • client (Object)

    An instance of Client

  • project (Object)

    An instance of Projects

  • 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.12



321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/support_readiness/gitlab/repositories.rb', line 321

def self.tags(client, project, filters = [])
  array = []
  page = 1
  loop do
    response = client.connection.get "projects/#{project.id}/repository/tags?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

.tree(client, project, filters = []) ⇒ Array

Lists a repository’s file tree. 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)
project = Readiness::GitLab::Projects.find!(client, 1083469)
tree = Readiness::GitLab::Repositories.tree(client, project)
pp tree.first['path']
# => ".gitlab/CODEOWNERS"

Parameters:

  • client (Object)

    An instance of Client

  • project (Object)

    An instance of Projects

  • 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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/support_readiness/gitlab/repositories.rb', line 34

def self.tree(client, project, filters = [])
  array = []
  url = "projects/#{project.id}/repository/tree?per_page=100&pagination=keyset&order_by=id&sort=asc&id_after=0&#{to_param_string(filters)}"
  loop do
    response = client.connection.get url
    handle_request_error(0, 'GitLab', response.status) unless response.status == 200
    body = Oj.load(response.body)
    array += body
    break if body.count < 100

    url = "projects/#{project.id}/repository/tree?per_page=100&pagination=keyset&order_by=id&sort=asc&id_after=0&#{to_param_string(filters)}"
  end
  array
end

.update_file!(client, project, path, params) ⇒ Array

Updates a single file in a repository. This will exit on error. You might want to instead use create_commit!

Examples:

require 'support_readiness'
config = Readiness::GitLab::Configuration.new
config.token = 'test123abc'
client = Readiness::GitLab::Client.new(config)
project = Readiness::GitLab::Projects.find!(client, 1083469)
file_params = {
  branch: 'master',
  commit_message: 'Fixing typo within test.file',
  content: 'This is a test file',
  execute_filemode: false
}
file = Readiness::GitLab::Repositories.update_file!(client, project, "spec/test.file", file_params)
pp file['path']
# => "spec/test.file"

Parameters:

  • client (Object)

    An instance of Client

  • project (Object)

    An instance of Projects

  • path (String)

    The full file path. Ex: lib/support_readiness/gitlab/repositories.rb

  • params (Hash)

    The parameters to use for the request

Returns:

  • (Array)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



161
162
163
164
165
# File 'lib/support_readiness/gitlab/repositories.rb', line 161

def self.update_file!(client, project, path, params)
  response = client.connection.put "projects/#{project.id}/repository/files/#{ERB::Util.url_encode(path)}", params.to_json
  handle_request_error(1, 'GitLab', response.status, { action: 'Update file in repo', id: "#{project.id}" }) unless response.status == 200
  Oj.load(response.body)
end

.update_submodule!(client, project, path, params) ⇒ Hash

Updates a submodule’s reference in a project. This will exit on error.

Examples:

require 'support_readiness'
config = Readiness::GitLab::Configuration.new
config.token = 'test123abc'
client = Readiness::GitLab::Client.new(config)
project = Readiness::GitLab::Projects.find!(client, 5)
update_params = {
  branch: 'main',
  commit_sha: '3ddec28ea23acc5caa5d8331a6ecb2a65fc03e88',
  commit_message: 'Update submodule reference'
}
update = Readiness::GitLab::Repositories.update_submodule!(client, project, "lib/modules/example", update_params)
pp update['parent_ids']
# => ["ae1d9fb46aa2b07ee9836d49862ec4e2c46fbbba"]

Parameters:

  • client (Object)

    An instance of Client

  • project (Object)

    An instance of Projects

  • path (String)

    The full file path. Ex: lib/support_readiness/gitlab/repositories.rb

  • params (Hash)

    The parameters to use for the request

Returns:

  • (Hash)

See Also:

Author:

  • Jason Colyer

Since:

  • 1.0.0



260
261
262
263
264
# File 'lib/support_readiness/gitlab/repositories.rb', line 260

def self.update_submodule!(client, project, path, params)
  response = client.connection.put "projects/#{project.id}/repository/submodules/#{ERB::Util.url_encode(path)}", params.to_json
  handle_request_error(1, 'GitLab', response.status, { action: 'Update submodule in repo', id: "#{project.id}" }) unless response.status == 200
  Oj.load(response.body)
end