Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
quinna-h committed Aug 27, 2024
1 parent 08cf387 commit 46bd0fd
Showing 1 changed file with 54 additions and 49 deletions.
103 changes: 54 additions & 49 deletions .github/scripts/version_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'bundler/cli'


# Run bundler and handle errors
def run_bundler_command(gemfile, command_args)
Bundler.reset!
ENV['BUNDLE_GEMFILE'] = gemfile
Expand All @@ -11,7 +12,57 @@ def run_bundler_command(gemfile, command_args)
puts "Error: #{e.message}"
end

def parse_lockfiles
# Update a gem in all its gemfiles
def update_gem(gem_to_update, gems_in)
# HACK - setting this to grpc while testing out the script
# gem_to_update = 'grpc'
gems_in[gem_to_update].each do |gemfile, version|
puts "Checking to see if #{gem_to_update} needs to be updated in #{gemfile}"
gemfile_lock = "#{gemfile}.lock"
definition = Bundler::Definition.build(gemfile, gemfile_lock, nil)
dependencies = definition.dependencies

dependencies.each do |dep|
next if gem_to_update != dep.name # only update the gem we chose at random
# puts "Skipping #{dep.name} as it is not #{gem_to_update}"

gem_name = dep.name
gem_version = dep.requirements_list
gem_requirements = Gem::Requirement.create(*gem_version)
gem_platforms = dep.platforms

puts "Gem Name: #{gem_name}"
puts "Gem Version: #{gem_version}"
puts "Gem Requirments: #{gem_requirements}"
puts "Gem Platforms: #{gem_platforms}"

latest = Gem.latest_version_for(gem_name)

if gem_requirements.satisfied_by?(latest)
puts "The latest (#{latest}) of #{gem_name} is satisfied by #{gem_requirements}"
bundler_args = ['lock', '--update', gem_name]

# do we have any platforms specified? If so, add them each with --add-platform
bundler_args.concat(dep.platforms.map { |platform| ['--add-platform', platform.to_s] }) if dep.platforms.any?

# NOTE: I'm seeing some platforms being removed
# For example gemfiles/ruby_2.5_contrib.gemfile.lock has "grpc (1.48.0-x86_64-linux)"
# But this platform isn't specified in the gemfile
# Run the command here removes this "grpc (1.48.0-x86_64-linux)" instance entirely
# TODO check with Marco/Tony/David for how this came to be as the PR they were added in was a while back and was just an update

puts "Updating #{gem_name} with: #{bundler_args}"
run_bundler_command(gemfile, bundler_args)
else
puts "The latest (#{latest}) of #{gem_name} is NOT satisfied by #{gem_requirements}"
# TODO - should we attempt to do a --conservative update here?
end
end
end
end


def run_update
# for each directory in /contrib/ read the hash of the loaded Gems
# https://www.rubydoc.info/github/rubygems/rubygems/Gem.loaded_specs
# taken from: https://github.com/DataDog/apm-shared-github-actions/blob/2b49e71feba54bdfaabdeeb026ec3032d819371f/.github/scripts/get-tested-integration-versions.rb#L36
Expand Down Expand Up @@ -58,54 +109,8 @@ def parse_lockfiles
# if the PR is the autogenerated PR to update the gem we could skip it
gem_to_update = integrated_gems.sample # HACKY pick a random gem to attempt to update
puts "Randomly chose #{gem_to_update} to update"

# HACK - setting this to grpc while testing out the script
# gem_to_update = 'grpc'
gems_in[gem_to_update].each do |gemfile, version|
puts "Checking to see if #{gem_to_update} needs to be updated in #{gemfile}"
# need the lockfile too
gemfile_lock = "#{gemfile}.lock"
definition = Bundler::Definition.build(gemfile, gemfile_lock, nil)
dependencies = definition.dependencies

dependencies.each do |dep|
next if gem_to_update != dep.name # only update the gem we chose at random
# puts "Skipping #{dep.name} as it is not #{gem_to_update}"

gem_name = dep.name
gem_version = dep.requirements_list
gem_requirements = Gem::Requirement.create(*gem_version)
gem_platforms = dep.platforms

puts "Gem Name: #{gem_name}"
puts "Gem Version: #{gem_version}"
puts "Gem Requirments: #{gem_requirements}"
puts "Gem Platforms: #{gem_platforms}"

latest = Gem.latest_version_for(gem_name)

if gem_requirements.satisfied_by?(latest)
puts "The latest (#{latest}) of #{gem_name} is satisfied by #{gem_requirements}"
bundler_args = ['lock', '--update', gem_name]

# do we have any platforms specified? If so, add them each with --add-platform
bundler_args.concat(dep.platforms.map { |platform| ['--add-platform', platform.to_s] }) if dep.platforms.any?

# NOTE: I'm seeing some platforms being removed
# For example gemfiles/ruby_2.5_contrib.gemfile.lock has "grpc (1.48.0-x86_64-linux)"
# But this platform isn't specified in the gemfile
# Run the command here removes this "grpc (1.48.0-x86_64-linux)" instance entirely
# TODO check with Marco/Tony/David for how this came to be as the PR they were added in was a while back and was just an update

puts "Updating #{gem_name} with: #{bundler_args}"
run_bundler_command(gemfile, bundler_args)
else
puts "The latest (#{latest}) of #{gem_name} is NOT satisfied by #{gem_requirements}"
# TODO - should we attempt to do a --conservative update here?
end
end
end
update_gem(gem_to_update, gems_in)
end


parse_lockfiles
run_update

0 comments on commit 46bd0fd

Please sign in to comment.