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 501d535
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 30 deletions.
27 changes: 8 additions & 19 deletions lib/pdk/module/release.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'pdk'
require 'uri'
require 'puppet/modulebuilder'
require 'puppet_forge'

module PDK
module Module
Expand Down Expand Up @@ -147,28 +148,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.0'

spec.metadata['rubygems_mfa_required'] = 'true'
end
14 changes: 3 additions & 11 deletions spec/unit/pdk/module/release_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -333,31 +333,23 @@

before do
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

context 'when the tarball does not exist' do
before do
expect(PDK::Util::Filesystem).to receive(:file?).with(tarball_path).and_return(false)
end

it 'raises' do
expect { instance.run_publish({}, tarball_path) }.to raise_error(PDK::CLI::ExitWithError)
end
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 501d535

Please sign in to comment.