Skip to content

Commit

Permalink
(CAT-643) Add forge gem upload functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
danadoherty639 committed Oct 10, 2024
1 parent e8cd9ae commit 8d1c7f9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 39 deletions.
26 changes: 7 additions & 19 deletions lib/pdk/module/release.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = URI(forge_upload_url).path
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

Expand Down
1 change: 1 addition & 0 deletions pdk.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 3 additions & 20 deletions spec/unit/pdk/module/release_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -310,21 +310,6 @@
end
end

describe '#run_build' do
let(:builder) { double(Puppet::Modulebuilder::Builder) } # rubocop:disable RSpec/VerifiedDoubles

before do
logger = Logger.new(File.open(File::NULL, 'w')) # rubocop:disable PDK/FileOpen
allow(PDK).to receive(:logger).and_return(logger)
allow(Puppet::Modulebuilder::Builder).to receive(:new).and_return(builder)
end

it 'calls Puppet::Modulebuilder::Builder.build' do
expect(builder).to receive(:build)
instance.run_build
end
end

describe '#run_publish' do
let(:tarball_path) { '/does/not/exist' }
let(:http_response) { Net::HTTPSuccess.new(nil, nil, nil) }
Expand All @@ -335,10 +320,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

Expand All @@ -353,11 +338,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
Expand Down

0 comments on commit 8d1c7f9

Please sign in to comment.