ruby-gem-release 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6ef326bc606b9d966fa15697bd078438b4d4afa90f85126ceccae0fba679dab1
4
- data.tar.gz: a4272a4fadf4ab595677181e1cbd779f3b7a359da9b9ea73ed8e1b695fe16a0a
3
+ metadata.gz: 6a4950ac3c14d5b45f4cfd872d6bb692cb18e8c47186804dd0e1278867b3cbea
4
+ data.tar.gz: 6446cb2d14aab627429cf4670368d36c4e978ad822dcaf69cadae7968ccd028f
5
5
  SHA512:
6
- metadata.gz: 7801b51e766bf896fd4112fbfb6ab983746d97bb528831f5665f945f6273d11834980f024fdc72c05adaf16d9fa1f2f2158f88e027f13c9d0ab764e5bf58ca97
7
- data.tar.gz: c6268c91f1fb9eeaddea3a19dbae5afea1ac2f98f316eec29d0f94ac375e66cc1b70ebbce1f73c42d6c7ea56bfe9d5003bdbce1ae77acba8ed42f7e87cf8457f
6
+ metadata.gz: 78e16cb2444ea49cae3cca6d1ab2b178cf83d374c07772273a8bcc6accd8a5a84e20d2dd3bb1999e2856d62bb09e5049d20b9f5daf4d8cd8b5348297939ba4b4
7
+ data.tar.gz: 8e7fc569858c3a93e99daab287036834ed839a5c3511cb00c97ad5bc63df6e68154597713b7248d3c4c42015c2658f45b65048045ec28c6186ff4bbbed0ad4bb
data/CHANGELOG.md CHANGED
@@ -1,5 +1,25 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.7.0 (2026/11/20)
4
+
5
+ - [#58](https://github.com/dblock/ruby-gem-release/pull/58): Replace the ruby rocket logo with a faceted ruby and upward release arrow - [@dblock](https://github.com/dblock).
6
+ - [#60](https://github.com/dblock/ruby-gem-release/pull/60): Preserve gem platform identity in archive filenames and manifests and verify RubyGems publications per platform - [@dblock](https://github.com/dblock).
7
+ - [#61](https://github.com/dblock/ruby-gem-release/pull/61): Control GitHub's latest-release selection during creation and idempotent metadata updates - [@dblock](https://github.com/dblock).
8
+ - [#62](https://github.com/dblock/ruby-gem-release/pull/62): **Breaking:** Make the native Markdown parser optional for portable core installation; Markdown users must explicitly add Commonmarker - [@dblock](https://github.com/dblock).
9
+ - [#63](https://github.com/dblock/ruby-gem-release/pull/63): Extract reviewed release notes with configurable headings and reference-link expansion - [@dblock](https://github.com/dblock).
10
+ - [#65](https://github.com/dblock/ruby-gem-release/pull/65): Audit exact archive file lists and Git source bytes with explicit generated-file exceptions - [@dblock](https://github.com/dblock).
11
+ - [#66](https://github.com/dblock/ruby-gem-release/pull/66): Publish explicit package collections and checksum-verified prebuilt archives with all-member preflight - [@dblock](https://github.com/dblock).
12
+ - [#67](https://github.com/dblock/ruby-gem-release/pull/67): Publish signed, already-reviewed source on explicit branches without editing files, creating tags, pushing Git refs, or bumping versions - [@dblock](https://github.com/dblock).
13
+ - [#68](https://github.com/dblock/ruby-gem-release/pull/68): Load prebuilt archives from original build manifests with explicit expected identities and content audits - [@dblock](https://github.com/dblock).
14
+ - [#69](https://github.com/dblock/ruby-gem-release/pull/69): Allow configuring the Intridea release date format with `date_format:`, and recognize release headings in any date format - [@numbata](https://github.com/numbata).
15
+ - [#70](https://github.com/dblock/ruby-gem-release/pull/70), [#75](https://github.com/dblock/ruby-gem-release/pull/75), [#76](https://github.com/dblock/ruby-gem-release/pull/76): Update configured version references in selected files and globs to the released version during the next development bump, wrapping Ruby constant, plain text, or custom version objects - [@dblock](https://github.com/dblock).
16
+ - [#71](https://github.com/dblock/ruby-gem-release/pull/71): Run Danger with full history, exporting reports for a separate commenting workflow so fork contributions need no write permissions - [@dblock](https://github.com/dblock).
17
+ - [#72](https://github.com/dblock/ruby-gem-release/pull/72): Support plain text version files - [@dblock](https://github.com/dblock).
18
+ - [#73](https://github.com/dblock/ruby-gem-release/pull/73): Run grouped installed-gem integration suites in parallel CI matrix jobs - [@dblock](https://github.com/dblock).
19
+ - [#76](https://github.com/dblock/ruby-gem-release/pull/76): **Breaking:** Require custom version objects to implement `read`, `set!`, and `increment!`, validated at workflow construction - [@dblock](https://github.com/dblock).
20
+ - [#77](https://github.com/dblock/ruby-gem-release/pull/77): Compose a shared lifecycle in the reviewed workflow instead of inheriting from the editing workflow; it no longer accepts `version:` or `changelog:` - [@dblock](https://github.com/dblock).
21
+ - [#78](https://github.com/dblock/ruby-gem-release/pull/78): Wrap Git in reviewed source control rather than inheriting from it, so reviewed releases cannot stage, commit, tag, or push - [@dblock](https://github.com/dblock).
22
+
3
23
  ## 0.6.0 (2026/11/11)
4
24
 
5
25
  - [#50](https://github.com/dblock/ruby-gem-release/pull/50): Add optional release spec helpers, local publication adapters, and a complete example for testing project-specific release customizations in isolated repositories - [@dblock](https://github.com/dblock).
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- <img src="assets/logo.png" alt="A ruby gemstone launching like a rocket" width="160">
1
+ <img src="assets/logo.png" alt="A faceted ruby gemstone with an upward release arrow" width="160">
2
2
 
3
3
  # Ruby Gem Release
4
4
 
@@ -6,7 +6,7 @@
6
6
 
7
7
  **Release your Ruby gem from GitHub Actions without maintaining custom release scripts or storing a RubyGems API key.**
8
8
 
9
- One dispatch updates the changelog, tests and publishes the gem, creates its GitHub Release, and prepares the next development iteration. Choose your version and changelog formats; the workflow handles the sequence.
9
+ Implements [Releases as Code](https://code.dblock.org/2026/11/12/releases-as-code.html) for Ruby gems. Release from GitHub Actions, run your project's tests, publish to RubyGems, optionally update the changelog, create a Git tag and GitHub Release, and prepare the next development iteration. Choose your version and changelog formats. See [npm-package-release](https://github.com/dblock/npm-package-release) and [rust-crate-release](https://github.com/dblock/rust-crate-release) for similar projects in the JavaScript and Rust ecosystems.
10
10
 
11
11
  The default setup below requires Ruby 3.3+, GitHub Actions, and RubyGems Trusted Publishing.
12
12
 
@@ -14,7 +14,7 @@ The default setup below requires Ruby 3.3+, GitHub Actions, and RubyGems Trusted
14
14
 
15
15
  ### 1. Configure Your Gem
16
16
 
17
- Add `ruby-gem-release` to the development dependencies and install the standard workflow in your Rakefile. Choose your version file and changelog format explicitly:
17
+ Add `ruby-gem-release` and `commonmarker ~> 2.10` to the development dependencies and install the standard workflow in your Rakefile. Commonmarker is optional for projects not using Markdown changelogs; the core has no native dependencies. Choose your version file and changelog format explicitly:
18
18
 
19
19
  ```ruby
20
20
  require 'ruby-gem-release/workflow/ruby_gem'
@@ -53,7 +53,7 @@ permissions:
53
53
 
54
54
  jobs:
55
55
  release:
56
- uses: dblock/ruby-gem-release/.github/workflows/release.yml@v0.5.1
56
+ uses: dblock/ruby-gem-release/.github/workflows/release.yml@v0.6.0
57
57
  with:
58
58
  ruby-version: '3.3'
59
59
  version: ${{ inputs.version }}
@@ -57,13 +57,6 @@ module RubyGemRelease
57
57
  raise ArgumentError, "cannot release #{version}: empty release notes in #{@path}; add release notes before retrying"
58
58
  end
59
59
 
60
- def notes?(node)
61
- return false if %i[heading html_block html_inline].include?(node.type)
62
- return !node.string_content.strip.empty? if %i[text code code_block].include?(node.type)
63
-
64
- node.each.any? { |child| notes?(child) }
65
- end
66
-
67
60
  def editable_lines(content)
68
61
  excluded = excluded_lines(content)
69
62
  offset = 0
@@ -5,12 +5,15 @@ module RubyGemRelease
5
5
  class Intridea < Base
6
6
  UNRELEASED = /\A\#{1,6} \d+\.\d+\.\d+ \(Next\)\z/
7
7
  PLACEHOLDER = '- Your contribution here.'
8
+ DATE_FORMAT = '%Y/%m/%d'
8
9
 
9
- def initialize(path: 'CHANGELOG.md', placeholder: PLACEHOLDER)
10
+ def initialize(path: 'CHANGELOG.md', placeholder: PLACEHOLDER, date_format: DATE_FORMAT)
10
11
  raise ArgumentError, 'placeholder must be a single nonempty line' unless placeholder.is_a?(String) && !placeholder.empty? && !placeholder.match?(/[\r\n]/)
12
+ raise ArgumentError, 'date_format must be a nonempty single-line strftime string or a callable' unless valid_date_format?(date_format)
11
13
 
12
14
  super(path: path)
13
15
  @placeholder = placeholder
16
+ @date_format = date_format
14
17
  end
15
18
 
16
19
  private
@@ -27,18 +30,25 @@ module RubyGemRelease
27
30
  prefix = heading_prefix(content, position)
28
31
  check_release_absent(content, version)
29
32
  section_end = section_end_position(content, position, prefix.length)
30
- content[position...section_end] = "#{prefix} #{version} (#{date.strftime('%Y/%m/%d')})" +
33
+ content[position...section_end] = release_heading(prefix, version, date) +
31
34
  release_notes(content[line_end(content, position, separator)...section_end], separator, version)
32
35
  File.write(@path, content)
33
36
  end
34
37
 
38
+ # Checked before writing, so a date_format that produces an unrecognizable heading leaves the file unchanged.
39
+ def release_heading(prefix, version, date)
40
+ heading = "#{prefix} #{version} (#{format_date(date)})"
41
+ raise ArgumentError, "date_format produced an invalid release heading: #{heading.inspect}" unless release_pattern(version).match?(heading)
42
+
43
+ heading
44
+ end
45
+
35
46
  def update_next(version:, released_version:)
36
47
  validate_version(version)
37
48
  validate_version(released_version)
38
49
  content = File.read(@path)
39
50
  separator = newline(content)
40
- pattern = %r{\A\#{1,6} #{Regexp.escape(released_version)} \(\d{4}/\d{2}/\d{2}\)\z}
41
- position = heading(content, pattern)
51
+ position = heading(content, release_pattern(released_version))
42
52
  prefix = heading_prefix(content, position)
43
53
  return if heading_positions(content, /\A\#{1,6} #{Regexp.escape(version)} \(Next\)\z/).one?
44
54
 
@@ -55,8 +65,22 @@ module RubyGemRelease
55
65
  check_absent(content, release_pattern(version), 'release heading already exists')
56
66
  end
57
67
 
68
+ # Matches a parenthesized heading with a digit, so a date in any format counts as released while (Next) and other words do not.
58
69
  def release_pattern(version)
59
- %r{\A\#{1,6} #{Regexp.escape(version)} \(\d{4}/\d{2}/\d{2}\)\z}
70
+ /\A\#{1,6} #{Regexp.escape(version)} \((?!Next\))[^\r\n]*\d[^\r\n]*\)\z/
71
+ end
72
+
73
+ def format_date(date)
74
+ formatted = @date_format.respond_to?(:call) ? @date_format.call(date) : date.strftime(@date_format)
75
+ raise ArgumentError, "date_format must return a String, got #{formatted.class}" unless formatted.is_a?(String)
76
+
77
+ formatted
78
+ end
79
+
80
+ def valid_date_format?(date_format)
81
+ return true if date_format.respond_to?(:call)
82
+
83
+ date_format.is_a?(String) && !date_format.empty? && !date_format.match?(/[\r\n]/)
60
84
  end
61
85
 
62
86
  def release_notes(section, separator, version)
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../markdown/source'
4
+
5
+ module RubyGemRelease
6
+ module Changelog
7
+ class Notes
8
+ include Markdown::Source
9
+
10
+ def initialize(path: 'CHANGELOG.md', heading: nil, expand_references: false)
11
+ @path = path
12
+ @heading = heading || ->(version) { /\A\#{1,6} #{Regexp.escape(version)} \((?!Next\))[^\r\n]*\d[^\r\n]*\)\z/ }
13
+ @expand_references = expand_references
14
+ end
15
+
16
+ def read(version)
17
+ content = File.read(@path)
18
+ position = heading(content, @heading.call(version))
19
+ first = line_end(content, position, newline(content))
20
+ last = section_end_position(content, position, heading_prefix(content, position).length)
21
+ section = content[first...last]
22
+ raise ArgumentError, "empty release notes in #{@path}" unless notes?(document(section))
23
+
24
+ @expand_references ? expanded_section(content, first, last).strip : section.strip
25
+ end
26
+
27
+ private
28
+
29
+ def expanded_section(content, first, last)
30
+ edits = section_edits(content, first, last)
31
+ edits.reverse_each { |range, replacement| content[range] = replacement }
32
+ finish = last + edits.sum { |range, replacement| replacement.length - range.size }
33
+ content[first...finish]
34
+ end
35
+
36
+ def section_edits(content, first, last)
37
+ edits = reference_edits(content).select { |range, _replacement| range.begin >= first && range.end <= last }
38
+ edits.reject { |range, _replacement| edits.any? { |other, _value| other != range && other.cover?(range.begin) } }
39
+ end
40
+
41
+ def reference_edits(content)
42
+ lines = content.lines
43
+ offsets = line_offsets(lines)
44
+ link_nodes(content).filter_map do |node|
45
+ range = source_range(node, lines, offsets)
46
+ replacement = reference_link(content[range], node)
47
+ [range, replacement] if replacement
48
+ end
49
+ end
50
+
51
+ def link_nodes(content)
52
+ nodes = []
53
+ document(content).walk { |node| nodes << node if %i[link image].include?(node.type) }
54
+ nodes
55
+ end
56
+
57
+ def source_range(node, lines, offsets)
58
+ position = node.source_position
59
+ start = source_offset(lines, offsets, position.fetch(:start_line), position.fetch(:start_column) - 1)
60
+ finish = source_offset(lines, offsets, position.fetch(:end_line), position.fetch(:end_column))
61
+ start...finish
62
+ end
63
+
64
+ def source_offset(lines, offsets, line, column)
65
+ offsets[line - 1] + lines[line - 1].byteslice(0, column).length
66
+ end
67
+
68
+ def reference_link(source, node)
69
+ label = source[/\A(!?\[(?:\\.|[^\[\]]|\[[^\]]*\])*\])(?:\[[^\]]*\])?\z/m, 1]
70
+ return unless label
71
+
72
+ url = node.url.gsub('&', '&amp;').gsub('<', '%3C').gsub('>', '%3E')
73
+ title = node.title.to_s.empty? ? '' : " #{node.title.dump}"
74
+ "#{label}(<#{url}>#{title})"
75
+ end
76
+ end
77
+ end
78
+ end
@@ -23,6 +23,8 @@ module RubyGemRelease
23
23
  end
24
24
 
25
25
  def publish!(release, package:)
26
+ return publish_collection!(release, package) if package.respond_to?(:packages)
27
+
26
28
  path = package.artifact(release)
27
29
  return if published?(release, package: package)
28
30
 
@@ -30,6 +32,8 @@ module RubyGemRelease
30
32
  end
31
33
 
32
34
  def published?(release, package:)
35
+ return publication_states(release, package).all?(&:last) if package.respond_to?(:packages)
36
+
33
37
  path = package.artifact(release)
34
38
  spec = Gem::Package.new(path).spec
35
39
  raise 'Package version does not match release' unless spec.version.to_s == release.version
@@ -39,8 +43,19 @@ module RubyGemRelease
39
43
 
40
44
  private
41
45
 
46
+ def publish_collection!(release, collection)
47
+ publication_states(release, collection).each do |package, published|
48
+ publish!(release, package: package) unless published
49
+ end
50
+ end
51
+
52
+ def publication_states(release, collection)
53
+ collection.artifacts(release)
54
+ collection.packages.map { |package| [package, published?(release, package: package)] }
55
+ end
56
+
42
57
  def matching_publication?(spec, path)
43
- response = version_response(spec.name, spec.version.to_s)
58
+ response = version_response(spec)
44
59
  return false if response.code == '404'
45
60
  raise "Cannot verify RubyGems publication (HTTP #{response.code})" unless response.code == '200'
46
61
 
@@ -58,11 +73,17 @@ module RubyGemRelease
58
73
  output
59
74
  end
60
75
 
61
- def version_response(name, version)
62
- uri = URI("https://rubygems.org/api/v2/rubygems/#{URI.encode_www_form_component(name)}/versions/#{URI.encode_www_form_component(version)}.json")
76
+ def version_response(spec)
77
+ uri = version_uri(spec)
63
78
  Net::HTTP.start(uri.host, uri.port, use_ssl: true, open_timeout: 10, read_timeout: 30, max_retries: 0) { |http| http.get(uri.request_uri) }
64
79
  rescue Timeout::Error, SocketError, SystemCallError, IOError, OpenSSL::SSL::SSLError => e
65
- raise "Cannot check RubyGems for #{name} #{version}: #{e.message}; verify connectivity before retrying"
80
+ raise "Cannot check RubyGems for #{spec.full_name}: #{e.message}; verify connectivity before retrying"
81
+ end
82
+
83
+ def version_uri(spec)
84
+ uri = URI("https://rubygems.org/api/v2/rubygems/#{URI.encode_www_form_component(spec.name)}/versions/#{URI.encode_www_form_component(spec.version.to_s)}.json")
85
+ uri.query = URI.encode_www_form(platform: spec.platform.to_s)
86
+ uri
66
87
  end
67
88
  end
68
89
  end
@@ -7,8 +7,9 @@ module RubyGemRelease
7
7
  module Hosting
8
8
  class GitHubRelease
9
9
  class Existing
10
- def initialize(repository:)
10
+ def initialize(repository:, latest: nil)
11
11
  @api = GitHubAPI.new(repository: repository)
12
+ @latest = latest
12
13
  end
13
14
 
14
15
  def missing(release, paths)
@@ -30,11 +31,21 @@ module RubyGemRelease
30
31
  def changed?(release)
31
32
  expected = { 'name' => "v#{release.version}", 'draft' => false, 'prerelease' => false }
32
33
  expected['body'] = release.notes if release.notes
33
- expected.any? { |key, value| @attributes[key] != value }
34
+ expected.any? { |key, value| @attributes[key] != value } || latest_changed?(release)
34
35
  end
35
36
 
36
37
  private
37
38
 
39
+ def latest_changed?(release)
40
+ return false if @latest.nil?
41
+
42
+ latest = @api.get('releases/latest')
43
+ raise 'Latest GitHub Release cannot be verified' if latest && (!latest.is_a?(Hash) || !latest['tag_name'].is_a?(String))
44
+
45
+ current = latest && latest['tag_name'] == "v#{release.version}"
46
+ @latest != !!current
47
+ end
48
+
38
49
  def validate_release(release)
39
50
  tag = "v#{release.version}"
40
51
  valid = @attributes.is_a?(Hash) && @attributes['tag_name'] == tag && @attributes['assets'].is_a?(Array)
@@ -7,16 +7,19 @@ require_relative 'github_release/existing'
7
7
  module RubyGemRelease
8
8
  module Hosting
9
9
  class GitHubRelease
10
- def initialize(repository: nil)
10
+ def initialize(repository: nil, latest: nil)
11
11
  repository ||= ENV.fetch('GITHUB_REPOSITORY', nil)
12
12
  raise ArgumentError, 'repository must be OWNER/REPO' unless repository.nil? || (repository.is_a?(String) && %r{\A[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+\z}.match?(repository))
13
13
 
14
14
  @repository = repository
15
+ raise ArgumentError, 'latest must be true, false, or nil' unless [true, false, nil].include?(latest)
16
+
17
+ @latest = latest
15
18
  end
16
19
 
17
20
  def publish!(release, package: nil, artifacts: [])
18
21
  paths = release_paths(release, package, artifacts)
19
- existing = Existing.new(repository: @repository)
22
+ existing = Existing.new(repository: @repository, latest: @latest)
20
23
  missing = existing.missing(release, paths)
21
24
  return upload_missing(release, existing, missing) if existing.exists?
22
25
 
@@ -34,7 +37,11 @@ module RubyGemRelease
34
37
  raise ArgumentError, 'release version must be MAJOR.MINOR.PATCH' unless version.is_a?(String) && /\A(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)\z/.match?(version)
35
38
  raise ArgumentError, 'release notes must not be empty' if release.notes && release.notes.strip.empty?
36
39
 
37
- artifact_paths(package ? [package.artifact(release)] : artifacts)
40
+ artifact_paths(package ? package_paths(package, release) : artifacts)
41
+ end
42
+
43
+ def package_paths(package, release)
44
+ package.respond_to?(:artifacts) ? package.artifacts(release) : [package.artifact(release)]
38
45
  end
39
46
 
40
47
  def upload_missing(release, existing, paths)
@@ -67,6 +74,7 @@ module RubyGemRelease
67
74
  def edit_release(release)
68
75
  tag = "v#{release.version}"
69
76
  arguments = ['gh', 'release', 'edit', tag, '--title', tag, '--draft=false', '--prerelease=false']
77
+ arguments.push("--latest=#{@latest}") unless @latest.nil?
70
78
  arguments.push('--repo', @repository) if @repository
71
79
  _output, error, status = run_with_notes(arguments, release.notes)
72
80
  raise "GitHub Release update failed: #{error.strip}" unless status.success?
@@ -76,6 +84,8 @@ module RubyGemRelease
76
84
  arguments = ['gh', 'release', 'create', tag]
77
85
  arguments.push('--repo', @repository) if @repository
78
86
  arguments.push('--verify-tag', '--title', tag)
87
+ arguments.push("--latest=#{@latest}") unless @latest.nil?
88
+ arguments
79
89
  end
80
90
 
81
91
  def artifact_paths(artifacts)
@@ -1,12 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'commonmarker'
4
-
5
3
  module RubyGemRelease
6
4
  module Markdown
7
5
  module Source
8
6
  private
9
7
 
8
+ def notes?(node)
9
+ return false if %i[heading html_block html_inline].include?(node.type)
10
+ return !node.string_content.strip.empty? if %i[text code code_block].include?(node.type)
11
+
12
+ node.each.any? { |child| notes?(child) }
13
+ end
14
+
10
15
  def heading(content, pattern)
11
16
  matches = heading_positions(content, pattern)
12
17
  raise ArgumentError, "expected exactly one matching heading in #{@path}" unless matches.one?
@@ -40,9 +45,16 @@ module RubyGemRelease
40
45
  end
41
46
 
42
47
  def document(content)
48
+ load_markdown_parser
43
49
  Commonmarker.parse(content, options: { extension: { shortcodes: false } })
44
50
  end
45
51
 
52
+ def load_markdown_parser
53
+ require 'commonmarker'
54
+ rescue LoadError => e
55
+ raise LoadError, "Markdown changelogs require the optional commonmarker gem (~> 2.10); add it to your Gemfile. #{e.message}"
56
+ end
57
+
46
58
  def line_offsets(lines)
47
59
  offset = 0
48
60
  lines.map do |line|
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubygems/package'
4
+
5
+ module RubyGemRelease
6
+ module Package
7
+ class Collection
8
+ attr_reader :packages
9
+
10
+ def initialize(packages:)
11
+ raise ArgumentError, 'packages must be a nonempty array' unless packages.is_a?(Array) && !packages.empty?
12
+
13
+ @packages = packages.dup.freeze
14
+ end
15
+
16
+ def version
17
+ versions = @packages.map(&:version).uniq
18
+ raise 'Package collection versions must agree' unless versions.one?
19
+
20
+ versions.first
21
+ end
22
+
23
+ def artifacts(release)
24
+ paths = @packages.map { |package| package.artifact(release) }
25
+ specs = paths.map { |path| Gem::Package.new(path).spec }
26
+ check_unique!(specs.map(&:full_name), 'package identities')
27
+ check_unique!(paths.map { |path| File.basename(path) }, 'attachment filenames')
28
+ check_versions!(specs, release)
29
+
30
+ paths
31
+ end
32
+
33
+ def build!(release)
34
+ @packages.each { |package| package.build!(release) }
35
+ artifacts(release)
36
+ end
37
+
38
+ def built?(release)
39
+ results = @packages.map { |package| package.built?(release) }
40
+ return false unless results.all?
41
+
42
+ artifacts(release)
43
+ true
44
+ end
45
+
46
+ private
47
+
48
+ def check_versions!(specs, release)
49
+ raise 'Package collection versions must match release' unless specs.all? { |spec| spec.version.to_s == release.version }
50
+ end
51
+
52
+ def check_unique!(values, label)
53
+ raise "Duplicate #{label} are not allowed" unless values.uniq == values
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubygems/package'
4
+ require 'open3'
5
+ require 'tmpdir'
6
+
7
+ module RubyGemRelease
8
+ module Package
9
+ class ContentAudit
10
+ def initialize(expected_files:, source: 'HEAD', root: Dir.pwd, generated: [])
11
+ @expected_files = expected_files
12
+ @source = source
13
+ @root = File.expand_path(root)
14
+ @generated = checked_paths(generated)
15
+ end
16
+
17
+ def verify!(artifact)
18
+ package = Gem::Package.new(artifact)
19
+ expected = expected_paths
20
+ raise 'Generated paths must be included in expected files' unless (@generated - expected).empty?
21
+
22
+ actual = checked_paths(package.contents)
23
+ raise "Archive file list differs: missing #{expected - actual}; unexpected #{actual - expected}" unless actual.sort == expected.sort
24
+
25
+ source = git('rev-parse', '--verify', '--end-of-options', "#{@source}^{commit}").strip
26
+ verify_files(package, expected, source)
27
+ artifact
28
+ end
29
+
30
+ private
31
+
32
+ def expected_paths
33
+ checked_paths(@expected_files.respond_to?(:call) ? @expected_files.call : @expected_files)
34
+ end
35
+
36
+ def checked_paths(paths)
37
+ raise ArgumentError, 'Expected an array of relative file paths' unless paths.is_a?(Array)
38
+ raise ArgumentError, 'Duplicate file paths are not allowed' unless paths.uniq == paths
39
+
40
+ paths.each do |path|
41
+ raise ArgumentError, "Unsafe file path: #{path.inspect}" unless safe_path?(path)
42
+ end
43
+ paths
44
+ end
45
+
46
+ def safe_path?(path)
47
+ path.is_a?(String) && !path.empty? && !path.match?(/[\x00\r\n\\]/) && !path.split('/', -1).intersect?(['', '.', '..'])
48
+ end
49
+
50
+ def verify_files(package, expected, source)
51
+ Dir.mktmpdir('ruby-gem-release-audit-') do |directory|
52
+ package.extract_files(directory)
53
+ expected.each { |path| verify_file(directory, path, source) }
54
+ end
55
+ end
56
+
57
+ def verify_file(directory, path, source)
58
+ file = File.join(directory, path)
59
+ raise "Archive entry must be a regular file: #{path}" unless File.file?(file) && !File.symlink?(file)
60
+ return if @generated.include?(path)
61
+
62
+ raise "Archive bytes differ from Git source: #{path}" unless File.binread(file) == git('show', "#{source}:#{path}").b
63
+ end
64
+
65
+ def git(*)
66
+ output, error, status = Open3.capture3('git', '-C', @root, *)
67
+ raise "Cannot verify Git source: #{error.strip}" unless status.success?
68
+
69
+ output
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubygems/package'
4
+ require 'rubygems/name_tuple'
5
+ require 'digest'
6
+ require 'json'
7
+
8
+ module RubyGemRelease
9
+ module Package
10
+ class Prebuilt
11
+ attr_reader :version
12
+
13
+ def self.from_manifest(manifest:, name:, version:, platform: 'ruby', audit: nil)
14
+ metadata = JSON.parse(File.read(manifest))
15
+ raise 'Manifest metadata differs from expectations' unless metadata.is_a?(Hash) && metadata.values_at('name', 'version', 'platform') == [name, version, platform]
16
+
17
+ filename = "#{Gem::NameTuple.new(name, Gem::Version.new(version), platform).full_name}.gem"
18
+ raise ArgumentError, 'Unsafe artifact filename' unless /\A[A-Za-z0-9][A-Za-z0-9_.-]*\.gem\z/.match?(filename)
19
+
20
+ new(path: File.expand_path(filename, File.dirname(manifest)), sha256: metadata.fetch('sha256'),
21
+ name: name, version: version, platform: platform, audit: audit)
22
+ end
23
+
24
+ def initialize(path:, sha256:, name:, version:, platform: 'ruby', audit: nil)
25
+ raise ArgumentError, 'sha256 must be a lowercase SHA256 digest' unless sha256.is_a?(String) && /\A[0-9a-f]{64}\z/.match?(sha256)
26
+ raise ArgumentError, 'path must name an existing archive' unless path.is_a?(String) && !path.empty? && File.file?(path)
27
+
28
+ @path = File.expand_path(path)
29
+ @sha256 = sha256
30
+ @name = name
31
+ @version = version
32
+ @platform = platform
33
+ @audit = audit
34
+ end
35
+
36
+ def artifact(release)
37
+ raise 'Package version does not match release' unless @version == release.version
38
+
39
+ verify_checksum!
40
+
41
+ spec = Gem::Package.new(@path).spec
42
+ verify_identity!(spec)
43
+ raise 'Prebuilt artifact filename must match its identity' unless File.basename(@path) == spec.file_name
44
+
45
+ @audit&.verify!(@path)
46
+ @path
47
+ end
48
+
49
+ def build!(release)
50
+ artifact(release)
51
+ end
52
+
53
+ def artifacts(release)
54
+ [artifact(release)]
55
+ end
56
+
57
+ def built?(release)
58
+ artifact(release)
59
+ true
60
+ end
61
+
62
+ private
63
+
64
+ def verify_checksum!
65
+ raise 'Prebuilt artifact changed; refusing publication' unless Digest::SHA256.file(@path).hexdigest == @sha256
66
+ end
67
+
68
+ def verify_identity!(spec)
69
+ raise 'Prebuilt artifact identity differs from expectations' unless [spec.name, spec.version.to_s, spec.platform.to_s] == [@name, @version, @platform]
70
+ end
71
+ end
72
+ end
73
+ end