ruby-gem-release 0.7.0 → 0.8.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: 6a4950ac3c14d5b45f4cfd872d6bb692cb18e8c47186804dd0e1278867b3cbea
4
- data.tar.gz: 6446cb2d14aab627429cf4670368d36c4e978ad822dcaf69cadae7968ccd028f
3
+ metadata.gz: 403f45385c4a0791cdfc2da2223caa74161997b6e73b7ace1a301d8c18b3f781
4
+ data.tar.gz: 855ecef5795b5d02d86fa9a5dbaccdd91557a24792478b13e8c3b0a3c7e35ae4
5
5
  SHA512:
6
- metadata.gz: 78e16cb2444ea49cae3cca6d1ab2b178cf83d374c07772273a8bcc6accd8a5a84e20d2dd3bb1999e2856d62bb09e5049d20b9f5daf4d8cd8b5348297939ba4b4
7
- data.tar.gz: 8e7fc569858c3a93e99daab287036834ed839a5c3511cb00c97ad5bc63df6e68154597713b7248d3c4c42015c2658f45b65048045ec28c6186ff4bbbed0ad4bb
6
+ metadata.gz: 8d343a40df9a8b5903c980be84e59f88bcd7361a7368eb19a6d150045a9985ca866f294393781e304934c6d9b9c9161e56b48ffe1ba012e489437e0a6568086f
7
+ data.tar.gz: adf8f52b5ec0fa1a71e5daa5c62811c995a49af142a0a34098286c458af122a44587227ee120db8c8f01284193401a5a81f0e2bba74c7df16818eaf43692e31f
data/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.8.0 (2026/11/20)
4
+
5
+ - [#79](https://github.com/dblock/ruby-gem-release/pull/79): Select the next development version part with `next_version: :minor` or `:major` on `Workflow::RubyGem` and `Workflow::SemanticRubyGem` - [@dblock](https://github.com/dblock).
6
+ - [#80](https://github.com/dblock/ruby-gem-release/pull/80): Skip posting empty Danger report comments - [@dblock](https://github.com/dblock).
7
+ - [#82](https://github.com/dblock/ruby-gem-release/pull/82): Create the default package and Git temporary directories only when release data is first used, so loading a workflow leaves none behind - [@dblock](https://github.com/dblock).
8
+ - [#83](https://github.com/dblock/ruby-gem-release/pull/83): Name Intridea subsections with `sections:`, dropping sections without notes from a release and starting every Next section with all of them - [@dblock](https://github.com/dblock).
9
+ - [#85](https://github.com/dblock/ruby-gem-release/pull/85): Verify RubyGems Trusted Publishing before every release with a `release:workflow:preflight` task, add a `dry-run` workflow input that runs only that check, and name the token claims in rejected exchanges - [@dblock](https://github.com/dblock).
10
+ - [#86](https://github.com/dblock/ruby-gem-release/pull/86): Check that the release token can push to the default branch and that no repository rules would reject the release commit or tag before every release - [@dblock](https://github.com/dblock).
11
+ - [#88](https://github.com/dblock/ruby-gem-release/pull/88): Pin every action used by the reusable workflow, the composite action, and this repository's workflows to a commit SHA, with Dependabot proposing updates - [@dblock](https://github.com/dblock).
12
+
3
13
  ## 0.7.0 (2026/11/20)
4
14
 
5
15
  - [#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).
data/README.md CHANGED
@@ -26,7 +26,7 @@ RubyGemRelease::Workflow::RubyGem.new(
26
26
  ).install_tasks
27
27
  ```
28
28
 
29
- These defaults select the gemspec and repository, reject empty pending notes, prepare the changelog, publish to RubyGems and GitHub Releases, and increment the patch version for the next development iteration. Dates use UTC.
29
+ These defaults select the gemspec and repository, reject empty pending notes, prepare the changelog, publish to RubyGems and GitHub Releases, and increment the patch version for the next development iteration; pass `next_version: :minor` or `:major` to [increment another part](docs/workflow/ruby_gem.md#next-version). Dates use UTC.
30
30
 
31
31
  ### 2. Add the Release Workflow
32
32
 
@@ -53,7 +53,7 @@ permissions:
53
53
 
54
54
  jobs:
55
55
  release:
56
- uses: dblock/ruby-gem-release/.github/workflows/release.yml@v0.6.0
56
+ uses: dblock/ruby-gem-release/.github/workflows/release.yml@v0.7.0
57
57
  with:
58
58
  ruby-version: '3.3'
59
59
  version: ${{ inputs.version }}
@@ -70,11 +70,13 @@ gh api --method PUT repos/{owner}/{repo}/environments/release
70
70
 
71
71
  For a new gem, open the [pending trusted publishers page](https://rubygems.org/profile/oidc/pending_trusted_publishers). For an existing gem, open your existing gem's **Trusted publishers** page (e.g. `https://rubygems.org/gems/example/trusted_publishers`). Create a [trusted publisher](https://guides.rubygems.org/trusted-publishing/#using-reusable-workflows). Set the repository fields to your gem's repository, the workflow filename to `release.yml`, and the environment to `release`. Set the separate workflow repository fields to `dblock` and `ruby-gem-release`; these identify the reusable workflow RubyGems verifies in the OIDC token.
72
72
 
73
+ To verify this setup without releasing, dispatch a [dry run](docs/actions/release.md#dry-run).
74
+
73
75
  Collaborators with write access can dispatch releases after this setup, without repository admin access or their own RubyGems credentials. Creating the environment alone does not restrict publication. Configure [required maintainer approval and deployment branches](RELEASING.md#maintainer-approved-publication) to control who approves publication.
74
76
 
75
77
  ### 4. Make a Release
76
78
 
77
- Your branch rules must permit release commits and tags to be pushed; see [branch permissions](RELEASING.md#one-time-setup). Dispatching starts a publishing run; when required reviewers are configured, the release job waits for approval from a designated maintainer. Run your project's tests and build checks, review the changes, and dispatch from your default branch:
79
+ Your branch rules must permit release commits and tags to be pushed; see [branch permissions](RELEASING.md#one-time-setup). Each release checks this before publishing. Dispatching starts a publishing run; when required reviewers are configured, the release job waits for approval from a designated maintainer. Run your project's tests and build checks, review the changes, and dispatch from your default branch:
78
80
 
79
81
  **First release:** Pass the numeric RubyGems account ID shown on the Pending Trusted Publishers page as `rubygems-owner`. This declares the account that will own the gem on its first publication. Replace `YOUR_ACCOUNT_ID` below with that ID:
80
82
 
@@ -89,6 +89,30 @@ module RubyGemRelease
89
89
  def check_absent(content, pattern, message)
90
90
  raise ArgumentError, "#{message} in #{@path}" unless heading_positions(content, pattern).empty?
91
91
  end
92
+
93
+ def single_line?(value)
94
+ value.is_a?(String) && !value.strip.empty? && !value.match?(/[\r\n]/)
95
+ end
96
+
97
+ def validate_sections(sections)
98
+ raise ArgumentError, 'sections must be an array of unique nonempty names' unless sections.is_a?(Array) && sections.all? { |name| single_line?(name) } && sections.uniq == sections
99
+ end
100
+
101
+ # Removes subsection headings left without notes in a released section.
102
+ def remove_empty_categories(section, separator, level)
103
+ heading_positions(section, /\A\#{#{level + 1}} /).reverse_each do |offset|
104
+ finish = section_end_position(section, offset, level + 1)
105
+ start = section.index(separator, offset) || section.length
106
+ remove_line(section, offset, separator) unless notes?(document(section[start...finish]))
107
+ end
108
+ end
109
+
110
+ def remove_line(section, offset, separator)
111
+ finish = section.index(separator, offset)
112
+ length = (finish ? finish + separator.length : section.length) - offset
113
+ length += separator.length if section[0...offset].end_with?(separator * 2) && section[offset + length, separator.length] == separator
114
+ section.slice!(offset, length)
115
+ end
92
116
  end
93
117
  end
94
118
  end
@@ -7,13 +7,15 @@ module RubyGemRelease
7
7
  PLACEHOLDER = '- Your contribution here.'
8
8
  DATE_FORMAT = '%Y/%m/%d'
9
9
 
10
- def initialize(path: 'CHANGELOG.md', placeholder: PLACEHOLDER, date_format: DATE_FORMAT)
10
+ def initialize(path: 'CHANGELOG.md', placeholder: PLACEHOLDER, date_format: DATE_FORMAT, sections: [])
11
11
  raise ArgumentError, 'placeholder must be a single nonempty line' unless placeholder.is_a?(String) && !placeholder.empty? && !placeholder.match?(/[\r\n]/)
12
12
  raise ArgumentError, 'date_format must be a nonempty single-line strftime string or a callable' unless valid_date_format?(date_format)
13
13
 
14
+ validate_sections(sections)
14
15
  super(path: path)
15
16
  @placeholder = placeholder
16
17
  @date_format = date_format
18
+ @sections = sections.dup
17
19
  end
18
20
 
19
21
  private
@@ -30,11 +32,14 @@ module RubyGemRelease
30
32
  prefix = heading_prefix(content, position)
31
33
  check_release_absent(content, version)
32
34
  section_end = section_end_position(content, position, prefix.length)
33
- content[position...section_end] = release_heading(prefix, version, date) +
34
- release_notes(content[line_end(content, position, separator)...section_end], separator, version)
35
+ content[position...section_end] = released_section(content[line_end(content, position, separator)...section_end], prefix, version, date, separator)
35
36
  File.write(@path, content)
36
37
  end
37
38
 
39
+ def released_section(notes, prefix, version, date, separator)
40
+ release_heading(prefix, version, date) + release_notes(notes, separator, version, prefix.length)
41
+ end
42
+
38
43
  # Checked before writing, so a date_format that produces an unrecognizable heading leaves the file unchanged.
39
44
  def release_heading(prefix, version, date)
40
45
  heading = "#{prefix} #{version} (#{format_date(date)})"
@@ -56,9 +61,17 @@ module RubyGemRelease
56
61
  write_next_section(content, position, prefix, version, separator)
57
62
  end
58
63
 
64
+ # Configured sections start every development section, including ones the
65
+ # previous release dropped because they had no notes.
59
66
  def write_next_section(content, position, prefix, version, separator)
60
- subsections = subsection_headings(content, position, prefix.length)
61
- File.write(@path, content.insert(position, build_next_section(prefix, version, subsections, separator)))
67
+ File.write(@path, content.insert(position, build_next_section(prefix, version, next_subsections(content, position, prefix), separator)))
68
+ end
69
+
70
+ def next_subsections(content, position, prefix)
71
+ return subsection_headings(content, position, prefix.length) if @sections.empty?
72
+ raise ArgumentError, 'section headings cannot be deeper than level six' if prefix.length == 6
73
+
74
+ @sections.map { |name| "#{prefix}# #{name}" }
62
75
  end
63
76
 
64
77
  def check_release_absent(content, version)
@@ -83,8 +96,10 @@ module RubyGemRelease
83
96
  date_format.is_a?(String) && !date_format.empty? && !date_format.match?(/[\r\n]/)
84
97
  end
85
98
 
86
- def release_notes(section, separator, version)
87
- check_notes(remove_placeholder(section, separator), version)
99
+ def release_notes(section, separator, version, level)
100
+ remove_placeholder(section, separator)
101
+ remove_empty_categories(section, separator, level) if @sections.any?
102
+ check_notes(section, version)
88
103
  end
89
104
 
90
105
  def remove_placeholder(section, separator)
@@ -16,11 +16,8 @@ module RubyGemRelease
16
16
 
17
17
  def validate_template(placeholder, sections)
18
18
  raise ArgumentError, 'placeholder must be a single nonempty line' unless placeholder.nil? || single_line?(placeholder)
19
- raise ArgumentError, 'sections must be an array of unique nonempty names' unless sections.is_a?(Array) && sections.all? { |name| single_line?(name) } && sections.uniq == sections
20
- end
21
19
 
22
- def single_line?(value)
23
- value.is_a?(String) && !value.strip.empty? && !value.match?(/[\r\n]/)
20
+ validate_sections(sections)
24
21
  end
25
22
 
26
23
  def release_pattern(version)
@@ -78,21 +75,6 @@ module RubyGemRelease
78
75
  section
79
76
  end
80
77
 
81
- def remove_empty_categories(section, separator, level)
82
- heading_positions(section, /\A\#{#{level + 1}} /).reverse_each do |offset|
83
- finish = section_end_position(section, offset, level + 1)
84
- start = section.index(separator, offset) || section.length
85
- remove_line(section, offset, separator) unless notes?(document(section[start...finish]))
86
- end
87
- end
88
-
89
- def remove_line(section, offset, separator)
90
- finish = section.index(separator, offset)
91
- length = (finish ? finish + separator.length : section.length) - offset
92
- length += separator.length if section[0...offset].end_with?(separator * 2) && section[offset + length, separator.length] == separator
93
- section.slice!(offset, length)
94
- end
95
-
96
78
  def next_section(prefix, separator)
97
79
  raise ArgumentError, 'category headings cannot be deeper than level six' if prefix.length == 6 && @sections.any?
98
80
 
@@ -10,22 +10,27 @@ require 'tmpdir'
10
10
  module RubyGemRelease
11
11
  module Package
12
12
  class RubyGem
13
- attr_reader :directory, :gemspec
13
+ attr_reader :gemspec
14
14
 
15
15
  def initialize(gemspec: nil, directory: nil, audit: nil)
16
16
  @gemspec = resolve_gemspec(gemspec || ENV.fetch('GEMSPEC_PATH', nil))
17
17
  directory ||= ENV.fetch('RELEASE_DATA_DIR', nil)
18
- @directory = directory.to_s.empty? ? Dir.mktmpdir('ruby-gem-release-') : directory
18
+ @directory = directory unless directory.to_s.empty?
19
19
  @audit = audit
20
20
  end
21
21
 
22
+ # Creates the default temporary directory only when artifacts are first used.
23
+ def directory
24
+ @directory ||= Dir.mktmpdir('ruby-gem-release-')
25
+ end
26
+
22
27
  def version
23
28
  script = 'spec = Gem::Specification.load(ARGV.fetch(0)); abort "Cannot load gemspec" unless spec; puts JSON.generate(spec.version.to_s)'
24
29
  JSON.parse(run(Gem.ruby, '-rrubygems', '-rjson', '-e', script, @gemspec))
25
30
  end
26
31
 
27
32
  def build!(release)
28
- FileUtils.mkdir_p(@directory)
33
+ FileUtils.mkdir_p(directory)
29
34
  run({ 'SOURCE_DATE_EPOCH' => source_date_epoch }, Gem.ruby, '-S', 'gem', 'build', @gemspec, '--output', archive)
30
35
  @manifest = build_manifest(release)
31
36
  File.write(manifest_path, JSON.generate(@manifest))
@@ -47,7 +52,7 @@ module RubyGemRelease
47
52
  platform = manifest.fetch('platform', 'ruby')
48
53
  raise 'Invalid package platform' unless /\A[A-Za-z0-9][A-Za-z0-9_.-]*\z/.match?(platform)
49
54
 
50
- path = File.join(@directory, Gem::Package.new(archive).spec.file_name)
55
+ path = File.join(directory, Gem::Package.new(archive).spec.file_name)
51
56
  FileUtils.cp(archive, path)
52
57
  path
53
58
  end
@@ -91,11 +96,11 @@ module RubyGemRelease
91
96
  end
92
97
 
93
98
  def archive
94
- File.join(@directory, 'release.gem')
99
+ File.join(directory, 'release.gem')
95
100
  end
96
101
 
97
102
  def manifest_path
98
- File.join(@directory, 'release.json')
103
+ File.join(directory, 'release.json')
99
104
  end
100
105
 
101
106
  def resolve_gemspec(path)
@@ -19,7 +19,7 @@ module RubyGemRelease
19
19
  @remote = remote
20
20
  @branch = branch || ENV.fetch('DEFAULT_BRANCH', nil)
21
21
  directory ||= ENV.fetch('RELEASE_DATA_DIR', nil)
22
- @directory = directory.to_s.empty? ? Dir.mktmpdir('ruby-gem-release-git-') : directory
22
+ @directory = directory unless directory.to_s.empty?
23
23
  end
24
24
 
25
25
  def configure!(name: 'ruby-gem-release', email: 'ruby-gem-release@localhost')
@@ -118,8 +118,8 @@ module RubyGemRelease
118
118
  end
119
119
 
120
120
  def write_bundle(release)
121
- FileUtils.mkdir_p(@directory)
122
- run('bundle', 'create', File.join(@directory, 'release.bundle'), 'HEAD', "refs/tags/v#{release.version}")
121
+ FileUtils.mkdir_p(directory)
122
+ run('bundle', 'create', File.join(directory, 'release.bundle'), 'HEAD', "refs/tags/v#{release.version}")
123
123
  end
124
124
 
125
125
  private
@@ -214,7 +214,12 @@ module RubyGemRelease
214
214
  end
215
215
 
216
216
  def preparation
217
- @preparation ||= Preparation.new(directory: @directory, &method(:run))
217
+ @preparation ||= Preparation.new(directory: directory, &method(:run))
218
+ end
219
+
220
+ # Creates the default temporary directory only when release data is first written or read.
221
+ def directory
222
+ @directory ||= Dir.mktmpdir('ruby-gem-release-git-')
218
223
  end
219
224
 
220
225
  def run(*)
@@ -8,6 +8,7 @@ require_relative 'version/ruby_constant'
8
8
  require_relative 'workflow/environment'
9
9
  require_relative 'workflow/failure'
10
10
  require_relative 'workflow/ruby_gems_credentials'
11
+ require_relative 'workflow/preflight'
11
12
  require_relative 'destination/ruby_gems'
12
13
 
13
14
  module RubyGemRelease
@@ -98,6 +99,7 @@ module RubyGemRelease
98
99
  namespace :workflow do
99
100
  install_workflow_context_tasks
100
101
  task(:failure_phase) { Workflow::Failure.phase!(provider: @provider) }
102
+ install_preflight_task
101
103
  task(:credentials) do
102
104
  with_failure_capture('release:workflow:credentials') do
103
105
  Workflow::RubyGemsCredentials.new(owner: @provider.fetch('RUBYGEMS_OWNER_ID', ''), provider: @provider).configure!
@@ -106,6 +108,12 @@ module RubyGemRelease
106
108
  end
107
109
  end
108
110
 
111
+ def install_preflight_task
112
+ task(:preflight) do
113
+ with_failure_capture('release:workflow:preflight') { Workflow::Preflight.new(destination: @destination, provider: @provider).check! }
114
+ end
115
+ end
116
+
109
117
  def install_workflow_context_tasks
110
118
  install_checkpoint_task
111
119
  install_capture_task
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubyGemRelease
4
- VERSION = '0.7.0'
4
+ VERSION = '0.8.0'
5
5
  end
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../github_api'
4
+ require_relative '../source_control/git'
5
+
6
+ module RubyGemRelease
7
+ module Workflow
8
+ # Checks that the job's GitHub token can push release commits and tags,
9
+ # without sending any objects or changing refs.
10
+ class GitHubPermissions
11
+ BRANCH_BLOCKERS = %w[pull_request required_status_checks required_signatures update required_deployments merge_queue].freeze
12
+ TAG_BLOCKERS = %w[creation required_signatures].freeze
13
+ BYPASS = %w[always exempt].freeze
14
+
15
+ def initialize(branch:, tag: nil, api: GitHubAPI.new, remote: 'origin')
16
+ raise ArgumentError, 'a default branch is required to check GitHub permissions' if branch.to_s.empty?
17
+
18
+ @branch = branch
19
+ @tag = tag
20
+ @api = api
21
+ @remote = remote
22
+ end
23
+
24
+ def check!
25
+ check_push!
26
+ failures = blocked_branch_rules + blocked_tag_rules
27
+ raise "GitHub rules would reject the release push: #{failures.join('; ')}" unless failures.empty?
28
+
29
+ ["Verified GitHub push access to #{@branch}#{" and tag #{@tag}" if @tag}", *warnings]
30
+ end
31
+
32
+ private
33
+
34
+ def check_push!
35
+ SourceControl::Git.command('push', '--dry-run', '--porcelain', @remote, "HEAD:refs/heads/#{@branch}")
36
+ rescue RuntimeError => e
37
+ raise "GitHub token cannot push to #{@branch}; grant contents: write. #{e.message}"
38
+ end
39
+
40
+ def blocked_branch_rules
41
+ rules = Array(@api.get("rules/branches/#{@branch}")).select { |rule| BRANCH_BLOCKERS.include?(rule['type']) }
42
+ rules.group_by { |rule| rule['ruleset_id'] }.filter_map do |id, matched|
43
+ ruleset = ruleset(id)
44
+ blocked(ruleset, id, "branch #{@branch}", matched.map { |rule| rule['type'] }.uniq) unless bypass?(ruleset)
45
+ end
46
+ end
47
+
48
+ def blocked_tag_rules
49
+ return [] unless @tag
50
+
51
+ Array(@api.get('rulesets?includes_parents=true&targets=tag')).filter_map do |summary|
52
+ ruleset = ruleset(summary['id'])
53
+ types = tag_blockers(ruleset)
54
+ blocked(ruleset, summary['id'], "tag #{@tag}", types) unless types.empty? || bypass?(ruleset)
55
+ end
56
+ end
57
+
58
+ def tag_blockers(ruleset)
59
+ return [] unless ruleset && ruleset['enforcement'] == 'active' && matches?(ruleset, "refs/tags/#{@tag}")
60
+
61
+ Array(ruleset['rules']).map { |rule| rule['type'] } & TAG_BLOCKERS
62
+ end
63
+
64
+ def ruleset(id)
65
+ @api.get("rulesets/#{id}?includes_parents=true")
66
+ end
67
+
68
+ def bypass?(ruleset)
69
+ ruleset && BYPASS.include?(ruleset['current_user_can_bypass'])
70
+ end
71
+
72
+ def matches?(ruleset, ref)
73
+ names = ruleset.dig('conditions', 'ref_name') || {}
74
+ included = Array(names['include']).any? { |pattern| pattern == '~ALL' || File.fnmatch?(pattern, ref) }
75
+ included && Array(names['exclude']).none? { |pattern| File.fnmatch?(pattern, ref) }
76
+ end
77
+
78
+ def blocked(ruleset, id, ref, types)
79
+ name = ruleset && ruleset['name']
80
+ "#{ref}: #{name ? "ruleset #{name.inspect}" : "ruleset #{id}"} requires #{types.join(', ')}"
81
+ end
82
+
83
+ def warnings
84
+ branch = @api.get("branches/#{@branch}")
85
+ return [] unless branch.is_a?(Hash) && branch['protected']
86
+
87
+ ["Warning: #{@branch} has branch protection; its push restrictions are not readable without admin access, so confirm the release token may push to it"]
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../execution_provider'
4
+ require_relative '../execution_provider/github'
5
+ require_relative 'ruby_gems_credentials'
6
+ require_relative 'github_permissions'
7
+
8
+ module RubyGemRelease
9
+ module Workflow
10
+ # Checks release prerequisites without editing files, pushing, or publishing.
11
+ class Preflight
12
+ def initialize(destination:, provider: ExecutionProvider.current, output: $stdout, github: nil)
13
+ @destination = destination
14
+ @provider = provider
15
+ @output = output
16
+ @github = github
17
+ end
18
+
19
+ def check!
20
+ @output.puts(trusted_publishing)
21
+ @output.puts(github_permissions)
22
+ end
23
+
24
+ private
25
+
26
+ def github_permissions
27
+ return 'Skipped GitHub permissions: not running on GitHub Actions' unless @github || @provider.is_a?(ExecutionProvider::GitHub)
28
+
29
+ (@github || GitHubPermissions.new(branch: @provider.fetch('DEFAULT_BRANCH', ''), tag: release_tag,
30
+ api: GitHubAPI.new(repository: @provider.fetch('GITHUB_REPOSITORY', nil)))).check!
31
+ end
32
+
33
+ def release_tag
34
+ version = [@provider.fetch('INPUT_VERSION', ''), @provider.fetch('GEMSPEC_VERSION', '')].find { |value| !value.empty? }
35
+ "v#{version}" if version
36
+ end
37
+
38
+ def trusted_publishing
39
+ return 'Skipped RubyGems Trusted Publishing: the destination does not use RubyGems authentication' unless @destination.authentication == 'rubygems'
40
+
41
+ summary = RubyGemsCredentials.new(owner: @provider.fetch('RUBYGEMS_OWNER_ID', ''), provider: @provider).check!
42
+ details = summary.except('scopes').map { |name, value| "#{name} #{value}" }
43
+ "Verified RubyGems Trusted Publishing: #{[*details, "scopes #{summary.fetch('scopes').join(', ')}"].join('; ')}"
44
+ end
45
+ end
46
+ end
47
+ end
@@ -12,14 +12,16 @@ module RubyGemRelease
12
12
  class RubyGem
13
13
  include Lifecycle
14
14
 
15
+ NEXT_VERSIONS = %i[major minor patch].freeze
16
+
15
17
  attr_reader :version, :changelog, :package, :destination, :git, :hosting, :provider
16
18
 
17
19
  def initialize(version:, changelog:, provider: ExecutionProvider.current,
18
20
  package: Package::RubyGem.new(gemspec: provider.fetch('GEMSPEC_PATH', provider.fetch('INPUT_GEMSPEC_PATH', '')) || '', directory: provider.directory),
19
21
  destination: Destination::RubyGems.new,
20
22
  git: SourceControl::Git.new(directory: provider.directory), hosting: Hosting::GitHubRelease.new,
21
- validate: nil, success: nil, failure: nil)
22
- @version = Version::Interface.check!(version)
23
+ validate: nil, success: nil, failure: nil, next_version: :patch)
24
+ configure_version!(version, next_version)
23
25
  @changelog = changelog
24
26
  @package = package
25
27
  @destination = destination
@@ -37,6 +39,23 @@ module RubyGemRelease
37
39
 
38
40
  private
39
41
 
42
+ def configure_version!(version, next_version)
43
+ @version = Version::Interface.check!(version)
44
+ part = next_version.to_sym if next_version.is_a?(String) || next_version.is_a?(Symbol)
45
+ raise ArgumentError, 'next_version must be :major, :minor, or :patch' unless NEXT_VERSIONS.include?(part)
46
+ raise ArgumentError, "version object's increment! must accept part: to use next_version: #{part.inspect}" unless part == :patch || increment_accepts_part?
47
+
48
+ @next_version = part
49
+ end
50
+
51
+ def increment_accepts_part?
52
+ @version.method(:increment!).parameters.any? { |type, name| %i[keyrest rest].include?(type) || (%i[key keyreq].include?(type) && name == :part) }
53
+ end
54
+
55
+ def increment_options
56
+ @next_version == :patch ? {} : { part: @next_version }
57
+ end
58
+
40
59
  def install_callbacks(tasks)
41
60
  tasks.validate { |ctx| validate_with_hooks(ctx) }
42
61
  tasks.prepare { |ctx| prepare_release(ctx) }
@@ -89,7 +108,7 @@ module RubyGemRelease
89
108
  next_ctx = ctx
90
109
  @git.stage_changes! do
91
110
  run_stage(:prepare_next, ctx, after_context: -> { next_ctx }) do
92
- next_ctx = ctx.with(release: ctx.release.with_next_version(@version.increment!(from: ctx.release.version)))
111
+ next_ctx = ctx.with(release: ctx.release.with_next_version(@version.increment!(from: ctx.release.version, **increment_options)))
93
112
  @changelog&.prepare_next!(next_ctx.release)
94
113
  end
95
114
  end
@@ -15,17 +15,41 @@ module RubyGemRelease
15
15
  @provider = provider
16
16
  end
17
17
 
18
+ CLAIMS = %w[repository job_workflow_ref environment].freeze
19
+
18
20
  def configure!
21
+ key, = exchange!
22
+ @provider.export(%w[RUBYGEMS_API_KEY BUNDLE_GEM__PUSH_KEY GEM_HOST_API_KEY].to_h { |name| [name, key] })
23
+ end
24
+
25
+ def check!
26
+ _key, body, claims = exchange!
27
+ scopes = body['scopes']
28
+ raise 'Trusted publisher credential does not grant push_rubygem' unless scopes.is_a?(Array) && scopes.include?('push_rubygem')
29
+
30
+ claims.merge('scopes' => scopes, 'expires_at' => body['expires_at'], 'owner' => body['owner']).compact
31
+ end
32
+
33
+ private
34
+
35
+ def exchange!
19
36
  token = @provider.oidc_token(audience: 'rubygems.org')
20
37
  @provider.mask(token)
21
- key = exchange_token(token)
38
+ claims = token_claims(token)
39
+ body = exchange_token(token, claims)
40
+ key = credential(body, 'rubygems_api_key')
22
41
  @provider.mask(key)
23
- @provider.export(%w[RUBYGEMS_API_KEY BUNDLE_GEM__PUSH_KEY GEM_HOST_API_KEY].to_h { |name| [name, key] })
42
+ [key, body, claims]
24
43
  end
25
44
 
26
- private
45
+ def token_claims(token)
46
+ payload = JSON.parse(token.split('.')[1].to_s.tr('-_', '+/').unpack1('m'))
47
+ payload.is_a?(Hash) ? payload.slice(*CLAIMS).select { |_name, value| value.is_a?(String) && !value.match?(/[\r\n]/) } : {}
48
+ rescue JSON::ParserError
49
+ {}
50
+ end
27
51
 
28
- def exchange_token(token)
52
+ def exchange_token(token, claims)
29
53
  uri = URI('https://rubygems.org/api/v1/oidc/trusted_publisher/exchange_token')
30
54
  request = Net::HTTP::Post.new(uri)
31
55
  request['Content-Type'] = 'application/json'
@@ -33,20 +57,26 @@ module RubyGemRelease
33
57
  body = { jwt: token }
34
58
  body[:owner] = @owner unless @owner.empty?
35
59
  request.body = JSON.generate(body)
36
- credential(response(uri, request), 'rubygems_api_key')
60
+ response(uri, request, claims)
37
61
  end
38
62
 
39
- def response(uri, request)
63
+ def response(uri, request, claims)
40
64
  result = Net::HTTP.start(uri.host, uri.port, use_ssl: true, open_timeout: 10, read_timeout: 30, max_retries: 0) do |http|
41
65
  http.request(request)
42
66
  end
43
- raise "Credential request failed (HTTP #{result.code}); verify trusted publisher configuration and owner ID if supplied" unless result.is_a?(Net::HTTPSuccess)
67
+ raise "Credential request failed (HTTP #{result.code}); #{publisher_hint(claims)}" unless result.is_a?(Net::HTTPSuccess)
44
68
 
45
69
  JSON.parse(result.body)
46
70
  rescue JSON::ParserError
47
71
  raise 'Credential response was not valid JSON'
48
72
  end
49
73
 
74
+ def publisher_hint(claims)
75
+ expected = claims.map { |name, value| "#{name} #{value}" }.join(', ')
76
+ hint = 'verify trusted publisher configuration and owner ID if supplied'
77
+ expected.empty? ? hint : "#{hint}; the token presented #{expected}"
78
+ end
79
+
50
80
  def credential(body, name)
51
81
  value = body[name] if body.is_a?(Hash)
52
82
  raise 'Credential response did not contain a valid token' unless value.is_a?(String) && !value.empty? && !value.match?(/[\r\n]/)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-gem-release
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Doubrovkine
@@ -82,8 +82,10 @@ files:
82
82
  - lib/ruby-gem-release/version/ruby_constant.rb
83
83
  - lib/ruby-gem-release/workflow/environment.rb
84
84
  - lib/ruby-gem-release/workflow/failure.rb
85
+ - lib/ruby-gem-release/workflow/github_permissions.rb
85
86
  - lib/ruby-gem-release/workflow/hooks.rb
86
87
  - lib/ruby-gem-release/workflow/lifecycle.rb
88
+ - lib/ruby-gem-release/workflow/preflight.rb
87
89
  - lib/ruby-gem-release/workflow/reviewed_ruby_gem.rb
88
90
  - lib/ruby-gem-release/workflow/ruby_gem.rb
89
91
  - lib/ruby-gem-release/workflow/ruby_gems_credentials.rb