Skip to content

Commit 302c910

Browse files
Merge pull request #1403 from puppetlabs/cat_643
(CAT-643) Add forge gem upload functionality
2 parents d2c874f + 8dce59e commit 302c910

File tree

3 files changed

+12
-24
lines changed

3 files changed

+12
-24
lines changed

lib/pdk/module/release.rb

+7-19
Original file line numberDiff line numberDiff line change
@@ -147,28 +147,16 @@ def run_publish(_opts, tarball_path)
147147
validate_publish_options!
148148
raise PDK::CLI::ExitWithError, format('Module tarball %{tarball_path} does not exist', tarball_path: tarball_path) unless PDK::Util::Filesystem.file?(tarball_path)
149149

150-
# TODO: Replace this code when the upload functionality is added to the forge ruby gem
151-
require 'base64'
152-
file_data = Base64.encode64(PDK::Util::Filesystem.read_file(tarball_path, open_args: 'rb'))
153-
154150
PDK.logger.info 'Uploading tarball to puppet forge...'
155-
uri = URI(forge_upload_url)
156-
require 'net/http'
157-
request = Net::HTTP::Post.new(uri.path)
158-
request['Authorization'] = "Bearer #{forge_token}"
159-
request['Content-Type'] = 'application/json'
160-
data = { file: file_data }
161-
162-
request.body = data.to_json
163-
164-
require 'openssl'
165-
use_ssl = uri.instance_of?(URI::HTTPS)
166-
response = Net::HTTP.start(uri.host, uri.port, use_ssl: use_ssl) do |http|
167-
http.request(request)
151+
begin
152+
require 'puppet_forge'
153+
PuppetForge.host = forge_upload_url
154+
PuppetForge::Connection.authorization = forge_token
155+
PuppetForge::V3::Release.upload(tarball_path)
156+
rescue StandardError => e
157+
raise PDK::CLI::ExitWithError, format('Error uploading to Puppet Forge: %{result}', result: e.message)
168158
end
169159

170-
raise PDK::CLI::ExitWithError, format('Error uploading to Puppet Forge: %{result}', result: response.body) unless response.is_a?(Net::HTTPSuccess)
171-
172160
PDK.logger.info 'Publish to Forge was successful'
173161
end
174162

pdk.gemspec

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Gem::Specification.new do |spec|
4949
spec.add_runtime_dependency 'diff-lcs', '>= 1.5.0'
5050
spec.add_runtime_dependency 'json_pure', '~> 2.6.3'
5151
spec.add_runtime_dependency 'pathspec', '~> 1.1'
52+
spec.add_runtime_dependency 'puppet_forge', '~> 5.0'
5253

5354
spec.metadata['rubygems_mfa_required'] = 'true'
5455
end

spec/unit/pdk/module/release_spec.rb

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require 'spec_helper'
22
require 'pdk/module/release'
33
require 'puppet/modulebuilder'
4+
require 'puppet_forge'
45

56
describe PDK::Module::Release do
67
let(:module_path) { nil }
@@ -335,10 +336,10 @@
335336
allow(instance).to receive_messages(forge_token: 'abc123', forge_upload_url: 'https://badapi.puppetlabs.com/v3/releases')
336337
allow(PDK::Util::Filesystem).to receive(:file?).with(tarball_path).and_return(true)
337338
allow(PDK::Util::Filesystem).to receive(:read_file).with(tarball_path, Hash).and_return('tarball_contents')
338-
allow(Net::HTTP).to receive(:start).and_return(http_response)
339339
end
340340

341341
it 'uploads the tarball to the Forge' do
342+
expect(PuppetForge::V3::Release).to receive(:upload).with(tarball_path).and_return(http_response)
342343
instance.run_publish({}, tarball_path)
343344
end
344345

@@ -353,11 +354,9 @@
353354
end
354355

355356
context 'when the Forge returns an error' do
356-
let(:http_response) { Net::HTTPUnauthorized.new(nil, nil, nil) }
357-
358357
it 'raises' do
359-
allow(http_response).to receive(:body)
360-
expect { instance.run_publish({}, tarball_path) }.to raise_error(PDK::CLI::ExitWithError)
358+
expect(PuppetForge::V3::Release).to receive(:upload).with(tarball_path).and_raise(PuppetForge::ReleaseForbidden)
359+
expect { instance.run_publish({}, tarball_path) }.to raise_error(PDK::CLI::ExitWithError, /Error uploading to Puppet Forge/)
361360
end
362361
end
363362
end

0 commit comments

Comments
 (0)