crew: Fix more SSL errors (#11861)

* crew: Fix more SSL errors

Signed-off-by: SupeChicken666 <me@supechicken666.dev>

* Also include URL query params in request

Signed-off-by: SupeChicken666 <me@supechicken666.dev>

* [skip ci] rubocop: Disable Layout/SpaceAroundMethodCallOperator

Signed-off-by: SupeChicken666 <me@supechicken666.dev>

* Revert "[skip ci] rubocop: Disable Layout/SpaceAroundMethodCallOperator"

This reverts commit 8f238860b7.

* Pass block as lambda

Signed-off-by: SupeChicken666 <me@supechicken666.dev>

---------

Signed-off-by: SupeChicken666 <me@supechicken666.dev>
This commit is contained in:
SupeChicken666
2025-05-01 23:42:29 +08:00
committed by GitHub
parent 20068081d0
commit 8cfa64a3da
3 changed files with 45 additions and 30 deletions

View File

@@ -1,8 +1,8 @@
# 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'
require_relative 'downloader'
class PackageUtils
def self.installed?(pkg_name)
@@ -45,27 +45,31 @@ class PackageUtils
def self.get_binary_url(pkg)
# Fall back to the old method of querying if the gitlab API doesn't work for whatever reason.
fallback_url = "#{CREW_GITLAB_PKG_REPO}/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("#{CREW_GITLAB_PKG_REPO}?package_name=#{pkg.name}&package_version=#{pkg.version}_#{ARCH}")))
response = get_http_response("#{CREW_GITLAB_PKG_REPO}?package_name=#{pkg.name}&package_version=#{pkg.version}_#{ARCH}")
packages = JSON.parse(response.body)
# 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
package_id = packages.select(&->(p) { p['name'] == pkg.name }).dig(0, 'id')
# 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?
return fallback_url unless package_id
# List all the package files for that package ID.
package_files = JSON.parse(Net::HTTP.get(URI("#{CREW_GITLAB_PKG_REPO}/#{package_id}/package_files")))
response = get_http_response("#{CREW_GITLAB_PKG_REPO}/#{package_id}/package_files")
package_files = JSON.parse(response.body)
# 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
file_id = package_files.select(&->(p) { p['file_sha256'] == pkg.binary_sha256[ARCH.to_sym] }).dig(0, 'id')
return "https://gitlab.com/chromebrew/binaries/-/package_files/#{file_id}/download" if file_id
# If we're still here, the likely cause is that the file sha256s are mismatched.
return fallback_url
end