Skip to content

Commit f8f5291

Browse files
authored
Merge pull request #12 from code0-tech/10-component-info-command
Add component info command
2 parents 48a9944 + aff594c commit f8f5291

4 files changed

Lines changed: 116 additions & 4 deletions

File tree

lib/pyxis/commands/components.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,27 @@
33
module Pyxis
44
module Commands
55
class Components < Thor
6-
desc 'update COMPONENT', 'Update a component in reticulum'
6+
desc 'info [BUILD_ID]', 'Get the component versions for a reticulum build'
7+
def info(build_id)
8+
component_versions = ManagedVersioning::ComponentInfo.new(build_id).execute
9+
10+
SemanticLogger.flush
11+
12+
puts 'Versions of each component'
13+
component_versions.each do |component, version|
14+
puts "#{component}: #{version}"
15+
end
16+
end
17+
18+
desc 'update [COMPONENT]', 'Update a component in reticulum'
719
def update(component)
820
updater(component).execute
921
end
1022

1123
desc 'list', 'List all available components'
1224
def list
1325
puts 'Available components:'
14-
Pyxis::Project.constants.each do |project|
15-
next if project == :Base
16-
26+
Pyxis::Project.components.each do |project|
1727
puts "- #{project.downcase}"
1828
end
1929
end

lib/pyxis/gitlab_client.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# frozen_string_literal: true
2+
3+
module Pyxis
4+
class GitlabClient
5+
include SemanticLogger::Loggable
6+
7+
GITLAB_URL = 'https://gitlab.com'
8+
9+
CLIENT_CONFIGS = {
10+
release_tools: {
11+
private_token: ENV.fetch('PYXIS_GL_RELEASE_TOOLS_PRIVATE_TOKEN'),
12+
user_id: 20643824,
13+
},
14+
}.freeze
15+
16+
def self.client(instance = :release_tools)
17+
CLIENT_CONFIGS[instance][:client] ||= create_client(instance)
18+
end
19+
20+
def self.create_client(instance)
21+
logger.info('Creating gitlab client', instance: instance)
22+
23+
client_config = CLIENT_CONFIGS[instance]
24+
options = {
25+
url: GITLAB_URL,
26+
headers: {
27+
'Private-Token': File.read(client_config[:private_token]),
28+
},
29+
}
30+
faraday = Faraday.new(options)
31+
faraday.use Pyxis::DryRunEnforcer::FaradayBlocker
32+
faraday.use Pyxis::Logger::FaradayLogger
33+
34+
enhance_faraday(faraday)
35+
36+
faraday
37+
end
38+
39+
def self.enhance_faraday(faraday)
40+
%i[get post put patch delete].each do |method|
41+
faraday.define_singleton_method(:"#{method}_json") do |*args, **kwargs|
42+
response = faraday.send(method, *args, **kwargs)
43+
json = response.body.blank? ? nil : JSON.parse(response.body)
44+
45+
if json.is_a?(Hash)
46+
Thor::CoreExt::HashWithIndifferentAccess.new(json)
47+
else
48+
json || response
49+
end
50+
end
51+
end
52+
end
53+
end
54+
end
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# frozen_string_literal: true
2+
3+
module Pyxis
4+
module ManagedVersioning
5+
class ComponentInfo
6+
include SemanticLogger::Loggable
7+
8+
attr_reader :build_id
9+
10+
def initialize(build_id)
11+
@build_id = build_id
12+
end
13+
14+
def execute
15+
pipeline = GitlabClient.client.get_json(
16+
"/api/v4/projects/#{Project::Reticulum.api_gitlab_path}/pipelines/#{build_id}"
17+
)
18+
reticulum_sha = pipeline.sha
19+
20+
components = {}
21+
22+
Pyxis::Project.components.each do |project_name|
23+
component_project_class = Pyxis::Project.const_get(project_name)
24+
version_file = "versions/#{component_project_class.component_name}"
25+
26+
begin
27+
version_content = GithubClient.octokit.contents(Project::Reticulum.github_path, path: version_file,
28+
ref: reticulum_sha)
29+
version = Base64.decode64(version_content.content)
30+
components[component_project_class.component_name] = version
31+
rescue Octokit::NotFound
32+
logger.warn("Version file not found for #{component_project_class.component_name} at SHA #{reticulum_sha}")
33+
end
34+
end
35+
36+
components
37+
end
38+
end
39+
end
40+
end

lib/pyxis/project/base.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,19 @@ def github_path
1616
paths[:github]
1717
end
1818

19+
def api_gitlab_path
20+
paths[:gitlab].gsub('/', '%2F')
21+
end
22+
1923
def component_name
2024
# noinspection RubyNilAnalysis
2125
name.split('::').last.downcase
2226
end
2327
end
2428
end
29+
30+
def self.components
31+
constants.reject { |c| %i[Base Reticulum].include?(c) }
32+
end
2533
end
2634
end

0 commit comments

Comments
 (0)