Skip to content

Commit

Permalink
Merge pull request #1403 from puppetlabs/cat_643
Browse files Browse the repository at this point in the history
(CAT-643) Add forge gem upload functionality
  • Loading branch information
jordanbreen28 authored Oct 15, 2024
2 parents d2c874f + 8dce59e commit 302c910
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 24 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 = 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

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
9 changes: 4 additions & 5 deletions spec/unit/pdk/module/release_spec.rb
Original file line number Diff line number Diff line change
@@ -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 }
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down

0 comments on commit 302c910

Please sign in to comment.