Documentation
¶
Overview ¶
Package github reads repositories from GitHub for the sync jobs: GitHub App or token authentication, REST calls through go-github, conditional requests with ETags, rate-limit observation and error classification. It returns snapshot value types.
Index ¶
- Variables
- func Classify(resp *gh.Response, err error) error
- func GraphQL(ctx context.Context, c *gh.Client, endpoint, query string, vars map[string]any, ...) error
- func IsNotFound(err error) bool
- func NotInstalled(err error) bool
- type Broker
- type Client
- type Delivery
- type Error
- type Grant
- type Holder
- type Kind
- type Source
- func (s *Source) App() bool
- func (s *Source) Blob(ctx context.Context, owner, name, sha string) ([]byte, error)
- func (s *Source) CheckToken(ctx context.Context, owner string, token []byte) (TokenCheck, error)
- func (s *Source) Deliveries(ctx context.Context, since time.Time) ([]Delivery, error)
- func (s *Source) Files(ctx context.Context, owner, name string, keep func(dir string) bool) ([]TreeEntry, error)
- func (s *Source) Forget(keep func(key string) bool)
- func (s *Source) Grants() (grants []Grant, known bool)
- func (s *Source) GraphQLURL() string
- func (s *Source) ListRepos(ctx context.Context, owner string) ([]snapshot.Repo, error)
- func (s *Source) ListWith(ctx context.Context, owner string, h Holder) ([]snapshot.Repo, error)
- func (s *Source) REST(ctx context.Context, owner, name string) (*gh.Client, error)
- func (s *Source) RefreshInstallations(ctx context.Context) error
- func (s *Source) Repo(ctx context.Context, owner, name string) (snapshot.Repo, error)
- func (s *Source) SetBroker(b Broker)
- func (s *Source) Tags(ctx context.Context, owner, name string) ([]string, error)
- func (s *Source) Tree(ctx context.Context, owner, name, ref string, recursive bool) ([]TreeEntry, error)
- func (s *Source) WebURL() string
- type TokenCheck
- type TreeEntry
Constants ¶
This section is empty.
Variables ¶
var ErrLogTruncated = errors.New("github: the webhook delivery log is longer than was read")
ErrLogTruncated says the log reaches further back than Deliveries read: the deliveries returned with it are the newest ones, and older ones, down to since, were not read.
var ErrNoDeliveryLog = errors.New("github: no webhook delivery log without an App")
ErrNoDeliveryLog is returned in token mode: only an App has a delivery log.
Functions ¶
func Classify ¶
Classify turns the failure of a go-github call into an *Error (a cancelled context stays as it is).
func GraphQL ¶
func GraphQL(ctx context.Context, c *gh.Client, endpoint, query string, vars map[string]any, out any) error
GraphQL posts query with vars to endpoint (the source's GraphQLURL) through c and decodes the answer's data into out. An answer with errors and no data is a Transient error coded "graphql" (or "graphql_rate_limited"): GraphQL has its own rate limit, so a caller treats it as a missing extra and never pauses the source. An answer with data and errors (an alias GitHub does not resolve) decodes the data and drops the errors.
func IsNotFound ¶
IsNotFound reports whether err is a NotFound classification.
func NotInstalled ¶
NotInstalled reports whether err says the source's GitHub App is not installed on the account (removed or suspended): GitHub's own answer that the App reads nothing there, not a failure to ask.
Types ¶
type Broker ¶
type Broker interface {
Listed(source, owner string) ([]snapshot.Repo, bool)
Holders(source, owner, name string) []Holder
}
Broker is what a source read with people's tokens asks of the people (scp enterprise): every repository of owner their fresh listings hold, and the tokens of the people whose fresh listing holds owner/name.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client holds one Source per configured source.
func New ¶
New builds a client for the configured sources. base is the HTTP transport underneath (nil: a cloned default transport).
func (*Client) Observe ¶
Observe sets fn to receive the HTTP status of every GitHub response (304 included). Call it before the first request.
func (*Client) RateRemaining ¶
RateRemaining returns, per source, the smallest fraction of the core rate limit left over its credentials (1 when unknown).
type Delivery ¶
type Delivery struct {
GUID string // the X-GitHub-Delivery id
Event string
Action string
RepositoryID int64 // 0 for an event about no repository (an installation)
At time.Time
Status int // the answer's HTTP status; 0 when none came (the portal was down or too slow)
}
Delivery is one line of the App's webhook delivery log: what GitHub sent, and what the portal answered.
type Error ¶
type Error struct {
Kind Kind
RetryAt time.Time // RateLimited: from Retry-After or the rate reset; zero when absent
Code string
Err error
}
Error is a classified GitHub failure.
func (*Error) AuthFailed ¶
func (*Error) IsNotFound ¶
type Grant ¶
type Grant struct {
Owner string
Permissions map[string]string
Events []string
Selection string // the repositories it was given: "selected" (those picked on GitHub) or "all"
}
Grant is what an App's installation on an account may read (permission name to "read" or "write", as GitHub names them: contents, metadata, issues, pull_requests, checks, statuses) and the webhook events it is sent.
type Holder ¶
Holder is one person's token a source read with people's tokens may use (scp enterprise). Key names the token, and changes when the token does: the credential made for it, with its rate book and its conditional answers, is kept under it.
type Kind ¶
type Kind uint8
Kind classifies a failed call by what the caller should do about it.
const ( Transient Kind = iota + 1 // 5xx, timeout, connection reset: retry with backoff RateLimited // wait until RetryAt Auth // the credential was refused NotFound // missing or not visible Permanent // will not succeed by retrying soon Waiting // no credential to read it with yet (a people source without a person's token): not an attempt )
type Source ¶
type Source struct {
// contains filtered or unexported fields
}
Source is one GitHub host with one credential.
func (*Source) CheckToken ¶
CheckToken asks GitHub with token who it belongs to (GET /user) and whether it reads any repository of the organisation owner (GET /orgs/{owner}/repos, one). The token goes straight to GitHub, past the conditional cache: its answers are not kept. A token that reads nothing of owner is the Permanent error reads_nothing; the others are classified as every call's.
func (*Source) Deliveries ¶
Deliveries reads the App's webhook delivery log, newest first, back to since (GET /app/hook/deliveries with the App's JWT; a read). It is how the portal learns of a delivery it never received, or answered with a failure, while it was down or restarting. When the log is longer than maxDeliveryPages it returns the newest deliveries with ErrLogTruncated.
func (*Source) Files ¶
func (s *Source) Files(ctx context.Context, owner, name string, keep func(dir string) bool) ([]TreeEntry, error)
Files returns the blobs of the default branch in the directories keep admits: the root always, another directory when keep admits its path (keep sees every directory's whole path, so it rules on its parents too). It reads the recursive tree as a stream and keeps only those entries, so a repository of any size costs the memory of what is kept. A tree GitHub truncated (past 100,000 entries or 7 MB) is read again directory by directory, shallow ones first, at most maxWalk calls: what the walk did not reach is left out, never a failure.
func (*Source) Forget ¶
Forget drops the credentials of the holders whose keys keep refuses: a removed or replaced token.
func (*Source) Grants ¶
Grants is what the App's installations may read, from the last listing; known is false in token mode, where GitHub does not tell (a fine-grained token's access cannot be asked), and before the first listing.
func (*Source) GraphQLURL ¶
GraphQLURL is the source's GraphQL endpoint: configured (enterprise.github), else beside its REST base.
func (*Source) ListRepos ¶
ListRepos returns every repository of owner, an organisation or a user account, most recently pushed first; with an App, what the installation on owner was given (the selected repositories or all), in GitHub's order. Any failed page fails the listing.
func (*Source) ListWith ¶
ListWith returns every repository of the organisation owner that h's token reads, most recently pushed first: what that person may see of owner. Any failed page fails the listing.
func (*Source) REST ¶
REST returns the go-github client for the credential that reads owner's repository name, for the calls only one feature makes; classify their failures with Classify.
func (*Source) RefreshInstallations ¶
RefreshInstallations lists the App's installations and keeps those of the configured organisations (filtered by app.installations when set). It does nothing in token mode.
func (*Source) SetBroker ¶
SetBroker gives a people source its broker. Call it before the first request; without one the source reads nothing.
func (*Source) Tags ¶
Tags are the names of a repository's tags, the first 100 GitHub lists (the newest first, by its own order).
func (*Source) Tree ¶
func (s *Source) Tree(ctx context.Context, owner, name, ref string, recursive bool) ([]TreeEntry, error)
Tree returns the entries of one tree; recursive includes every entry below it, with paths relative to it. ref is a tree SHA or a branch name; "HEAD" is the default branch. The answer streams (readTree); a recursive tree GitHub truncated holds what it sent, and Files is the reader for a search of a whole repository.
func (*Source) WebURL ¶
WebURL is the web host of the source with a trailing slash: https://github.com/ for github.com, the GHES host otherwise.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package githubtest is a fake GitHub REST server for tests: App and token authentication, organisation and user account listings with pagination, single repositories, rate-limit headers, ETags and 304s, fault injection and a request ledger.
|
Package githubtest is a fake GitHub REST server for tests: App and token authentication, organisation and user account listings with pagination, single repositories, rate-limit headers, ETags and 304s, fault injection and a request ledger. |