Skip to content

Commit 118dd6f

Browse files
Preserve draft releases when the release list is stale.
1 parent a93ad87 commit 118dd6f

7 files changed

Lines changed: 75 additions & 13 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/.bundle/
22
/.context/
3+
/.covered.db
34
/coverage/
45
/gems.locked
56
/pkg/

‎config/sus.rb‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
# Released under the MIT License.
4+
# Copyright, 2026, by Samuel Williams.
5+
6+
require "covered/sus"
7+
include Covered::Sus

‎fixtures/bake/gem/github/recovery_publisher.rb‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module GitHub
1212
# real-repository integration tests; this fixture exercises interruption/retry.
1313
class RecoveryPublisher < Publisher
1414
attr_accessor :remote_digest, :fail_release, :fail_attestation, :fail_receipt_verification
15-
attr_accessor :releases, :artifacts, :fail_preservation
15+
attr_accessor :releases, :artifacts, :fail_preservation, :stale_release_list
1616
attr_reader :commands, :stored_files
1717

1818
def initialize(root)
@@ -44,8 +44,6 @@ def system(*arguments, **options)
4444
@remote_digest = load_receipt.fetch(:sha256)
4545
end
4646
case arguments[0, 3]
47-
when ["gh", "release", "create"]
48-
@releases << {"tag_name" => arguments[3], "draft" => true, "target_commitish" => arguments[arguments.index("--target") + 1], "assets" => []}
4947
when ["gh", "release", "upload"]
5048
raise "Preservation failed" if @fail_preservation
5149
file = arguments[4]
@@ -63,11 +61,25 @@ def system(*arguments, **options)
6361

6462
def readlines(*arguments, **options)
6563
@commands << arguments
66-
return [JSON.generate([@releases])] if arguments[0, 2] == ["gh", "api"]
64+
if arguments[0, 2] == ["gh", "api"]
65+
if arguments[2] == "graphql"
66+
tag = arguments.find{|argument| argument.start_with?("tag=")}.delete_prefix("tag=")
67+
index = @releases.index{|release| release.fetch("tag_name") == tag}
68+
return [JSON.generate(data: {repository: {release: index && {databaseId: index + 1}}})]
69+
end
70+
if arguments.include?("POST")
71+
release = JSON.parse(File.read(arguments[arguments.index("--input") + 1])).merge("assets" => [])
72+
@releases << release
73+
return [JSON.generate(release)]
74+
end
75+
releases = @releases.each_with_index.map{|release, index| release.merge("id" => index + 1)}
76+
return [JSON.generate([@stale_release_list ? [] : releases])]
77+
end
6778
[]
6879
end
6980

7081
def api(path)
82+
return @releases.fetch(Integer(path.delete_prefix("releases/")) - 1) if path.start_with?("releases/")
7183
{"artifacts" => @artifacts}
7284
end
7385

‎gems.rb‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
gemspec
66

77
group :test do
8+
gem "covered"
89
gem "sus"
910
gem "rubocop"
1011
gem "rubocop-md"

‎lib/bake/gem/github/publisher.rb‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,16 @@ def github_release(tag)
120120
pages = JSON.parse(readlines("gh", "api", "--paginate", "--slurp", "repos/#{@repository}/releases?per_page=100", chdir: @root).join)
121121
matches = pages.flatten(1).select{|release| release.fetch("tag_name") == tag}
122122
raise "Multiple GitHub releases have the same tag." if matches.size > 1
123-
matches.first
123+
if release = matches.first
124+
return api("releases/#{release.fetch('id')}")
125+
end
126+
# Resolve pending tags directly when the REST list has not caught up:
127+
owner, name = @repository.split("/", 2)
128+
query = "query($owner: String!, $name: String!, $tag: String!) { repository(owner: $owner, name: $name) { release(tagName: $tag) { databaseId } } }"
129+
result = JSON.parse(readlines("gh", "api", "graphql", "-f", "query=#{query}", "-f", "owner=#{owner}", "-f", "name=#{name}", "-f", "tag=#{tag}", chdir: @root).join)
130+
if release = result.fetch("data").fetch("repository").fetch("release")
131+
api("releases/#{release.fetch('databaseId')}")
132+
end
124133
end
125134

126135
def guard_release(release, commit)
@@ -137,12 +146,15 @@ def preserve_release(receipt)
137146
tag = "v#{receipt.fetch(:version)}"
138147
release = github_release(tag)
139148
unless release
140-
Tempfile.create("release-notes") do |file|
141-
file.write("#{receipt.fetch(:pull_request_url)}\n\nSource: #{receipt.fetch(:commit)}\nSHA256: #{receipt.fetch(:sha256)}\n\nSee releases.md at the release tag for release notes.\n")
149+
Tempfile.create("release") do |file|
150+
file.write(JSON.generate(
151+
tag_name: tag, draft: true, target_commitish: receipt.fetch(:commit), name: "#{receipt.fetch(:name)} #{tag}",
152+
body: "#{receipt.fetch(:pull_request_url)}\n\nSource: #{receipt.fetch(:commit)}\nSHA256: #{receipt.fetch(:sha256)}\n\nSee releases.md at the release tag for release notes.\n"
153+
))
142154
file.flush
143-
system("gh", "release", "create", tag, "--repo", @repository, "--draft", "--target", receipt.fetch(:commit), "--title", "#{receipt.fetch(:name)} #{tag}", "--notes-file", file.path, chdir: @root)
155+
# The release list can remain stale after a successful creation:
156+
release = JSON.parse(readlines("gh", "api", "repos/#{@repository}/releases", "--method", "POST", "--input", file.path, chdir: @root).join)
144157
end
145-
release = github_release(tag) or raise "Draft release was not retained."
146158
end
147159
guard_release(release, receipt.fetch(:commit))
148160
assets = release.fetch("assets")

‎releases.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Releases
22

3+
## Unreleased
4+
5+
- Preserve and recover release files even when GitHub's release list is stale.
6+
37
## v0.0.4
48

59
- Preserve verified release files in a draft GitHub release before uploading to RubyGems, so reruns can recover when Actions artifacts disappear.

‎test/recovery.rb‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,36 @@ def restore
125125
expect(@publisher.commands.count{|args| args[0, 3] == ["gh", "release", "upload"]}).to be == 4
126126
end
127127

128-
it "requires confirmation that a newly created draft is retained" do
128+
it "uses the created draft response when the release list remains stale" do
129+
@publisher.stale_release_list = true
130+
receipt = @publisher.publish(42)
131+
expect(@publisher.releases.size).to be == 1
132+
expect(@publisher.releases.first.fetch("draft")).to be == false
133+
expect(@publisher.remote_digest).to be == receipt.fetch(:sha256)
134+
expect(@publisher.stored_files.keys.sort).to be == ["example-1.0.1.gem", "example-1.0.1.gem.sigstore.json", "provenance.sigstore.json", "release.json"]
135+
end
136+
137+
it "restores an existing draft hidden by a stale release list without creating another" do
138+
@publisher.fail_release = true
139+
expect{@publisher.publish(42)}.to raise_exception(RuntimeError, message: be =~ /GitHub unavailable/)
140+
@publisher.stale_release_list = true
141+
@publisher.fail_release = false
142+
restore
143+
@publisher.publish(42)
144+
expect(@publisher.releases.size).to be == 1
145+
expect(@publisher.releases.first.fetch("draft")).to be == false
146+
expect(@publisher.commands.count{|args| args[0, 2] == ["gem", "push"]}).to be == 1
147+
end
148+
149+
it "does not interpret a failed direct lookup as an absent release" do
129150
mock(@publisher) do |wrapper|
130-
wrapper.replace(:readlines){|*arguments, **options| arguments[0, 2] == ["gh", "api"] ? ["[[]]"] : []}
151+
wrapper.wrap(:readlines) do |original, *arguments, **options|
152+
raise "GitHub lookup failed" if arguments[0, 3] == ["gh", "api", "graphql"]
153+
original.call(*arguments, **options)
154+
end
131155
end
132-
expect{@publisher.publish(42)}.to raise_exception(RuntimeError, message: be =~ /Draft release was not retained/)
156+
expect{@publisher.publish(42)}.to raise_exception(RuntimeError, message: be =~ /lookup failed/)
157+
expect(@publisher.releases).to be == []
133158
expect(@publisher.remote_digest).to be_nil
134159
end
135160

@@ -143,7 +168,7 @@ def restore
143168
it "refuses a published version containing different bytes" do
144169
@publisher.remote_digest = Digest::SHA256.hexdigest("Someone else's artifact")
145170
expect{@publisher.publish(42)}.to raise_exception(RuntimeError, message: be =~ /different bytes/)
146-
expect(@publisher.commands.any?{|args| args[0, 3] == ["gh", "release", "create"]}).to be == false
171+
expect(@publisher.commands.any?{|args| args.include?("POST")}).to be == false
147172
end
148173

149174
it "stops before publication when the receipt attestation is invalid" do

0 commit comments

Comments
 (0)