Documentation
¶
Overview ¶
Package githubclient owns GitHub token resolution, token caching, and construction of an authenticated *github.Client. This file is the package's design record; see PATTERN-github-auth for the machine-enforced half of the same contract.
Scope ¶
The package exposes no per-operation wrapper methods, only auth and client construction. Consumers call go-github's typed API directly on the returned *github.Client.
Token resolution order and the cache ¶
Sources are tried in fixed order: GH_TOKEN, GITHUB_TOKEN, the on-disk cache, then a bounded `gh auth token` shell-out. Environment variables always outrank the cache. The cache only avoids the shell-out and is never a source of truth; an unreadable, malformed, or foreign-shaped cache file is a miss, never a fatal error.
The cache file lives at a machine-global location outside every repository, so a token never sits in a checkout. It holds the token in plaintext, readable only by its owner; that is the accepted cost of never blocking. CredentialDir names that directory for the Slack credential file, which `internal/slackengine` alone reads and writes.
Why WithAuthToken is banned ¶
Exactly one layer owns the Authorization header: the authenticating transport, installed outermost. github.WithAuthToken is never used to construct a client here. It would give the header a second owner, and a 401 replay would re-send its stale token.
The 401 replay ¶
On a 401 the transport invalidates the cache, re-resolves once, rewinds the body and replays once. A second consecutive 401 is returned unchanged. A token from GH_TOKEN or GITHUB_TOKEN is never replayed, since re-resolving reproduces the same value. The transport returns an error wrapping ErrTokenRejected instead, so a caller can tell a refused token from a transport failure.
The two deadlines ¶
The `gh auth token` shell-out is bounded at 5s, and the client's transport bounds a call, replay included, at 30s. Both bounds are needed: http.DefaultTransport sets no response timeout, and an unbounded credential path is indistinguishable from a hang to an autonomous process.
Never-block-on-credentials ¶
`gh auth login` is never invoked, and every resolution step returns quickly or returns ErrTokenUnresolvable. A process blocked on a credential prompt looks like a hang to an autonomous lym run. That property is why the package is its own leaf.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrTokenRejected = errors.New("githubclient: GitHub rejected the token")
ErrTokenRejected is wrapped into the error returned when GitHub answers 401 to a GH_TOKEN or GITHUB_TOKEN value. Replaying the request cannot change that outcome. The error reaches the caller inside a *url.Error, so a caller that classifies transient failures checks for it first.
var ErrTokenUnresolvable = errors.New("githubclient: no GitHub token available (set GH_TOKEN or GITHUB_TOKEN, or run `gh auth login`)")
ErrTokenUnresolvable is returned by resolveToken when no credential source produced a usable token. Callers surface it as a typed error rather than waiting or prompting; nothing here reaches `gh auth login`.
Functions ¶
func CredentialDir ¶
CredentialDir returns the per-user lym config directory the GitHub token cache lives in. The Slack token file sits beside it. It returns ("", false) when no usable environment variable is set.
func IsTransient ¶
IsTransient reports whether err's chain holds a GitHub API failure that a retry a moment later is likely to clear: a 5xx response, a transport-level *url.Error other than a cancellation, or context.DeadlineExceeded. Rate-limit errors are not transient, since an immediate retry hits the same limit; nor are context.Canceled, any 4xx response, or an error with none of these in its chain. A *url.Error from parsing a request URL or from a TLS certificate failure is not transport-level, so it is not transient either.
func New ¶
New returns an authenticated *github.Client against the real GitHub API. Credentials are resolved lazily and non-blockingly by authRT.
func NewWithBaseURL ¶
NewWithBaseURL returns an authenticated *github.Client that sends requests to baseURL. An empty baseURL keeps the real GitHub API. Its inner transport is httpClient's Transport, or the default when httpClient is nil. Tests use this to target httptest servers.
func ParseOwnerRepo ¶
ParseOwnerRepo splits remoteURL into its owner and repo segments. It accepts four shapes, each with an optional ".git" suffix and an optional single trailing slash. The SSH form is git@github.com:owner/repo.git; the ssh:// form is ssh://git@github.com/owner/repo.git. The HTTPS form is https://github.com/owner/repo.git. The HTTPS form with user info is https://user@github.com/owner/repo.git. It makes no network call and no go-github call. A non-GitHub host, a URL without an owner/repo pair, and an unrecognized shape each yield a distinct wrapped error. A caller can report which failure it hit.
func StatusCode ¶
StatusCode returns the HTTP status of the *github.ErrorResponse in err's chain, or 0 when the chain holds none. A rate-limit refusal is its own error type and reads 0, so a 403 it carries is never taken for a refused token.
Types ¶
This section is empty.