Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6.0.1
with:
ref: ${{ github.event.pull_request.head.ref || github.ref }}

- name: Setup Ruby
uses: ruby/setup-ruby@v1.284.0
Expand Down
86 changes: 47 additions & 39 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -273,53 +273,52 @@ def add_message(type, file, line_number, message)
CACHE_FILE = File.expand_path("../.api-cache.json", __dir__)
CACHE_TTL_SECONDS = 24 * 60 * 60 # 24 hours

# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
def load_api_cache!
return unless File.exist?(CACHE_FILE)

data = JSON.parse(File.read(CACHE_FILE))
now = Time.now.to_i
ttl = CACHE_TTL_SECONDS

if data["repos"]
data["repos"].each do |key, entry|
cached_at = entry["cached_at"]
next unless cached_at
next if now - cached_at.to_i > ttl

result = entry["value"]
# Reconstruct a minimal object that responds to .full_name
cached = if result.nil?
nil
else
next unless result["full_name"]

Struct.new(:full_name).new(result["full_name"])
end
NewOctokit.class_variable_get(:@@repos)[key] = cached
end
data["repos"]&.each do |key, entry|
cached_at = entry["cached_at"]
next unless cached_at
next if now - cached_at.to_i > ttl

result = entry["value"]
# Reconstruct a minimal object that responds to .full_name
cached = if result.nil?
nil
else
next unless result["full_name"]

Struct.new(:full_name).new(result["full_name"])
end
NewOctokit.class_variable_get(:@@repos)[key] = cached
end

if data["users"]
data["users"].each do |key, entry|
cached_at = entry["cached_at"]
next unless cached_at
next if now - cached_at.to_i > ttl

result = entry["value"]
cached = if result.nil?
nil
else
next unless result["login"]

Struct.new(:login).new(result["login"])
end
NewOctokit.class_variable_get(:@@users)[key] = cached
end
data["users"]&.each do |key, entry|
cached_at = entry["cached_at"]
next unless cached_at
next if now - cached_at.to_i > ttl

result = entry["value"]
cached = if result.nil?
nil
else
next unless result["login"]

Struct.new(:login).new(result["login"])
end
NewOctokit.class_variable_get(:@@users)[key] = cached
end
rescue JSON::ParserError, StandardError => e
warn "Failed to load API cache: #{e.message}"
rescue StandardError => error
warn "Failed to load API cache: #{error.message}"
end
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity

# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
def save_api_cache!
now = Time.now.to_i
repos_data = {}
Expand All @@ -331,7 +330,11 @@ def save_api_cache!

repos_data[key.to_s] = {
"cached_at" => now,
"value" => value.nil? ? nil : { "full_name" => value.respond_to?(:full_name) ? value.full_name : value.to_s },
"value" => if value.nil?
nil
else
{ "full_name" => value.respond_to?(:full_name) ? value.full_name : value.to_s }
end,
}
end

Expand All @@ -341,14 +344,19 @@ def save_api_cache!

users_data[key.to_s] = {
"cached_at" => now,
"value" => value.nil? ? nil : { "login" => value.respond_to?(:login) ? value.login : value.to_s },
"value" => if value.nil?
nil
else
{ "login" => value.respond_to?(:login) ? value.login : value.to_s }
end,
}
end

File.write(CACHE_FILE, JSON.pretty_generate({ "repos" => repos_data, "users" => users_data }))
rescue StandardError => e
warn "Failed to save API cache: #{e.message}"
rescue StandardError => error
warn "Failed to save API cache: #{error.message}"
end
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity

# Load cached API results at startup
load_api_cache!
Expand Down
Loading