Skip to content

Commit 04568cd

Browse files
Will Kimmereromus
andauthored
Catch error from stale GitHub token (#1041)
* Catch error from authenticate with ENV and obtain a token * Prompt before obtaining a token * Prompt for auth using omus' code above * Actually do something with exception :/ * Token may be stale * Only catch error when it contains a 401 * Update src/wizard/github.jl Co-authored-by: Curtis Vogt <[email protected]> * Update src/wizard/utils.jl Co-authored-by: Curtis Vogt <[email protected]> Co-authored-by: Curtis Vogt <[email protected]>
1 parent 678bfb3 commit 04568cd

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/wizard/github.jl

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,26 @@ using Registrator
55
const _github_auth = Ref{GitHub.Authorization}()
66

77
function github_auth(;allow_anonymous::Bool=true)
8-
if !isassigned(_github_auth) || !allow_anonymous && isa(_github_auth[], GitHub.AnonymousAuth)
9-
# If the user is feeding us a GITHUB_TOKEN token, use it!
10-
if length(get(ENV, "GITHUB_TOKEN", "")) == 40
8+
9+
if (!isassigned(_github_auth) || !allow_anonymous && isa(_github_auth[], GitHub.AnonymousAuth)) && length(get(ENV, "GITHUB_TOKEN", "")) == 40
10+
try
1111
_github_auth[] = GitHub.authenticate(ENV["GITHUB_TOKEN"])
12-
else
13-
if allow_anonymous
14-
_github_auth[] = GitHub.AnonymousAuth()
12+
catch e
13+
if occursin("401", e.msg)
14+
@warn "GitHub was unable to authenticate using the token from `ENV[\"GITHUB_TOKEN\"]`, it may be stale. Falling back on alternate authentication methods."
1515
else
16-
# If we're not allowed to return an anonymous authorization,
17-
# then let's create a token.
18-
_github_auth[] = GitHub.authenticate(obtain_token())
16+
rethrow()
1917
end
2018
end
2119
end
20+
21+
if !isassigned(_github_auth) && allow_anonymous
22+
_github_auth[] = GitHub.AnonymousAuth()
23+
end
24+
25+
if !isassigned(_github_auth)
26+
_github_auth[] = GitHub.authenticate(obtain_token())
27+
end
2228

2329
if !isa(_github_auth[], GitHub.AnonymousAuth)
2430
# Also shove this into Registrator, so it uses the same token.

0 commit comments

Comments
 (0)