Skip to content

Commit

Permalink
Gracefully handle the case where an installation is gone
Browse files Browse the repository at this point in the history
  • Loading branch information
oprypin committed Nov 24, 2024
1 parent ff3fa26 commit d25c792
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
7 changes: 4 additions & 3 deletions src/github_api.cr
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class GitHubAppAuth
end
end

private def new_token(installation_id : InstallationId) : InstallationToken
private def new_token(installation_id : InstallationId) : InstallationToken?
Tasker.in(TOKEN_EXPIRATION * 0.98) do
if (old_token = @@token.read(installation_id))
old_token.installation_id = nil # Suppress logging.
Expand All @@ -83,6 +83,7 @@ class GitHubAppAuth
json: {permissions: {actions: "read"}},
headers: {Authorization: jwt}
)
return nil if resp.status_code == 404
resp.raise_for_status
tok = InstallationToken.from_json(resp.body)
tok.installation_id = installation_id
Expand All @@ -91,7 +92,7 @@ class GitHubAppAuth

TOKEN_EXPIRATION = 55.minutes

def token(installation_id : InstallationId, *, new : Bool = false) : InstallationToken
def token(installation_id : InstallationId, *, new : Bool = false) : InstallationToken?
if new
@@token.write(installation_id, new_token(installation_id), expires_in: TOKEN_EXPIRATION)
else
Expand All @@ -102,7 +103,7 @@ class GitHubAppAuth
end

@@jwt = MemoryCache(Int32, AppToken).new
@@token = CleanedMemoryCache(InstallationId, InstallationToken).new
@@token = CleanedMemoryCache(InstallationId, InstallationToken?).new
end

GitHub = Halite::Client.new do
Expand Down
11 changes: 6 additions & 5 deletions src/nightly_link.cr
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ record RepoInstallation,
{% if tok %}
args = {installation.id, token}
{% else %}
args = {GitHubApp.token(installation.id)}
args = {GitHubApp.token(installation.id).not_nil!}
{% end %}
Repositories.for_installation(*args) do |repo|
if repo.owner == installation.account.login
Expand Down Expand Up @@ -121,10 +121,11 @@ record RepoInstallation,
) : {InstallationToken, String?}
if (inst = RepoInstallation.read(db, repo_owner: repo_owner))
h = inst.verify(repo_name: repo_name, h: h)
{GitHubApp.token(inst.installation_id), h}
else
{GitHubApp.token(FALLBACK_INSTALL_ID), nil}
if (tok = GitHubApp.token(inst.installation_id))
return {tok, h}
end
end
return {GitHubApp.token(FALLBACK_INSTALL_ID).not_nil!, nil}
end
end

Expand Down Expand Up @@ -233,7 +234,7 @@ class NightlyLink
example_dest = abs_url(NightlyLink.gen_by_branch(**args))

run, artifact = @@examples_cache.fetch(example_workflow) do
token = GitHubApp.token(FALLBACK_INSTALL_ID)
token = GitHubApp.token(FALLBACK_INSTALL_ID).not_nil!
run_ = get_latest_run(
args[:repo_owner], args[:repo_name],
workflow: args[:workflow] + ".yml", branch: args[:branch], status: "success", token: token
Expand Down

0 comments on commit d25c792

Please sign in to comment.