|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Released under the MIT License. |
| 4 | +# Copyright, 2026, by Samuel Williams. |
| 5 | + |
| 6 | +require "bake/gem/github/publisher" |
| 7 | + |
| 8 | +module Bake |
| 9 | + module Gem |
| 10 | + module GitHub |
| 11 | + # A simulated registry and GitHub finalizer. Core content validation has its own |
| 12 | + # real-repository integration tests; this fixture exercises interruption/retry. |
| 13 | + class RecoveryPublisher < Publisher |
| 14 | + attr_accessor :remote_digest, :fail_release, :fail_attestation, :fail_receipt_verification |
| 15 | + attr_accessor :releases, :artifacts, :fail_preservation |
| 16 | + attr_reader :commands, :stored_files |
| 17 | + |
| 18 | + def initialize(root) |
| 19 | + super |
| 20 | + @commands = [] |
| 21 | + @releases = [] |
| 22 | + @artifacts = [] |
| 23 | + @stored_files = {} |
| 24 | + @release = Object.new |
| 25 | + def @release.resolve(reference) |
| 26 | + "a" * 40 |
| 27 | + end |
| 28 | + def @release.validate(**options) |
| 29 | + {name: "example", version: "1.0.1", commit: "a" * 40} |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + def merged(number) |
| 34 | + {"merge_commit_sha" => "a" * 40, "number" => 42, "html_url" => "https://github.com/socketry/example/pull/42"} |
| 35 | + end |
| 36 | + |
| 37 | + def system(*arguments, **options) |
| 38 | + @commands << arguments |
| 39 | + raise "Attestation verification failed" if @fail_attestation && arguments[0, 3] == ["gem", "exec", "sigstore-cli:0.2.3"] |
| 40 | + if @fail_receipt_verification && arguments[0, 3] == ["gh", "attestation", "verify"] && arguments[3].end_with?("/release.json") |
| 41 | + raise "Receipt attestation verification failed" |
| 42 | + end |
| 43 | + if arguments[0, 2] == ["gem", "push"] |
| 44 | + @remote_digest = load_receipt.fetch(:sha256) |
| 45 | + end |
| 46 | + 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" => []} |
| 49 | + when ["gh", "release", "upload"] |
| 50 | + raise "Preservation failed" if @fail_preservation |
| 51 | + file = arguments[4] |
| 52 | + @stored_files[File.basename(file)] = File.binread(file) |
| 53 | + @releases.first.fetch("assets") << {"name" => File.basename(file), "digest" => "sha256:#{Digest::SHA256.file(file).hexdigest}"} |
| 54 | + when ["gh", "release", "download"], ["gh", "run", "download"] |
| 55 | + path = arguments[arguments.index("--dir") + 1] |
| 56 | + @stored_files.each{|name, content| File.binwrite(File.join(path, name), content)} |
| 57 | + when ["gh", "release", "edit"] |
| 58 | + raise "GitHub unavailable after upload" if @fail_release |
| 59 | + @releases.first["draft"] = false |
| 60 | + end |
| 61 | + true |
| 62 | + end |
| 63 | + |
| 64 | + def readlines(*arguments, **options) |
| 65 | + @commands << arguments |
| 66 | + return [JSON.generate([@releases])] if arguments[0, 2] == ["gh", "api"] |
| 67 | + [] |
| 68 | + end |
| 69 | + |
| 70 | + def api(path) |
| 71 | + {"artifacts" => @artifacts} |
| 72 | + end |
| 73 | + |
| 74 | + private |
| 75 | + |
| 76 | + def gem_command(*arguments) |
| 77 | + system("gem", *arguments, chdir: @root) |
| 78 | + end |
| 79 | + |
| 80 | + def guard_environment |
| 81 | + end |
| 82 | + |
| 83 | + def registry_digest(name, version) |
| 84 | + @remote_digest |
| 85 | + end |
| 86 | + |
| 87 | + def registry_get(url) |
| 88 | + JSON.generate([{bundle: {mediaType: "test"}}]) |
| 89 | + end |
| 90 | + end |
| 91 | + end |
| 92 | + end |
| 93 | +end |
0 commit comments