Skip to content

Commit 54ce911

Browse files
hsbtmatzbot
authored andcommitted
[ruby/rubygems] Reverse to use attestation condition
ruby/rubygems@ea1f43c4ae
1 parent d2f9872 commit 54ce911

2 files changed

Lines changed: 56 additions & 42 deletions

File tree

lib/rubygems/commands/push_command.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,10 @@ def send_gem(name)
9292
private
9393

9494
def send_push_request(name, args)
95-
if RUBY_ENGINE == "jruby" || !attestation_supported_host?
96-
return send_push_request_without_attestation(name, args)
97-
end
98-
99-
begin
95+
# Attestation is only supported on rubygems.org with GitHub Actions (not JRuby)
96+
if RUBY_ENGINE != "jruby" && attestation_supported_host? && ENV["GITHUB_ACTIONS"]
10097
send_push_request_with_attestation(name, args)
101-
rescue StandardError => e
102-
alert_warning "Failed to push with attestation, retrying without attestation.\n#{e.full_message}"
98+
else
10399
send_push_request_without_attestation(name, args)
104100
end
105101
end
@@ -137,6 +133,9 @@ def send_push_request_with_attestation(name, args)
137133
], "multipart/form-data")
138134
request.add_field "Authorization", api_key
139135
end
136+
rescue StandardError => e
137+
alert_warning "Failed to push with attestation, retrying without attestation.\n#{e.full_message}"
138+
send_push_request_without_attestation(name, args)
140139
end
141140

142141
def attest!(name)

test/rubygems/test_gem_commands_push_command.rb

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -105,61 +105,76 @@ def test_execute_host
105105
def test_execute_attestation
106106
omit if RUBY_ENGINE == "jruby"
107107

108-
@response = "Successfully registered gem: freewill (1.0.0)"
109-
@fetcher.data["#{Gem.host}/api/v1/gems"] = HTTPResponseFactory.create(body: @response, code: 200, msg: "OK")
108+
ENV["GITHUB_ACTIONS"] = "true"
109+
begin
110+
@response = "Successfully registered gem: freewill (1.0.0)"
111+
@fetcher.data["#{Gem.host}/api/v1/gems"] = HTTPResponseFactory.create(body: @response, code: 200, msg: "OK")
110112

111-
File.write("#{@path}.sigstore.json", "attestation")
112-
@cmd.options[:args] = [@path]
113-
@cmd.options[:attestations] = ["#{@path}.sigstore.json"]
113+
File.write("#{@path}.sigstore.json", "attestation")
114+
@cmd.options[:args] = [@path]
115+
@cmd.options[:attestations] = ["#{@path}.sigstore.json"]
114116

115-
@cmd.execute
117+
@cmd.execute
116118

117-
assert_equal Gem::Net::HTTP::Post, @fetcher.last_request.class
118-
content_length = @fetcher.last_request["Content-Length"].to_i
119-
assert_equal content_length, @fetcher.last_request.body.length
120-
assert_attestation_multipart Gem.read_binary("#{@path}.sigstore.json")
119+
assert_equal Gem::Net::HTTP::Post, @fetcher.last_request.class
120+
content_length = @fetcher.last_request["Content-Length"].to_i
121+
assert_equal content_length, @fetcher.last_request.body.length
122+
assert_attestation_multipart Gem.read_binary("#{@path}.sigstore.json")
123+
ensure
124+
ENV.delete("GITHUB_ACTIONS")
125+
end
121126
end
122127

123128
def test_execute_attestation_auto
124129
omit if RUBY_ENGINE == "jruby"
125130

126-
@response = "Successfully registered gem: freewill (1.0.0)"
127-
@fetcher.data["#{Gem.host}/api/v1/gems"] = HTTPResponseFactory.create(body: @response, code: 200, msg: "OK")
131+
ENV["GITHUB_ACTIONS"] = "true"
132+
begin
133+
@response = "Successfully registered gem: freewill (1.0.0)"
134+
@fetcher.data["#{Gem.host}/api/v1/gems"] = HTTPResponseFactory.create(body: @response, code: 200, msg: "OK")
128135

129-
attestation_path = "#{@path}.sigstore.json"
130-
attestation_content = "auto-attestation"
131-
File.write(attestation_path, attestation_content)
132-
@cmd.options[:args] = [@path]
136+
attestation_path = "#{@path}.sigstore.json"
137+
attestation_content = "auto-attestation"
138+
File.write(attestation_path, attestation_content)
139+
@cmd.options[:args] = [@path]
133140

134-
@cmd.stub(:attest!, attestation_path) do
135-
@cmd.execute
136-
end
141+
@cmd.stub(:attest!, attestation_path) do
142+
@cmd.execute
143+
end
137144

138-
assert_equal Gem::Net::HTTP::Post, @fetcher.last_request.class
139-
content_length = @fetcher.last_request["Content-Length"].to_i
140-
assert_equal content_length, @fetcher.last_request.body.length
141-
assert_attestation_multipart attestation_content
145+
assert_equal Gem::Net::HTTP::Post, @fetcher.last_request.class
146+
content_length = @fetcher.last_request["Content-Length"].to_i
147+
assert_equal content_length, @fetcher.last_request.body.length
148+
assert_attestation_multipart attestation_content
149+
ensure
150+
ENV.delete("GITHUB_ACTIONS")
151+
end
142152
end
143153

144154
def test_execute_attestation_fallback
145155
omit if RUBY_ENGINE == "jruby"
146156

147-
@response = "Successfully registered gem: freewill (1.0.0)"
148-
@fetcher.data["#{Gem.host}/api/v1/gems"] = HTTPResponseFactory.create(body: @response, code: 200, msg: "OK")
157+
ENV["GITHUB_ACTIONS"] = "true"
158+
begin
159+
@response = "Successfully registered gem: freewill (1.0.0)"
160+
@fetcher.data["#{Gem.host}/api/v1/gems"] = HTTPResponseFactory.create(body: @response, code: 200, msg: "OK")
149161

150-
@cmd.options[:args] = [@path]
162+
@cmd.options[:args] = [@path]
151163

152-
@cmd.stub(:attest!, proc { raise Gem::Exception, "boom" }) do
153-
use_ui @ui do
154-
@cmd.execute
164+
@cmd.stub(:attest!, proc { raise Gem::Exception, "boom" }) do
165+
use_ui @ui do
166+
@cmd.execute
167+
end
155168
end
156-
end
157169

158-
assert_match "Failed to push with attestation, retrying without attestation.", @ui.error
159-
assert_equal Gem::Net::HTTP::Post, @fetcher.last_request.class
160-
assert_equal Gem.read_binary(@path), @fetcher.last_request.body
161-
assert_equal "application/octet-stream",
162-
@fetcher.last_request["Content-Type"]
170+
assert_match "Failed to push with attestation, retrying without attestation.", @ui.error
171+
assert_equal Gem::Net::HTTP::Post, @fetcher.last_request.class
172+
assert_equal Gem.read_binary(@path), @fetcher.last_request.body
173+
assert_equal "application/octet-stream",
174+
@fetcher.last_request["Content-Type"]
175+
ensure
176+
ENV.delete("GITHUB_ACTIONS")
177+
end
163178
end
164179

165180
def test_execute_attestation_skipped_on_non_rubygems_host

0 commit comments

Comments
 (0)