avm-gitlab 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: db42605094ad5c83d8d884f54920e68aca4ec7b269e66b24d32ac0af54991e25
4
+ data.tar.gz: ad8ad8fa0243c049472e9a559a6975c7319c313976239ef2f6ee0318f251e067
5
+ SHA512:
6
+ metadata.gz: f1a726bcddd62285aac514555456e94434bb9c9753a4c7adc1885fc39f8fcac5f86f56b8c9b014d925322be500f30d0939e9ce494df8967c395ed44da6591fdc
7
+ data.tar.gz: 7dd45d3dc052bf5d32001b01537aa866f23dd3d42c4b9250c898b752ae2c8123f1054a06d7bb5338740c7822886bc103d6c34155c4d67dc7cfe14cb7a091ebf9
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'avm/instances/base'
4
+ require 'avm/gitlab/rest_api'
5
+ require 'eac_ruby_utils/core_ext'
6
+
7
+ module Avm
8
+ module Gitlab
9
+ class Instance < ::Avm::Instances::Base
10
+ # @return [Avm::Gitlab::RestApi]
11
+ def rest_api
12
+ @rest_api ||= ::Avm::Gitlab::RestApi.new(web_url)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_rest/api'
4
+ require 'eac_rest/entity'
5
+ require 'eac_ruby_utils/core_ext'
6
+
7
+ module Avm
8
+ module Gitlab
9
+ class RestApi < ::EacRest::Api
10
+ class BaseEntity < ::EacRest::Entity
11
+ enable_simple_cache
12
+
13
+ def encode_id(id)
14
+ return id if id.is_a?(::Integer)
15
+
16
+ ::CGI.escape(id.to_s)
17
+ end
18
+
19
+ def fetch_entity(suffix, klass, not_found_message = nil)
20
+ validate_response_data(
21
+ dump_response(api.request_json(suffix).response),
22
+ not_found_message
23
+ ).if_present { |v| klass.new(api, v) }
24
+ end
25
+
26
+ def fetch_entities(suffix, klass)
27
+ r = []
28
+ request = api.request_json(suffix)
29
+ while request
30
+ response = request.response
31
+ r += validate_response_data(response).map { |rr| klass.new(api, rr) }
32
+ request = response.link('next').if_present do |v|
33
+ api.request_json(v)
34
+ end
35
+ end
36
+ r
37
+ end
38
+
39
+ def dump_response(response)
40
+ basename = response.url.to_s.variableize[0..99]
41
+ { data: response.body_data, headers: response.headers, links: response.links }
42
+ .each { |part, value| dump_debug("#{basename}_#{part}", value) }
43
+ response
44
+ end
45
+
46
+ def dump_debug(basename, data)
47
+ file = ::Pathname.new('/tmp').join('gitlab_temp', basename + '.yaml')
48
+ file.parent.mkpath
49
+ ::EacRubyUtils::Yaml.dump_file(file, data)
50
+ end
51
+
52
+ def validate_response_data(response, not_found_message = nil)
53
+ if response.body_data.is_a?(::Hash)
54
+ response.body_data['error'].if_present do |v|
55
+ raise ::RuntimeError, "URL: #{response.url}, Data: #{v}"
56
+ end
57
+
58
+ response.body_data['message'].if_present do |v|
59
+ return nil if v == not_found_message
60
+ end
61
+ end
62
+
63
+ response.body_data
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'avm/gitlab/rest_api/base_entity'
4
+ require 'eac_rest/api'
5
+ require 'eac_ruby_utils/core_ext'
6
+
7
+ module Avm
8
+ module Gitlab
9
+ class RestApi < ::EacRest::Api
10
+ class File < ::Avm::Gitlab::RestApi::BaseEntity
11
+ FIELDS = %w[file_name file_path size encoding content_sha256 ref blob_id commit_id
12
+ last_commit_id].freeze
13
+
14
+ FIELDS.each do |field|
15
+ define_method field do
16
+ data.fetch(field)
17
+ end
18
+ end
19
+
20
+ def content
21
+ case encoding
22
+ when 'base64' then ::Base64.decode64(encoded_content)
23
+ else nyi("Unmapped encoding: #{encoding}")
24
+ end
25
+ end
26
+
27
+ def encoded_content
28
+ data.fetch('content')
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'avm/gitlab/rest_api/node'
4
+ require 'avm/gitlab/rest_api/project'
5
+ require 'eac_rest/api'
6
+ require 'eac_ruby_utils/core_ext'
7
+
8
+ module Avm
9
+ module Gitlab
10
+ class RestApi < ::EacRest::Api
11
+ class Group < ::Avm::Gitlab::RestApi::Node
12
+ FIELDS = %w[id web_url name path description visibility share_with_group_lock
13
+ require_two_factor_authentication two_factor_grace_period project_creation_level
14
+ auto_devops_enabled subgroup_creation_level emails_disabled lfs_enabled
15
+ avatar_url request_access_enabled full_name full_path parent_id].freeze
16
+
17
+ FIELDS.each do |field|
18
+ define_method field do
19
+ data.fetch(field)
20
+ end
21
+ end
22
+
23
+ def to_s
24
+ full_path
25
+ end
26
+
27
+ private
28
+
29
+ def members_uncached
30
+ fetch_entities(
31
+ "/groups/#{id}/members",
32
+ ::Avm::Gitlab::RestApi::Member
33
+ )
34
+ end
35
+
36
+ def projects_uncached
37
+ fetch_entities(
38
+ "/groups/#{id}/projects?order_by=path&sort=asc&per_page=9999",
39
+ ::Avm::Gitlab::RestApi::Project
40
+ )
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'avm/gitlab/rest_api/base_entity'
4
+ require 'eac_rest/api'
5
+ require 'eac_ruby_utils/core_ext'
6
+
7
+ module Avm
8
+ module Gitlab
9
+ class RestApi < ::EacRest::Api
10
+ class Member < ::Avm::Gitlab::RestApi::BaseEntity
11
+ FIELDS = %w[id name username state avatar_url web_url access_level expires_at].freeze
12
+
13
+ FIELDS.each do |field|
14
+ define_method field do
15
+ data.fetch(field)
16
+ end
17
+ end
18
+
19
+ def to_s
20
+ [name, username, state, access_level].join(' / ')
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'avm/gitlab/rest_api/base_entity'
4
+ require 'avm/gitlab/rest_api/file'
5
+ require 'eac_rest/api'
6
+ require 'eac_ruby_utils/core_ext'
7
+
8
+ module Avm
9
+ module Gitlab
10
+ class RestApi < ::EacRest::Api
11
+ class Node < ::Avm::Gitlab::RestApi::BaseEntity
12
+ compare_by :id
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,111 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'avm/gitlab/rest_api/base_entity'
4
+ require 'avm/gitlab/rest_api/project'
5
+ require 'eac_rest/api'
6
+ require 'eac_ruby_utils/core_ext'
7
+
8
+ module Avm
9
+ module Gitlab
10
+ class RestApi < ::EacRest::Api
11
+ class NodesSet
12
+ IDS_PREFIXES = {
13
+ '@@' => :group_and_projects, '@-' => :group_projects, '@' => :group
14
+ }.freeze
15
+
16
+ GROUP_AND_PROJECTS_ID_PARSER = /\A\@@(.*)\z/.to_parser { |m| m[1] }
17
+ GROUP_ID_PARSER = /\A\@(.*)\z/.to_parser { |m| m[1] }
18
+
19
+ class << self
20
+ def parse_id(id)
21
+ IDS_PREFIXES.each do |prefix, type|
22
+ /\A#{::Regexp.quote(prefix)}(.*)\z/.if_match(id, false) do |m|
23
+ return [m[1], type]
24
+ end
25
+ end
26
+
27
+ [id, :project]
28
+ end
29
+ end
30
+
31
+ attr_reader :rest_api
32
+
33
+ def initialize(rest_api, *ids)
34
+ self.rest_api = rest_api
35
+ ids.each { |id| add(id) }
36
+ end
37
+
38
+ def add(id)
39
+ parsed_id, type = self.class.parse_id(id)
40
+
41
+ send("add_by_#{type}_id", parsed_id)
42
+ end
43
+
44
+ # @return [Array<Avm::Gitlab::RestApi::Group>]
45
+ def group
46
+ nodes.select { |node| node.is_a?(::Avm::Gitlab::RestApi::Group) }
47
+ end
48
+
49
+ # @return [Array<Avm::Gitlab::RestApi::Project>]
50
+ def projects
51
+ nodes.select { |node| node.is_a?(::Avm::Gitlab::RestApi::Project) }
52
+ end
53
+
54
+ # @return [Array<Avm::Gitlab::RestApi::Node>]
55
+ def nodes
56
+ nodes_set.sort_by { |p| [p.full_path] }
57
+ end
58
+
59
+ private
60
+
61
+ attr_writer :rest_api
62
+
63
+ # @return [Set<Avm::Gitlab::RestApi::Project>]
64
+ def nodes_set
65
+ @nodes_set ||= ::Set.new
66
+ end
67
+
68
+ # @return [Avm::Gitlab::RestApi::Node]
69
+ def add_node(node)
70
+ nodes_set.add(node)
71
+
72
+ node
73
+ end
74
+
75
+ # @return [Array<Avm::Gitlab::RestApi::Group>]
76
+ def add_by_group_id(id)
77
+ group = rest_api.root.group(id)
78
+
79
+ raise "No group found with ID = \"#{id}\"" unless group
80
+
81
+ [add_node(group)]
82
+ end
83
+
84
+ # @return [Array<Avm::Gitlab::RestApi::Project>]
85
+ def add_by_group_projects_id(id)
86
+ group = rest_api.root.group(id)
87
+
88
+ raise "No group found with ID = \"#{id}\"" unless group
89
+
90
+ group.projects.map do |project|
91
+ add_node(project)
92
+ end
93
+ end
94
+
95
+ # @return [Array<Avm::Gitlab::RestApi::Node>]
96
+ def add_by_group_and_projects_id(id)
97
+ add_by_group_id(id) + add_by_group_projects_id(id)
98
+ end
99
+
100
+ # @return [Array<Avm::Gitlab::RestApi::Project>]
101
+ def add_by_project_id(id)
102
+ project = rest_api.root.project(id)
103
+
104
+ raise "No project found with ID = \"#{id}\"" unless project
105
+
106
+ [add_node(project)]
107
+ end
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'avm/gitlab/rest_api/file'
4
+ require 'avm/gitlab/rest_api/member'
5
+ require 'avm/gitlab/rest_api/node'
6
+ require 'eac_rest/api'
7
+ require 'eac_ruby_utils/core_ext'
8
+
9
+ module Avm
10
+ module Gitlab
11
+ class RestApi < ::EacRest::Api
12
+ class Project < ::Avm::Gitlab::RestApi::Node
13
+ FIELDS = %w[id description name name_with_namespace path path_with_namespace created_at
14
+ default_branch tag_list ssh_url_to_repo http_url_to_repo web_url readme_url
15
+ avatar_url star_count forks_count last_activity_at empty_repo archived
16
+ visibility resolve_outdated_diff_discussions container_registry_enabled
17
+ issues_enabled merge_requests_enabled wiki_enabled jobs_enabled snippets_enabled
18
+ issues_access_level repository_access_level merge_requests_access_level
19
+ wiki_access_level builds_access_level snippets_access_level
20
+ shared_runners_enabled lfs_enabled creator_id import_status
21
+ ci_default_git_depth public_jobs build_timeout auto_cancel_pending_pipelines
22
+ build_coverage_regex ci_config_path shared_with_groups
23
+ only_allow_merge_if_pipeline_succeeds request_access_enabled
24
+ only_allow_merge_if_all_discussions_are_resolved
25
+ remove_source_branch_after_merge printing_merge_request_link_enabled
26
+ merge_method auto_devops_enabled auto_devops_deploy_strategy].freeze
27
+
28
+ FIELDS.each do |field|
29
+ define_method field do
30
+ data.fetch(field)
31
+ end
32
+ end
33
+
34
+ def full_path
35
+ path_with_namespace
36
+ end
37
+
38
+ def file(path)
39
+ fetch_entity(
40
+ "/projects/#{encode_id(id)}/repository/files/#{encode_id(path)}?ref=#{default_branch}",
41
+ ::Avm::Gitlab::RestApi::File,
42
+ '404 File Not Found'
43
+ )
44
+ end
45
+
46
+ def to_s
47
+ path_with_namespace
48
+ end
49
+
50
+ private
51
+
52
+ def members_uncached
53
+ fetch_entities(
54
+ "/projects/#{id}/members",
55
+ ::Avm::Gitlab::RestApi::Member
56
+ )
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'avm/gitlab/rest_api/base_entity'
4
+ require 'avm/gitlab/rest_api/project'
5
+ require 'eac_rest/api'
6
+ require 'eac_ruby_utils/core_ext'
7
+
8
+ module Avm
9
+ module Gitlab
10
+ class RestApi < ::EacRest::Api
11
+ class Root < ::Avm::Gitlab::RestApi::BaseEntity
12
+ def group(id)
13
+ fetch_entity(
14
+ "/groups/#{encode_id(id)}?with_projects=false",
15
+ ::Avm::Gitlab::RestApi::Group,
16
+ '404 Group Not Found'
17
+ )
18
+ end
19
+
20
+ def project(id)
21
+ fetch_entity(
22
+ "/projects/#{encode_id(id)}",
23
+ ::Avm::Gitlab::RestApi::Project,
24
+ '404 Project Not Found'
25
+ )
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_rest/api'
4
+ require 'eac_ruby_utils/core_ext'
5
+
6
+ module Avm
7
+ module Gitlab
8
+ class RestApi < ::EacRest::Api
9
+ require_sub __FILE__
10
+
11
+ API_SUFFIX = '/api/v4'
12
+
13
+ def auth_token
14
+ ENV.fetch('TRF1_GITLAB_PRIVATE_TOKEN')
15
+ end
16
+
17
+ def build_service_url_suffix(suffix)
18
+ r = super(suffix)
19
+ r.path = API_SUFFIX + r.path
20
+ r
21
+ end
22
+
23
+ def custom_headers
24
+ {
25
+ 'PRIVATE-TOKEN' => auth_token
26
+ }
27
+ end
28
+
29
+ # @return [Avm::Gitlab::RestApi::ProjectsSet]
30
+ def nodes_set(*projects_ids)
31
+ ::Avm::Gitlab::RestApi::NodesSet.new(self, *projects_ids)
32
+ end
33
+
34
+ def request(service_url_suffix, headers = {}, &body_data_proc)
35
+ super(service_url_suffix, custom_headers.merge(headers), &body_data_proc)
36
+ end
37
+
38
+ def root
39
+ @root ||= ::Avm::Gitlab::RestApi::Root.new(self, {})
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Avm
4
+ module Gitlab
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
data/lib/avm/gitlab.rb ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/core_ext'
4
+
5
+ module Avm
6
+ module Gitlab
7
+ require_sub __FILE__
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: avm-gitlab
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Put here the authors
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-05-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: eac_ruby_utils
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.93'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.93'
27
+ - !ruby/object:Gem::Dependency
28
+ name: eac_ruby_gem_support
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.5'
41
+ description:
42
+ email:
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - lib/avm/gitlab.rb
48
+ - lib/avm/gitlab/instance.rb
49
+ - lib/avm/gitlab/rest_api.rb
50
+ - lib/avm/gitlab/rest_api/base_entity.rb
51
+ - lib/avm/gitlab/rest_api/file.rb
52
+ - lib/avm/gitlab/rest_api/group.rb
53
+ - lib/avm/gitlab/rest_api/member.rb
54
+ - lib/avm/gitlab/rest_api/node.rb
55
+ - lib/avm/gitlab/rest_api/nodes_set.rb
56
+ - lib/avm/gitlab/rest_api/project.rb
57
+ - lib/avm/gitlab/rest_api/root.rb
58
+ - lib/avm/gitlab/version.rb
59
+ homepage:
60
+ licenses: []
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubygems_version: 3.1.6
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: Put here de description.
81
+ test_files: []