Class: Kettle::Dev::GhaShaPinsCLI::GitHubClient
- Inherits:
-
Object
- Object
- Kettle::Dev::GhaShaPinsCLI::GitHubClient
- 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
- #commit_sha(repo_ref, ref) ⇒ Object
-
#initialize(token:, api_base:, user_agent:, persistent_cache: nil, refresh_cache: false) ⇒ 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) ⇒ GitHubClient
Returns a new instance of GitHubClient.
1051 1052 1053 1054 1055 1056 1057 1058 1059 |
# File 'lib/kettle/dev/gha_sha_pins_cli.rb', line 1051 def initialize(token:, api_base:, user_agent:, persistent_cache: nil, refresh_cache: false) @token = token @api_base = api_base @user_agent = user_agent @persistent_cache = persistent_cache @refresh_cache = refresh_cache @commit_cache = {} @release_cache = {} end |
Instance Method Details
#commit_sha(repo_ref, ref) ⇒ Object
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 |
# File 'lib/kettle/dev/gha_sha_pins_cli.rb', line 1105 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) data = request_json("/repos/#{repo_ref}/commits/#{uri_encode(ref)}") sha = if data.is_a?(Hash) data.fetch("sha", "")[0, 40] end @commit_cache[cache_key] = sha sha end |
#release_latest_sha(repo_ref) ⇒ Object
1119 1120 1121 1122 1123 |
# File 'lib/kettle/dev/gha_sha_pins_cli.rb', line 1119 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
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 |
# File 'lib/kettle/dev/gha_sha_pins_cli.rb', line 1061 def versions_for_repo(repo_ref) return [] if repo_ref.to_s.empty? return @release_cache[repo_ref] if @release_cache.key?(repo_ref) unless @refresh_cache cached = @persistent_cache&.versions_for_repo(repo_ref, fresh: true) if cached @release_cache[repo_ref] = cached return cached end end data = request_json("/repos/#{repo_ref}/releases?per_page=100") unless data.is_a?(Array) fallback = @persistent_cache&.versions_for_repo(repo_ref, fresh: false) return fallback if fallback return [] end tag_shas = tag_ref_shas(repo_ref) releases = data.filter_map do |release| next unless release.is_a?(Hash) next if release["prerelease"] == true tag = release["tag_name"].to_s parsed = parse_release_version_text(tag) next unless parsed { tag: tag, version_obj: parsed, version: parsed.to_s, sha: tag_shas[tag] } end releases.sort_by! { |release| release[:version_obj] } releases.reverse! @persistent_cache&.write_versions(repo_ref, releases) @release_cache[repo_ref] = releases releases end |