Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(CAT-643) Add forge gem upload functionality #1403

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading