Skip to content

Commit

Permalink
refactor: apply Rubocop to Ruby generator (google#956)
Browse files Browse the repository at this point in the history
Cherry-pick of G-Rath/osv-detector#240

---

This is all pretty harmless but there's some nice small improvements in
here
  • Loading branch information
G-Rath authored May 6, 2024
1 parent 7360e3a commit 9df6829
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions scripts/generators/generate-rubygems-versions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# such comparisons in the detector would in fact be wrong.
#
# @type [Array<String>]
UNSUPPORTED_COMPARISONS = []
UNSUPPORTED_COMPARISONS = [].freeze

# @param [String] line
# @return [Boolean]
Expand All @@ -24,12 +24,12 @@ def is_unsupported_comparison?(line)
# @param [String] line
# @return [String]
def uncomment(line)
line.sub(/^#|\/\//, "")
line.sub(%r{^#|//}, "")
end

def download_rubygems_db
URI.open("https://osv-vulnerabilities.storage.googleapis.com/RubyGems/all.zip") do |zip|
File.open("rubygems-db.zip", "wb") { |f| f.write(zip.read) }
File.binwrite("rubygems-db.zip", zip.read)
end
end

Expand All @@ -38,6 +38,8 @@ def extract_packages_with_versions(osvs)

osvs.each do |osv|
osv["affected"].each do |affected|
next unless affected["package"]["ecosystem"] == "RubyGems"

package = affected["package"]["name"]

packages[package] ||= []
Expand All @@ -47,7 +49,7 @@ def extract_packages_with_versions(osvs)
end
end

packages.map { |k, v| [k, v.uniq.sort] }.to_h
packages.transform_values { |v| v.uniq.sort }
end

def compare_version(v1, op, v2)
Expand All @@ -72,7 +74,7 @@ def compare_versions(lines, select = :all)
next
end

parts = line.split(" ")
parts = line.split
v1 = parts[0]
op = parts[1]
v2 = parts[2]
Expand Down Expand Up @@ -117,15 +119,15 @@ def generate_version_compares(versions)
def generate_package_compares(packages)
comparisons = []

packages.each { |_, versions| comparisons.concat(generate_version_compares(versions)) }
packages.each_value { |versions| comparisons.concat(generate_version_compares(versions)) }

comparisons
end

def fetch_packages_versions
download_rubygems_db

osvs = Zip::File.open("rubygems-db.zip").map { |f| JSON.load(f.get_input_stream.read) }
osvs = Zip::File.open("rubygems-db.zip").map { |f| JSON.parse(f.get_input_stream.read) }

extract_packages_with_versions(osvs)
end
Expand All @@ -134,7 +136,7 @@ def fetch_packages_versions

packs = fetch_packages_versions

File.open(outfile, "w") { |f| f.write(generate_package_compares(packs).uniq.join("\n") + "\n") }
File.write(outfile, "#{generate_package_compares(packs).uniq.join("\n")}\n")

# set this to either "failures" or "successes" to only have those comparison results
# printed; setting it to anything else will have all comparison results printed
Expand Down

0 comments on commit 9df6829

Please sign in to comment.