Reland: Download the correct file when there are multiple versions of a package binary (#10336)

This commit is contained in:
Maximilian Downey Twiss
2024-09-03 16:23:40 +10:00
committed by GitHub
parent 03a8088300
commit a532c3fc47
5 changed files with 65 additions and 18 deletions

View File

@@ -1,6 +1,7 @@
# lib/package_utils.rb
# Utility functions that take either a package object or a component of a package object as primary input.
require 'json'
require 'net/http'
require_relative 'const'
class PackageUtils
@@ -23,7 +24,7 @@ class PackageUtils
def self.get_url(pkg, build_from_source: false)
if !build_from_source && pkg.binary_sha256&.key?(ARCH.to_sym)
return "https://gitlab.com/api/v4/projects/26210301/packages/generic/#{pkg.name}/#{pkg.version}_#{ARCH}/#{pkg.name}-#{pkg.version}-chromeos-#{ARCH}.#{pkg.binary_compression}"
return get_binary_url(pkg)
elsif pkg.source_url.is_a?(Hash) && pkg.source_url&.key?(ARCH.to_sym)
return pkg.source_url[ARCH.to_sym]
else
@@ -41,6 +42,34 @@ class PackageUtils
end
end
def self.get_binary_url(pkg)
# Fall back to the old method if querying the gitlab API doesn't work for whatever reason.
fallback_url = "https://gitlab.com/api/v4/projects/26210301/packages/generic/#{pkg.name}/#{pkg.version}_#{ARCH}/#{pkg.name}-#{pkg.version}-chromeos-#{ARCH}.#{pkg.binary_compression}"
# List all the packages with the name and version of the package file.
# The name search is fuzzy, so we need to refine it further (otherwise packages like vim, gvim and vim_runtime would break).
packages = JSON.parse(Net::HTTP.get(URI("https://gitlab.com/api/v4/projects/26210301/packages?package_name=#{pkg.name}&package_version=#{pkg.version}_#{ARCH}")))
# Loop over each result until we get an exact name match, then return the package ID for that match.
package_id = 0
(0..packages.count - 1).each do |i|
next unless packages[i]['name'] == pkg.name
package_id = packages[i]['id']
end
# Return early if we weren't able to find the package ID, so that the CREW_CACHE_ENABLED hack to test packages without uploading them still works.
# When we're doing that, we're calling download knowing that there isn't an actual file to download, but relying on CREW_CACHE_ENABLED to save us before we get there.
return fallback_url if package_id.zero?
# List all the package files for that package ID.
package_files = JSON.parse(Net::HTTP.get(URI("https://gitlab.com/api/v4/projects/26210301/packages/#{package_id}/package_files")))
# Bail out if we weren't actually able to find a package.
return fallback_url if package_files.is_a?(Hash) && package_files['message'] == '404 Not found'
# Loop over each result until we find a matching file_sha256 to our binary_sha256.
(0..package_files.count - 1).each do |i|
next unless package_files[i]['file_sha256'] == pkg.binary_sha256[ARCH.to_sym]
return "https://gitlab.com/chromebrew/binaries/-/package_files/#{package_files[i]['id']}/download"
end
# If we're still here, the likely cause is that the file sha256s are mismatched.
return fallback_url
end
def self.get_clean_version(pkg_version)
# Trim kde- suffixes in qt5 packages so nothing else gets confused.
pkg_version.delete_prefix!('kde-')