From 8dce59eda8a0bce1c16986cad31f02993f791ba1 Mon Sep 17 00:00:00 2001 From: Dana Doherty Date: Fri, 4 Oct 2024 10:47:59 +0100 Subject: [PATCH] (CAT-643) Add forge gem upload functionality --- lib/pdk/module/release.rb | 26 +++++++------------------- pdk.gemspec | 1 + spec/unit/pdk/module/release_spec.rb | 9 ++++----- 3 files changed, 12 insertions(+), 24 deletions(-) diff --git a/lib/pdk/module/release.rb b/lib/pdk/module/release.rb index 0ea7d31d7..926f0d723 100644 --- a/lib/pdk/module/release.rb +++ b/lib/pdk/module/release.rb @@ -147,28 +147,16 @@ def run_publish(_opts, tarball_path) validate_publish_options! raise PDK::CLI::ExitWithError, format('Module tarball %{tarball_path} does not exist', tarball_path: tarball_path) unless PDK::Util::Filesystem.file?(tarball_path) - # TODO: Replace this code when the upload functionality is added to the forge ruby gem - require 'base64' - file_data = Base64.encode64(PDK::Util::Filesystem.read_file(tarball_path, open_args: 'rb')) - PDK.logger.info 'Uploading tarball to puppet forge...' - uri = URI(forge_upload_url) - require 'net/http' - request = Net::HTTP::Post.new(uri.path) - request['Authorization'] = "Bearer #{forge_token}" - request['Content-Type'] = 'application/json' - data = { file: file_data } - - request.body = data.to_json - - require 'openssl' - use_ssl = uri.instance_of?(URI::HTTPS) - response = Net::HTTP.start(uri.host, uri.port, use_ssl: use_ssl) do |http| - http.request(request) + begin + require 'puppet_forge' + PuppetForge.host = forge_upload_url + PuppetForge::Connection.authorization = forge_token + PuppetForge::V3::Release.upload(tarball_path) + rescue StandardError => e + raise PDK::CLI::ExitWithError, format('Error uploading to Puppet Forge: %{result}', result: e.message) end - raise PDK::CLI::ExitWithError, format('Error uploading to Puppet Forge: %{result}', result: response.body) unless response.is_a?(Net::HTTPSuccess) - PDK.logger.info 'Publish to Forge was successful' end diff --git a/pdk.gemspec b/pdk.gemspec index 496570c56..2bbc26432 100644 --- a/pdk.gemspec +++ b/pdk.gemspec @@ -49,6 +49,7 @@ Gem::Specification.new do |spec| spec.add_runtime_dependency 'diff-lcs', '>= 1.5.0' spec.add_runtime_dependency 'json_pure', '~> 2.6.3' spec.add_runtime_dependency 'pathspec', '~> 1.1' + spec.add_runtime_dependency 'puppet_forge', '~> 5.0' spec.metadata['rubygems_mfa_required'] = 'true' end diff --git a/spec/unit/pdk/module/release_spec.rb b/spec/unit/pdk/module/release_spec.rb index eb941c4c6..192e8db59 100644 --- a/spec/unit/pdk/module/release_spec.rb +++ b/spec/unit/pdk/module/release_spec.rb @@ -1,6 +1,7 @@ require 'spec_helper' require 'pdk/module/release' require 'puppet/modulebuilder' +require 'puppet_forge' describe PDK::Module::Release do let(:module_path) { nil } @@ -335,10 +336,10 @@ allow(instance).to receive_messages(forge_token: 'abc123', forge_upload_url: 'https://badapi.puppetlabs.com/v3/releases') allow(PDK::Util::Filesystem).to receive(:file?).with(tarball_path).and_return(true) allow(PDK::Util::Filesystem).to receive(:read_file).with(tarball_path, Hash).and_return('tarball_contents') - allow(Net::HTTP).to receive(:start).and_return(http_response) end it 'uploads the tarball to the Forge' do + expect(PuppetForge::V3::Release).to receive(:upload).with(tarball_path).and_return(http_response) instance.run_publish({}, tarball_path) end @@ -353,11 +354,9 @@ end context 'when the Forge returns an error' do - let(:http_response) { Net::HTTPUnauthorized.new(nil, nil, nil) } - it 'raises' do - allow(http_response).to receive(:body) - expect { instance.run_publish({}, tarball_path) }.to raise_error(PDK::CLI::ExitWithError) + expect(PuppetForge::V3::Release).to receive(:upload).with(tarball_path).and_raise(PuppetForge::ReleaseForbidden) + expect { instance.run_publish({}, tarball_path) }.to raise_error(PDK::CLI::ExitWithError, /Error uploading to Puppet Forge/) end end end