Class: Kettle::Dev::GhaShaPinsCLI::GitHubClient

Inherits:
Object
  • Object
show all
Defined in:
lib/kettle/dev/gha_sha_pins_cli.rb

Overview

Lightweight GitHub API client for commit and release SHA resolution.

Instance Method Summary collapse

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.



1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
# File 'lib/kettle/dev/gha_sha_pins_cli.rb', line 1106

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 = {}
end

Instance Method Details

#commit_sha(repo_ref, ref) ⇒ Object



1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
# File 'lib/kettle/dev/gha_sha_pins_cli.rb', line 1150

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



1177
1178
1179
1180
1181
# File 'lib/kettle/dev/gha_sha_pins_cli.rb', line 1177

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



1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
# File 'lib/kettle/dev/gha_sha_pins_cli.rb', line 1119

def versions_for_repo(repo_ref)
  return [] if repo_ref.to_s.empty?
  return @release_cache[repo_ref] if @release_cache.key?(repo_ref)

  stale = nil
  unless @refresh_cache
    cached = @persistent_cache&.versions_for_repo(repo_ref, fresh: true)
    if cached
      @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