Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions .github/scripts/check_lockfile_checksums.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

# Fails unless the lockfile only uses rubygems.org and the local gemspec, and every gem from
# rubygems.org has a sha256 in its CHECKSUMS section.
# Run by CI before bundle install. Standard library only.
# Usage: ruby .github/scripts/check_lockfile_checksums.rb Gemfile.lock

def fail_check(message)
warn "check_lockfile_checksums: #{message}. Regenerate with .github/scripts/update-lockfiles.sh"
exit 1
end

fail_check('usage: check_lockfile_checksums.rb <lockfile>') unless ARGV.size == 1
path = ARGV.first
fail_check("#{path} is not a regular file") unless File.file?(path) && !File.symlink?(path)

sections = Hash.new { |hash, key| hash[key] = [] }
current = nil
File.foreach(path, chomp: true) do |line|
line = line.delete_suffix("\r")
if line.match?(/\A[A-Z][A-Z ]*\z/)
current = line
elsif current
sections[current] << line
end
end

foreign = sections.keys & ['GIT', 'PLUGIN SOURCE', 'GEM SOURCE']
unless foreign.empty?
fail_check("#{path} has #{foreign.join(' and ')} sources; only rubygems.org and the local gemspec are allowed")
end
remotes = (sections['GEM'] + sections['PATH']).filter_map { |line| line[/\A remote: (.+)\z/, 1] }
fail_check("#{path} has unexpected remotes: #{remotes.inspect}") unless remotes.sort == ['.', 'https://rubygems.org/']

gems = sections['GEM'].filter_map { |line| line[/\A {4}(\S+ \([^)]+\))\z/, 1] }
fail_check("#{path} lists no gems") if gems.empty?
fail_check("#{path} has no CHECKSUMS section") unless sections.key?('CHECKSUMS')

checksums = sections['CHECKSUMS'].each_with_object({}) do |line, found|
match = line.match(/\A {2}(\S+ \([^)]+\))(?: sha256=([0-9a-f]{64}))?\z/)
found[match[1]] = match[2] if match
end

missing = gems.reject { |gem| checksums[gem] }
fail_check("no sha256 for #{missing.join(', ')}") unless missing.empty?

path_gems = sections['PATH'].filter_map { |line| line[/\A {4}(\S+ \([^)]+\))\z/, 1] }
unknown = checksums.keys - gems - path_gems
fail_check("CHECKSUMS names gems that are not locked: #{unknown.join(', ')}") unless unknown.empty?

puts "check_lockfile_checksums: #{gems.size} gems in #{path}, all with sha256"
41 changes: 41 additions & 0 deletions .github/scripts/set_version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

# Sets the gem version in lib/checkout_sdk/version.rb and in the checkout_sdk lines of the committed
# lockfiles, so a version bump needs neither Docker nor a dependency resolve. Dependency changes still
# need .github/scripts/update-lockfiles.sh. Standard library only; run from the repository root.
# Usage: ruby .github/scripts/set_version.rb <version>

VERSION_PATTERN = /\A\d+\.\d+\.\d+(\.(alpha|beta|rc|pre)\.?\d+)?\z/.freeze
VERSION_FILE = 'lib/checkout_sdk/version.rb'
# Lockfile => number of "checkout_sdk (X.Y.Z)" lines it holds (PATH specs, plus CHECKSUMS when present).
LOCKFILES = { 'Gemfile.lock' => 2, 'gemfiles/ruby_2_7.gemfile.lock' => 1 }.freeze

def fail_check(message)
warn "set_version: #{message}"
exit 1
end

# Replaces every match of pattern in path, after checking it matches exactly expected times.
def rewrite(path, pattern, expected)
fail_check("#{path} is not a regular file") unless File.file?(path) && !File.symlink?(path)
text = File.read(path)
found = text.scan(pattern).size
fail_check("expected #{expected} version line(s) in #{path}, found #{found}") unless found == expected
File.write(path, yield(text))
end

fail_check('usage: set_version.rb <version>') unless ARGV.size == 1
version = ARGV.first
fail_check('the version is not a release or pre-release version') unless version.match?(VERSION_PATTERN)

version_line = /^ VERSION = '[^']*'$/
rewrite(VERSION_FILE, version_line, 1) { |text| text.sub(version_line, " VERSION = '#{version}'") }

lock_line = /^( {2}| {4})checkout_sdk \([^)]+\)$/
LOCKFILES.each do |path, expected|
rewrite(path, lock_line, expected) do |text|
text.gsub(lock_line) { "#{Regexp.last_match(1)}checkout_sdk (#{version})" }
end
end

puts "set_version: #{version} in #{VERSION_FILE} and #{LOCKFILES.keys.join(', ')}"
60 changes: 60 additions & 0 deletions .github/scripts/update-lockfiles.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env bash
# Regenerates the committed lockfiles in linux/amd64 containers, so they resolve for the CI
# runners and not for the local machine. Needs Docker, except with --version-only.
#
# Gemfile.lock Ruby 3.1, Bundler 2.6.9, with gem checksums.
# Ruby 3.1 and 3.3 CI legs and the release workflow.
# gemfiles/ruby_2_7.gemfile.lock Ruby 2.7, Bundler 2.4.22 (the last Bundler for Ruby 2.7,
# which cannot record checksums). Ruby 2.7 CI leg only.
#
# CI installs with frozen lockfiles and fails when they are missing or out of date, so run this
# after changing the Gemfile, the gemspec or lib/checkout_sdk/version.rb (the lock records the
# gem's own version). Without arguments it keeps every locked version that still satisfies the
# Gemfile; `--update` re-resolves all gems to the newest allowed versions.
#
# After a version bump only, `--version-only` copies the version from lib/checkout_sdk/version.rb
# into both lockfiles with any local Ruby, no Docker and no dependency resolve.
#
# Usage: .github/scripts/update-lockfiles.sh [--update | --version-only]
set -euo pipefail

cd "$(dirname "$0")/../.."

update=''
case "${1:-}" in
'') ;;
--update) update='--update' ;;
--version-only)
version="$(ruby -e 'load "lib/checkout_sdk/version.rb"; print CheckoutSdk::VERSION')"
ruby .github/scripts/set_version.rb "$version"
echo 'Lockfiles updated. Review the diff before committing: only checkout_sdk lines may change.'
exit 0
;;
*) echo "usage: $0 [--update | --version-only]" >&2; exit 2 ;;
esac

if ! docker info > /dev/null 2>&1; then
echo 'Docker is not running. After a version bump only, use --version-only instead.' >&2
exit 1
fi

lock() {
local ruby_version="$1"
local bundler_version="$2"
local gemfile="$3"
local extra_lock_args="$4"
docker run --rm --platform linux/amd64 \
-v "$PWD:/src" -w /src \
-e BUNDLE_GEMFILE="$gemfile" -e UPDATE="$update" -e BUNDLER_VERSION="$bundler_version" -e EXTRA="$extra_lock_args" \
"ruby:${ruby_version}" bash -euc '
git config --global --add safe.directory /src
gem install bundler -v "$BUNDLER_VERSION" --no-document --silent
# shellcheck disable=SC2086
bundle "_${BUNDLER_VERSION}_" lock $UPDATE --add-platform ruby x86_64-linux $EXTRA
'
}

lock 3.1 2.6.9 Gemfile --add-checksums
lock 2.7 2.4.22 gemfiles/ruby_2_7.gemfile ''

echo 'Lockfiles updated. Review the diff before committing.'
69 changes: 69 additions & 0 deletions .github/scripts/verify_gem.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# frozen_string_literal: true

# Checks the gem built by the release workflow before it is uploaded for publishing.
# Standard library only. Usage: ruby .github/scripts/verify_gem.rb <version>
#
# Fails unless the working directory holds exactly checkout_sdk-<version>.gem, and that gem:
# - is named checkout_sdk and carries <version>;
# - has no native extensions and no executables (nothing runs when merchants install it);
# - packages only regular files (no symlinks or links), only under lib/, sig/ and the known
# top-level files (no lockfiles, CI files or specs).

require 'rubygems/package'
require 'zlib'

VERSION_PATTERN = /\A\d+\.\d+\.\d+(\.(alpha|beta|rc|pre)\.?\d+)?\z/.freeze
ALLOWED_TOP_LEVEL = %w[
.overcommit.yml .rspec .rubocop.yml .ruby-version CODE_OF_CONDUCT.md Gemfile LICENSE.md README.md Rakefile
].freeze
ALLOWED_DIRECTORIES = %r{\A(?:lib|sig)/}.freeze

# Names of the entries in the gem's data archive that are not regular files.
def non_regular_entries(gem_path)
found = []
File.open(gem_path, 'rb') do |io|
Gem::Package::TarReader.new(io).each do |outer|
next unless outer.full_name == 'data.tar.gz'

Zlib::GzipReader.wrap(outer) do |gzip|
Gem::Package::TarReader.new(gzip).each { |entry| found << entry.full_name unless entry.file? }
end
end
end
found
end

def fail_check(message)
warn "verify_gem: #{message}"
exit 1
end

fail_check('usage: verify_gem.rb <version>') unless ARGV.size == 1
version = ARGV.first
fail_check('the version is not a release or pre-release version') unless version.match?(VERSION_PATTERN)

expected = "checkout_sdk-#{version}.gem"
built = Dir.children(Dir.pwd).select { |name| name.end_with?('.gem') }
fail_check("expected only #{expected}, found #{built.sort.inspect}") unless built == [expected]

package = Gem::Package.new(expected)
spec = package.spec
fail_check("gem name is #{spec.name}") unless spec.name == 'checkout_sdk'
fail_check("gem version is #{spec.version}") unless spec.version.to_s == version
fail_check('the gem declares native extensions') unless spec.extensions.empty?
fail_check('the gem declares executables') unless spec.executables.empty?

contents = package.contents
fail_check('the gem file list does not match its contents') unless contents.sort == spec.files.sort
fail_check('lib/checkout_sdk/version.rb is missing') unless contents.include?('lib/checkout_sdk/version.rb')

links = non_regular_entries(expected)
fail_check("entries that are not regular files: #{links.sort.inspect}") unless links.empty?

unsafe = contents.select { |path| path.start_with?('/') || path.split('/').include?('..') }
fail_check("unsafe paths in the gem: #{unsafe.sort.inspect}") unless unsafe.empty?

unexpected = contents.reject { |path| ALLOWED_DIRECTORIES.match?(path) || ALLOWED_TOP_LEVEL.include?(path) }
fail_check("unexpected files in the gem: #{unexpected.sort.inspect}") unless unexpected.empty?

puts "verify_gem: #{expected} OK (#{contents.size} files)"
29 changes: 26 additions & 3 deletions .github/workflows/build-master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ on:
branches:
- "master"

permissions:
contents: read

jobs:
build:
if: "!contains(github.event.commits[0].message, 'Release')"
Expand All @@ -20,13 +23,33 @@ jobs:
'3.3',
]
name: Ruby ${{ matrix.ruby-version }}
env:
# Ruby 2.7 cannot install every gem in Gemfile.lock (resolved for Ruby 3.1): it has its own lock.
BUNDLE_GEMFILE: ${{ matrix.ruby-version == '2.7' && 'gemfiles/ruby_2_7.gemfile' || 'Gemfile' }}
BUNDLE_FROZEN: 'true'
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
# Requires the committed lockfile.
- id: require-lockfile
run: |
if [ ! -f "${BUNDLE_GEMFILE}.lock" ]; then
echo "::error::${BUNDLE_GEMFILE}.lock is missing. Run .github/scripts/update-lockfiles.sh and commit it."
exit 1
fi
- id: setup-ruby-and-bundle
uses: ruby/setup-ruby@v1
uses: ruby/setup-ruby@b3aaceaef7e0ee231b499e7b5f49c03fc1d32199 # v1.338.0
with:
ruby-version: ${{ matrix.ruby-version }}
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
# Gems are installed fresh on every run.
bundler-cache: false
# Checksum check for the legs that install from Gemfile.lock.
- id: check-lockfile-checksums
if: env.BUNDLE_GEMFILE == 'Gemfile'
run: ruby .github/scripts/check_lockfile_checksums.rb Gemfile.lock
- id: install-dependencies
run: bundle install
- id: begin-rubocop-scan
run: bundle exec rubocop lib
- id: build-and-analyse
Expand Down
29 changes: 26 additions & 3 deletions .github/workflows/build-pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ on:
- "master"
- "feature/**"

permissions:
contents: read

jobs:
build:
runs-on: ubuntu-latest
Expand All @@ -20,13 +23,33 @@ jobs:
'3.3',
]
name: Ruby ${{ matrix.ruby-version }}
env:
# Ruby 2.7 cannot install every gem in Gemfile.lock (resolved for Ruby 3.1): it has its own lock.
BUNDLE_GEMFILE: ${{ matrix.ruby-version == '2.7' && 'gemfiles/ruby_2_7.gemfile' || 'Gemfile' }}
BUNDLE_FROZEN: 'true'
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
# Requires the committed lockfile.
- id: require-lockfile
run: |
if [ ! -f "${BUNDLE_GEMFILE}.lock" ]; then
echo "::error::${BUNDLE_GEMFILE}.lock is missing. Run .github/scripts/update-lockfiles.sh and commit it."
exit 1
fi
- id: setup-ruby-and-bundle
uses: ruby/setup-ruby@v1
uses: ruby/setup-ruby@b3aaceaef7e0ee231b499e7b5f49c03fc1d32199 # v1.338.0
with:
ruby-version: ${{ matrix.ruby-version }}
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
# Gems are installed fresh on every run.
bundler-cache: false
# Checksum check for the legs that install from Gemfile.lock.
- id: check-lockfile-checksums
if: env.BUNDLE_GEMFILE == 'Gemfile'
run: ruby .github/scripts/check_lockfile_checksums.rb Gemfile.lock
- id: install-dependencies
run: bundle install
- id: begin-rubocop-scan
run: bundle exec rubocop lib
- id: build-and-analyse
Expand Down
Loading
Loading