2020-10-10 14:16:11 +02:00
|
|
|
# typed: false
|
2020-06-29 10:16:58 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-06-30 11:23:34 -05:00
|
|
|
require "utils/curl"
|
2020-07-03 01:51:38 +00:00
|
|
|
|
2020-08-26 09:11:39 +02:00
|
|
|
# Repology API client.
|
|
|
|
#
|
|
|
|
# @api private
|
2020-07-06 03:32:18 +00:00
|
|
|
module Repology
|
2021-02-17 02:04:13 +05:30
|
|
|
HOMEBREW_CORE = "homebrew"
|
|
|
|
HOMEBREW_CASK = "homebrew_casks"
|
|
|
|
|
2020-07-01 02:50:21 +00:00
|
|
|
module_function
|
2020-06-29 09:57:21 -05:00
|
|
|
|
2020-08-19 09:10:57 -05:00
|
|
|
MAX_PAGINATION = 15
|
2020-08-26 09:11:39 +02:00
|
|
|
private_constant :MAX_PAGINATION
|
2020-08-19 09:10:57 -05:00
|
|
|
|
2021-06-30 08:51:33 +01:00
|
|
|
def query_api(_last_package_in_response = "", repository:)
|
|
|
|
{}
|
|
|
|
# TODO: uncomment (and remove lines above) when we have a fix for Repology
|
|
|
|
# `curl` issues
|
|
|
|
# last_package_in_response += "/" if last_package_in_response.present?
|
|
|
|
# url = "https://repology.org/api/v1/projects/#{last_package_in_response}?inrepo=#{repository}&outdated=1"
|
|
|
|
|
|
|
|
# output, _errors, _status = curl_output(url.to_s)
|
|
|
|
# JSON.parse(output)
|
2020-06-29 09:57:21 -05:00
|
|
|
end
|
2020-06-29 09:51:58 -05:00
|
|
|
|
2021-02-17 00:09:02 +05:30
|
|
|
def single_package_query(name, repository:)
|
2021-06-30 08:51:33 +01:00
|
|
|
# TODO: uncomment when we have a fix for Repology `curl` issues
|
|
|
|
# url = "https://repology.org/tools/project-by?repo=#{repository}&" \
|
|
|
|
# "name_type=srcname&target_page=api_v1_project&name=#{name}"
|
|
|
|
|
|
|
|
# output, _errors, _status = curl_output("--location", url.to_s)
|
|
|
|
|
|
|
|
# begin
|
|
|
|
# data = JSON.parse(output)
|
|
|
|
# { name => data }
|
|
|
|
# rescue
|
|
|
|
# nil
|
|
|
|
# end
|
2020-07-28 09:30:19 -05:00
|
|
|
end
|
|
|
|
|
2021-02-17 00:09:02 +05:30
|
|
|
def parse_api_response(limit = nil, repository:)
|
|
|
|
package_term = case repository
|
2021-02-17 02:04:13 +05:30
|
|
|
when HOMEBREW_CORE
|
2021-02-17 00:09:02 +05:30
|
|
|
"formula"
|
2021-02-17 02:04:13 +05:30
|
|
|
when HOMEBREW_CASK
|
2021-02-17 00:09:02 +05:30
|
|
|
"cask"
|
|
|
|
else
|
|
|
|
"package"
|
|
|
|
end
|
|
|
|
|
|
|
|
ohai "Querying outdated #{package_term.pluralize} from Repology"
|
2020-06-29 09:51:58 -05:00
|
|
|
|
2020-08-18 22:25:17 +00:00
|
|
|
page_no = 1
|
2020-08-19 10:12:38 -05:00
|
|
|
outdated_packages = {}
|
2021-02-17 00:09:02 +05:30
|
|
|
last_package = ""
|
2020-06-29 09:51:58 -05:00
|
|
|
|
2020-08-19 10:12:38 -05:00
|
|
|
while page_no <= MAX_PAGINATION
|
2020-07-06 09:08:41 -05:00
|
|
|
odebug "Paginating Repology API page: #{page_no}"
|
2020-06-29 09:51:58 -05:00
|
|
|
|
2021-02-17 00:09:02 +05:30
|
|
|
response = query_api(last_package, repository: repository)
|
2020-06-29 09:57:21 -05:00
|
|
|
outdated_packages.merge!(response)
|
2021-02-17 00:09:02 +05:30
|
|
|
last_package = response.keys.last
|
2020-08-19 10:12:38 -05:00
|
|
|
|
2020-08-19 09:10:57 -05:00
|
|
|
page_no += 1
|
2021-09-30 10:13:43 +01:00
|
|
|
break if (limit && outdated_packages.size >= limit) || response.size <= 1
|
2020-06-29 09:51:58 -05:00
|
|
|
end
|
2020-08-18 22:25:17 +00:00
|
|
|
|
2021-02-17 00:09:02 +05:30
|
|
|
puts "#{outdated_packages.size} outdated #{package_term.pluralize(outdated_packages.size)} found"
|
2020-08-16 18:45:50 -05:00
|
|
|
puts
|
2020-06-29 09:57:21 -05:00
|
|
|
|
|
|
|
outdated_packages
|
2020-06-29 09:51:58 -05:00
|
|
|
end
|
2020-07-30 22:58:30 +00:00
|
|
|
|
2021-01-11 08:29:34 +05:30
|
|
|
def latest_version(repositories)
|
2021-01-24 19:07:17 +05:30
|
|
|
# The status is "unique" when the package is present only in Homebrew, so
|
|
|
|
# Repology has no way of knowing if the package is up-to-date.
|
2021-01-11 08:29:34 +05:30
|
|
|
is_unique = repositories.find do |repo|
|
|
|
|
repo["status"] == "unique"
|
|
|
|
end.present?
|
2020-07-30 22:58:30 +00:00
|
|
|
|
2021-01-11 08:29:34 +05:30
|
|
|
return "present only in Homebrew" if is_unique
|
2020-08-13 09:56:41 -05:00
|
|
|
|
2021-01-11 08:29:34 +05:30
|
|
|
latest_version = repositories.find do |repo|
|
|
|
|
repo["status"] == "newest"
|
2020-07-30 22:58:30 +00:00
|
|
|
end
|
|
|
|
|
2021-01-24 19:07:17 +05:30
|
|
|
# Repology cannot identify "newest" versions for packages without a version
|
|
|
|
# scheme
|
2021-01-11 08:29:34 +05:30
|
|
|
return "no latest version" if latest_version.blank?
|
2020-07-31 10:52:37 -05:00
|
|
|
|
2021-01-11 08:29:34 +05:30
|
|
|
latest_version["version"]
|
2020-07-31 10:52:37 -05:00
|
|
|
end
|
2020-06-29 09:51:58 -05:00
|
|
|
end
|