Class: Kettle::Gha::Pins::GitHubClient
- Inherits:
-
Object
- Object
- Kettle::Gha::Pins::GitHubClient
- Defined in:
- lib/kettle/gha/pins/github_client.rb
Overview
Lightweight GitHub API client for commit and release SHA resolution.
Instance Attribute Summary collapse
-
#last_versions_cache_hit ⇒ Object
readonly
Returns the value of attribute last_versions_cache_hit.
Instance Method Summary collapse
- #commit_sha(repo_ref, ref) ⇒ Object
-
#initialize(token:, api_base:, user_agent:, persistent_cache: nil, refresh_cache: false, open_timeout: DEFAULT_HTTP_OPEN_TIMEOUT_SECONDS, read_timeout: DEFAULT_HTTP_READ_TIMEOUT_SECONDS, refresh_timeout: DEFAULT_HTTP_REFRESH_TIMEOUT_SECONDS) ⇒ GitHubClient
constructor
A new instance of GitHubClient.
- #release_latest_sha(repo_ref) ⇒ Object
- #versions_for_repo(repo_ref) ⇒ Object
Constructor Details
#initialize(token:, api_base:, user_agent:, persistent_cache: nil, refresh_cache: false, open_timeout: DEFAULT_HTTP_OPEN_TIMEOUT_SECONDS, read_timeout: DEFAULT_HTTP_READ_TIMEOUT_SECONDS, refresh_timeout: DEFAULT_HTTP_REFRESH_TIMEOUT_SECONDS) ⇒ GitHubClient
Returns a new instance of GitHubClient.
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/kettle/gha/pins/github_client.rb', line 13 def initialize(token:, api_base:, user_agent:, persistent_cache: nil, refresh_cache: false, open_timeout: DEFAULT_HTTP_OPEN_TIMEOUT_SECONDS, read_timeout: DEFAULT_HTTP_READ_TIMEOUT_SECONDS, refresh_timeout: DEFAULT_HTTP_REFRESH_TIMEOUT_SECONDS) @token = token @api_base = api_base @user_agent = user_agent @persistent_cache = persistent_cache @refresh_cache = refresh_cache @open_timeout = open_timeout @read_timeout = read_timeout @refresh_timeout = refresh_timeout @commit_cache = {} @release_cache = {} @last_versions_cache_hit = false end |
Instance Attribute Details
#last_versions_cache_hit ⇒ Object (readonly)
Returns the value of attribute last_versions_cache_hit.
27 28 29 |
# File 'lib/kettle/gha/pins/github_client.rb', line 27 def last_versions_cache_hit @last_versions_cache_hit end |
Instance Method Details
#commit_sha(repo_ref, ref) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/kettle/gha/pins/github_client.rb', line 65 def commit_sha(repo_ref, ref) return nil if repo_ref.to_s.empty? || ref.to_s.empty? cache_key = "commit:#{repo_ref}:#{ref}" return @commit_cache[cache_key] if @commit_cache.key?(cache_key) unless @refresh_cache cached = @persistent_cache&.ref_sha(repo_ref, ref, fresh: true) if cached @commit_cache[cache_key] = cached return cached end end data = request_json("/repos/#{repo_ref}/commits/#{uri_encode(ref)}") sha = if data.is_a?(Hash) data.fetch("sha", "")[0, 40] end if sha.to_s.empty? sha = @persistent_cache&.ref_sha(repo_ref, ref, fresh: false) else @persistent_cache&.write_ref_sha(repo_ref, ref, sha) end @commit_cache[cache_key] = sha sha end |
#release_latest_sha(repo_ref) ⇒ Object
92 93 94 95 96 |
# File 'lib/kettle/gha/pins/github_client.rb', line 92 def release_latest_sha(repo_ref) versions = versions_for_repo(repo_ref) latest = versions.first latest ? version_entry_sha(repo_ref, latest) : nil end |
#versions_for_repo(repo_ref) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/kettle/gha/pins/github_client.rb', line 29 def versions_for_repo(repo_ref) @last_versions_cache_hit = false return [] if repo_ref.to_s.empty? if @release_cache.key?(repo_ref) @last_versions_cache_hit = true return @release_cache[repo_ref] end stale = nil unless @refresh_cache cached = @persistent_cache&.versions_for_repo(repo_ref, fresh: true) if cached @last_versions_cache_hit = true @release_cache[repo_ref] = cached return cached end stale = @persistent_cache&.versions_for_repo(repo_ref, fresh: false) end releases = nil Timeout.timeout(@refresh_timeout) do data = request_json("/repos/#{repo_ref}/releases?per_page=100") return cached_versions(repo_ref, stale) unless data.is_a?(Array) tag_shas = tag_ref_shas(repo_ref) return cached_versions(repo_ref, stale) unless tag_shas releases = build_release_versions(data, tag_shas) end @persistent_cache&.write_versions(repo_ref, releases) @release_cache[repo_ref] = releases releases rescue Timeout::Error cached_versions(repo_ref, stale) end |