Class: Kettle::Gha::Pins::GitHubClient

Inherits:
Object
  • Object
show all
Defined in:
lib/kettle/gha/pins/github_client.rb

Overview

Lightweight GitHub API client for commit and release SHA resolution.

Defined Under Namespace

Classes: CacheMissError, RefreshError

Constant Summary collapse

ANNOTATED_TAG_GIT_FALLBACK_THRESHOLD =
4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token:, api_base:, user_agent:, persistent_cache: nil, refresh_cache: false, offline: false, fail_on_refresh_error: 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.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/kettle/gha/pins/github_client.rb', line 18

def initialize(token:, api_base:, user_agent:, persistent_cache: nil, refresh_cache: false, offline: false, fail_on_refresh_error: 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
  @offline = offline
  @fail_on_refresh_error = fail_on_refresh_error
  @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_hitObject (readonly)

Returns the value of attribute last_versions_cache_hit.



34
35
36
# File 'lib/kettle/gha/pins/github_client.rb', line 34

def last_versions_cache_hit
  @last_versions_cache_hit
end

Instance Method Details

#commit_sha(repo_ref, ref, refresh: false) ⇒ Object

Raises:



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/kettle/gha/pins/github_client.rb', line 81

def commit_sha(repo_ref, ref, refresh: false)
  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) && !refresh

  unless @refresh_cache || refresh
    cached = @persistent_cache&.ref_sha(repo_ref, ref, fresh: !@offline)
    if cached
      @commit_cache[cache_key] = cached
      return cached
    end
  end

  raise CacheMissError, "offline cache miss for #{repo_ref}@#{ref}" if @offline

  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



110
111
112
113
114
# File 'lib/kettle/gha/pins/github_client.rb', line 110

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



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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/kettle/gha/pins/github_client.rb', line 36

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

  if @offline
    cached = @persistent_cache&.versions_for_repo(repo_ref, fresh: false)
    raise CacheMissError, "offline cache miss for #{repo_ref}" if cached.nil?

    @last_versions_cache_hit = true
    @release_cache[repo_ref] = cached
    return cached
  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 refresh_failed!(repo_ref, stale, "invalid releases response") unless data.is_a?(Array)

    tag_shas = tag_ref_shas(repo_ref)
    return refresh_failed!(repo_ref, stale, "invalid tag response") 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
  refresh_failed!(repo_ref, stale, "GitHub refresh timed out")
end