From dee0a7eb816ce453607ecb3b6c44afac0f202778 Mon Sep 17 00:00:00 2001 From: Stanislav Kolotinskiy Date: Tue, 21 Dec 2021 10:05:20 +0200 Subject: [PATCH 1/7] Add TAM options --- lib/travis/cli.rb | 1 + lib/travis/cli/tam.rb | 42 ++++++++++++++++++++++++++++++++++++++++++ lib/travis/version.rb | 2 +- travis.gemspec | 3 ++- 4 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 lib/travis/cli/tam.rb diff --git a/lib/travis/cli.rb b/lib/travis/cli.rb index c751098c..e3fafdad 100644 --- a/lib/travis/cli.rb +++ b/lib/travis/cli.rb @@ -50,6 +50,7 @@ module CLI autoload :Sshkey, 'travis/cli/sshkey' autoload :Status, 'travis/cli/status' autoload :Sync, 'travis/cli/sync' + autoload :Tam, 'travis/cli/tam' autoload :Version, 'travis/cli/version' autoload :Whatsup, 'travis/cli/whatsup' autoload :Whoami, 'travis/cli/whoami' diff --git a/lib/travis/cli/tam.rb b/lib/travis/cli/tam.rb new file mode 100644 index 00000000..a3f50d15 --- /dev/null +++ b/lib/travis/cli/tam.rb @@ -0,0 +1,42 @@ +require 'travis/cli' + +module Travis + module CLI + class Tam < ApiCommand + description "TAM (Travis Artifact Manager) actions" + + on('-c', '--create-image IMAGE_NAME', 'Create image with given name') + on('-u', '--update-image IMAGE_NAME', 'Update image with given name') + on('-d', '--delete-image IMAGE_NAME', 'Delete image with given name') + + def run + error("Please specify an action") if !create_image? && !update_image? && !delete_image? + error(".travis.lxd.yml file not found in the current directory or is empty") if (create_image? || update_image?) && (!File.exist?('.travis.lxd.yml') || File.read('.travis.lxd.yml').empty?) + + authenticate + + endpoint = if create_image? + 'v3/artifacts/config/create' + elsif update_image? + 'v3/artifacts/config/update' + else + # TODO + end + + params = JSON.dump( + image_name: create_image || update_image || delete_image, + config: File.read('.travis.lxd.yml') + ) + response = session.post(endpoint, params, 'Content-Type' => 'application/json') + + if create_image? + say 'Image created' + elsif update_image? + say 'Image updated' + else + warn 'Image deleted' + end + end + end + end +end diff --git a/lib/travis/version.rb b/lib/travis/version.rb index 4bf02222..91d91671 100644 --- a/lib/travis/version.rb +++ b/lib/travis/version.rb @@ -1,3 +1,3 @@ module Travis - VERSION = '1.11.0' + VERSION = '1.12.0' end diff --git a/travis.gemspec b/travis.gemspec index 6e4ce659..ef9393de 100644 --- a/travis.gemspec +++ b/travis.gemspec @@ -2,7 +2,7 @@ Gem::Specification.new do |s| # general info s.name = "travis" - s.version = "1.11.0" + s.version = "1.12.0" s.required_ruby_version = ">= 2.3.0" s.description = "CLI and Ruby client library for Travis CI" s.homepage = "https://github.com/travis-ci/travis.rb" @@ -325,6 +325,7 @@ Gem::Specification.new do |s| "lib/travis/cli/sshkey.rb", "lib/travis/cli/status.rb", "lib/travis/cli/sync.rb", + "lib/travis/cli/tam.rb", "lib/travis/cli/token.rb", "lib/travis/cli/version.rb", "lib/travis/cli/whatsup.rb", From 4f5925e9ce0137b3fb30f5c2853107c4760d7dd7 Mon Sep 17 00:00:00 2001 From: Stanislav Kolotinskiy Date: Fri, 28 Jan 2022 16:36:32 +0200 Subject: [PATCH 2/7] Add some more endpoints --- lib/travis/cli/tam.rb | 80 ++++++++++++++++++----- lib/travis/client.rb | 1 + lib/travis/client/artifacts_image_info.rb | 15 +++++ travis.gemspec | 1 + 4 files changed, 82 insertions(+), 15 deletions(-) create mode 100644 lib/travis/client/artifacts_image_info.rb diff --git a/lib/travis/cli/tam.rb b/lib/travis/cli/tam.rb index a3f50d15..262cb1a7 100644 --- a/lib/travis/cli/tam.rb +++ b/lib/travis/cli/tam.rb @@ -3,15 +3,18 @@ module Travis module CLI class Tam < ApiCommand - description "TAM (Travis Artifact Manager) actions" + description 'TAM (Travis Artifact Manager) actions' - on('-c', '--create-image IMAGE_NAME', 'Create image with given name') - on('-u', '--update-image IMAGE_NAME', 'Update image with given name') - on('-d', '--delete-image IMAGE_NAME', 'Delete image with given name') + on('-c', '--create-image', 'Create an image from .travis.lxd.yml') + on('-u', '--update-image', 'Update the image based on .travis.lxd.yml') + on('-d', '--delete-image IMAGE_NAME', 'Delete the image with the given name') + on('-i', '--image-info IMAGE_NAME', 'Get info about the image') + on('-l', '--logs IMAGE_NAME', 'Get the latest build log for the image') + on('-s', '--build-status IMAGE_NAME', 'Get the latest build status for the image') def run - error("Please specify an action") if !create_image? && !update_image? && !delete_image? - error(".travis.lxd.yml file not found in the current directory or is empty") if (create_image? || update_image?) && (!File.exist?('.travis.lxd.yml') || File.read('.travis.lxd.yml').empty?) + error('Please specify an action') if !create_image? && !update_image? && !delete_image? && !image_info? && !logs? && !build_status? + error('.travis.lxd.yml file not found in the current directory or is empty') if (create_image? || update_image?) && (!File.exist?('.travis.lxd.yml') || File.read('.travis.lxd.yml').empty?) authenticate @@ -19,22 +22,69 @@ def run 'v3/artifacts/config/create' elsif update_image? 'v3/artifacts/config/update' + elsif delete_image? + "v3/artifacts/#{delete_image}" + elsif image_info? + "v3/artifacts/#{image_info}/info" + elsif logs? + "v3/artifacts/#{logs}/logs" else - # TODO + "v3/artifacts/#{build_status}/build_status" end - params = JSON.dump( - image_name: create_image || update_image || delete_image, - config: File.read('.travis.lxd.yml') - ) - response = session.post(endpoint, params, 'Content-Type' => 'application/json') + if delete_image? + session.delete(endpoint) + elsif create_image? || update_image? + params = { + config: File.read('.travis.lxd.yml') + } + + session.post(endpoint, JSON.dump(params), 'Content-Type' => 'application/json') + else + data = nil + begin + data = session.get_raw(endpoint) + rescue Travis::Client::ValidationFailed => e + error 'Failed to fetch build status' + return + end + if image_info? + image_information = Travis::Client::ArtifactsImageInfo.new(session, 0) + image_information.update_attributes(data) + end + end if create_image? say 'Image created' - elsif update_image? + return + end + + if update_image? say 'Image updated' - else - warn 'Image deleted' + return + end + + if delete_image? + warn 'Image deleted!' + return + end + + if image_info? + say "Name: #{image_information.name}" + say "Size: #{(image_information.image_size.to_f / 1024**2).round(2)}MB" + say "Description: #{image_information.description.presence || ''}" + return + end + + if logs? + say data['log'] + return + end + + if build_status? + say data['status'] + + return end end end diff --git a/lib/travis/client.rb b/lib/travis/client.rb index 2a8b4c75..b0622b9a 100644 --- a/lib/travis/client.rb +++ b/lib/travis/client.rb @@ -23,6 +23,7 @@ require 'travis/client/request' require 'travis/client/listener' require 'travis/client/lint_result' +require 'travis/client/artifacts_image_info' module Travis module Client diff --git a/lib/travis/client/artifacts_image_info.rb b/lib/travis/client/artifacts_image_info.rb new file mode 100644 index 00000000..0f3cd000 --- /dev/null +++ b/lib/travis/client/artifacts_image_info.rb @@ -0,0 +1,15 @@ +require 'travis/client' + +module Travis + module Client + class ArtifactsImageInfo < Entity + preloadable + + attributes :name, :config_content, :description, :image_size + inspect_info :name + + one :artifacts_image_info + many :artifacts_image_infos + end + end +end diff --git a/travis.gemspec b/travis.gemspec index ef9393de..0f640fd0 100644 --- a/travis.gemspec +++ b/travis.gemspec @@ -333,6 +333,7 @@ Gem::Specification.new do |s| "lib/travis/client.rb", "lib/travis/client/account.rb", "lib/travis/client/artifact.rb", + "lib/travis/client/artifacts_image_info.rb", "lib/travis/client/auto_login.rb", "lib/travis/client/broadcast.rb", "lib/travis/client/build.rb", From 96d449c58f7cf8ae3491d301c181ed3b3ee24532 Mon Sep 17 00:00:00 2001 From: Andrii Mysko Date: Thu, 3 Feb 2022 14:40:45 +0200 Subject: [PATCH 3/7] Escape TAM image name --- lib/travis/cli/tam.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/travis/cli/tam.rb b/lib/travis/cli/tam.rb index 262cb1a7..b5cfd5d5 100644 --- a/lib/travis/cli/tam.rb +++ b/lib/travis/cli/tam.rb @@ -23,13 +23,13 @@ def run elsif update_image? 'v3/artifacts/config/update' elsif delete_image? - "v3/artifacts/#{delete_image}" + "v3/artifacts/#{CGI.escape(delete_image)}" elsif image_info? - "v3/artifacts/#{image_info}/info" + "v3/artifacts/#{CGI.escape(image_info)}/info" elsif logs? - "v3/artifacts/#{logs}/logs" + "v3/artifacts/#{CGI.escape(logs)}/logs" else - "v3/artifacts/#{build_status}/build_status" + "v3/artifacts/#{CGI.escape(build_status)}/build_status" end if delete_image? From bcc6a4e2b39d6bab9240c00b4feac63bf5070618 Mon Sep 17 00:00:00 2001 From: Andrii Mysko Date: Thu, 3 Feb 2022 17:06:03 +0200 Subject: [PATCH 4/7] Show errors --- lib/travis/cli/tam.rb | 7 ++++++- lib/travis/client/error.rb | 2 ++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/travis/cli/tam.rb b/lib/travis/cli/tam.rb index b5cfd5d5..ca654a49 100644 --- a/lib/travis/cli/tam.rb +++ b/lib/travis/cli/tam.rb @@ -39,7 +39,12 @@ def run config: File.read('.travis.lxd.yml') } - session.post(endpoint, JSON.dump(params), 'Content-Type' => 'application/json') + begin + session.post(endpoint, JSON.dump(params), 'Content-Type' => 'application/json') + rescue Travis::Client::ValidationFailed => e + error e.message + return + end else data = nil begin diff --git a/lib/travis/client/error.rb b/lib/travis/client/error.rb index 6e7ef277..0877f260 100644 --- a/lib/travis/client/error.rb +++ b/lib/travis/client/error.rb @@ -47,6 +47,8 @@ def parse_message(message) if @errors = response['errors'] and @errors.any? readable = @errors.map { |e| "#{e['field']}: #{e['code'].gsub('_', ' ')}" } message += " (#{readable.join(', ')})" + elsif @errors = response['error_message'] + message = response['error_message'] end message rescue JSON::ParserError From 5d4385713a8b7de6709b822da5259cdb34f7ba10 Mon Sep 17 00:00:00 2001 From: Stanislav Kolotinskiy Date: Thu, 3 Feb 2022 19:22:09 +0200 Subject: [PATCH 5/7] Fix image deletion --- lib/travis/cli/tam.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/travis/cli/tam.rb b/lib/travis/cli/tam.rb index ca654a49..e0ae679f 100644 --- a/lib/travis/cli/tam.rb +++ b/lib/travis/cli/tam.rb @@ -33,7 +33,7 @@ def run end if delete_image? - session.delete(endpoint) + session.delete_raw(endpoint) elsif create_image? || update_image? params = { config: File.read('.travis.lxd.yml') From 73bf215785ee4721e596832edb2e61f9222ec63a Mon Sep 17 00:00:00 2001 From: Andrii Mysko Date: Tue, 8 Feb 2022 11:23:45 +0200 Subject: [PATCH 6/7] Show warnings --- lib/travis/cli/tam.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/travis/cli/tam.rb b/lib/travis/cli/tam.rb index e0ae679f..d7e48936 100644 --- a/lib/travis/cli/tam.rb +++ b/lib/travis/cli/tam.rb @@ -40,7 +40,11 @@ def run } begin - session.post(endpoint, JSON.dump(params), 'Content-Type' => 'application/json') + response = session.post_raw(endpoint, JSON.dump(params), 'Content-Type' => 'application/json') + unless response['warnings'].nil? + warn color('Following warnings were generated:', [:bold, 'yellow']) + response['warnings'].each { |warning| warn color(warning, 'yellow') } + end rescue Travis::Client::ValidationFailed => e error e.message return From cbdc3687c068e89598aeaf9ab43bc69d141ef8d8 Mon Sep 17 00:00:00 2001 From: Andrii Mysko Date: Tue, 8 Feb 2022 15:37:27 +0200 Subject: [PATCH 7/7] Fix warnings --- lib/travis/cli/tam.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/travis/cli/tam.rb b/lib/travis/cli/tam.rb index d7e48936..6301fd74 100644 --- a/lib/travis/cli/tam.rb +++ b/lib/travis/cli/tam.rb @@ -41,7 +41,7 @@ def run begin response = session.post_raw(endpoint, JSON.dump(params), 'Content-Type' => 'application/json') - unless response['warnings'].nil? + unless response['warnings'].empty? warn color('Following warnings were generated:', [:bold, 'yellow']) response['warnings'].each { |warning| warn color(warning, 'yellow') } end