github

package
v0.0.0-...-0c72368 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2026 License: Apache-2.0 Imports: 22 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Constants

This section is empty.

Variables

View Source
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.

View Source
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

func Classify(resp *gh.Response, err error) error

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

func IsNotFound(err error) bool

IsNotFound reports whether err is a NotFound classification.

func NotInstalled

func NotInstalled(err error) bool

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

func New(cfg config.GitHub, base http.RoundTripper, log *slog.Logger) (*Client, error)

New builds a client for the configured sources. base is the HTTP transport underneath (nil: a cloned default transport).

func (*Client) Observe

func (c *Client) Observe(fn func(source string, status int))

Observe sets fn to receive the HTTP status of every GitHub response (304 included). Call it before the first request.

func (*Client) RateRemaining

func (c *Client) RateRemaining() map[string]float64

RateRemaining returns, per source, the smallest fraction of the core rate limit left over its credentials (1 when unknown).

func (*Client) Remaining

func (c *Client) Remaining(source, owner string) (float64, time.Time)

Remaining reports the fraction of the core rate limit left for owner's credential and when it resets. Unknown limits report 1.

func (*Client) Source

func (c *Client) Source(id string) *Source

Source returns the source with id, or nil.

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.

func (Delivery) OK

func (d Delivery) OK() bool

OK says the portal answered the delivery with a success.

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 (e *Error) AuthFailed() bool

func (*Error) Error

func (e *Error) Error() string

func (*Error) ErrorCode

func (e *Error) ErrorCode() string

func (*Error) IsNotFound

func (e *Error) IsNotFound() bool

func (*Error) Permanent

func (e *Error) Permanent() bool

func (*Error) RateLimitedUntil

func (e *Error) RateLimitedUntil() (time.Time, bool)

func (*Error) Unwrap

func (e *Error) Unwrap() error

func (*Error) Waiting

func (e *Error) Waiting() bool

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

type Holder struct {
	Key   string
	Token []byte
}

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) App

func (s *Source) App() bool

App reports whether the source reads through a GitHub App, not a token.

func (*Source) Blob

func (s *Source) Blob(ctx context.Context, owner, name, sha string) ([]byte, error)

Blob returns the content of a blob.

func (*Source) CheckToken

func (s *Source) CheckToken(ctx context.Context, owner string, token []byte) (TokenCheck, error)

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

func (s *Source) Deliveries(ctx context.Context, since time.Time) ([]Delivery, error)

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

func (s *Source) Forget(keep func(key string) bool)

Forget drops the credentials of the holders whose keys keep refuses: a removed or replaced token.

func (*Source) Grants

func (s *Source) Grants() (grants []Grant, known bool)

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

func (s *Source) GraphQLURL() string

GraphQLURL is the source's GraphQL endpoint: configured (enterprise.github), else beside its REST base.

func (*Source) ListRepos

func (s *Source) ListRepos(ctx context.Context, owner string) ([]snapshot.Repo, error)

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

func (s *Source) ListWith(ctx context.Context, owner string, h Holder) ([]snapshot.Repo, error)

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

func (s *Source) REST(ctx context.Context, owner, name string) (*gh.Client, error)

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

func (s *Source) RefreshInstallations(ctx context.Context) error

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) Repo

func (s *Source) Repo(ctx context.Context, owner, name string) (snapshot.Repo, error)

Repo returns one repository.

func (*Source) SetBroker

func (s *Source) SetBroker(b Broker)

SetBroker gives a people source its broker. Call it before the first request; without one the source reads nothing.

func (*Source) Tags

func (s *Source) Tags(ctx context.Context, owner, name string) ([]string, error)

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

func (s *Source) WebURL() string

WebURL is the web host of the source with a trailing slash: https://github.com/ for github.com, the GHES host otherwise.

type TokenCheck

type TokenCheck struct {
	Login   string
	Email   string    // the account's public email; empty when it shows none
	Expires time.Time // zero: GitHub named no end
}

TokenCheck is what GitHub says of a person's token (scp enterprise): whose it is and when it ends.

type TreeEntry

type TreeEntry struct {
	Path string // relative to the tree
	Type string // blob | tree | commit
	SHA  string
	Size int64 // blobs only
}

TreeEntry is one entry of a git tree.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL