Skip to content

Commit b08008a

Browse files
committed
Add command to trigger reticulum build with version overrides
1 parent 0f9e61c commit b08008a

4 files changed

Lines changed: 112 additions & 5 deletions

File tree

lib/pyxis/commands/components.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,28 @@ def info
1919
result
2020
end
2121

22+
desc 'build', 'Start a reticulum build with specific versions'
23+
method_option :aquila_sha, desc: 'Commit SHA of aquila to build', type: :string
24+
method_option :draco_sha, desc: 'Commit SHA of draco to build', type: :string
25+
method_option :sagittarius_sha, desc: 'Commit SHA of sagittarius to build', type: :string
26+
method_option :sculptor_sha, desc: 'Commit SHA of sculptor to build', type: :string
27+
method_option :taurus_sha, desc: 'Commit SHA of taurus to build', type: :string
28+
def build
29+
version_overrides = {
30+
aquila: options[:aquila_sha],
31+
draco: options[:draco_sha],
32+
sagittarius: options[:sagittarius_sha],
33+
sculptor: options[:sculptor_sha],
34+
taurus: options[:taurus_sha],
35+
}.compact
36+
37+
pipeline = Pyxis::Services::CreateReticulumBuildService.new(version_overrides).execute
38+
39+
raise Pyxis::MessageError, 'Failed to create pipeline' if pipeline.nil?
40+
41+
"Created reticulum build at #{pipeline.web_url}"
42+
end
43+
2244
desc 'update', 'Update a component in reticulum'
2345
method_option :component, aliases: '-c', desc: 'The component to update', required: true, type: :string
2446
def update

lib/pyxis/github_client.rb

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,26 @@ def without_auto_pagination(octokit)
3131
octokit.instance_variable_set(:@auto_paginate, current_auto_paginate)
3232
end
3333

34-
private
35-
36-
def create_octokit(instance)
37-
logger.info('Creating octokit client', instance: instance)
34+
def create_installation_access_token(instance, options = {})
3835
config = CLIENT_CONFIGS[instance]
3936
global_client = Octokit::Client.new(bearer_token: create_jwt(config[:private_key_location], config[:app_id]))
4037
logger.debug('Created JWT for client', app: global_client.app.slug)
4138
installation_token = Pyxis::GlobalStatus.with_faraday_dry_run_bypass do
42-
global_client.create_app_installation_access_token(config[:installation_id])
39+
global_client.create_app_installation_access_token(config[:installation_id], options)
4340
end
41+
4442
logger.debug('Created app installation access token',
4543
installation_token: installation_token.to_h.except(:token))
44+
45+
installation_token
46+
end
47+
48+
private
49+
50+
def create_octokit(instance)
51+
logger.info('Creating octokit client', instance: instance)
52+
53+
installation_token = create_installation_access_token(instance)
4654
Octokit::Client.new(bearer_token: installation_token[:token])
4755
end
4856

lib/pyxis/project/base.rb

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

19+
def github_repository_name
20+
github_path.split('/')[1]
21+
end
22+
1923
def api_gitlab_path
2024
paths[:gitlab].gsub('/', '%2F')
2125
end
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# frozen_string_literal: true
2+
3+
module Pyxis
4+
module Services
5+
class CreateReticulumBuildService
6+
InvalidVersionOverride = Class.new(Pyxis::Error)
7+
8+
include SemanticLogger::Loggable
9+
10+
attr_reader :version_overrides
11+
12+
def initialize(version_overrides)
13+
@version_overrides = version_overrides
14+
end
15+
16+
def execute
17+
logger.info('Creating build with version overrides', version_overrides: version_overrides)
18+
19+
version_overrides.each_pair do |component, version|
20+
validate_override!(component, version)
21+
end
22+
23+
pipeline = GitlabClient.client.post_json(
24+
"/api/v4/projects/#{Project::Reticulum.api_gitlab_path}/pipeline",
25+
{
26+
ref: Project::Reticulum.default_branch,
27+
variables: version_override_variables + token_variable,
28+
}
29+
)
30+
31+
pipeline.body if pipeline.response.status == 201
32+
end
33+
34+
private
35+
36+
def validate_override!(component, version)
37+
project = Pyxis::Project.const_get(component.capitalize)
38+
39+
begin
40+
GithubClient.octokit.tag(project.github_path, version)
41+
rescue Octokit::UnprocessableEntity, Octokit::NotFound
42+
begin
43+
GithubClient.octokit.commit(project.github_path, version)
44+
rescue Octokit::UnprocessableEntity, Octokit::NotFound
45+
raise InvalidVersionOverride, "Invalid version '#{version}' for component '#{component}'"
46+
end
47+
end
48+
end
49+
50+
def version_override_variables
51+
variables = []
52+
53+
version_overrides.each_pair do |component, version|
54+
variables << {
55+
key: "OVERRIDE_#{component}_VERSION",
56+
value: version,
57+
}
58+
end
59+
60+
variables
61+
end
62+
63+
def token_variable
64+
[
65+
{
66+
key: 'C0_GH_TOKEN',
67+
value: File.read(ENV.fetch('PYXIS_GH_RETICULUM_PUBLISH_TOKEN')),
68+
}
69+
]
70+
end
71+
end
72+
end
73+
end

0 commit comments

Comments
 (0)