shared_audits: prevent duplicate eol_data fetches

The `eol_data` method uses `@eol_data["#{product}/#{cycle}"] ||=`,
which can unncessarily allow a duplicate API call if the same
product/cycle combination was previously tried but returned a 404
(Not Found) response. In this scenario, the value would be `nil` but
the existing logic doesn't check whether this is a missing key or a
`nil` value. If the key is present, we shouldn't make the same
request again.

This updates the method to return the existing value if the key
exists, which effectively prevents duplicate fetches. This new logic
only modifies `@eol_data` if `curl` is successful, so it does allow
the request to be made again if it failed before.

That said, this shouldn't normally be an issue and this is mostly
about refactoring the method to allow for nicer code organization.
This approach reduces the `begin` block to only the `JSON.parse` call,
which allows us to use `return unless result.status.success?` (this
previously led to a RuboCop offense because it was called within a
`begin` block).
This commit is contained in:
Sam Ford 2025-05-03 21:15:11 -04:00
parent 53c0780d85
commit 69dcbacb71
No known key found for this signature in database
GPG Key ID: 7AF5CBEE1DD6F76D

View File

@ -11,16 +11,19 @@ module SharedAudits
sig { params(product: String, cycle: String).returns(T.nilable(T::Hash[String, T.untyped])) }
def self.eol_data(product, cycle)
@eol_data ||= T.let({}, T.nilable(T::Hash[String, T.untyped]))
@eol_data["#{product}/#{cycle}"] ||= begin
result = Utils::Curl.curl_output("--location", "https://endoflife.date/api/v1/products/#{product}/releases/#{cycle}")
key = "#{product}/#{cycle}"
return @eol_data[key] if @eol_data.key?(key)
if result.status.success?
begin
JSON.parse(result.stdout)
rescue JSON::ParserError
nil
end
end
result = Utils::Curl.curl_output(
"--location",
"https://endoflife.date/api/v1/products/#{product}/releases/#{cycle}",
)
return unless result.status.success?
@eol_data[key] = begin
JSON.parse(result.stdout)
rescue JSON::ParserError
nil
end
end