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.
@@ -12,10 +12,11 @@ module RubyGemRelease
12
12
  class RubyGem
13
13
  attr_reader :directory, :gemspec
14
14
 
15
- def initialize(gemspec: nil, directory: nil)
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
18
  @directory = directory.to_s.empty? ? Dir.mktmpdir('ruby-gem-release-') : directory
19
+ @audit = audit
19
20
  end
20
21
 
21
22
  def version
@@ -39,14 +40,22 @@ module RubyGemRelease
39
40
  end
40
41
 
41
42
  def artifact(release)
42
- name = verified_manifest(release).fetch('name')
43
+ manifest = verified_manifest(release)
44
+ name = manifest.fetch('name')
43
45
  raise 'Invalid package name' unless /\A[A-Za-z0-9][A-Za-z0-9_.-]*\z/.match?(name)
44
46
 
45
- path = File.join(@directory, "#{name}-#{release.version}.gem")
47
+ platform = manifest.fetch('platform', 'ruby')
48
+ raise 'Invalid package platform' unless /\A[A-Za-z0-9][A-Za-z0-9_.-]*\z/.match?(platform)
49
+
50
+ path = File.join(@directory, Gem::Package.new(archive).spec.file_name)
46
51
  FileUtils.cp(archive, path)
47
52
  path
48
53
  end
49
54
 
55
+ def artifacts(release)
56
+ [artifact(release)]
57
+ end
58
+
50
59
  private
51
60
 
52
61
  def source_date_epoch
@@ -58,7 +67,7 @@ module RubyGemRelease
58
67
  spec = Gem::Package.new(archive).spec
59
68
  raise "Built version #{spec.version} does not match #{release.version}" unless spec.version.to_s == release.version
60
69
 
61
- { 'name' => spec.name, 'version' => spec.version.to_s, 'sha256' => Digest::SHA256.file(archive).hexdigest }
70
+ identity(spec).merge('sha256' => Digest::SHA256.file(archive).hexdigest)
62
71
  end
63
72
 
64
73
  def verified_manifest(release)
@@ -67,12 +76,18 @@ module RubyGemRelease
67
76
  raise 'Built artifact changed; refusing publication' unless Digest::SHA256.file(archive).hexdigest == manifest.fetch('sha256')
68
77
 
69
78
  verify_metadata!(manifest, release)
79
+ @audit&.verify!(archive)
70
80
  manifest
71
81
  end
72
82
 
73
83
  def verify_metadata!(manifest, release)
74
84
  spec = Gem::Package.new(archive).spec
75
- raise 'Artifact metadata differs from manifest' unless spec.name == manifest.fetch('name') && spec.version.to_s == release.version
85
+ expected = { 'name' => manifest.fetch('name'), 'version' => release.version, 'platform' => manifest.fetch('platform', 'ruby') }
86
+ raise 'Artifact metadata differs from manifest' unless identity(spec) == expected
87
+ end
88
+
89
+ def identity(spec)
90
+ { 'name' => spec.name, 'version' => spec.version.to_s, 'platform' => spec.platform.to_s }
76
91
  end
77
92
 
78
93
  def archive
@@ -8,6 +8,13 @@ require_relative 'git/preparation'
8
8
  module RubyGemRelease
9
9
  module SourceControl
10
10
  class Git
11
+ def self.command(*)
12
+ output, error, status = Open3.capture3('git', *)
13
+ raise "Git command failed: #{error.strip}\n#{output.strip}" unless status.success?
14
+
15
+ output
16
+ end
17
+
11
18
  def initialize(branch: nil, remote: 'origin', directory: nil)
12
19
  @remote = remote
13
20
  @branch = branch || ENV.fetch('DEFAULT_BRANCH', nil)
@@ -110,6 +117,11 @@ module RubyGemRelease
110
117
  { commit: candidate, version: version, tree: run('rev-parse', "#{candidate}^{tree}").strip } if version
111
118
  end
112
119
 
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}")
123
+ end
124
+
113
125
  private
114
126
 
115
127
  def reuse_release!(existing)
@@ -135,11 +147,6 @@ module RubyGemRelease
135
147
  remote_head == expected || (existing && ancestor?(head, remote_head))
136
148
  end
137
149
 
138
- def write_bundle(release)
139
- FileUtils.mkdir_p(@directory)
140
- run('bundle', 'create', File.join(@directory, 'release.bundle'), 'HEAD', "refs/tags/v#{release.version}")
141
- end
142
-
143
150
  def push_iteration!
144
151
  return if ancestor?(head, remote_head)
145
152
 
@@ -211,10 +218,7 @@ module RubyGemRelease
211
218
  end
212
219
 
213
220
  def run(*)
214
- output, error, status = Open3.capture3('git', *)
215
- raise "Git command failed: #{error.strip}\n#{output.strip}" unless status.success?
216
-
217
- output
221
+ self.class.command(*)
218
222
  end
219
223
  end
220
224
  end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'git'
4
+
5
+ module RubyGemRelease
6
+ module SourceControl
7
+ # Read-only source control for already reviewed, signed releases. Wraps a
8
+ # Git checkout and exposes no operations that commit, tag, stage, or push.
9
+ class ReviewedGit
10
+ def initialize(branch:, branches:, remote: 'origin', directory: nil)
11
+ raise ArgumentError, 'branches must be a nonempty array of explicit branch names' unless branches.is_a?(Array) && !branches.empty?
12
+
13
+ branches.each { |name| Git.command('check-ref-format', '--branch', name) }
14
+ @branch = branch
15
+ @branches = branches.dup.freeze
16
+ @remote = remote
17
+ @git = Git.new(branch: branch, remote: remote, directory: directory)
18
+ end
19
+
20
+ def head
21
+ @git.head
22
+ end
23
+
24
+ def timestamp(commit)
25
+ @git.timestamp(commit)
26
+ end
27
+
28
+ def configure!(**)
29
+ @git.configure!(**)
30
+ end
31
+
32
+ def check_clean!
33
+ @git.check_clean!
34
+ end
35
+
36
+ def check_branch!(ref:)
37
+ return if @branches.include?(@branch) && ref == "refs/heads/#{@branch}"
38
+
39
+ raise 'Reviewed releases must run from an explicitly allowed branch'
40
+ end
41
+
42
+ # Reviewed sources must be at the exact remote tip; callers' ancestor
43
+ # allowances, such as capture's, do not apply.
44
+ def check_current!(expected: head, **)
45
+ @git.check_current!(expected: expected, allow_ancestor: false)
46
+ end
47
+
48
+ def verify!(release, expected: head)
49
+ raise 'Reviewed checkout differs from captured source commit' unless head == expected
50
+
51
+ check_clean!
52
+ check_current!(expected: expected)
53
+ verify_tag!(release)
54
+ head
55
+ end
56
+
57
+ def preserve!(release, expected: head)
58
+ verify!(release, expected: expected)
59
+ @git.write_bundle(release)
60
+ end
61
+
62
+ private
63
+
64
+ def verify_tag!(release)
65
+ ref = "refs/tags/v#{release.version}"
66
+ raise 'Reviewed release requires an existing tag at the checkout commit' unless @git.tag_commit(release.version) == head
67
+
68
+ object = Git.command('rev-parse', ref).strip
69
+ remote = Git.command('ls-remote', '--refs', @remote, ref).split
70
+ raise 'Reviewed release tag must match the exact existing remote tag' unless remote == [object, ref]
71
+
72
+ Git.command('verify-tag', ref)
73
+ end
74
+ end
75
+ end
76
+ end
@@ -118,7 +118,7 @@ module RubyGemRelease
118
118
  with_failure_capture('release:workflow:capture') do
119
119
  capture_context = @provider.context(release: nil, task: 'release:workflow:capture', state: Workflow::State.new, workflow: @workflow)
120
120
  lifecycle(:capture, capture_context, after_context: -> { context('release:workflow:capture', preparing: true) }) do
121
- Workflow::Environment.new(provider: @provider).capture!(package: @package)
121
+ workflow_environment.capture!(package: @package)
122
122
  end
123
123
  end
124
124
  end
@@ -128,7 +128,7 @@ module RubyGemRelease
128
128
  task(:resolve) do
129
129
  with_failure_capture('release:workflow:resolve') do
130
130
  lifecycle(:resolve_version, context('release:workflow:resolve', preparing: true), after_context: -> { context('release:workflow:resolve') }) do
131
- Workflow::Environment.new(provider: @provider).resolve!(package: @package, destination: @destination, analysis: state.analysis)
131
+ workflow_environment.resolve!(package: @package, destination: @destination, analysis: state.analysis)
132
132
  end
133
133
  end
134
134
  end
@@ -144,6 +144,10 @@ module RubyGemRelease
144
144
  end
145
145
  end
146
146
 
147
+ def workflow_environment
148
+ @workflow.respond_to?(:environment) ? @workflow.environment : Workflow::Environment.new(provider: @provider)
149
+ end
150
+
147
151
  def install_publish_task
148
152
  desc 'Run the release publication callback'
149
153
  task :publish do
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyGemRelease
4
+ module Version
5
+ module Components
6
+ PATTERN = /\A(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)\z/
7
+
8
+ def next_version(part: :patch, from: read)
9
+ parts = parse(from)
10
+ index = { 'major' => 0, 'minor' => 1, 'patch' => 2 }.fetch(part.to_s) do
11
+ raise ArgumentError, 'bump part must be major, minor, or patch'
12
+ end
13
+ parts[index] += 1
14
+ ((index + 1)...parts.size).each { |i| parts[i] = 0 }
15
+ parts.join('.')
16
+ end
17
+
18
+ private
19
+
20
+ def parse(value)
21
+ match = PATTERN.match(value) if value.is_a?(String)
22
+ raise ArgumentError, "invalid version: #{value.inspect}; expected MAJOR.MINOR.PATCH" unless match
23
+
24
+ match.captures.map(&:to_i)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyGemRelease
4
+ module Version
5
+ module Interface
6
+ METHODS = %i[read set! increment!].freeze
7
+
8
+ def self.check!(version)
9
+ missing = METHODS.reject { |name| version.respond_to?(name) }
10
+ raise ArgumentError, "version object must implement #{missing.join(', ')}" unless missing.empty?
11
+
12
+ version
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'components'
4
+
5
+ module RubyGemRelease
6
+ module Version
7
+ class PlainText
8
+ include Components
9
+
10
+ LINE_ENDING = /\r?\n\z/
11
+
12
+ def initialize(path: 'VERSION')
13
+ @path = path
14
+ end
15
+
16
+ def read
17
+ version_in(File.read(@path))
18
+ end
19
+
20
+ def set!(value)
21
+ parse(value)
22
+ original = File.read(@path)
23
+ version_in(original)
24
+ File.write(@path, "#{value}#{original[LINE_ENDING]}")
25
+ value
26
+ end
27
+
28
+ def increment!(part: :patch, from: read)
29
+ set!(next_version(part: part, from: from))
30
+ end
31
+
32
+ private
33
+
34
+ def version_in(content)
35
+ content.sub(LINE_ENDING, '').tap { |value| parse(value) }
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'interface'
4
+
5
+ module RubyGemRelease
6
+ module Version
7
+ class References
8
+ def initialize(version:, references:)
9
+ raise ArgumentError, 'references must map files to arrays of patterns with a named version capture' unless valid_references?(references)
10
+
11
+ @version = Interface.check!(version)
12
+ @references = references
13
+ end
14
+
15
+ def read
16
+ @version.read
17
+ end
18
+
19
+ def set!(value)
20
+ @version.set!(value)
21
+ end
22
+
23
+ def increment!(from: read, **)
24
+ files = reference_files
25
+ files.each { |path, patterns| updated_content(File.read(path), patterns, from) }
26
+ result = @version.increment!(from: from, **)
27
+ files.each do |path, patterns|
28
+ original = File.read(path)
29
+ updated = updated_content(original, patterns, from)
30
+ File.write(path, updated) unless original == updated
31
+ end
32
+ result
33
+ end
34
+
35
+ private
36
+
37
+ def valid_references?(references)
38
+ references.is_a?(Hash) && references.values.all? do |patterns|
39
+ patterns.is_a?(Array) && patterns.all? { |pattern| pattern.is_a?(Regexp) && pattern.names.include?('version') }
40
+ end
41
+ end
42
+
43
+ def reference_files
44
+ @references.each_with_object({}) do |(selector, patterns), files|
45
+ selector = File.path(selector)
46
+ paths = selector.match?(/[*?\[\]{}]/) ? Dir.glob(selector).select { |path| File.file?(path) } : [selector]
47
+ paths.each { |path| (files[File.realpath(path)] ||= []).concat(patterns) }
48
+ end
49
+ end
50
+
51
+ def updated_content(content, patterns, version)
52
+ patterns.reduce(content) do |updated, pattern|
53
+ updated.gsub(pattern) { replace_capture(Regexp.last_match, version) }
54
+ end
55
+ end
56
+
57
+ def replace_capture(match, version)
58
+ raise ArgumentError, 'version capture did not match' unless match.begin(:version)
59
+
60
+ offset = match.begin(:version) - match.begin(0)
61
+ match[0][0...offset] + version + match[0][(offset + match[:version].length)..]
62
+ end
63
+ end
64
+ end
65
+ end
@@ -1,9 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'components'
4
+
3
5
  module RubyGemRelease
4
6
  module Version
5
7
  class RubyConstant
6
- PATTERN = /\A(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)\z/
8
+ include Components
9
+
10
+ PATTERN = Components::PATTERN
7
11
 
8
12
  def initialize(path:, constant:)
9
13
  raise ArgumentError, 'constant must be a Ruby constant name' unless constant.is_a?(String) && /\A[A-Z]\w*\z/.match?(constant)
@@ -31,25 +35,8 @@ module RubyGemRelease
31
35
  set!(next_version(part: part, from: from))
32
36
  end
33
37
 
34
- def next_version(part: :patch, from: read)
35
- parts = parse(from)
36
- index = { 'major' => 0, 'minor' => 1, 'patch' => 2 }.fetch(part.to_s) do
37
- raise ArgumentError, 'bump part must be major, minor, or patch'
38
- end
39
- parts[index] += 1
40
- ((index + 1)...parts.size).each { |i| parts[i] = 0 }
41
- parts.join('.')
42
- end
43
-
44
38
  private
45
39
 
46
- def parse(value)
47
- match = PATTERN.match(value) if value.is_a?(String)
48
- raise ArgumentError, "invalid version: #{value.inspect}; expected MAJOR.MINOR.PATCH" unless match
49
-
50
- match.captures.map(&:to_i)
51
- end
52
-
53
40
  def assignment(content)
54
41
  matches = content.to_enum(:scan, @assignment).map { Regexp.last_match }
55
42
  raise ArgumentError, "expected exactly one version assignment in #{@path}" unless matches.one?
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubyGemRelease
4
- VERSION = '0.6.0'
4
+ VERSION = '0.7.0'
5
5
  end
@@ -11,8 +11,9 @@ require_relative '../execution_provider'
11
11
  module RubyGemRelease
12
12
  module Workflow
13
13
  class Environment
14
- def initialize(provider: ExecutionProvider.current)
14
+ def initialize(provider: ExecutionProvider.current, source: nil)
15
15
  @provider = provider
16
+ @source = source
16
17
  end
17
18
 
18
19
  def configure!
@@ -37,8 +38,8 @@ module RubyGemRelease
37
38
  package ||= Package::RubyGem.new(gemspec: @provider.fetch('GEMSPEC_PATH', @provider.fetch('INPUT_GEMSPEC_PATH', '')) || '', directory: @provider.directory)
38
39
  authentication = authentication_for(destination)
39
40
  version = prepared_version(package, analysis)
40
- git = SourceControl::Git.new(directory: @provider.directory)
41
- git.stage_changes!
41
+ git = @source || SourceControl::Git.new(directory: @provider.directory)
42
+ @source ? git.check_clean! : git.stage_changes!
42
43
  @provider.export('RELEASE_VERSION' => version, 'RELEASE_AUTH_MODE' => authentication)
43
44
  end
44
45
 
@@ -93,10 +94,12 @@ module RubyGemRelease
93
94
  end
94
95
 
95
96
  def configured_git
96
- SourceControl::Git.new(directory: @provider.directory).tap { |git| git.configure!(**@provider.git_identity) }
97
+ (@source || SourceControl::Git.new(directory: @provider.directory)).tap { |git| git.configure!(**@provider.git_identity) }
97
98
  end
98
99
 
99
100
  def check_branch
101
+ return @source.check_branch!(ref: @provider.ref) if @source
102
+
100
103
  return if @provider.ref == "refs/heads/#{@provider.fetch('DEFAULT_BRANCH')}"
101
104
 
102
105
  raise 'Releases must run from the default branch'
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../tasks'
4
+ require_relative 'hooks'
5
+
6
+ module RubyGemRelease
7
+ module Workflow
8
+ # Shared hook registration, task installation, and publication checks.
9
+ # Including workflows set @package, @destination, and @provider, and
10
+ # define install_callbacks, validate_release, and prepare_publication.
11
+ module Lifecycle
12
+ def before(stage, &)
13
+ hooks.register(:before, stage, &)
14
+ self
15
+ end
16
+
17
+ def after(stage, &)
18
+ hooks.register(:after, stage, &)
19
+ self
20
+ end
21
+
22
+ def run_stage(stage, context, **, &)
23
+ hooks.run(stage, context, **, &)
24
+ end
25
+
26
+ def failure_details(error)
27
+ hooks.failure_details(error)
28
+ end
29
+
30
+ def hooks?(stage)
31
+ hooks.registered?(stage)
32
+ end
33
+
34
+ def install_tasks
35
+ Tasks.install(workflow: self, package: @package, destination: @destination, provider: @provider) do |tasks|
36
+ install_callbacks(tasks)
37
+ end
38
+ end
39
+
40
+ def checkpoint!(context)
41
+ run_stage(:checkpoint, context) { prepare_publication(context) }
42
+ end
43
+
44
+ def published?(release)
45
+ @package.respond_to?(:built?) && @package.built?(release) && @destination.published?(release, package: @package)
46
+ end
47
+
48
+ private
49
+
50
+ def hooks
51
+ @hooks ||= Hooks.new
52
+ end
53
+
54
+ def validate_with_hooks(ctx)
55
+ run_stage(:validate, ctx, after_context: -> { ctx.refresh }) do
56
+ raise ArgumentError, 'Release validation failed: callback returned false' if validate_release(ctx) == false
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lifecycle'
4
+ require_relative '../destination/ruby_gems'
5
+ require_relative '../hosting/github_release'
6
+ require_relative '../source_control/reviewed_git'
7
+
8
+ module RubyGemRelease
9
+ module Workflow
10
+ class ReviewedRubyGem
11
+ include Lifecycle
12
+
13
+ attr_reader :package, :destination, :git, :hosting, :provider
14
+
15
+ def initialize(package:, branches:, notes: nil, provider: ExecutionProvider.current, git: nil,
16
+ destination: Destination::RubyGems.new, hosting: Hosting::GitHubRelease.new,
17
+ validate: nil, success: nil, failure: nil)
18
+ @package = package
19
+ @destination = destination
20
+ @git = git || SourceControl::ReviewedGit.new(branch: provider.ref.delete_prefix('refs/heads/'), branches: branches, directory: provider.directory)
21
+ @hosting = hosting
22
+ @provider = provider
23
+ @notes = notes
24
+ @validate = validate
25
+ @success = success
26
+ @failure = failure
27
+ end
28
+
29
+ def environment
30
+ Environment.new(provider: @provider, source: @git)
31
+ end
32
+
33
+ private
34
+
35
+ def install_callbacks(tasks)
36
+ tasks.validate { |ctx| validate_with_hooks(ctx) }
37
+ tasks.prepare { |ctx| prepare_release(ctx) }
38
+ tasks.publish { |ctx| publish(ctx) }
39
+ tasks.success(&@success) if @success
40
+ tasks.failure(&@failure) if @failure
41
+ end
42
+
43
+ def verify_source!(release)
44
+ @git.verify!(release, expected: @provider.fetch('SOURCE_COMMIT', @git.head))
45
+ end
46
+
47
+ def validate_release(ctx)
48
+ verify_source!(ctx.release)
49
+ publication_release(ctx.release)
50
+ @validate&.call(ctx)
51
+ end
52
+
53
+ def prepare_release(ctx)
54
+ run_stage(:prepare_release, ctx) { verify_source!(ctx.release) }
55
+ verify_source!(ctx.release)
56
+ end
57
+
58
+ def prepare_publication(ctx)
59
+ verify_source!(ctx.release)
60
+ run_stage(:build_package, ctx) { @package.build!(ctx.release) }
61
+ @git.preserve!(ctx.release, expected: @provider.fetch('SOURCE_COMMIT'))
62
+ end
63
+
64
+ def publish(ctx)
65
+ release = publication_release(ctx.release)
66
+ prepare_publication(ctx)
67
+ run_stage(:publish_registry, ctx) { @destination.publish!(release, package: @package) }
68
+ verify_source!(release)
69
+ run_stage(:publish_hosted_release, ctx) { @hosting.publish!(release, package: @package) } if @hosting
70
+ end
71
+
72
+ def publication_release(release)
73
+ return release unless @notes
74
+
75
+ notes = @notes.read(release.version)
76
+ raise ArgumentError, 'reviewed release notes must be a nonempty string' unless notes.is_a?(String) && !notes.strip.empty?
77
+
78
+ Release.new(version: release.version, gemspec_version: release.gemspec_version, date: release.date, notes: notes)
79
+ end
80
+ end
81
+ end
82
+ end