Skip to content

Commit

Permalink
Terraform requirements updater - update update_greatest_version to ha…
Browse files Browse the repository at this point in the history
…ndle both less than and less-than/equal operators

- 'index_to_update' would sometimes pick the middle or first segement
  instead of the last segment leading to the wrong version
segment being incremented
- less-than/equals would always get incremented instead of taking the
  version as-is
- minor or patch version would sometimes get set to 0 once the
  'index_to_update' was set
  • Loading branch information
bryan-bar committed Oct 18, 2024
1 parent c47cfb9 commit bb6c535
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions terraform/lib/dependabot/terraform/requirements_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,17 +190,20 @@ def update_greatest_version(requirement, version_to_be_permitted)
op, version = requirement.requirements.first
version = version.release if version.prerelease?

index_to_update =
version.segments.map.with_index { |seg, i| seg.zero? ? 0 : i }.max

new_segments = version.segments.map.with_index do |_, index|
if index < index_to_update
# When 'less than'/'<',
# increment the last available segment only so that the new version is within the constraint
if op == "<"
new_segments = version.segments.map.with_index do |_, index|
version_to_be_permitted.segments[index]
elsif index == index_to_update
version_to_be_permitted.segments[index].to_i + 1
else
0
end
new_segments[-1] += 1
# When 'less-than/equal'/'<=', use the new version as-is even when previously set as a non-semver version
# Terraform treats shortened versions the same as a version with any remaining segments as 0
# Example: '0.2' is treated as '0.2.0' | '1' is treated as '1.0.0'
elsif op == "<="
new_segments = version_to_be_permitted.segments
else
raise "Unexpected operation: #{op}"
end

requirement_class.new("#{op} #{new_segments.join('.')}")
Expand Down

0 comments on commit bb6c535

Please sign in to comment.